Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python上纠正井字游戏的While循环?_Python_While Loop_Tic Tac Toe - Fatal编程技术网

如何在Python上纠正井字游戏的While循环?

如何在Python上纠正井字游戏的While循环?,python,while-loop,tic-tac-toe,Python,While Loop,Tic Tac Toe,我正在尝试在Python上构建一个tic-tac-toe游戏。以上是我到目前为止所写的代码,我被困在以下几点 玩家1的第一步未注册 玩家2的第一步是宣布他是赢家,没有任何一个玩家的进一步行动 我的while循环无法检查是否所有职位都已填补 我是Python新手,请您提供一些关于我哪里出错的指导。我在代码中发现错误的地方都添加了注释。当我测试什么不起作用时,有一些额外的评论打印语句。请随意移除它们。您的代码中有一些错误,导致您的选择语句计算不正确,下面指出了这些错误。我已经测试过了,它现在似乎可以

我正在尝试在Python上构建一个tic-tac-toe游戏。以上是我到目前为止所写的代码,我被困在以下几点

  • 玩家1的第一步未注册
  • 玩家2的第一步是宣布他是赢家,没有任何一个玩家的进一步行动
  • 我的while循环无法检查是否所有职位都已填补

  • 我是Python新手,请您提供一些关于我哪里出错的指导。

    我在代码中发现错误的地方都添加了注释。当我测试什么不起作用时,有一些额外的评论打印语句。请随意移除它们。您的代码中有一些错误,导致您的选择语句计算不正确,下面指出了这些错误。我已经测试过了,它现在似乎可以工作了

    def display_board(board):
        print(board[7]+"|"+board[8]+"|"+board[9])
        print("-|-|-")
        print(board[4]+"|"+board[5]+"|"+board[6])
        print("-|-|-")
        print(board[1]+"|"+board[2]+"|"+board[3])
        print("-|-|-")   
    
    testing_board=[" "]*10
    display_board(testing_board)
    
    def player_input():
        marker=" "
        while marker !="x" or marker !="o":
            marker= input("player1 choose x,o: ")
    
            if marker=="o":
                return ("o","x")
            else:
                return ("x","o")
            break
    
    player_input()
    
    import random
    
    def first_move():
        if random.randint(0,1)==1:
            return "player 2"
        else:
            return "player 1"
    
    def handle_turn(board,marker,position):
        position=input("select a position from 1-9: ")
        position=int(position)
        board[position]=marker
    
    handle_turn(testing_board,"x",8)
    display_board(testing_board)
    
    def check_win(board, mark):
        return ((board[7]==mark and board[8]==mark and board[9]==mark)or
        (board[4]==mark and board[5]==mark and board[6]==mark)or
        (board[1]==mark and board[2]==mark and board[3]==mark)or
        (board[7]==mark and board[4]==mark and board[1]==mark)or
        (board[8]==mark and board[5]==mark and board[2]==mark)or
        (board[9]==mark and board[6]==mark and board[3]==mark)or
        (board[7]==mark and board[5]==mark and board[3]==mark)or
        (board[9]==mark and board[5]==mark and board[1]==mark))
    
    check_win(testing_board,"x")
    
    def space_free(board,position):
        return board[position]==" "
    
    def fullboardcheck(board):
        for i in range(1,10):
            if space_free(board, i):
                return False
        return True
    
    def player_choice(board):
        position = 0
    
        while position not in [1,2,3,4,5,6,7,8,9] or not space_free(board,position):
            return position
    
    #game start
    #board,marker,first player to go
    while True:
        theBoard = [' '] * 10
        player1_marker, player2_marker = player_input()
        turn = first_move()
        print (turn + " will go first")
    
       #game play 
        play_game = input('Are you ready to play? Enter Yes or No.')
    
        if play_game == "Yes":
            game_is_on = True
        else:
            game_is_on = False
    
        while game_is_on:
            #player1 turn
            if turn=="player 1":
                display_board(theBoard)
                position=player_choice(theBoard)
                handle_turn(theBoard,player1_marker,position)
    
                if check_win(theBoard,player1_marker):
                    display_board(theBoard)
                    print('Congratulations! You have won the game!')
                    game_is_on=False
                else:
                    if fullboardcheck(theBoard):
                        display_board(theBoard)
                        print('The game is a draw!')
                    else:
                        turn = 'player 2'
    
            else:
                display_board(theBoard)
                position = player_choice(theBoard)
                handle_turn(theBoard, player2_marker, position)
    
                if check_win(theBoard, player2_marker):
                    display_board(theBoard)
                    print('player 2 has won!')
                    game_is_on=False
    
                else:
                    if fullboardcheck(theBoard):
                        display_board(theBoard)
                        print('The game is a draw!')
                    else:
                        turn = 'player 1'
    
    要解决评论中的OP问题,请执行以下操作:

    def display_board(board):
        print(board[7]+"|"+board[8]+"|"+board[9])
        print("-|-|-")
        print(board[4]+"|"+board[5]+"|"+board[6])
        print("-|-|-")
        print(board[1]+"|"+board[2]+"|"+board[3])
        print("-|-|-")
    
    testing_board=[" "]*10
    display_board(testing_board)
    
    def player_input():
        marker=" "
        while marker !="x" or marker !="o":
            marker= input("player1 choose x,o: ")
    
            if marker=="o":
                return ("o","x")
            else:
                return ("x","o")
    
    player_input()
    
    import random
    
    def first_move():
        if random.randint(0,1)==0:
            return "player 2"
        else:
            return "player 1"
    
    def handle_turn(board,marker,position):
        #print("Handling turn")
        position=position #error in this line, you were requesting position again even though you passed it as a parameter. Instead of an input just store the value passed from player_choice.
        position=int(position)
        board[position]=marker
        #print(board[position])
    #handle_turn(testing_board,"x"," ")
    #display_board(testing_board)
    
    def check_win(board, mark):
        return ((board[7]==mark and board[8]==mark and board[9]==mark)or #error in this line, changed commas to and
        (board[4]==mark and board[5]==mark and board[6]==mark)or
        (board[1]==mark and board[2]==mark and board[3]==mark)or
        (board[7]==mark and board[4]==mark and board[1]==mark)or
        (board[8]==mark and board[5]==mark and board[2]==mark)or
        (board[9]==mark and board[6]==mark and board[3]==mark)or
        (board[7]==mark and board[5]==mark and board[3]==mark)or
        (board[9]==mark and board[5]==mark and board[1]==mark))
    
    check_win(testing_board,"x")
    
    def space_free(board,position):
        return board[position]==" "
    
    def fullboardcheck(board):
        for i in range(1,10):
            if space_free(board, i):
                return False
        return True
    
    def player_choice(board):
        position = 0
        #print("player_choice func")
        while position not in [1,2,3,4,5,6,7,8,9] or not space_free(board, position):
            position = int(input('Choose your next position: (1-9) '))
    
        return position
    
    #game start
    #board,marker,first player to go
    while True:
        theBoard = [' '] * 10
        player1_marker, player2_marker = player_input()
        turn = first_move()
        print (turn + " will go first")
    
       #game play 
        play_game = input('Are you ready to play? Enter Yes or No.')
    
        if play_game == "Yes":
            game_is_on = True
        else:
            game_is_on = False
    
        while game_is_on:
            #print(turn)
            #print("Player 1:" + player1_marker)
            #print("Player 2:" + player2_marker)
            if turn=="player 1": #typo in this line, you needed a space after player and before 1
                #print("yes")
                display_board(theBoard)
                position=player_choice(theBoard)
                handle_turn(theBoard,player1_marker,position)
                #print(check_win(theBoard,player1_marker))
                if check_win(theBoard,player1_marker):
                    display_board(theBoard)
                    print('Congratulations! You have won the game!')
                    game_is_on=False
                else:
                    if fullboardcheck(theBoard):
                        display_board(theBoard)
                        print('The game is a draw!')
                    else:
                        turn = 'player 2' #typo in this line, player needed to be lowercase
    
            else:
                display_board(theBoard)
                position = player_choice(theBoard)
                handle_turn(theBoard, player2_marker, position)
    
                if check_win(theBoard, player2_marker):
                    display_board(theBoard)
                    print('Player 2 has won!')
                    game_is_on=False
    
                else:
                    if fullboardcheck(theBoard):
                        display_board(theBoard)
                        print('The game is a draw!')
                    else:
                        turn = 'player 1' #typo in this line, player needed to be lowercase
    

    我在代码中发现错误的地方都添加了注释。当我测试什么不起作用时,有一些额外的评论打印语句。请随意移除它们。您的代码中有一些错误,导致您的选择语句计算不正确,下面指出了这些错误。我已经测试过了,它现在似乎可以工作了

    def display_board(board):
        print(board[7]+"|"+board[8]+"|"+board[9])
        print("-|-|-")
        print(board[4]+"|"+board[5]+"|"+board[6])
        print("-|-|-")
        print(board[1]+"|"+board[2]+"|"+board[3])
        print("-|-|-")   
    
    testing_board=[" "]*10
    display_board(testing_board)
    
    def player_input():
        marker=" "
        while marker !="x" or marker !="o":
            marker= input("player1 choose x,o: ")
    
            if marker=="o":
                return ("o","x")
            else:
                return ("x","o")
            break
    
    player_input()
    
    import random
    
    def first_move():
        if random.randint(0,1)==1:
            return "player 2"
        else:
            return "player 1"
    
    def handle_turn(board,marker,position):
        position=input("select a position from 1-9: ")
        position=int(position)
        board[position]=marker
    
    handle_turn(testing_board,"x",8)
    display_board(testing_board)
    
    def check_win(board, mark):
        return ((board[7]==mark and board[8]==mark and board[9]==mark)or
        (board[4]==mark and board[5]==mark and board[6]==mark)or
        (board[1]==mark and board[2]==mark and board[3]==mark)or
        (board[7]==mark and board[4]==mark and board[1]==mark)or
        (board[8]==mark and board[5]==mark and board[2]==mark)or
        (board[9]==mark and board[6]==mark and board[3]==mark)or
        (board[7]==mark and board[5]==mark and board[3]==mark)or
        (board[9]==mark and board[5]==mark and board[1]==mark))
    
    check_win(testing_board,"x")
    
    def space_free(board,position):
        return board[position]==" "
    
    def fullboardcheck(board):
        for i in range(1,10):
            if space_free(board, i):
                return False
        return True
    
    def player_choice(board):
        position = 0
    
        while position not in [1,2,3,4,5,6,7,8,9] or not space_free(board,position):
            return position
    
    #game start
    #board,marker,first player to go
    while True:
        theBoard = [' '] * 10
        player1_marker, player2_marker = player_input()
        turn = first_move()
        print (turn + " will go first")
    
       #game play 
        play_game = input('Are you ready to play? Enter Yes or No.')
    
        if play_game == "Yes":
            game_is_on = True
        else:
            game_is_on = False
    
        while game_is_on:
            #player1 turn
            if turn=="player 1":
                display_board(theBoard)
                position=player_choice(theBoard)
                handle_turn(theBoard,player1_marker,position)
    
                if check_win(theBoard,player1_marker):
                    display_board(theBoard)
                    print('Congratulations! You have won the game!')
                    game_is_on=False
                else:
                    if fullboardcheck(theBoard):
                        display_board(theBoard)
                        print('The game is a draw!')
                    else:
                        turn = 'player 2'
    
            else:
                display_board(theBoard)
                position = player_choice(theBoard)
                handle_turn(theBoard, player2_marker, position)
    
                if check_win(theBoard, player2_marker):
                    display_board(theBoard)
                    print('player 2 has won!')
                    game_is_on=False
    
                else:
                    if fullboardcheck(theBoard):
                        display_board(theBoard)
                        print('The game is a draw!')
                    else:
                        turn = 'player 1'
    
    要解决评论中的OP问题,请执行以下操作:

    def display_board(board):
        print(board[7]+"|"+board[8]+"|"+board[9])
        print("-|-|-")
        print(board[4]+"|"+board[5]+"|"+board[6])
        print("-|-|-")
        print(board[1]+"|"+board[2]+"|"+board[3])
        print("-|-|-")
    
    testing_board=[" "]*10
    display_board(testing_board)
    
    def player_input():
        marker=" "
        while marker !="x" or marker !="o":
            marker= input("player1 choose x,o: ")
    
            if marker=="o":
                return ("o","x")
            else:
                return ("x","o")
    
    player_input()
    
    import random
    
    def first_move():
        if random.randint(0,1)==0:
            return "player 2"
        else:
            return "player 1"
    
    def handle_turn(board,marker,position):
        #print("Handling turn")
        position=position #error in this line, you were requesting position again even though you passed it as a parameter. Instead of an input just store the value passed from player_choice.
        position=int(position)
        board[position]=marker
        #print(board[position])
    #handle_turn(testing_board,"x"," ")
    #display_board(testing_board)
    
    def check_win(board, mark):
        return ((board[7]==mark and board[8]==mark and board[9]==mark)or #error in this line, changed commas to and
        (board[4]==mark and board[5]==mark and board[6]==mark)or
        (board[1]==mark and board[2]==mark and board[3]==mark)or
        (board[7]==mark and board[4]==mark and board[1]==mark)or
        (board[8]==mark and board[5]==mark and board[2]==mark)or
        (board[9]==mark and board[6]==mark and board[3]==mark)or
        (board[7]==mark and board[5]==mark and board[3]==mark)or
        (board[9]==mark and board[5]==mark and board[1]==mark))
    
    check_win(testing_board,"x")
    
    def space_free(board,position):
        return board[position]==" "
    
    def fullboardcheck(board):
        for i in range(1,10):
            if space_free(board, i):
                return False
        return True
    
    def player_choice(board):
        position = 0
        #print("player_choice func")
        while position not in [1,2,3,4,5,6,7,8,9] or not space_free(board, position):
            position = int(input('Choose your next position: (1-9) '))
    
        return position
    
    #game start
    #board,marker,first player to go
    while True:
        theBoard = [' '] * 10
        player1_marker, player2_marker = player_input()
        turn = first_move()
        print (turn + " will go first")
    
       #game play 
        play_game = input('Are you ready to play? Enter Yes or No.')
    
        if play_game == "Yes":
            game_is_on = True
        else:
            game_is_on = False
    
        while game_is_on:
            #print(turn)
            #print("Player 1:" + player1_marker)
            #print("Player 2:" + player2_marker)
            if turn=="player 1": #typo in this line, you needed a space after player and before 1
                #print("yes")
                display_board(theBoard)
                position=player_choice(theBoard)
                handle_turn(theBoard,player1_marker,position)
                #print(check_win(theBoard,player1_marker))
                if check_win(theBoard,player1_marker):
                    display_board(theBoard)
                    print('Congratulations! You have won the game!')
                    game_is_on=False
                else:
                    if fullboardcheck(theBoard):
                        display_board(theBoard)
                        print('The game is a draw!')
                    else:
                        turn = 'player 2' #typo in this line, player needed to be lowercase
    
            else:
                display_board(theBoard)
                position = player_choice(theBoard)
                handle_turn(theBoard, player2_marker, position)
    
                if check_win(theBoard, player2_marker):
                    display_board(theBoard)
                    print('Player 2 has won!')
                    game_is_on=False
    
                else:
                    if fullboardcheck(theBoard):
                        display_board(theBoard)
                        print('The game is a draw!')
                    else:
                        turn = 'player 1' #typo in this line, player needed to be lowercase
    

    函数
    handle\u turn()
    接收电路板
    的副本,只更改副本。这意味着大while循环中的
    theBoard
    实例保持不变。board是一个列表,列表是可变的。该函数确实会更改函数外部的列表,但问题是出现了一些打字错误,特别是在回合处理方面。您也没有使用player\u input()调用的结果。函数
    handle\u turn()
    接收
    棋盘的副本,并且只更改副本。这意味着大while循环中的
    theBoard
    实例保持不变。board是一个列表,列表是可变的。该函数确实会更改函数外部的列表,但问题是出现了一些拼写错误,特别是在回合处理方面,而且您没有使用player_input()调用的结果。感谢您阅读我的代码。您提供的解决方案解决了许多问题。然而,我仍然面临着最后一个问题,我想知道你们是否也可以分享你们在这方面的意见。当一个位置已经被玩家占据,而第二个玩家尝试在同一位置输入时,第一个玩家的输入会被覆盖,这在理想情况下是不应该的。我该如何解决这个问题?你确定你抄袭了我的资料吗?我只是再次测试了它,它并没有覆盖它。我在上面编辑了我的文章,向你们展示了当我尝试这样做时会发生什么。谢谢你们阅读我的代码。您提供的解决方案解决了许多问题。然而,我仍然面临着最后一个问题,我想知道你们是否也可以分享你们在这方面的意见。当一个位置已经被玩家占据,而第二个玩家尝试在同一位置输入时,第一个玩家的输入会被覆盖,这在理想情况下是不应该的。我该如何解决这个问题?你确定你抄袭了我的资料吗?我只是再次测试了它,它并没有覆盖它。我在上面编辑了我的帖子,向你们展示了当我尝试去做的时候会发生什么。