国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python實現(xiàn)簡單的井字棋

瀏覽:92日期:2022-06-18 13:05:40

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

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

import randomboard = [[’+’,’+’,’+’],[’+’,’+’,’+’],[’+’,’+’,’+’]]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 1def 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 Falsedef main(): print('初始棋盤:') oput(board) flag = 0 t = input('human first? Y/N human for X, machine for On') 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('落子位置不對')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('落子位置不對')continueif __name__ == '__main__': main()

結(jié)果:

python實現(xiàn)簡單的井字棋

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 商南县| 连南| 墨脱县| 抚顺县| 阳信县| 体育| 中宁县| 白沙| 龙泉市| 陵水| 太湖县| 丰原市| 新化县| 平原县| 墨玉县| 佛学| 石门县| 常山县| 浪卡子县| 西畴县| 承德县| 和林格尔县| 无极县| 黔东| 洪湖市| 清新县| 东城区| 威远县| 沙坪坝区| 于田县| 乳山市| 广汉市| 孝感市| 湘乡市| 鹤山市| 乡宁县| 安龙县| 宝坻区| 韶山市| 长垣县| 余姚市|