# 五子棋棋盘类
class RenjuBoard:
def __init__(self):
self._board = [[EMPTY] * BOARD_SIZE for _ in range(BOARD_SIZE)]
# 重置棋盘
def reset(self):
for row in range(BOARD_SIZE):
self._board[row] = [EMPTY] * BOARD_SIZE
# 下棋
def move(self, row, col, is_black):
if self._board[row][col] == EMPTY:
self._board[row][col] = BLACK if is_black else WHITE
return True
return False
# 绘制棋盘与棋子
def draw(self, screen):
# 画棋盘线
for h in range(1, BOARD_SIZE + 1):
pygame.draw.line(screen, BLACK_COLOR, [GRID_SIZE, h * GRID_SIZE], [BOARD_SIZE * GRID_SIZE, h * GRID_SIZE], 1)
pygame.draw.line(screen, BLACK_COLOR, [h * GRID_SIZE, GRID_SIZE], [h * GRID_SIZE, BOARD_SIZE * GRID_SIZE], 1)
# 画外框
pygame.draw.rect(screen, BLACK_COLOR, [GRID_SIZE - BORDER_WIDTH, GRID_SIZE - BORDER_WIDTH, (BOARD_SIZE + 1) * GRID_SIZE, (BOARD_SIZE + 1) * GRID_SIZE], BORDER_WIDTH)
# 画棋盘特殊点位
pygame.draw.circle(screen, BLACK_COLOR, [GRID_SIZE * 8, GRID_SIZE * 8], 5, 0) # 天元点
for x in [GRID_SIZE * 4, GRID_SIZE * 12]:
for y in [GRID_SIZE * 4, GRID_SIZE * 12]:
pygame.draw.circle(screen, BLACK_COLOR, [x, y], 3, 0)
# 画棋子
for row in range(BOARD_SIZE):
for col in range(BOARD_SIZE):
if self._board[row][col] != EMPTY:
color = BLACK_COLOR if self._board[row][col] == BLACK else WHITE_COLOR
pos = [GRID_SIZE * (col + 1), GRID_SIZE * (row + 1)]
pygame.draw.circle(screen, color, pos, 18, 0)