-1

try the code for ur self to understand what im saying :).... sry if my question is not that understandable English is not my 1st language.

how can i fix it? i think theirs a problem in def choose_cell... but im not sure.

#output Hi! Let's play a game. X goes first.

#Current player X

#Enter cell number (1 to 9): 1

#| X | * | * | | * | * | * | | * | * | * | Current player O and when I input 1 again it will show "This position is already taken."

and my curr_player will switch to "X" but it should stay at "O"

how can I fix it?

 print("Hi! Let's play a game. X goes first.")
        
        board = [['*', '*', '*'], ['*', '*', '*'], ['*', '*', '*']]
        rows = 3
        cols = 3
        player ="x"
        curr_player = player
        def showBoard(board):
            for x in range(rows):
                print("")
                print("|", end="")
                for y in range(cols):
                    print("", board[x][y], end=" |")
        
        
        
        def chooseCell(board):
            cell_number = int(input("\nEnter cell number (1 to 9): "))
            if cell_number > 9 or cell_number < 1:
                print("Error. Enter an integer between 1 and 9.")
        
            else:
                cell_number= int(cell_number) - 1
                row = int(cell_number / 3)
                col = cell_number
                if col > 2: col = int(col % 3)
                if board[row][col] != "*":
                    print("This position is already taken.")
                else:
                    return cell_number
        
        def appendBoard(board,cell_Num,curr_player):
        
            if(cell_Num == 0):
                board[0][0] = curr_player
            elif(cell_Num == 1):
                board[0][1] = curr_player
            elif (cell_Num == 2):
                board[0][2] = curr_player
            elif (cell_Num == 3):
                board[1][0] = curr_player
            elif (cell_Num == 4):
                board[1][1] = curr_player
            elif (cell_Num == 5):
                board[1][2] = curr_player
            elif (cell_Num == 6):
                board[2][0] = curr_player
            elif (cell_Num == 7):
                board[2][1] = curr_player
            elif (cell_Num == 8):
                board[2][2] = curr_player
        
        
        def switchPlayer(curr_player):
            if curr_player : return "X"
            else: return "O"
        
        while True:
            active_user = switchPlayer(curr_player)
            print("\nCurrent player", active_user)
            user = chooseCell(board)
            appendBoard(board,user,active_user)
            showBoard(board)
            curr_player = not curr_player
  • You need to update logic for ChooseCell so that it only returns cell_number if the cell is not already taken. You could do this in many ways – itprorh66 Oct 29 '22 at 15:12

1 Answers1

0

Following up on my comment, here is an approach to fixing your problem.

def chooseCell(board):
    cell_number = None
    while True:
        cell_number = int(input("\nEnter cell number (1 to 9): "))
        if cell_number > 9 or cell_number < 1:
            print("Error. Enter an integer between 1 and 9.")
        cell_number= int(cell_number) - 1
        row = int(cell_number / 3)
        col = cell_number
        if col > 2: col = int(col % 3)       
        if board[row][col] != "*":
            print("This position is already taken.")
        else:
            return cell_number

I would like to point out, while the above code fixes your immediate question, you still have to include logic which detects the end of the game, declares a winner/loser and also detects no further moves are possible.

You should also clean up your code for append_board, since this could be accomplished in far fewer lines with more efficiency.

itprorh66
  • 3,110
  • 4
  • 9
  • 21