简易五子棋小游戏(Python)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | import random MAX_SIZE = 5 def print_board(board): # 打印棋盘,并加上行号和列号 print ( "\t" + "\t" .join( str (i) for i in range ( 1 , MAX_SIZE + 1 ))) for i in range (MAX_SIZE): print ( str (i + 1 ), end = "\t" ) for j in range (MAX_SIZE): if board[i][j] = = 'X' : print ( "\033[1;31mX\033[0m" , end = "\t" ) # 红色表示玩家X elif board[i][j] = = 'O' : print ( "\033[1;34mO\033[0m" , end = "\t" ) # 蓝色表示玩家O else : print ( "." , end = "\t" ) print () def ai_move(board): # 随机选择一个可用位置 available_moves = [(i, j) for i in range (MAX_SIZE) for j in range (MAX_SIZE) if board[i][j] = = "." ] return random.choice(available_moves) if available_moves else None def check_end(board, row, col, player): if check_win(board, row, col, player): return True # 检查平局 if is_board_full(board): return True return False def is_board_full(board): for row in board: for cell in row: if cell = = '.' : return False return True def check_win(board, row, col, player): directions = [( 1 , 0 ), ( 0 , 1 ), ( 1 , 1 ), ( 1 , - 1 )] # 水平、垂直、正斜、反斜 for dr, dc in directions: count = 1 # 包括当前位置 # 检查正方向 for i in range ( 1 , 5 ): r = row + dr * i c = col + dc * i if 0 < = r < MAX_SIZE and 0 < = c < MAX_SIZE and board[r][c] = = player: count + = 1 else : break # 检查负方向 for i in range ( 1 , 5 ): r = row - dr * i c = col - dc * i if 0 < = r < MAX_SIZE and 0 < = c < MAX_SIZE and board[r][c] = = player: count + = 1 else : break if count > = 5 : return True return False def get_valid_input(prompt, max_value): while True : try : value = int ( input (prompt)) if 1 < = value < = max_value: return value - 1 else : print (f "Please enter a number between 1 and {max_value}." ) except ValueError: print ( "Please enter a valid number." ) def main(): print ( "Welcome to Five in a Row Game!" ) print ( "Rules:" ) print ( "1. Players take turns to place their pieces on the board." ) print ( "2. The first player to get five of their pieces in a row, horizontally, vertically, or diagonally, wins." ) print ( "3. The game ends in a draw if the board is full and no player has won." ) print ( "4. Rows and columns are numbered from 1 to 15." ) print ( "Let's start the game!\n" ) while True : board = [[ "." ] * MAX_SIZE for _ in range (MAX_SIZE)] players = [ 'X' , 'O' ] current_player = 0 while True : print ( "\nCurrent Board:" ) print_board(board) if players[current_player] = = 'X' : print (f "\nPlayer {players[current_player]}'s turn:" ) row = get_valid_input(f "Enter row (1-{MAX_SIZE}): " , MAX_SIZE) col = get_valid_input(f "Enter col (1-{MAX_SIZE}): " , MAX_SIZE) else : print ( "\nAI's turn:" ) ai_row, ai_col = ai_move(board) row = ai_row col = ai_col print (f "AI placed its piece at row {row+1}, col {col+1}" ) if board[row][col] = = "." : board[row][col] = players[current_player] if check_win(board, row, col, players[current_player]): print ( "\nCurrent Board:" ) print_board(board) print (f "\nPlayer {players[current_player]} wins!" ) break if check_end(board, row, col, players[current_player]): print ( "It's a draw!" ) break current_player = (current_player + 1 ) % 2 else : print ( "That spot is already taken!" ) play_again = input ( "Do you want to play again? (yes[Y]/no[N]): " ) if play_again.lower() ! = "Y" : print ( "Thank you for playing! Goodbye!" ) break |
-
print_board(board)
: 这个函数用于打印当前的游戏棋盘,包括玩家的棋子以及行号列号。 -
ai_move(board)
: 这个函数是AI的移动函数,它会随机选择一个可用的位置进行落子。 -
check_end(board, row, col, player)
: 这个函数检查游戏是否结束,包括检查玩家是否连成五子或者棋盘是否填满。 -
is_board_full(board)
: 这个函数检查棋盘是否已满,如果棋盘上没有空位置了,则返回True,表示平局。 -
check_win(board, row, col, player)
: 这个函数检查在给定的位置(row, col)下,某个玩家是否已经连成五子。 -
get_valid_input(prompt, max_value)
: 这个函数用于获取有效的玩家输入,确保输入的行列号在合法范围内。 -
main()
: 这是游戏的主函数,负责整个游戏的流程控制,包括初始化棋盘、玩家轮流落子、检查游戏是否结束以及询问是否重新开始游戏。
总体来说,这个程序提供了一个简单的五子棋游戏,玩家可以与一个简单的AI对战。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律