0

In this tic-tac-toe game i made

I have it printing winner variable every turn, and when someone wins, the winner = 1 doesn't work. If I change winner = 1 to

winner = winner + 1

It comes up with a bunch of errors... Spent a while troubleshooting, and I'm really not sure.

I'm gonna try to figure it out myself, but if I don't get it id love some help!! :)

I'm sure there's many other places with bad practice, so let me know! Here to learn.

Thank you.

counter = 0
winner  = 0
blankBoard = ['','','','','','','','','','']
board = ['','','','','','','','','','']


def checkX(board):

if board[1] and board[2] and board[3] == 'x':
    print('x  Wins!')
    winner = 1
if board[4] and board[5] and board[6] == 'x':
    print('x  Wins!')
    winner = 1
if board[7] and board[8] and board[9] == 'x':
    print('x  Wins!')
    winner = 1

if board[1] and board[5] and board[9] == 'x':
    print('x  Wins!')
    winner = 1
if board[7] and board[5] and board[3] == 'x':
    print('x  Wins!')
    winner = 1


if board[1] and board[4] and board[7] == 'x':
    print('x  Wins!')
    winner = 1
if board[2] and board[5] and board[8] == 'x':
    print('x  Wins!')
    winner = 1
if board[3] and board[6] and board[9] == 'x':
    print('x  Wins!')
    winner = 1

def checkO(board):


if board[1] and board[2] and board[3] == 'o':
    print('o  Wins!')
    winner = 1
if board[4] and board[5] and board[6] == 'o':
    print('o  Wins!')
    winner = 1
if board[7] and board[8] and board[9] == 'o':
    print('o  Wins!')
    winner = 1

if board[1] and board[5] and board[9] == 'o':
    print('o  Wins!')
    winner = 1
if board[7] and board[5] and board[3] == 'o':
    print('o  Wins!')
    winner = 1


if board[1] and board[4] and board[7] == 'o':
    print('o  Wins!')
    winner = 1
if board[2] and board[5] and board[8] == 'o':
    print('o  Wins!')
    winner = 1
if board[3] and board[6] and board[9] == 'o':
    print('xo Wins!')
    winner = 1

def checkTie(board):

   if 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] != '':
        print('Tie game!')
        flipPlayer()

def flipPlayer():
    if counter == 0:
        counter = 1

    if counter == 1:
        counter = 0

    newGame = input('would you like to play again?')

    if newGame == 'yes':

        drawboard(blankBoard)

    else:
        print('goodbye')


def checkTerminal(board):
    winner  = 0

    checkX(board)
    checkO(board)
    checkTie(board)

    if winner == 1:
        flipPlayer()

def drawBoard(board):
    print(board[1] + '|' + board[2] + '|' + board[3] + '|')
    print(board[4] + '|' + board[5] + '|' + board[6] + '|')
    print(board[7] + '|' + board[8] + '|' + board[9] + '|')

    playerMove(board)

def playerMove(board):
    print(winner)

    global counter


    checkTerminal(board)

    move = int(input('What Space Would you Like to Move to?'))

    if counter == 0:
        board[move] = 'x'
        counter = 1
        drawBoard(board)
        playerMove(board)

    if counter == 1:
        board[move] = 'o'
        counter = 0
        drawBoard(board)
        playerMove(board)


#if winner == 1:
        #flipPlayer()


drawBoard(board)





#Board
#Display Board
#Check Winner
    #Check Row
    #Check Column
    #check Diagonal
#Check Tie
#Flip Player
  • 1
    Your indentation is wrong. Please edit the code so that the body of functions are indented one level. Also include the traceback when asking questions. The reason python gives you these long error messages is to give you information to help you debug problems. – Håken Lid May 04 '20 at 11:19
  • For the problem you’re working on right now/asking about, see the duplicate. Separately, note that `board[1] and board[4] and board[7] == 'x'` checks if `board[7] == 'x'` but only that `board[1]` and `board[4]` are non-empty; you can use `'x' == board[1] == board[4] == board[7]` to check them all in Python. – Ry- May 04 '20 at 11:21

0 Answers0