# 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 x in range(BOARD_SIZE):
for y in range(BOARD_SIZE):
if board[x][y] == player:
# 水平方向
if y <= BOARD_SIZE - 5 and all(board[x][y + i] == player for i in range(5)):
return True
# 垂直方向
if x <= BOARD_SIZE - 5 and all(board[x + i][y] == player for i in range(5)):
return True
# 主对角线方向
if x <= BOARD_SIZE - 5 and y <= BOARD_SIZE - 5 and all(board[x + i][y + i] == player for i in range(5)):
return True
# 副对角线方向
if x >= 4 and y <= BOARD_SIZE - 5 and all(board[x - i][y + i] == player for i 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()python教程
python版五子棋小游戏源代码
python教程
51源码
2023-03-27
共人阅读
版权声明:文章搜集于网络,如有侵权请联系本站,转载请说明出处:https://www.51yma.cn/jiaocheng/python/1181.html
文章来源:
上一篇:python字典的底层原理
下一篇: 返回列表
热门推荐
-
01Day2:列表与字典 22
-
02python写的淘了个宝抢购小帮手 17
-
03Python自动爬取轻壁纸网站脚本 16
-
04hook框架frida某个类下的所有方法名字模板 16
-
05python 基础练习题 16
