YlemUteli - 1 year ago 59
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"}
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("-------------")

x=input("X Turn: ")
a[x]="x"
else:
print("Choose Another Number")
o=input("O Turn: ")
a[o]="o"
else:
print("Choose Another Number")
def Win():

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

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

tictactoe(a)
i+=1
``````

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

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

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

``````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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download