标题: OpenAI ChatGPT用Python写的五子棋框架代码 创建: 2023-02-09 15:21 链接: https://scz.617.cn/python/202302091521.txt 测试如下 pip3 install pygame vi wuzigame_gui.py python3 wuzigame_gui.py 非人机对战,只是提供交互式GUI。鼠标点击走棋,自动检查五子连线,一旦出现, 游戏退出。最初ChatGPT提供的check_win是空函数,说留给我练手;我说你还是补全 吧,它就单补了个check_win实现,两相一凑,成了。我说你这个有URL吗,它说它自 个儿写的,没有URL;但我未放狗验证丫是否自吹自擂。对话都是用英文进行的。 -------------------------------------------------------------------------- import pygame # initialize game engine pygame.init() # set screen size SCREEN_SIZE = (500, 500) screen = pygame.display.set_mode(SCREEN_SIZE) # set title pygame.display.set_caption("Wuzi Game") # define colors RED = (255, 0, 0) BLACK = (0, 0, 0) BLUE = (0, 0, 255) # initialize board board = [] for row in range(15): board.append([0] * 15) # draw board function def draw_board(): for row in range(15): for col in range(15): pygame.draw.rect(screen, RED, (col * 33 + 3, row * 33 + 3, 30, 30), 1) # draw pieces function def draw_pieces(board): for row in range(15): for col in range(15): if board[row][col] == 1: pygame.draw.circle(screen, BLACK, (col * 33 + 16, row * 33 + 16), 14) elif board[row][col] == 2: pygame.draw.circle(screen, BLUE, (col * 33 + 16, row * 33 + 16), 14) # check if the game is won def check_win(board, x, y): # Check rows for row in range(15): for col in range(11): if board[row][col] != 0 and all(board[row][col] == board[row][col + i] for i in range(5)): return board[row][col] # Check columns for col in range(15): for row in range(11): if board[row][col] != 0 and all(board[row][col] == board[row + i][col] for i in range(5)): return board[row][col] # Check left-to-right diagonals for row in range(11): for col in range(11): if board[row][col] != 0 and all(board[row][col] == board[row + i][col + i] for i in range(5)): return board[row][col] # Check right-to-left diagonals for row in range(11): for col in range(4, 15): if board[row][col] != 0 and all(board[row][col] == board[row + i][col - i] for i in range(5)): return board[row][col] # No win return 0 # game loop running = True turn = 1 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONUP: x, y = pygame.mouse.get_pos() row = y // 33 col = x // 33 if board[row][col] == 0: board[row][col] = turn if check_win(board, row, col): running = False turn = 3 - turn screen.fill((255, 255, 255)) draw_board() draw_pieces(board) pygame.display.update() # quit game engine pygame.quit() --------------------------------------------------------------------------