以下是Python五子棋代码示例:
import numpy as np
import tkinter as tk
WIDTH = 600
HEIGHT = 600
MARGIN = 30
GRID_SIZE = 30
ROWS = (HEIGHT - 2 * MARGIN) // GRID_SIZE
COLS = (WIDTH - 2 * MARGIN) // GRID_SIZE
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
EMPTY = 0
PLAYER_ONE = 1
PLAYER_TWO = 2
class Game:
def __init__(self):
self.board = np.zeros((ROWS, COLS))
self.turn = PLAYER_ONE
self.winner = None
self.game_over = False
def play_turn(self, row, col):
if self.is_valid_move(row, col):
self.board[row][col] = self.turn
if self.check_winner(row, col):
self.winner = self.turn
self.game_over = True
self.switch_turn()
def is_valid_move(self, row, col):
if self.game_over:
return False
if 0 <= row < ROWS and 0 &l