主頁(yè) > 知識(shí)庫(kù) > python實(shí)現(xiàn)簡(jiǎn)單的井字棋

python實(shí)現(xiàn)簡(jiǎn)單的井字棋

熱門標(biāo)簽:語(yǔ)音系統(tǒng) 客戶服務(wù) 呼叫中心市場(chǎng)需求 百度AI接口 Win7旗艦版 企業(yè)做大做強(qiáng) 硅谷的囚徒呼叫中心 電話運(yùn)營(yíng)中心

本文實(shí)例為大家分享了python實(shí)現(xiàn)簡(jiǎn)單的井字棋的具體代碼,供大家參考,具體內(nèi)容如下

使用python實(shí)現(xiàn)井字棋游戲,沒有具體算法,只是用隨機(jī)下棋簡(jiǎn)單實(shí)現(xiàn):

import random
board = [['+','+','+'],['+','+','+'],['+','+','+']]
def ma(board):
    if isempty(board):
        a = random.randint(0, 2)
        b = random.randint(0, 2)
        if board[a][b] != 'X' and board[a][b] != 'O':
            print("機(jī)器走:")
            board[a][b] = 'O'
            oput(board)
        else:
            ma(board)
    else:
        print("平局")
def oput(board):
    print("  0 1 2")
    for i in range(3):
        print(i, end=' ')
        for j in range(3):
            print(board[i][j], end=" ")
        print("")
def winput(i,j):
    if board[i][j] == 'X':
        print("human win")
    else:
        print("machine win")
    return 1

def test(board):
    for i in range(3):
        for j in range(3):
            if board[i][j] != '+':
                if j == 0:
                    if board[i][j] == board[i][j + 1] == board[i][j + 2]:
                        return winput(i,j)
                if i == 0:
                    if board[i][j] == board[i + 1][j] == board[i + 2][j]:
                        return winput(i,j)
                if i == 0 and j == 0:
                    if board[i][j] == board[i + 1][j + 1] == board[i + 2][j + 2]:
                        return winput(i,j)
                if i == 2 and j == 0:
                    if board[i][j] == board[i - 1][j + 1] == board[i - 2][j + 2]:
                        return winput(i,j)

def isempty(board):
    for i in range(3):
        for j in range(3):
            if board[i][j] == '+':
                return True
    return False
def main():
    print("初始棋盤:")
    oput(board)
    flag = 0
    t = input("human first? Y/N  human for X, machine for O\n")
    if t == 'Y':
        while isempty(board):
            print("人走:  ")
            a, b = map(int, input("輸入落子縱橫坐標(biāo): a,b \n").split(','))
            if board[a][b] == '+':
               board[a][b] = 'X'
               oput(board)
               flag = test(board)
               if flag == 1:
                  break
            else:
                print("落子位置不對(duì)")
                continue
            ma(board)
            flag = test(board)
            if flag == 1:
                break
            if isempty(board) == 0 and flag == 0:
                print("平局")
                break
    elif t == 'N':
        while isempty(board):
            ma(board)
            flag = test(board)
            if isempty(board) == 0 and flag == 0:
                print("平局")
                break
            if flag == 1:
                break
            print("人走:  ")
            a, b = map(int, input("輸入落子縱橫坐標(biāo): a,b \n").split(','))
            if board[a][b] == '+':
                board[a][b] = 'X'
                oput(board)
                flag = test(board)
                if flag == 1:
                    break
            else:
                print("落子位置不對(duì)")
                continue

if __name__ == "__main__":
    main()

結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • python實(shí)現(xiàn)簡(jiǎn)單的井字棋游戲(gui界面)
  • 基于python純函數(shù)實(shí)現(xiàn)井字棋游戲
  • Python實(shí)現(xiàn)井字棋小游戲
  • python入門之井字棋小游戲
  • python實(shí)現(xiàn)簡(jiǎn)單井字棋小游戲
  • python實(shí)現(xiàn)簡(jiǎn)單井字棋游戲
  • 基于python實(shí)現(xiàn)井字棋小游戲
  • Python實(shí)現(xiàn)的井字棋(Tic Tac Toe)游戲示例
  • python實(shí)現(xiàn)井字棋游戲
  • python實(shí)現(xiàn)帶界面的井字棋小游戲

標(biāo)簽:長(zhǎng)沙 喀什 崇左 安康 濟(jì)南 山西 海南 山西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python實(shí)現(xiàn)簡(jiǎn)單的井字棋》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266