Tic tac toe是一款非常流行的游戏,所以让我们使用Python实现一个自动的Tic tac toe游戏。
null
游戏由程序自动进行,因此不需要用户输入。尽管如此,开发一款自动游戏还是很有趣的。让我们看看怎么做。
numpy
和 random
Python库用于构建这个游戏。code不要求用户在黑板上做标记,而是随机选择黑板上的一个位置并做标记。除非玩家获胜,否则它将在每回合后显示棋盘。如果比赛打成平局,那么结果是-1。
说明: play_game()
是主要功能,执行以下任务:
- 调用create_board()创建一个9×9板,并用0初始化。
- 对于每个玩家(1或2),调用random_place()函数随机选择棋盘上的一个位置,或者用玩家编号标记该位置。
- 每次移动后打印电路板。
- 在每次移动后评估棋盘,检查一行、一列或一条对角线是否有相同的玩家编号。如果是,则显示获胜者姓名。如果在9步之后,没有赢家,则显示-1。
下面是上述游戏的代码:
# Tic-Tac-Toe Program using # random number in Python # importing all necessary libraries import numpy as np import random from time import sleep # Creates an empty board def create_board(): return (np.array([[ 0 , 0 , 0 ], [ 0 , 0 , 0 ], [ 0 , 0 , 0 ]])) # Check for empty places on board def possibilities(board): l = [] for i in range ( len (board)): for j in range ( len (board)): if board[i][j] = = 0 : l.append((i, j)) return (l) # Select a random place for the player def random_place(board, player): selection = possibilities(board) current_loc = random.choice(selection) board[current_loc] = player return (board) # Checks whether the player has three # of their marks in a horizontal row def row_win(board, player): for x in range ( len (board)): win = True for y in range ( len (board)): if board[x, y] ! = player: win = False continue if win = = True : return (win) return (win) # Checks whether the player has three # of their marks in a vertical row def col_win(board, player): for x in range ( len (board)): win = True for y in range ( len (board)): if board[y][x] ! = player: win = False continue if win = = True : return (win) return (win) # Checks whether the player has three # of their marks in a diagonal row def diag_win(board, player): win = True y = 0 for x in range ( len (board)): if board[x, x] ! = player: win = False if win: return win win = True if win: for x in range ( len (board)): y = len (board) - 1 - x if board[x, y] ! = player: win = False return win # Evaluates whether there is # a winner or a tie def evaluate(board): winner = 0 for player in [ 1 , 2 ]: if (row_win(board, player) or col_win(board,player) or diag_win(board,player)): winner = player if np. all (board ! = 0 ) and winner = = 0 : winner = - 1 return winner # Main function to start the game def play_game(): board, winner, counter = create_board(), 0 , 1 print (board) sleep( 2 ) while winner = = 0 : for player in [ 1 , 2 ]: board = random_place(board, player) print ( "Board after " + str (counter) + " move" ) print (board) sleep( 2 ) counter + = 1 winner = evaluate(board) if winner ! = 0 : break return (winner) # Driver Code print ( "Winner is: " + str (play_game())) |
输出:
[[0 0 0] [0 0 0] [0 0 0]] Board after 1 move [[0 0 0] [0 0 0] [1 0 0]] Board after 2 move [[0 0 0] [0 2 0] [1 0 0]] Board after 3 move [[0 1 0] [0 2 0] [1 0 0]] Board after 4 move [[0 1 0] [2 2 0] [1 0 0]] Board after 5 move [[1 1 0] [2 2 0] [1 0 0]] Board after 6 move [[1 1 0] [2 2 0] [1 2 0]] Board after 7 move [[1 1 0] [2 2 0] [1 2 1]] Board after 8 move [[1 1 0] [2 2 2] [1 2 1]] Winner is: 2
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END