日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

《Python游戏编程快速上手》第十章TicTacToe

發(fā)布時(shí)間:2024/9/30 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《Python游戏编程快速上手》第十章TicTacToe 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

《Python游戲編程快速上手》第九章的內(nèi)容是對(duì)上一個(gè)游戲進(jìn)行了些小拓展,所以我就不寫博客了,非常簡(jiǎn)單大家自己去看吧。
所以我們今天看下第十章,第十章主要是講了一個(gè)叫TicTacToe的游戲,這個(gè)游戲其實(shí)就是大家玩過的OX棋。如圖:

執(zhí)O先行,要盡量使自己先連成一條線,并盡可能阻攔對(duì)方連成一條線。
規(guī)則很簡(jiǎn)單,說實(shí)話我的這個(gè)游戲的代碼有點(diǎn)亂,但我暫時(shí)不想去調(diào)整它,所以大家將就看下就好,有問題隨時(shí)歡迎提出:

import randomdef chooseXO():while True:print("Do you want to be X or O?")player = input()if player is 'X':return ['X', 'O']elif player is 'O':return ['O', 'X']else:print("Please input correcter character.")def showTable(table):print(table[0] + '|' + table[1] + '|' + table[2])print("-+-+-")print(table[3] + '|' + table[4] + '|' + table[5])print("-+-+-")print(table[6] + '|' + table[7] + '|' + table[8])def judgeWinner(table):for i in range(3):if table[i*3 + 0] == table[i*3 + 1] == table[i*3 + 2] != ' ':return 1if table[i] == table[i + 3] == table[i + 6] != ' ':return 1if table[0] == table[4] == table[8] != ' ':return 1if table[2] == table[4] == table[6] != ' ':return 1if ' ' not in table:return -1return 0def playerPos(table, player):while True:print("Please input a position.")p = int(input())if p not in range(1, 10) or table[p - 1] != ' ':print("You can only input between 1 and 9")else:#showTable(table)table[p - 1] = playerreturndef computePlayer(table, computer):closeWin = [[computer, computer, ' '],[computer, ' ', computer], [' ', computer, computer]]for i in range(3):if table[i*3 + 0:i*3+2] == closeWin[0]:return i+2+1elif table[i*3 + 0:i*3+2] == closeWin[1]:return i+1+1elif table[i*3 + 0:i*3+2] == closeWin[2]:return i+1if [table[i + 0],table[i+3], table[i+6]] == closeWin[0]:return i+6+1elif [table[i + 0],table[i+3], table[i+6]] == closeWin[1]:return i+3+1elif [table[i + 0],table[i+3], table[i+6]] == closeWin[2]:return i+1if [table[0], table[4], table[8]] == closeWin[0]:return 8+1elif [table[0], table[4], table[8]] == closeWin[1]:return 4+1elif [table[0], table[4], table[8]] == closeWin[2]:return 0+1if [table[2], table[4], table[6]] == closeWin[0]:return 6+1elif [table[2], table[4], table[6]] == closeWin[1]:return 4+1elif [table[2], table[4], table[6]] == closeWin[2]:return 2+1first = [i for i in [0, 2, 6, 8] if table[i] == ' ']if len(first) != 0:return random.choice(first)+1if table[4] == ' ':return 4+1else:third = [i for i in [1, 3, 5, 7] if table[i] == ' ']return random.choice(third)+1def printResult(res, who):if res == 1:print(who + " win!")elif res == -1:print("There is no block.No winner!")def main():print("Welcome !")print("This game 'O' is first.")character = chooseXO()player = character[0]computer = character[1]table = [' ' for i in range(9)]showTable(table)if "O" == player:print("You are the first.")playerPos(table, player)else:print("Computer is first.")while (True):print("Computer position.")compute = computePlayer(table, computer)table[compute - 1] = computershowTable(table)res = judgeWinner(table)if res != 0:printResult(res, 'Computer')breakprint("You turn.")playerPos(table, player)showTable(table)res = judgeWinner(table)if res != 0:printResult(res, 'You')breakif __name__ == '__main__':while True:main()print("Do you want try again?y or n")if input() == 'n':break

這個(gè)的代碼質(zhì)量真心不高,以后有時(shí)間再改吧。(挖坑中。。。)


加油啊,少年!

總結(jié)

以上是生活随笔為你收集整理的《Python游戏编程快速上手》第十章TicTacToe的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。