好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > python實現簡單的井字棋

python實現簡單的井字棋

熱門標簽:400電話辦理哪種 手機網頁嵌入地圖標注位置 電銷機器人的風險 地圖標注線上如何操作 開封自動外呼系統怎么收費 開封語音外呼系統代理商 天津電話機器人公司 河北防封卡電銷卡 應電話機器人打電話違法嗎

本文實例為大家分享了python實現簡單的井字棋的具體代碼,供大家參考,具體內容如下

使用python實現井字棋游戲,沒有具體算法,只是用隨機下棋簡單實現:

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("機器走:")
            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("輸入落子縱橫坐標: a,b \n").split(','))
            if board[a][b] == '+':
               board[a][b] = 'X'
               oput(board)
               flag = test(board)
               if flag == 1:
                  break
            else:
                print("落子位置不對")
                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("輸入落子縱橫坐標: a,b \n").split(','))
            if board[a][b] == '+':
                board[a][b] = 'X'
                oput(board)
                flag = test(board)
                if flag == 1:
                    break
            else:
                print("落子位置不對")
                continue

if __name__ == "__main__":
    main()

結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

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

標簽:駐馬店 六盤水 成都 常州 江蘇 蘭州 山東 宿遷

巨人網絡通訊聲明:本文標題《python實現簡單的井字棋》,本文關鍵詞  python,實現,簡單,的,井字,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python實現簡單的井字棋》相關的同類信息!
  • 本頁收集關于python實現簡單的井字棋的相關信息資訊供網民參考!
  • 推薦文章