Home > front end >  python tic tac toe scoreboard not updating
python tic tac toe scoreboard not updating

Time:12-06

hi so me & my classmates are all new to coding so any help will be appreciated. As you can see we are attempting to build the game TicTac Toe. in the game there is a user input, scoreboard, board operated by a numeric keypad, and a option to play again. So as you can see there is somewhat of the code we have to tidy up our Tic Tac toe game that is written in python. the problem the we have is the scoreboard isn't updating as the game has a winner or draw.it doesn't print at the end and only is shown in the beginning when it is ran. so thats a requirement we need to fix. its just not so much we know on how to fix it. hence we tagged one stack overflow professionals:) is it a loop we need to implement? should we rearrange parts of the code?

here is the code :

import time



# Gabrielle W Begins
board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
def drawboard():
      print(" %c | %c | %c " % (board[1], board[2], board[3]))
      print("___|___|___")
      print(" %c | %c | %c " % (board[4], board[5], board[6]))
      print("___|___|___")
      print(" %c | %c | %c " % (board[7], board[8], board[9]))
      print("   |   |   ")
      

  # This Function Checks position is empty or not
def CheckPosition(x):
      if (board[x] == ' '):
          return True
      else:
        print ("Invalid Slot. . . Try Again!")

#Daniela Start
def print_scoreboard(score_board):
    print("--------------------------------")
    print("            SCOREBOARD          ")
    print("--------------------------------")
 
    players = list(score_board.keys())
    print("   ", players[0], "    ", score_board[players[0]])
    print("   ", players[1], "    ", score_board[players[1]])
 
    print("--------------------------------\n")

if __name__ == "__main__":
 
    print("Player 1")
    player1 = input("Please enter your name: ")
    print("\n")
 
    print("Player 2")
    player2 = input("Please enter your name: ")
    print("\n") 

     
# Stores the scoreboard
score_board = {player1: 0, player2: 0}
print_scoreboard(score_board)

#End Daniela             

      #This Function Checks player has won or not
def CheckWin():
      global Game
      # Horizontal winning condition
      if (board[1] == board[2] and board[2] == board[3] and board[1] != ' '):
          Game = Win
      elif (board[4] == board[5] and board[5] == board[6] and board[4] != ' '):
          Game = Win
      elif (board[7] == board[8] and board[8] == board[9] and board[7] != ' '):
          Game = Win
          # Vertical Winning Condition
      elif (board[1] == board[4] and board[4] == board[7] and board[1] != ' '):
          Game = Win
      elif (board[2] == board[5] and board[5] == board[8] and board[2] != ' '):
          Game = Win
      elif (board[3] == board[6] and board[6] == board[9] and board[3] != ' '):
          Game = Win
          # Diagonal Winning Condition
      elif (board[1] == board[5] and board[5] == board[9] and board[5] != ' '):
          Game = Win
      elif (board[3] == board[5] and board[5] == board[7] and board[5] != ' '):
          Game = Win
          # Match Tie or Draw Condition
      elif (board[1] != ' ' and board[2] != ' ' and board[3] != ' '
            and board[4] != ' ' and board[5] != ' ' and board[6] != ' '
            and board[7] != ' ' and board[8] != ' ' and board[9] != ' '):
          Game = Draw
      else:
          Game = foward
          


print("Tic-Tac-Toe Game ")
print("Please Wait...")
time.sleep(3)

#End of Gabrielle Part


#Cassandra D. Part#
def player_turn(player):
    while(Game == foward):    
        drawboard()     
        if player % 2 != 0:    
            print("Player 1's Turn")    
            box_slot = 'X'    
        else:    
            print("Player 2's Turn")    
            box_slot = 'O'    
        choice = int(input("Enter between (1-9) to choose box: "))    
        if(CheckPosition(choice)):    
            board[choice] = box_slot    
            player  = 1    
            CheckWin() 

 
    
def game_winner(player):
    if Game==Draw:    
        print("Game Draw")
        return 'D'
    elif Game==Win:    
      player-=1    
      if player != 0:    
        print("Player 1 Won") 
        return   
      else:    
        print("Player 2 Won") 
        return
#mina
repeat = 'yes'
while repeat == 'yes':
#mina
  #Cassandra D
  Win = 1
  Draw = -1
  foward = 0
  Game = foward
  box_slot = "X"
  player = 1
  #Cassandra D end
  player_turn(player)
  drawboard()
  game_winner(player)
  repeat = input('Do you want to play again?')
  board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']

repeat = 'no'
while repeat == 'no':
  break
#mina

  #Daniela Start
if 'no':
  print ("Thank you for playing!")
  print (print_scoreboard(score_board))
#daniela end 

CodePudding user response:

So, I just ran your code and managed to get through one game correctly. You are right that upon completion of one game, entering "yes" does not show the scoreboard correctly.

You already have your code set up as various functions, which is good. What you might want to do is have a play(board, player:str, move:int) function that accepts the player and the respective move and updates the board each time. You will need a modified while loop, because a draw, win or lose condition would end the game. The play() function should just keep track of the current player, and switch between 0 and 1, respectively as each move is played.

You might have something like:

# Set player 0 to start, X
start_player = 0
while not checkGameEnd():
    # Take player input
    move = int(input("Where do you want to move?"))
    # Make the play
    play(board, start_player, move)
    # Print the board
    printBoard()
    # Change player
    start_player = 1 if start_player is 0 else 1
# Game should end here
printScores()
playAgain()

This is sort of pseudo-code, but you can modify the logic as per your usecase.

CodePudding user response:

The scoreboard printed at the end for me when I responded n to the question 'Do you want to play again?'.

However, it showed 0-0 because the code doesn't update the scores. I think you want to add the following in the game_winner function:

elif Game ==Win:    
    player -= 1    
    if player != 0:
        print("Player 1 Won")
        score_board[player1]  = 1
        return
    else:
        print("Player 2 Won")
        score_board[player2]  = 1
        return

Also, you'll need to move the game_winner(player) function call at the end outside of the while loop so that it only called once.

CodePudding user response:

you need to modify the score_board variable at the end of each match, so that when you print it at the end of the game it will be updated with the final score,to do so you can modify your game_winner function like this:

def game_winner(player):
    if Game==Draw:    
        print("Game Draw")
        score_board[player1]  = 0.5
        score_board[player2]  = 0.5
        return 'D' 
    elif Game ==Win:    
        player -= 1    
        if player != 0:
            print("Player 1 Won")
            score_board[player1]  = 1
            return
        else:
            print("Player 2 Won")
            score_board[player2]  = 1
            return  

  • Related