试图为两个人类付款人tic tac toe游戏编写代码,但出现错误。已成功制造了一台计算机和一台人,但被卡在这台计算机中。如果有人可以帮助我找到错误,那将非常好,谢谢
def drawBoard(board):
# This function prints out the board that it was passed.
# "board" is a list of 10 strings representing the board (ignore index 0)
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
print('---+---+---')
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
print('---+---+---')
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
def inputPlayerLetter():
# Lets the player type which letter they want to be.
# Returns a list with the player’s letter as the first item, and the computer's letter as the second.
letter = ''
while not (letter == 'X' or letter == 'O'):
print('Do you want to be X or O?')
letter = input().upper()
# the first element in the list is the player’s letter, the second is the computer's letter.
if letter == 'X':
return ['X', 'O']
else:
return ['O', 'X']
def whoGoesFirst():
# Randomly choose the player who goes first.
if random.randint(0, 1) == 0:
return 'player2'
else:
return 'player1'
# This function returns True if the player wants to play again, otherwise it returns False.
def playAgain():
print('Do you want to play again? (yes or no)')
return input().lower().startswith('y')
def makeMove(board, letter, move):
board[move] = letter
# Given a board and a player’s letter, this function returns True if that player has won.
# We use bo instead of board and le instead of letter so we don’t have to type as much.
def isWinner(bo, le):
return ((bo[1] == le and bo[2] == le and bo[3] == le) or # across the top
(bo[4] == le and bo[5] == le and bo[6] == le) or # across the middle
(bo[7] == le and bo[8] == le and bo[9] == le) or # across the bottom
(bo[1] == le and bo[4] == le and bo[7] == le) or # down the left side
(bo[2] == le and bo[5] == le and bo[8] == le) or # down the middle
(bo[3] == le and bo[6] == le and bo[9] == le) or # down the right side
(bo[1] == le and bo[5] == le and bo[9] == le) or # diagonal
(bo[3] == le and bo[5] == le and bo[7] == le)) # diagonal
# Make a duplicate of the board list and return it the duplicate.
def getBoardCopy(board):
dupeBoard = []
for i in board:
dupeBoard.append(i)
return dupeBoard
# Return true if the passed move is free on the passed board.
def isSpaceFree(board, move):
return board[move] == ' '
# Let the player type in their move.
def getPlayer1Move(board):
move = ' '
while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):
print('What is your next move? (1-9)')
move = input()
return int(move)
def getPlayer2Move(board):
move = ' '
while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):
print('What is your next move? (1-9)')
move = input()
return int(move)
# Returns a valid move from the passed list on the passed board.
# Returns None if there is no valid move.
# Given a board and the computer's letter, determine where to move and return that move.
def getPlayer2Move(board, player2Letter):
if player2Letter == 'X':
player1Letter = 'O'
else:
player1Letter = 'X'
# Here is our algorithm for our Tic Tac Toe AI:
# First, check if we can win in the next move
print('Welcome to Tic Tac Toe!')
# Reset the board
while True:
theBoard = [' '] * 10
player1Letter, player2Letter = inputPlayerLetter()
turn = whoGoesFirst()
print('The ' + turn + ' will go first.')
gameIsPlaying = True
# Player’s turn.
while gameIsPlaying:
if turn == 'player1':
drawBoard(theBoard)
move = getPlayer1Move(theBoard)
makeMove(theBoard, player1Letter, move)
if isWinner(theBoard, player1Letter):
drawBoard(theBoard)
print('Hooray! You have won the game!')
gameIsPlaying = False
else:
if isBoardFull(theBoard):
drawBoard(theBoard)
print('The game is a tie!')
break
else:
turn = 'player2'
else:
# Computer’s turn
move = getplayer2Move(theBoard, player2Letter)
makeMove(theBoard, player2Letter, move)
if isWinner(theBoard, player2Letter):
drawBoard(theBoard)
print('The computer has beaten you! You lose.')
gameIsPlaying = False
else:
if isBoardFull(theBoard):
drawBoard(theBoard)
print('The game is a tie!')
break
else:
turn = 'player1'
if not playAgain():
break
曾经尝试过人类计算机,但是想这样做,所以不确定我在做什么错代码。
答案 0 :(得分:0)
您的功能似乎还可以。要使其起作用:
因此,结果如下:
import random
def drawBoard(board):
# This function prints out the board that it was passed.
# "board" is a list of 10 strings representing the board (ignore index 0)
print(' ' + board[0] + ' | ' + board[1] + ' | ' + board[2])
print('---+---+---')
print(' ' + board[3] + ' | ' + board[4] + ' | ' + board[5])
print('---+---+---')
print(' ' + board[6] + ' | ' + board[7] + ' | ' + board[8])
def inputPlayerLetter():
# Lets the player type which letter they want to be.
# Returns a list with the player’s letter as the first item, and the computer's letter as the second.
letter = ''
while not (letter == 'X' or letter == 'O'):
print('Do you want to be X or O?')
letter = input().upper()
# the first element in the list is the player’s letter, the second is the computer's letter.
if letter == 'X':
return ['X', 'O']
return ['O', 'X']
def whoGoesFirst():
# Randomly choose the player who goes first.
if random.randint(0, 1) == 0:
return 'player2'
return 'player1'
# This function returns True if the player wants to play again, otherwise it returns False.
def playAgain():
print('Do you want to play again? (yes or no)')
return input().lower().startswith('y')
def makeMove(board, letter, move):
board[move-1] = letter
# Given a board and a player’s letter, this function returns True if that player has won.
# We use bo instead of board and le instead of letter so we don’t have to type as much.
def isWinner(bo, le):
return ((bo[0] == le and bo[1] == le and bo[2] == le) or # across the top
(bo[3] == le and bo[4] == le and bo[5] == le) or # across the middle
(bo[6] == le and bo[7] == le and bo[8] == le) or # across the bottom
(bo[0] == le and bo[3] == le and bo[6] == le) or # down the left side
(bo[1] == le and bo[4] == le and bo[7] == le) or # down the middle
(bo[2] == le and bo[5] == le and bo[8] == le) or # down the right side
(bo[0] == le and bo[4] == le and bo[8] == le) or # diagonal
(bo[2] == le and bo[4] == le and bo[6] == le)) # diagonal
# Return true if the passed move is free on the passed board.
def isSpaceFree(board, move):
return board[move-1] == ' '
# Let the player type in their move.
def getPlayerMove(board, player):
move = 0
while move not in range(1, 10) or not isSpaceFree(board, move):
print(f'What is your next move {player}? (1-9)')
move = int(input())
return move
def isBoardFull(board):
return not any([x == ' ' for x in board])
# Here is our algorithm for our Tic Tac Toe AI:
# First, check if we can win in the next move
print('Welcome to Tic Tac Toe!')
# Reset the board
while True:
theBoard = [' '] * 9
player1Letter, player2Letter = inputPlayerLetter()
turn = whoGoesFirst()
print('The ' + turn + ' will go first.')
gameIsPlaying = True
# Player’s turn.
while gameIsPlaying:
# Player's move
if turn == "player1":
letter = player1Letter
else:
letter = player2Letter
drawBoard(theBoard)
move = getPlayerMove(theBoard, turn)
makeMove(theBoard, letter, move)
# Check if the game is over
if isWinner(theBoard, letter):
drawBoard(theBoard)
print('Hooray! You have won the game!')
gameIsPlaying = False
elif isBoardFull(theBoard):
drawBoard(theBoard)
print('The game is a tie!')
gameIsPlaying = False
# Change player's turn
if turn == "player1":
turn = "player2"
else:
turn = "player1"
if not playAgain():
print("Goodbye!")
break