生活随笔
收集整理的這篇文章主要介紹了
《Python游戏编程快速上手》第十章TicTacToe
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
《Python游戲編程快速上手》第九章的內容是對上一個游戲進行了些小拓展,所以我就不寫博客了,非常簡單大家自己去看吧。
所以我們今天看下第十章,第十章主要是講了一個叫TicTacToe的游戲,這個游戲其實就是大家玩過的OX棋。如圖:
執O先行,要盡量使自己先連成一條線,并盡可能阻攔對方連成一條線。
規則很簡單,說實話我的這個游戲的代碼有點亂,但我暫時不想去調整它,所以大家將就看下就好,有問題隨時歡迎提出:
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
這個的代碼質量真心不高,以后有時間再改吧。(挖坑中。。。)
加油啊,少年!
總結
以上是生活随笔為你收集整理的《Python游戏编程快速上手》第十章TicTacToe的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。