本帖最后由 wushaominkk 于 2023-3-17 08:37 编辑
[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 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()
[/mw_shl_code]
想搞成一个窗口小游戏,还不会搞,没学到 |