python版五子棋小游戏源代码

# coding:UTF-8

# [url=home.php?mod=space&uid=238618]@Time[/url] : 2023/3/16 21:17

# @Autor : 菜皇

# [url=home.php?mod=space&uid=267492]@file[/url] : damo1.py

# [url=home.php?mod=space&uid=371834]@SOFTWARE[/url] : PyCharm

  

import sys

  

BOARD_SIZE = 15

  

# 初始化棋盘

def init_board():

    board = [['.' for in range(BOARD_SIZE)] for in range(BOARD_SIZE)]

    return board

  

# 绘制棋盘

def print_board(board):

    for row in board:

        print(" ".join(row))

    print()

  

# 检查是否有玩家获胜

def check_winner(board, player):

    for in range(BOARD_SIZE):

        for in range(BOARD_SIZE):

            if board[x][y] == player:

                # 水平方向

                if y <= BOARD_SIZE - 5 and all(board[x][y + i] == player for in range(5)):

                    return True

                # 垂直方向

                if x <= BOARD_SIZE - 5 and all(board[x + i][y] == player for in range(5)):

                    return True

                # 主对角线方向

                if x <= BOARD_SIZE - 5 and y <= BOARD_SIZE - 5 and all(board[x + i][y + i] == player for in range(5)):

                    return True

                # 副对角线方向

                if x >= 4 and y <= BOARD_SIZE - 5 and all(board[x - i][y + i] == player for in range(5)):

                    return True

    return False

  

def main():

    board = init_board()

    print_board(board)

    players = ['X''O']

    current_turn = 0

  

    while True:

        try:

            x, y = map(int,

                       input("玩家 {} 的回合,请输入落子坐标(逗号分隔,0-14):".format(players[current_turn])).split(','))

        except ValueError:

            print("输入错误,请输入逗号分隔的两个数字(0-14)。")

            continue

  

        if x < 0 or x >= BOARD_SIZE or y < 0 or y >= BOARD_SIZE:

            print("坐标超出范围,请输入0-14之间的数字。")

            continue

  

        if board[x][y] != '.':

            print("该位置已有棋子,请重新输入。")

            continue

  

        board[x][y] = players[current_turn]

        print_board(board)

  

        if check_winner(board, players[current_turn]):

            print("恭喜玩家 {} 获胜!".format(players[current_turn]))

            break

  

        current_turn = (current_turn + 1% 2

  

if __name__ == "__main__":

    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执刀人的工具库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值