我正在设置一个2人游戏的井字游戏(这是一项作业)。但是我不明白我犯了什么错误。
def start_move(board, letter, move):
board[move] = letter
def next_player(board, player_1_letter):
#determine 2 players' letter position
if player_1_letter == "X":
player_2_letter = "O"
else:
player_2_letter = "X"
def if_board_full(board):
#if the board is full, back to "True"
for i in range(1, 10):
if free_space(board, i):
return False
return True
print("Welcome to tic tac toe !!!")
while True:
#renew the board
the_board = [" "] * 10
player_1_letter, player_2_letter = input_letter()
turn = first_turn()
print(turn + " will go first.")
gamestarts = True
while gamestarts:
if turn == "Player who choose O.":
gameboard(the_board)
move = firstplayermove(the_board)
start_move(the_board, player_1_letter, move)
if winner(the_board, player_1_letter):
gameboard(the_board)
print("Congratulationsss!!!!" + player_1_letter + " has won!")
gamestarts = False
else:
if free_space(the_board):
gameboard(the_board)
print("Tie!")
break
else:
turn = "Player who choose X"
else:
#player 2's turn
move = next_player(the_board, player_2_letter)
start_move(the_board, player_2_letter, move)
if winner(the_board, player_2_letter):
gameboard(the_board)
print("Player 2 has won!!! Congratulations!")
gamestarts = False
else:
if free_space(the_board):
gameboard(the_board)
print("Tie!!!")
break
else:
turn = "Player who choose O"
if not play_again():
break
我希望游戏(代码)对我来说还可以...但是它一直在说。
追踪(最近一次通话):文件“ C:\ Users \ ASUS \ Desktop \ Python \ projectdemo.py”,第122行,在start_move(the_board,player_2_letter,移动),文件“ C:\ Users \ ASUS \ Desktop \ Python \ projectdemo.py”,第51行,位于start_moveboard [move] =字母TypeError:列表索引必须是整数或切片,而不是NoneType
您尚未将move
定义为board[move]
中的任何内容。它不知道该怎么办。
在第50行上设置move = next_player(the_board, player_2_letter)
。函数next_player()
不返回任何值,因此move
没有获得值。
将next_player()
更改为此:
def next_player(board, player_1_letter):
#determine 2 players' letter position
if player_1_letter == "X":
player_2_letter = "O"
else:
player_2_letter = "X"
return player_2_letter