0

I am making a simple console tic tac toe game. But it isn't working how it is supposed to. If you place a mark tow in a row it declares the player winner. I don't know what's wrong. I searched the whole internet but didn't found anything.

win = True
tie = True
player = 1
        
Board = [1, 2, 3,
         4, 5, 6,
         7, 8, 9]
        
def display_Board():
    print(f"| {Board[0]} | {Board[1]} | {Board[2]} |")
    print(f"| {Board[3]} | {Board[4]} | {Board[5]} |")
    print(f"| {Board[6]} | {Board[7]} | {Board[8]} |")    
        
def play(m):
    global player
    try:
        if player == 1:
            x = Board.index(m)
            Board[x] = "X"
            player = 2
        elif player == 2:
            y = Board.index(m)
            Board[y] = "Y"
            player = 1
    except ValueError:
        print("You can't overwrite other players mark")
        
def Tie():
    global player
    if any(isinstance(j,int) for j in Board):
        print(f"Player {player} turn")
        return True
    else:
        print("It's a Tie")
        return False

this is the Win function. Most of the problem Would be her

def Win():
    if Board[0] and Board[1] and Board[2] == "X":
        print("Player 1 Won")
        return False
    elif Board[3] and Board[4] and Board[5] == "X":
        print("Player 1 Won")
        return False
    elif Board[6] and Board[7] and Board[8] == "X":
        print("Player 1 Won")
        return False
    elif Board[0] and Board[3] and Board[6] == "X":
        print("Player 1 Won")
        return False
    elif Board[1] and Board[4] and Board[7] == "X":
        print("Player 1 Won")
        return False
    elif Board[2] and Board[5] and Board[8] == "X":
       print("Player 1 Won")
       return False
    elif Board[0] and Board[4] and Board[8] == "X":
       print("Player 1 Won")
       return False
    elif Board[2] and Board[4] and Board[6] == "X":
       print("Player 1 Won")
       return False
    elif Board[0] and Board[1] and Board[2] == "Y":
       print("Player 2 Won")
       return False
    elif Board[3] and Board[4] and Board[5] == "Y":
       print("Player 2 Won")
       return False
    elif Board[6] and Board[7] and Board[8] == "Y":
       print("Player 2 Won")
       return False
    elif Board[0] and Board[3] and Board[6] == "Y":
       print("Player 2 Won")
       return False
    elif Board[1] and Board[4] and Board[7] == "Y":
       print("Player 2 Won")
       return False
    elif Board[2] and Board[5] and Board[8] == "Y":
        print("Player 2 Won")
        return False
    elif Board[0] and Board[4] and Board[8] == "Y":
        print("Player 2 Won")
        return False
    elif Board[2] and Board[4] and Board[6] == "Y":
        print("Player 2 Won")
        return False
    else:
        return True

Here the main loop starts

print("Enter the respective digit to mark")
display_Board()    
        
while win and tie:
    play(int(input("Where you want to mark: ")))
    display_Board()
    tie = Tie()
    win = Win()
DrBwts
  • 3,470
  • 6
  • 38
  • 62
  • 1
    I would really really suggest you to learn how to use loops, what if you wanted to make tic tac toe but on a 5x5 board? how many if/elif statements would you have then? also that is not really how you are supposed to use classes EDIT: function but since per PEP 8 classes have `CapitalCase` names I thought it was a class, for function and variable names you should use `snake_case` (per PEP 8, not mandatory but I would really really suggest) – Matiiss Aug 14 '21 at 10:33
  • Does this answer your question? [Why does \`a == x or y or z\` always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true) – Matiiss Aug 14 '21 at 10:37
  • You are telling me not to use if/else statements then how am i supposed to detect if player won – Sarthak Hingankar Aug 14 '21 at 10:38
  • I agree you need to find a way to generalize the check for win, you can do so many if. Talking about if, you should do board[i] == X for every i. You are only doing for the last one – Victor Gonzalez Chamorro Aug 14 '21 at 10:38
  • @IXI33SarthakHingankar I didn't say don't use `if/elif`, I said to use loops so that you don't have that many `if/elif` – Matiiss Aug 14 '21 at 10:38
  • I don't get it how am I suppose to use a loop to check if player win somebody pls explain – Sarthak Hingankar Aug 14 '21 at 10:41
  • There are 8 ways to win for either player, put those 8 sets of wining sequences in a list & loop through the list. – DrBwts Aug 14 '21 at 10:49

1 Answers1

1

You are using a and b and c == x which is wrong...you have to use a == b == c == x.

Also you can decrease your if statements with the help of loops.Something like this..

def horizontalWin(player): # player is 'X' or '0'
    # iterate from 0 to 6 with step 3 ..(0, 3, 6)
    for row in range(0, 7, 3):
        # since variable row is the first in a row... check if the second and the third are the same as the player whos win is being checked.
        if Board[row] == Board[row + 1] == Board[row + 2] == player: return False

And do similar functions for vertical and diagonals.I suggest you watch some youtube videos about checking wins in tic tac toe or connect 4 to grasp the concepts.