YlemUteli YlemUteli - 1 month ago 8
Python Question

Hi, Need Help To make tic tac toe in Pyhton

i started learning python few weeks ago. im not bad at it, so recently i decided to make tic tac toe by myself but i have a little problem :'( i made tic tac toe table, i have pretty much done half work, now i need to set winner anyone cna help me out?
i have done this code so far:

a={"1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9"}
enteredalready=[]
def tictactoe(a):
print("-------------")
print("|",a["1"],"|",a["2"],"|",a["3"],"|")
print("-------------")
print("|",a["4"],"|",a["5"],"|",a["6"],"|")
print("-------------")
print("|",a["7"],"|",a["8"],"|",a["9"],"|")
print("-------------")

def in_x(enteredalready):
x=input("X Turn: ")
if not(x in enteredalready):
enteredalready+=[x]
a[x]="x"
else:
print("Choose Another Number")
in_x(enteredalready)
def in_o(enteredalready):
o=input("O Turn: ")
if not(o in enteredalready):
enteredalready+=[o]
a[o]="o"
else:
print("Choose Another Number")
in_o(enteredalready)
def Win():

if a["1"]==a["2"]==a["3"]:
print("Win")


tictactoe(a)
i=1
while i<=9:
in_x(enteredalready)
tictactoe(a)
if i==9:
print("draw !!!!!!")
break
i+=1

in_o(enteredalready)
tictactoe(a)
i+=1


def win() is a place where i need help, may someone can give me some hints or advices?

zeo zeo
Answer

So, basically you need to check if all the characters in any one row or diagonal are equal. It's simply a syntax problem.

Taking your example (the first row), instead of writing

if a["1"]==a["2"]==a["3"]:

You can instead write:

def win(a)
    if (a["1"] == a["2"]) and (a["2"] == a["3"]):
        return True
    else if ....:
        return True
    ....

This way, it simply checks if either player has won the game. So you'll have to call the win function after every move. Write similar if/else statements for all of the rows and columns and diagonals.

Hope this helps.