#πŸ”’ Why does the game not register a vertical win?

7 messages Β· Page 1 of 1 (latest)

left timber
#

The check_win should return the variable and hence register as a win. Here's the code:

def game():
    P1=Player()
    P2=Player()
    P1.name=input("Enter player 1's name: ")
    P2.name=input("Enter player 2's name: ")
    choice=random.randint(1,2)
    if choice==1:
        choosesign(P1,P2)
        toss=1
    else:
        choosesign(P2,P1)
        toss=2
    gamewin=False
    lastinput=" "
    while (gamewin==False and is_full()==False):
        for row in board:
            print(row)
        if lastinput==" " and toss==1:
            inputsign(P1.sign)
            lastinput=P1.sign
        elif lastinput==" " and toss==2:
            inputsign(P2.sign)
            lastinput=P2.sign
        elif lastinput==P1.sign:
            inputsign(P2.sign)
            lastinput=P2.sign
        else:
            inputsign(P1.sign)
            lastinput=P1.sign
        if check_win()==P1.sign:
            print(f"{P1.name} Wins!")
            gamewin=True
        elif check_win()==P2.sign:
            print(f"{P2.name} Wins!")
            gamewin=True
    if is_full()==True:
        for row in board:
            print(row)
        print("Game ended on a draw!")
def check_win():
        for row in board:
            if row[0] == row[1] == row[2] and row[0] != " ":
                return row[0]
            for col in range(3):
                if board[0][col]==board[1][col]==board[2][col]!=" ": 
                    return [0][col]
            if board[0][0] == board[1][1] == board[2][2] and board[0][0] != " ":
                return board[0][0]
            if board[0][2] == board[1][1] == board[2][0] and board[0][2] != " ":
                return board[0][2]
            else:
                return False
game()
gilded salmonBOT
#

@left timber

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

left timber
#

This is not the full code due to it reaching 2k character limit

#

But it should be enough

#

!close

gilded salmonBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.