,这个代码中程序运行忽略了对输赢的判断以及预测玩家下棋的输赢预测堵截,有大神可以指导一下问题点解决方法吗,不要太难基础的,谢谢
#和人类对手下井字棋
#Tic-Tac-Toe(伪码)
# 显示该游戏的操作指南
# 决定谁先走
# 创建一个空的井字棋棋盘
# 把棋盘显示出来
# 当没有人获胜且不是平局时
# 如果轮到玩家
# 得到玩家的行棋位置
# 根据行棋位置更新棋盘
# 否则
# 计算得出机器人的行棋位置
# 根据行棋位置更新棋盘
# 显示棋盘
# 切换行棋方
# 向赢家表示恭喜或声明平局
#全局变量
X = "X"
O = "O"
EMPTY = " "#表示棋盘上的一个空方格
TIE = "TIE"#表示平局
NUM_SQUARES = 9#是井字棋盘上的方格数
def display_instruct():#打印游戏说明
"""Display game instructions."""
print(
"""
Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe.
This will be a showdown between your human brain and my silicon processor.
You will make your move known by entering a number,0 - 8. The number will
correspnd to the board position as illustrated:
0 | 1 | 2
---------
3 | 4 | 5
---------
6 | 7 | 8
Prepare yourself, human. The ultimate battle is about to begin. \n
""")
def ask_yes_no(question):#接收一个问题,返回“y”或“n”
"""Ask a yes or no question."""
response = None
while response not in ("y","n"):
response = input(question).lower()
return response
def ask_number(question,low,high):#请求用户给出指定范围的一个数字
"""Ask for a number within a range."""
response = None
while response not in range(low,high):
response = int(input(question))
return response
def pieces():#询问玩家是否希望先行棋,然后据此返回机器人和玩家的棋子
"""Determine if player or computer goes first."""#根据井字棋的玩法继续走
go_first = ask_yes_no("Do you require the first move?(y/n):")
#该函数调用了另一个函数(ask_yes_no())。这是没有问题的
#任何函数都可调用别的函数
if go_first == "y":
print("\nThen take the first move. You will meed it.")
human = X
computer = O
else:
print("\nYour bravery will be your undoing…… I will go first.")
computer = X
human = O
return computer,human
def new_board():#创建新棋盘(一个长度为9的列表,各元素均被设置为EMPTY),然后将其返回
"""Create new game board."""
board = []
for square in range(NUM_SQUARES):
board.append(EMPTY)
return board
def display_board(board):#会将传给他的棋盘显示出来
"""Display game board on screen."""
print("\n\t", board[0], "|", board[1], "|", board[2])
print("\t","---------")
print("\t", board[3], "|", board[4], "|", board[5])
print("\t","---------")
print("\t", board[6], "|", board[7], "|", board[8], "\n")
def legal_moves(board):#对棋盘进行迭代,找到一个空方格,就添加到合法行棋的列表中
"""Create list of legal moves."""
moves = []
for square in range(NUM_SQUARES):
if board[square] == EMPTY:
moves.append(square)
return moves#返回合法行棋的列表
def winner(board):
"""Determine the game winner."""
WAYS_TO_WIN = ((0, 1, 2),
(3, 4, 5),
(6, 7, 8),
(0, 3, 6),
(1, 4, 7),
(2, 5, 8),
(0, 4, 8),
(2, 4, 6))
for row in WAYS_TO_WIN:
if board[row[0]] == board[row[1]] == board[row[2]] != EMPTY:
winner = board[row[0]]
return winner
if EMPTY not in board:
return TIE
return None
def human_move(board,human):
"""Get human move"""
legal = legal_moves(board)
move = None
while move not in legal:
move = ask_number("Where will you move?(0 - 8):",0,NUM_SQUARES)
if move not in legal:
print("\nThat square is already occupied,foolish human.Choose another.\n")
print("Fine……")
return move
def computer_move(board,computer,human):
"""Make computer move"""
#由于该函数会对列表造成修改,所以需要创建一个副本
board = board[:]
#按优劣顺序排列的行棋位置
BEST_MOVES =(4,0,2,6,8,1,3,5,7)
print("I shall take square number", end = " ")
#如果机器人能赢,就走那个位置
for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print(move)
return move
#结束对当前行棋方案的测试,并取消
board[move] = EMPTY
#如果玩家可以赢,就堵住那个位置
for move in legal_moves(board):
board[move] = human
if winner(board) == human:
print(move)
return move
#结束对当前行棋方案的测试,并取消
board[move] = EMPTY
#由于本轮输液赢不了,所以挑选最佳的空位来走
for move in BEST_MOVES:
if move in legal_moves(board):
print(move)
return move
def next_turn(turn):
"""Switch turns"""
if turn == X:
return O
else:
return X
def congrat_winner(the_winner,computer,human):
"""Congratulate the winner."""
if the_winner != TIE:
print(the_winner, "won!\n")
else:
print("It's a tie!\n")
if the_winner == computer:
print("As I predicted,human, I am triumphant once more. \n" \
"Proof that computers are superior to humans in all regards.")
elif the_winner == human:
print("No,no! It canot be! Somehow you tricked me,human. \n" \
"Celebrate today……for this is the best you will ever achieve.")
def main():
display_instruct()
computer,human = pieces()
turn = X
board = new_board()
display_board(board)
while not winner(board):
if turn == human:
move = human_move(board,human)
board[move] = human
else:
move = computer_move(board,computer,human)
board[move] = computer
display_board(board)
turn = next_turn(turn)
the_winner = winner(board)
congrat_winner(the_winner,computer,human)
#启动程序
main()
input("\n\nPress the enter key to quit.")
相关推荐
多机器人协同引导非凝聚目标:障碍感知下的智能牧羊策略与实践
多智能体系统与机器人协同控制是当前机器人研究的前沿领域,其核心在于通过分布式决策与协调,使多个机器人能够共同完成复杂任务。其基本原理涉及感知、规划、控制与通信的深度融合,通过共享环境信息与任务状态,实现高效的任务分配与执行。这一技术的核心价值在于能够应对动态、不确定的复杂环境,提升系统的鲁棒性与适应性。在物流分拣、灾难救援、无人集群引导等场景中,多机器人系统展现出巨大潜力。本文聚焦于多机器人障碍感知非凝聚目标牧羊这一具体应用,探讨了在目标缺乏自然内聚力的条件下,如何通过分层决策架构、混合导航算法以及动态任务
应对2026年DevSecOps合规风暴:AI代码工具安全审计与SITS2026标准解读
在软件开发生命周期中,DevSecOps通过将安全实践左移和自动化,实现了安全与开发运维的深度融合。其核心原理在于通过持续集成/持续部署(CI/CD)流水线嵌入安全门禁,确保每次代码提交都经过自动化安全测试。这一模式的技术价值在于能够显著降低漏洞修复成本,提升软件供应链的整体安全性。随着AI代码生成工具的广泛应用,其生成代码的安全性和合规性成为新的挑战。SITS2026标准应运而生,为AI代码工具提供了全面的安全审计框架。本文聚焦于SITS2026标准与ISO/IEC 27001 DevSecOps专项认证
DeepSeek-V4-Pro前端实测:代码稳定性、视觉理解与工程落地深度分析
大模型生成前端代码正从概念验证走向真实工程应用,其核心能力涵盖语法正确性、UI视觉表达、交互逻辑完整性及性能可控性。DeepSeek-V4-Pro作为支持百万上下文的国产大模型,在CSS水墨渲染、分形动画、物理模拟等数学密集型任务中展现出高准确率与强一致性;但在长代码标点稳定性、跨帧状态管理、硬件感知优化等工程细节上仍存明显短板。本文基于6个可验证单HTML前端案例,结合JCode本地直连API测试方法,系统揭示其Token消耗规律、缓存增益边界与典型错误模式,为开发者提供面向生产环境的提示词约束策略、ES
吃豆人游戏开发:从核心机制到复刻实现
游戏开发的核心在于理解其底层机制与设计逻辑。以经典的吃豆人街机游戏为例,其看似简单的玩法背后,是一套精密的网格化移动系统、基于规则的状态机管理以及独特的幽灵AI行为模式。这些机制共同构成了游戏的核心循环,确保了游戏的可玩性与挑战性。在技术实现层面,开发者需要处理碰撞检测、路径寻找算法以及游戏状态管理等问题。通过复刻吃豆人,开发者不仅能深入理解游戏设计的基本原则,还能掌握游戏开发中的关键技术,如状态机、AI行为树和性能优化。本文将从游戏机制解构出发,探讨如何从零开始实现一个原汁原味的吃豆人复刻版,并分享在开发
春秋BC742年世界历史地图矢量文件SHP格式.zip
本资源为《春秋BC742年世界历史地图矢量文件SHP格式》压缩包,文件大小约 26.55 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。


