0

I've been trying to make tic-tac-toe (noughts and crosses) in python to improve my skills. However, when testing, I discovered that when I put my move on the bottom row, the player wins regardless of whether or not the win conditions I have set out are met. I know that there are other ways of doing this task, but here is the code I came up with...

import random
toprow=[1,2,3]
midrow=[4,5,6]
botrow=[7,8,9]
availablenums=[1,2,3,4,5,6,7,8,9]
pname=input("What is your name?\n")
print(toprow)
print(midrow)
print(botrow)
while True:
    pinput=int(input("Please enter a number from 1-9 (1 being the top-left square and 9 being the bottom-right square)\n"))
    if pinput==1:
        toprow[0]="X"
        availablenums.remove(1)
    if pinput==2:
        toprow[1]="X"
        availablenums.remove(2)
    if pinput==3:
        toprow[2]="X"
        availablenums.remove(3)
    if pinput==4:
        midrow[0]="X"
        availablenums.remove(4)
    if pinput==5:
        midrow[1]="X"
        availablenums.remove(5)
    if pinput==6:
        midrow[2]="X"
        availablenums.remove(6)
    if pinput==7:
        botrow[0]="X"
        availablenums.remove(7)
    if pinput==8:
        botrow[1]="X"
        availablenums.remove(8)
    if pinput==9:
        botrow[2]="X"
        availablenums.remove(9)
    compinput=random.choice(availablenums)
    if compinput==1:
        toprow[0]="O"
        availablenums.remove(1)
    if compinput==2:
        toprow[1]="O"
        availablenums.remove(2)
    if compinput==3:
        toprow[2]="O"
        availablenums.remove(3)
    if compinput==4:
        midrow[0]="O"
        availablenums.remove(4)
    if compinput==5:
        midrow[1]="O"
        availablenums.remove(5)
    if compinput==6:
        midrow[2]="O"
        availablenums.remove(6)
    if compinput==7:
        botrow[0]="O"
        availablenums.remove(7)
    if compinput==8:
        botrow[1]="O"
        availablenums.remove(8)
    if compinput==9:
        botrow[2]="O"
        availablenums.remove(9)
    print("___________")
    print(toprow)
    print(midrow)
    print(botrow)
    print("___________")
    if toprow[0] and toprow[1] and toprow[2] == "X":
        print(pname,"wins! Well done",pname,"!")
        break
    elif midrow[0] and midrow[1] and midrow[2] == "X":
        print(pname,"wins! Well done",pname,"!")
        break
    elif botrow[0] and botrow[1] and botrow[2] == "X":
        print(pname,"wins! Well done",pname,"!")
        break
    elif toprow[0] and midrow[0] and botrow[0] == "X":
        print(pname,"wins! Well done",pname,"!")
        break
    elif toprow[1] and midrow[1] and botrow[1] == "X":
        print(pname,"wins! Well done",pname,"!")
        break
    elif toprow[2] and midrow[2] and botrow[2] == "X":
        print(pname,"wins! Well done",pname,"!")
        break
    elif toprow[0] and midrow[1] and botrow[2] == "X":
        print(pname,"wins! Well done",pname,"!")
        break
    elif toprow[2] and midrow[1] and botrow[0] == "X":
        print(pname,"wins! Well done",pname,"!")
        break
Finnja
  • 1

2 Answers2

0
elif botrow[0] and botrow[1] and botrow[2] == "X":

only checks if botrow[2] is equal to "X" and if botrow[0] and botrow[1] have values, that are implicitly True. For more about boolean treatment check this article. Since you initialize your rows as

toprow=[1,2,3]
midrow=[4,5,6]
botrow=[7,8,9]

everyone of those integers is implicitly true.

elif botrow[0] == "X" and botrow[1] == "X" and botrow[2] == "X":

would be the easiest fix for your problem.

I don't know why this problem doesn't occur on your other elif statements. Have you checked those as well?

Honn
  • 737
  • 2
  • 18
0

You need to use

elif toprow[1] == "X" and midrow[1] == "X" and botrow[1] == "X":

on each of those statements, checking if all three are an X. Currently the code only checks if the last part of the elif statement is equal to X (and you have botrow as the last part like in the example above, hence always winning if any of the bottom row is an X).

Also, you have no code to check if the computer wins, so copy the if and elif statement for the computer's win conditions.

Daniel
  • 275
  • 1
  • 9