0

I need help making a score board for each round of my Tic Tac Toe game, I would like it so whenever a round ends it updates cscore (computer score)or pscore (player score) by 1.

Any feedback on my code would be greatly appreciated since I am fairly new to python.

I have put the score related code I already have below

import random
def main():
    global cscore
    cscore = 0
    global pscore
    pscore = 0
if turn == 'player':
    drawBoard(theBoard)
    move = getPlayerMove(theBoard)
    makeMove(theBoard, playerLetter, move)
    if isWinner(theBoard, playerLetter):
        drawBoard(theBoard)
        print('Hooray! You have won the game!')
        pscore += 1
        print(main)
                
        gameIsPlaying = False
    else:
        if isBoardFull(theBoard):
            drawBoard(theBoard)
            print('The game is a tie!')
            print(main)
                

            break
        else:
            turn = 'computer'
else:
    move = getComputerMove(theBoard, computerLetter)
    makeMove(theBoard, computerLetter, move)
    if isWinner(theBoard, computerLetter):
        drawBoard(theBoard)
        print('The computer has beaten you! You lose.')
        cscore += 1
        print(main)
              
        gameIsPlaying = False
    else:
        if isBoardFull(theBoard):
            drawBoard(theBoard)
            print('The game is a tie!')
            print(main)
                   

            break
        else:
            turn = 'player'
if not playAgain():
break

main()
Matiiss
  • 5,970
  • 2
  • 12
  • 29
Slaya
  • 1
  • 1
  • please fix the indentation also what happened when you tried to make the score board? – Matiiss Aug 13 '21 at 14:10
  • Also, those `print(main)` lines will output something like `function main at 0x.....`. What did you want to do there? Note that if you _called_ the function instead or _referencing_ it the result would be even worse: it would output `None` but you would reset the scores to 0! – gimix Aug 13 '21 at 15:46

1 Answers1

0

at the start of the function with the loop of the game, that loop that doesn't stop, you need to add

global cscore, pscore

Just like in the main() function. If you didn't - this is way it doesn't updating to you.

By the way, better way will be to create a class for a player (I am aware that you are new to python so this - https://www.youtube.com/watch?v=ZDa-Z5JzLYM might help you), the class would look a bit like that:

class Player():

    def __init__(self, is_human):
        self.score = 0
        self.is_human = is_human
Eladtopaz
  • 1,036
  • 1
  • 6
  • 21