,这个代码中程序运行忽略了对输赢的判断以及预测玩家下棋的输赢预测堵截,有大神可以指导一下问题点解决方法吗,不要太难基础的,谢谢

weixin_45573123 2019-10-26 01:43:52
#Tic-Tac-Toe
#和人类对手下井字棋


#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.")
...全文
63 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧