Hướng dẫn tic-tac-toe python while loop - tic-tac-toe python vòng lặp while

Tôi đã thêm nhận xét vào mã ở mọi nơi tôi tìm thấy một lỗi.Có một số tuyên bố in thêm nhận xét trong khi tôi thử nghiệm để xem những gì không hoạt động.Hãy thoải mái loại bỏ chúng.Bạn đã có một số lỗi chính tả trong mã của bạn khiến các câu lệnh Lựa chọn của bạn đánh giá không chính xác, những lỗi chính tả đó được chỉ ra bên dưới.Tôi đã thử nghiệm điều này và nó dường như đang hoạt động ngay bây giờ.

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

Để giải quyết các mối quan tâm của OP trong các ý kiến:

player1 choose x,o: x
player 2 will go first
Are you ready to play? Enter Yes or No.Yes
 | | 
-|-|-
 | | 
-|-|-
 | | 
-|-|-
Choose your next position: (1-9) 1
 | | 
-|-|-
 | | 
-|-|-
o| | 
-|-|-
Choose your next position: (1-9) 1
Choose your next position: (1-9) 1
Choose your next position: (1-9) 1
Choose your next position: (1-9) 2
 | | 
-|-|-
 | | 
-|-|-
o|x| 
-|-|-
Choose your next position: (1-9) 1
Choose your next position: (1-9) 1
Choose your next position: (1-9) 1
Choose your next position: (1-9) 5
 | | 
-|-|-
 |o| 
-|-|-
o|x| 
-|-|-
Choose your next position: (1-9) 3
 | | 
-|-|-
 |o| 
-|-|-
o|x|x
-|-|-
Choose your next position: (1-9) 3
Choose your next position: (1-9) 2
Choose your next position: (1-9) 1