Python 五子棋游戏代码实现

import numpy as np

# 定义棋盘的大小
BOARD_SIZE = 15

# 定义棋子的类型
EMPTY = 0
BLACK = 1
WHITE = 2

# 定义棋子的显示符号
SYMBOLS = {
    EMPTY: '.',
    BLACK: 'x',
    WHITE: 'o',
}

# 初始化棋盘
board = np.zeros((BOARD_SIZE, BOARD_SIZE), dtype=np.int8)

# 定义函数:打印棋盘
def print_board():
    print('   ', end='')
    for i in range(BOARD_SIZE):
        print(chr(ord('a') + i), end=' ')
    print()
    for i in range(BOARD_SIZE):
        print('%2d ' % (i + 1), end='')
        for j in range(BOARD_SIZE):
            print(SYMBOLS[board[i][j]], end=' ')
        print()

# 定义函数:判断落子是否合法
def is_valid_move(x, y):
    return x >= 0 and x < BOARD_SIZE and y >= 0 and y < BOARD_SIZE and board[x][y] == EMPTY

# 定义函数:判断是否有五子连珠
def has_five_in_a_row(x, y):
    # 水平方向
    count = 1
    i = 1
    while x + i < BOARD_SIZE and board[x + i][y] == board[x][y]:
        count += 1
        i += 1
    i = 1
    while x - i >= 0 and board[x - i][y] == board[x][y]:
        count += 1
        i += 1
    if count >= 5:
        return True
    # 垂直方向
    count = 1
    i = 1
    while y + i < BOARD_SIZE and board[x][y + i] == board[x][y]:
        count += 1
        i += 1
    i = 1
    while y - i >= 0 and board[x][y - i] == board[x][y]:
        count += 1
        i += 1
    if count >= 5:
        return True
    # 左上到右下方向
    count = 1
    i = 1
    while x + i < BOARD_SIZE and y + i < BOARD_SIZE and board[x + i][y + i] == board[x][y]:
        count += 1
        i += 1
    i = 1
    while x - i >= 0 and y - i >= 0 and board[x - i][y - i] == board[x][y]:
        count += 1
        i += 1
    if count >= 5:
        return True
    # 左下到右上方向
    count = 1
    i = 1
    while x + i < BOARD_SIZE and y - i >= 0 and board[x + i][y - i] == board[x][y]:
        count += 1
        i += 1
    i = 1
    while x - i >= 0 and y + i < BOARD_SIZE and board[x - i][y + i] == board[x][y]:
        count += 1
        i += 1
    if count >= 5:
        return True
    return False

# 游戏主循环
player = BLACK
while True:
    print_board()
    if player == BLACK:
        print('黑方落子:', end='')
    else:
        print('白方落子:', end='')
    move = input().strip()
    if move == 'q':
        break
    x, y = ord(move[0]) - ord('a'), int(move[1:]) - 1
    if not is_valid_move(x, y):
        print('落子不合法!')
        continue
    board[x][y] = player
    if has_five_in_a_row(x, y):
        print_board()
        if player == BLACK:
            print('黑方获胜!')
        else:
            print('白方获胜!')
        break
    player = WHITE if player == BLACK else BLACK

功能说明:

  • 初始化一个 15x15 的棋盘。
  • 定义棋子类型:EMPTY、BLACK 和 WHITE,以及对应的显示符号。
  • 定义函数print_board()打印棋盘,并用不同的符号表示不同的棋子。
  • 定义函数is_valid_move()判断落子是否合法。
  • 定义函数has_five_in_a_row()判断是否有五子连珠。
  • 游戏主循环:
    • 打印棋盘。
    • 提示玩家落子。
    • 读取玩家输入的落子位置。
    • 判断落子是否合法。
    • 将棋子落到棋盘上。
    • 判断是否有五子连珠,如果有则游戏结束。
    • 切换玩家。

使用方法:

  1. 将代码保存为 .py 文件。
  2. 运行代码,即可开始游戏。
  3. 玩家输入落子位置,例如 a1 表示在棋盘的第一个格子上落子。
  4. 输入 q 退出游戏。

注意:

  • 代码只实现了简单的五子棋游戏,没有 AI 对战功能。
  • 代码中的 BOARD_SIZE 可以修改棋盘的大小。
  • 代码中的 SYMBOLS 可以修改棋子的显示符号。
Python 五子棋游戏代码实现

原文地址: https://www.cveoy.top/t/topic/m5IN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录