-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic Tac Toe game in python
52 lines (45 loc) · 1.72 KB
/
Tic Tac Toe game in python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//TIC TAC TOE GAME IN PYTHON
def sum(a,b,c):
return a+b+c
def printBoard(xstate,zstate):
print('X' if xstate[0] else('O' if zstate[0] else 0), "|" ,
'X' if xstate[1] else('O' if zstate[1] else 1), "|",
'X' if xstate[2] else('O' if zstate[2] else 2))
print('--|---|---')
print('X' if xstate[3] else('O' if zstate[3] else 3), "|" ,
'X' if xstate[4] else('O' if zstate[4] else 4), "|",
'X' if xstate[5] else('O' if zstate[5] else 5))
print('--|---|---')
print('X' if xstate[6] else('O' if zstate[6] else 6), "|" ,
'X' if xstate[7] else('O' if zstate[7] else 7), "|",
'X' if xstate[8] else('O' if zstate[8] else 8))
def checkwin(xstate,zstate):
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]]
for win in wins:
if(sum(xstate[win[0]],xstate[win[1]],xstate[win[2]])==3):
print('\n======> X WON MATCH <=====')
return 1;
if(sum(zstate[win[0]],zstate[win[1]],zstate[win[2]])==3):
print('\n=====> 0 WON MATCH <=====')
return 0;
return -1
if __name__ == "__main__":
xstate = [0,0,0,0,0,0,0,0,0]
zstate = [0,0,0,0,0,0,0,0,0]
turn = 1
print('WELCOME TO ---- TIC TAC TOE ----')
while(True):
printBoard(xstate,zstate)
if(turn == 1):
print("x's chance")
value = int(input("Please enter a value : "))
xstate[value]=1
else :
print("0's chance")
value = int(input("Please enter a value : "))
zstate[value]=1
cwin = checkwin(xstate,zstate)
if(cwin!=-1):
print("\n-----MATCH OVER-----")
break;
turn = 1-turn