Nate Rearick
Nate Rearick

Reputation: 33

Function that checks if something is a variable

I have recently came across another problem with using functions for a tic tac toe game. What I want this function to do is check if any of the players have won through the function 'win'. The function works in my original code but that required me to re write it 4 times. I wanted to simplify it by making it a function but I cannot seem to get it to work. I have tried adding global variables in the beginning of the function, but that doesn't seem to be the case.

num1 = '1'
num2 = '2'
num3 = '3'
num4 = '4'
num5 = '5'
num6 = '6'
num7 = '7'
num8 = '8'
num9 = '9'
player1_mark = "X"
player2_mark = "O"
endgame = False

def drawBoard():
    ''' Prints the board'''
    print

def win(x):
    if (num1 == x and num2 == x and num3 == x):
        drawBoard()
        print "The computer wins!"
        endgame = True
    elif (num1 == x and num5 == x and num9 == x):
        drawBoard()
        print "The computer wins!"
        endgame = True
    elif (num1 == x and num4 == x and num7 == x):
        drawBoard()
        print "The computer wins!"
        endgame = True
    elif (num4 == x and num5 == x and num6 == x):
        drawBoard()
        print "The computer wins!"
        endgame = True
    elif (num7 == x and num8 == x and num9 == x):
        drawBoard()
        print "The computer wins!"
        endgame = True
    elif (num7 == x and num5 == x and num3 == x):
        drawBoard()
        print "The computer wins!"
        endgame = True
    elif (num1 == x and num2 == x and num3 == x):
        drawBoard()
        print "The computer wins!"
        endgame = True
    elif (num8 == x and num5 == x and num2 == x):
        drawBoard()
        print "The computer wins!"
        endgame = True
    elif (num9 == x and num6 == x and num3 == x):
        drawBoard()
        print "The computer wins!" 
        endgame = True
    elif (num1 != '1' and num2 != '2' and num3 != '3' and num4 != '4' and num5 != '5' and num6 != '6' and num7 != '7' and num8 != '8' and num9 != '9'):
        drawBoard()
        print "Draw"
        endgame = True

'''Lets say that the player gets the row 1 2 and 3'''
num1 = 'X'
num2 = 'X'
num3 = 'X'
while True:
    win(player1_mark)
    while endgame == True:
        print 'You win!'
        break

Cheers

Upvotes: 0

Views: 75

Answers (2)

Till
Till

Reputation: 4523

A quick modification would look like this (return the equal value, remove the second while, and remove duplicate code with a longer boolean formula) :

num1 = '1'
num2 = '2'
num3 = '3'
num4 = '4'
num5 = '5'
num6 = '6'
num7 = '7'
num8 = '8'
num9 = '9'
player1_mark = "X"
player2_mark = "O"

def drawBoard():
    ''' Prints the board'''
    print

def win(x):
    if (num1 == x and num2 == x and num3 == x) or (
        num1 == x and num5 == x and num9 == x) or (
        num1 == x and num4 == x and num7 == x) or (
        num4 == x and num5 == x and num6 == x) or (
        num7 == x and num8 == x and num9 == x) or (
        num7 == x and num5 == x and num3 == x) or (
        num1 == x and num2 == x and num3 == x) or (
        num8 == x and num5 == x and num2 == x) or (
        num9 == x and num6 == x and num3 == x) :
        drawBoard()
        print "The computer wins!"
        return True
    elif (num1 != '1' and num2 != '2' and num3 != '3' and num4 != '4' and num5 != '5' and num6 != '6' and num7 != '7' and num8 != '8' and num9 != '9'):
        drawBoard()
        print "Draw"
        return True
    else:
        return False
'''Lets say that the player gets the row 1 2 and 3'''
num1 = 'X'
num2 = 'X'
num3 = 'X'
while True:
    endgame = win(player1_mark)
    if(endgame):
        print 'You win!'
        break

Upvotes: 1

DavedeKoning
DavedeKoning

Reputation: 190

I do not understand your code, but you can make it work by returning endgame from the function. Add this at the end of the function:

def win(x):
    ...
    #all the if/elif statements#
    ...
    return endgame

Also I do not know why you call all the while loops. I would suggest the following:

endgame = win(player1_mark)
if endgame == True:
     print "You win!"

Cheers

Upvotes: 1

Related Questions