以下是与问题相关的代码片段。该代码询问用户他/她是否想与其他玩家或计算机对战。代码的PvC部分有效,但是在执行PvP时,player2插入值会崩溃。
创建带有10个空值的板,以便用户可以从1-9而不是0-8输入
board = [' ' for x in range(10)]
打印具有空行和空列的电路板的功能
def printBoard(board):
print(' | |')
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
print(' | |')
print('-----------')
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
print(' | |')
print('-----------')
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
print(' | |')
用于将X或O插入板的功能
def insertLetter(letter, position):
board[position] = letter
使玩家可以输入动作的功能
def playerOneMove():
run = True
while run: #keep looping until we get a valid move
move = input('please select a position to a place an \'X\' (1-9): ')
try:
move = int(move)
if move > 0 and move < 10:
if spaceIsFree(move):
run = False
insertLetter('X', move)
else:
print('This position is occupied!')
else:
print('Please type a number within the range!')
except:
print('Please type a number!')
使玩家两个可以输入动作的功能
def playerTwoMove():
run = True
while run:
move = input('please select a position to place an \'O\' (1-9): ')
try:
move = int(move)
if move > 0 and move < 10:
if spaceIsFree(move):
run = False
insertLetter('O', move)
else:
print('This position is occupied!')
else:
print('Please type a number within the range!')
except:
print('Please type a number!')
检查用户要输入的空格是否可用的功能
def spaceIsFree(position):
#in order to know if the space is free and if it contains a letter.
return board[position] == ' '
检查电路板是否已满的功能
def isBoardFull(board):
if board.count(' ') > 1: #always going to be one blank element in board '' so must be greater than 1
return False
else:
True
运行允许用户再次播放而不是使用计算机的代码。
def optionOne():
print('Welcome to Tic Tac Toe')
printBoard(board)
while not(isBoardFull(board)):
if not(isWinner(board, 'O')):
playerOneMove()
printBoard(board)
else:
print('O\'s win this time...')
break
if not(isWinner(board, 'X')):
move = playerTwoMove()
if move == 0:
print('Game is a Tie! No more spaces left to move.....')
break
else:
insertLetter('O',move)
print('player placed an \'O\' in position',move,':')
printBoard(board)
else:
print('X\'s win. good job!')
break
if isBoardFull(board):
print('Game is a tie! No more spaces left to move.')
while True:
answer = input('do you want to play again? (Y/N)')
if answer.lower() == 'y' or answer.lower == 'YES':
print("""----MENU----
Enter number to selct option from the menu
[1] Play against another player
[2] Play against computer
: """)
option = int(input())
if option == 1:
board = [' ' for x in range(10)]
print('-------------------------------------------')
optionOne()
elif option == 2:
board = [' ' for x in range(10)]
print('-------------------------------------------')
optionTwo()
else:
print("enter a valid option from the menu")
更新此:
while True:
answer = input('do you want to play again? (Y/N)')
if answer.lower() == 'y' or answer.lower == 'YES':
print("""----MENU----
Enter number to selct option from the menu
[1] Play against another player
[2] Play against computer
: """)
对此
while True:
answer = input('do you want to play again? (Y/N)')
if answer.lower() == 'y' or answer.lower() == 'yes':
print("""----MENU----
Enter number to selct option from the menu
[1] Play against another player
[2] Play against computer
: """)
[answer.lower
.lower()
。此外,answer.lower()
绝不等于“ YES”,因为.lower()将确保您的值中没有大写字母。您可以改为针对answer.upper()
进行测试,并使用“ YES”检查是否相等]