import os import sys BOARD_SIZE = 15 def print_board(board): for row in board: print(" ".join(row)) def check_win(board, x, y, player): directions = [(1, 1), (1, 0), (1, 1), (0, 1)] for dx, dy in directions: count = 1 for i in range(1, 5): nx, ny = x + i * dx, y + i * dy if 0 <= nx < BOARD_SIZE and 0 <= ny < BOARD_SIZE and board[nx][ny] == player: count += 1 else: break for i in range(1, 5): nx, ny = x i * dx, y i * dy if 0 <= nx < BOARD_SIZE and 0 <= ny < BOARD_SIZE and board[nx][ny] == player: count += 1 else: break if count >= 5: return True return False def main(): board = [["." for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)] players = ["X", "O"] current_player = 0 while True: os.system("clear") print_board(board) print(f"轮到 {players[current_player]} 下棋") x, y = map(int, input("请输入坐标(格式:x y):").split()) if board[x][y] != ".": print("该位置已有棋子,请重新输入!") continue board[x][y] = players[current_player] if check_win(board, x, y, players[current_player]): os.system("clear") print_board(board) print(f"{players[current_player]} 获胜!") break current_player = 1 current_player if __name__ == "__main__": main()
这是一个简单的五子棋游戏源码,使用Python编写,游戏棋盘大小为15×15,玩家可以输入坐标来下棋,游戏会检查每次下棋后是否有一方获胜,如果有一方获胜,游戏结束。
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1071061.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复