1
+ # print("Namste Duniya")
2
+
3
+ def sum (a , b , c ):
4
+ return a + b + c
5
+
6
+
7
+ def printBoard (xState , zState ):
8
+ one = 'X' if xState [1 ] else ('0' if zState [1 ] else 1 )
9
+ two = 'X' if xState [2 ] else ('0' if zState [2 ] else 2 )
10
+ three = 'X' if xState [3 ] else ('0' if zState [3 ] else 3 )
11
+ four = 'X' if xState [4 ] else ('0' if zState [4 ] else 4 )
12
+ five = 'X' if xState [5 ] else ('0' if zState [5 ] else 5 )
13
+ six = 'X' if xState [6 ] else ('0' if zState [6 ] else 6 )
14
+ senven = 'X' if xState [7 ] else ('0' if zState [7 ] else 7 )
15
+ eight = 'X' if xState [8 ] else ('0' if zState [8 ] else 8 )
16
+ nine = 'X' if xState [9 ] else ('0' if zState [9 ] else 9 )
17
+ print (f"{ one } | { two } | { three } " )
18
+ print (f"--|---|---" )
19
+ print (f"{ four } | { five } | { six } " )
20
+ print (f"--|---|---" )
21
+ print (f"{ senven } | { eight } | { nine } " )
22
+
23
+ def checkWin (xState , zState ):
24
+ wins = [[0 , 1 , 2 ], [3 , 4 , 5 ], [6 , 7 , 8 ], [0 , 3 , 6 ], [1 , 4 , 7 ], [2 , 5 , 8 ], [0 , 4 , 8 ], [2 , 4 , 6 ]]
25
+ for win in wins :
26
+ if (sum (xState [win [0 ]], xState [win [1 ]], xState [win [2 ]]) == 3 ):
27
+ print ("X's win" )
28
+ return 1
29
+ if (sum (zState [win [0 ]], zState [win [1 ]], zState [win [2 ]]) == 3 ):
30
+ print ("O's win" )
31
+ return 0
32
+ return - 1
33
+
34
+
35
+
36
+ if __name__ == "__main__" :
37
+ xState = [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
38
+ zState = [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
39
+ turn = 1 # 1for X and for 0
40
+ print ("Welcome to tic tac toe" )
41
+ while (True ):
42
+ printBoard (xState , zState )
43
+ if (turn == 1 ):
44
+ print ("X's Chance" )
45
+ value = int (input ("Please enter a value: " ))
46
+ xState [value ] = 1
47
+ else :
48
+ print ("0's Chance" )
49
+ value = int (input ("Please enter a value: " ))
50
+ zState [value ] = 1
51
+ cwin = checkWin (xState , zState )
52
+ if (cwin != - 1 ):
53
+ print ("Match Over" )
54
+ break
55
+
56
+ turn = 1 - turn
0 commit comments