当然!以下是一个使用 Python 编写的简单五子棋游戏示例:

# 导入必要的模块
import numpy as np

# 定义棋盘大小
BOARD_SIZE = 15

# 创建棋盘
board = np.zeros((BOARD_SIZE, BOARD_SIZE))

# 定义棋手
player = 1

# 判断当前位置是否合法
def is_valid_move(x, y):
    return x >= 0 and x < BOARD_SIZE and y >= 0 and y < BOARD_SIZE and board[x][y] == 0

# 下子
def move(x, y, player):
    board[x][y] = player

# 判断是否五子连珠
def is_win(x, y):
    directions = [(1, 0), (0, 1), (1, 1), (1, -1)]  # 横、竖、对角线方向
    for dx, dy in directions:
        count = 1
        tx, ty = x, y
        while count < 5:
            tx += dx
            ty += dy
            if not is_valid_move(tx, ty) or board[tx][ty] != player:
                break
            count += 1
        if count == 5:
            return True
    return False

# 游戏主循环
while True:
    print(board)
    print('请玩家', player, '下棋:')
    x = int(input('请输入行号:'))
    y = int(input('请输入列号:'))
    if is_valid_move(x, y):
        move(x, y, player)
        if is_win(x, y):
            print('玩家', player, '获胜!')
            break
        player = 2 if player == 1 else 1
    else:
        print('该位置已被占据或超出棋盘范围,请重新输入!')

这只是一个简单的示例,可能还有许多功能和优化可以添加。您可以根据自己的需求进行修改和扩展。希望这个示例对您有帮助!

Python 五子棋游戏代码示例:简单易懂,轻松入门

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

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