简易五子棋小游戏(Python)
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对战。