简单的2个玩家Tic Tac Toe Python

时间:2019-10-14 08:59:39

标签: python

我正在设置一个2人游戏tic tac toe游戏(这是一项作业)。但是我不明白我犯了什么错误。

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_move       board [move] =字母TypeError:列表索引必须是整数或切片,而不是NoneType

2 个答案:

答案 0 :(得分:1)

在第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

答案 1 :(得分:0)

您尚未在move中将board[move]定义为任何内容。它不知道该怎么办。