Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 breaking even if conditions is not met
#1
hello, i am making a tic tac toe game for learning purposes
def win():
    global gameover
    gameover = False
    if first is True:
        win1 = board[0] == x and board[1] == x and board[2] == x
        win2 = board[3] == x and board[4] == x and board[5] == x
        win3 = board[6] == x and board[7] == x and board[8] == x
        win4 = board[0] == x and board[3] == x and board[6] == x
        win5 = board[1] == x and board[4] == x and board[7] == x
        win6 = board[2] == x and board[5] == x and board[8] == x
        win7 = board[0] == x and board[4] == x and board[8] == x
        win8 = board[2] == x and board[4] == x and board[6] == x
        if (win1 or win2 or win3 or win4 or win5 or win6 or win7 or win8) is True:
            print("You won!")
            gameover = True
            return gameover
    if first is not True:
        wino1 = board[0] == o and board[1] == o and board[2] == o
        wino2 = board[3] == o and board[4] == o and board[5] == o
        wino3 = board[6] == o and board[7] == o and board[8] == o
        wino4 = board[0] == o and board[3] == o and board[6] == o
        wino5 = board[1] == o and board[4] == o and board[7] == o
        wino6 = board[2] == o and board[5] == o and board[8] == o
        wino7 = board[0] == o and board[4] == o and board[8] == o
        wino8 = board[2] == o and board[4] == o and board[6] == o
        if (wino1 or wino2 or wino3 or wino4 or wino5 or wino6 or wino7 or wino8) is True:
            print("You won!")
            gameover = True
            return gameover


def game():
    print("welcome to tic-tac-toe")
    chosexo()
    while True:
        if gameover is True:
            break
        if first is True:
            win()
            appenders()
            theboard()
        if first is False:
            win()
            theboard()
            appenders()
Error:
welcome to tic-tac-toe choose X or O: x You start first Process finished with exit code 0
the break statement triggers even if the conditions are not met (if gameover is True)
thank you!
here is the full code:
Quote
#2
Hi,

You have not declared the gameover variable. if you remove the try except at the end you should get an error message that tells you what is wrong.


if first:
this piece of code is equivelant to "if first is True:"

I have modified some of your code so you get the general idea.
also if you are going to check if first is true or false you can use if, else statement instead of if true if false

def win():
    if first is True:
        win1 = board[0] == x and board[1] == x and board[2] == x
        win2 = board[3] == x and board[4] == x and board[5] == x
        win3 = board[6] == x and board[7] == x and board[8] == x
        win4 = board[0] == x and board[3] == x and board[6] == x
        win5 = board[1] == x and board[4] == x and board[7] == x
        win6 = board[2] == x and board[5] == x and board[8] == x
        win7 = board[0] == x and board[4] == x and board[8] == x
        win8 = board[2] == x and board[4] == x and board[6] == x
        if (win1 or win2 or win3 or win4 or win5 or win6 or win7 or win8) is True:
            print("You won!")
            return True
        else:
            return False
    else:
        wino1 = board[0] == o and board[1] == o and board[2] == o
        wino2 = board[3] == o and board[4] == o and board[5] == o
        wino3 = board[6] == o and board[7] == o and board[8] == o
        wino4 = board[0] == o and board[3] == o and board[6] == o
        wino5 = board[1] == o and board[4] == o and board[7] == o
        wino6 = board[2] == o and board[5] == o and board[8] == o
        wino7 = board[0] == o and board[4] == o and board[8] == o
        wino8 = board[2] == o and board[4] == o and board[6] == o
        if (wino1 or wino2 or wino3 or wino4 or wino5 or wino6 or wino7 or wino8) is True:
            print("You won!")
            return True
        else:
            return False


def game():
    print("welcome to tic-tac-toe")
    chosexo()
    while True:
        appenders()
        theboard()
        gameover = win()

        if gameover:
            break

game()

Quote
#3
also:
if gameover is True:
is bad programming style, use:
if gameover:
    ... do this ...
else:
    ... do this ...
Quote
#4
I just observe that in function win() there is no 'first' defined and it should give NameError. Same applies to x and o which are also not defined. Shouldn't these be 'x' and 'o'?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote
#5
(Feb-02-2019, 11:40 PM)perfringo Wrote: I just observe that in function win() there is no 'first' defined and it should give NameError. Same applies to x and o which are also not defined. Shouldn't these be 'x' and 'o'?

hi, i have defined all the variables check the spoilers thanks

(Feb-02-2019, 10:03 PM)Larz60+ Wrote: also:
if gameover is True:
is bad programming style, use:
if gameover:
    ... do this ...
else:
    ... do this ...

thanks ill keep that in mind

(Feb-02-2019, 06:06 PM)danielsvens Wrote: Hi,

You have not declared the gameover variable. if you remove the try except at the end you should get an error message that tells you what is wrong.


if first:
this piece of code is equivelant to "if first is True:"

I have modified some of your code so you get the general idea.
also if you are going to check if first is true or false you can use if, else statement instead of if true if false

def win():
    if first is True:
        win1 = board[0] == x and board[1] == x and board[2] == x
        win2 = board[3] == x and board[4] == x and board[5] == x
        win3 = board[6] == x and board[7] == x and board[8] == x
        win4 = board[0] == x and board[3] == x and board[6] == x
        win5 = board[1] == x and board[4] == x and board[7] == x
        win6 = board[2] == x and board[5] == x and board[8] == x
        win7 = board[0] == x and board[4] == x and board[8] == x
        win8 = board[2] == x and board[4] == x and board[6] == x
        if (win1 or win2 or win3 or win4 or win5 or win6 or win7 or win8) is True:
            print("You won!")
            return True
        else:
            return False
    else:
        wino1 = board[0] == o and board[1] == o and board[2] == o
        wino2 = board[3] == o and board[4] == o and board[5] == o
        wino3 = board[6] == o and board[7] == o and board[8] == o
        wino4 = board[0] == o and board[3] == o and board[6] == o
        wino5 = board[1] == o and board[4] == o and board[7] == o
        wino6 = board[2] == o and board[5] == o and board[8] == o
        wino7 = board[0] == o and board[4] == o and board[8] == o
        wino8 = board[2] == o and board[4] == o and board[6] == o
        if (wino1 or wino2 or wino3 or wino4 or wino5 or wino6 or wino7 or wino8) is True:
            print("You won!")
            return True
        else:
            return False


def game():
    print("welcome to tic-tac-toe")
    chosexo()
    while True:
        appenders()
        theboard()
        gameover = win()

        if gameover:
            break

game()


thanks for your reply!, i have a question why you removed the variable gamemode in win()?, and just returned True if you won?

Error:
Traceback (most recent call last): File "D:/Python/Medi/Medi.py", line 105, in <module> game() File "D:/Python/Medi/Medi.py", line 93, in game if gameover is True: NameError: name 'gameover' is not defined
so i did remove the try func
i did define the variable in win() "global gameover" did i miss something? or just this is a bad programming style?

Quote
#6
(Feb-03-2019, 10:55 AM)Naito Wrote: hi, i have defined all the variables check the spoilers thanks

From spoiler I found keywords learning and AI. In learning process you could have a look at tic-tac-toe Minimax just to get an idea how others approach this.
Naito likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Breaking subprocess loop from parent process kapibara 2 75 9 hours ago
Last Post: Gribouillis
  How to define Missing value on some certain conditions? FreddieTheCat 3 385 Jan-14-2019, 01:35 AM
Last Post: scidam
  If conditions with time limit unknowntothem 4 445 Nov-09-2018, 08:59 PM
Last Post: nilamo
  3 conditions to check and return ilcaa72 3 687 May-24-2018, 03:13 AM
Last Post: ilcaa72
  Fill as list (with conditions). EmericCrue 6 1,081 May-11-2018, 03:21 PM
Last Post: nilamo
  for loop with 2 conditions vaison 9 1,395 Apr-19-2018, 01:28 PM
Last Post: vaison
  How do you check if something exists or doesn't in the conditions of an if? Klar 1 678 Dec-15-2017, 03:47 PM
Last Post: sparkz_alot
  Help with multiple conditions on ATM program jakegold98 6 2,962 Dec-06-2017, 05:18 PM
Last Post: jakegold98
  applying 2 conditions to a loop Pedroski55 1 801 Nov-08-2017, 07:11 PM
Last Post: nilamo
  Simplifying multiple "or" conditions in if statement. rhubarbpieguy 8 33,635 Jul-22-2017, 12:19 PM
Last Post: rhubarbpieguy

Forum Jump:


Users browsing this thread: 1 Guest(s)