-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_toe_toe_system.py
115 lines (110 loc) · 7.78 KB
/
tic_toe_toe_system.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import random
def display_board( state ):
print ("-------------")
print ("| %i | %i | %i |" % (state[0], state[1], state[2]))
print ("-------------")
print ("| %i | %i | %i |" % (state[3], state[4], state[5]))
print ("-------------")
print ("| %i | %i | %i |" % (state[6], state[7], state[8]))
print ("-------------")
def checkPos(state,vv):
y=random.randint(0,8)
print("------%i"%y)
if y not in vv:
if((state[0]==1 and state[1]==1) or (state[3]==1 and state[4]==1) or (state[6]==1 and state[7]==1) or (state[0]==1 and state[4]==1) or (state[2]==1 and state[4]==1) or (state[0]==1 and state[3]==1) or (state[1]==1 and state[4]==1) or (state[2]==1 and state[5]==1)or (state[6]==1 and state[3]==1)or (state[7]==1 and state[4]==1) or (state[8]==1 and state[5]==1) or (state[1]==1 and state[2]==1) or (state[4]==1 and state[5]==1) or (state[7]==1 and state[8]==1) or (state[8]==1 and state[4]==1) or (state[6]==1 and state[4]==1) or (state[0]==1 and state[6]==1) or (state[6]==1 and state[8]==1) or (state[2]==1 and state[8]==1) or (state[0]==1 and state[2]==1) or (state[8]==1 and state[0]==1) or (state[2]==1 and state[6]==1) or (state[3]==1 and state[5]==1)):
#print("%i"%y)
if((state[0]==1 and state[1]==1) or (state[8]==1 and state[5]==1) or (state[6]==1 and state[4]==1)):
if(state[2]==2):
return 2
if((state[3]==1 and state[4]==1) or (state[2]==1 and state[8]==1)):
if(state[5]==2):
return 5
if((state[6]==1 and state[7]==1) or (state[0]==1 and state[4]==1) or (state[2]==1 and state[5]==1)):
if(state[8]==2):
return 8
if((state[2]==1 and state[4]==1) or (state[0]==1 and state[3]==1) or (state[8]==1 and state[7]==1)):
if(state[6]==2):
return 6
if((state[1]==1 and state[4]==1) or (state[8]==1 and state[6]==1)):
if(state[7]==2):
return 7
if((state[2]==1 and state[1]==1) or (state[6]==1 and state[3]==1)):
if(state[0]==2):
return 0
if((state[5]==1 and state[4]==1) or (state[0]==1 and state[6]==1)):
if(state[3]==2):
return 3
if((state[7]==1 and state[4]==1) or (state[0]==1 and state[2]==1)):
if(state[1]==2):
return 1
if((state[0]==1 and state[8]==1) or (state[6]==1 and state[2]==1) or (state[3]==1 and state[5]==1)):
if(state[4]==2):
return 4
return True
if((state[0]==0 and state[1]==0) or (state[3]==0 and state[4]==0) or (state[6]==0 and state[7]==0) or (state[0]==0 and state[4]==0) or (state[2]==0 and state[4]==0) or (state[0]==0 and state[3]==0) or (state[1]==0 and state[4]==0) or (state[2]==0 and state[5]==0)or (state[6]==0 and state[3]==0)or (state[7]==0 and state[4]==0) or (state[8]==0 and state[5]==0) or (state[1]==0 and state[2]==0) or (state[4]==0 and state[5]==0) or (state[7]==0 and state[8]==0) or (state[8]==0 and state[4]==0) or (state[6]==0 and state[4]==0) or (state[0]==0 and state[6]==0) or (state[6]==0 and state[8]==0) or (state[2]==0 and state[8]==0) or (state[0]==0 and state[2]==0) or (state[8]==0 and state[0]==0) or (state[2]==0 and state[6]==0) or (state[3]==0 and state[5]==0)):
print("%i"%y)
if((state[0]==0 and state[1]==0) or (state[8]==0 and state[5]==0) or (state[6]==0 and state[4]==0)):
if(state[2]==2):
return 2
if((state[3]==0 and state[4]==0) or (state[2]==0 and state[8]==0)):
if(state[5]==2):
return 5
if((state[6]==0 and state[7]==0) or (state[0]==0 and state[4]==0) or (state[2]==0 and state[5]==0)):
if(state[8]==2):
return 8
if((state[2]==0 and state[4]==0) or (state[0]==0 and state[3]==0) or (state[8]==0 and state[7]==0)):
if(state[6]==2):
return 6
if((state[1]==0 and state[4]==0) or (state[6]==0 and state[8]==0)):
if(state[7]==2):
return 7
if((state[2]==0 and state[1]==0) or (state[6]==0 and state[3]==0)):
if(state[0]==2):
return 0
if((state[5]==0 and state[4]==0) or (state[0]==0 and state[6]==0)):
if(state[3]==2):
return 3
if((state[7]==0 and state[4]==0) or (state[0]==0 and state[2]==0)):
if(state[1]==2):
return 1
if((state[0]==0 and state[8]==0) or (state[6]==0 and state[2]==0) or (state[3]==0 and state[5]==0)):
if(state[4]==2):
return 4
return True
else:
return y
return True
else:
ans = checkPos(state,vv)
return ans
def tic_toe_toe_sys(state):
player1=True
player2=False
i=9
v=0
vv=[]
while(i>0):
if((state[0]==1 and state[1]==1 and state[2]==1) or (state[3]==1 and state[4]==1 and state[5]==1) or (state[6]==1 and state[7]==1 and state[8]==1) or (state[0]==1 and state[4]==1 and state[8]==1) or (state[2]==1 and state[4]==1 and state[6]==1) or (state[0]==1 and state[3]==1 and state[6]==1) or (state[1]==1 and state[4]==1 and state[7]==1) or (state[2]==1 and state[5]==1 and state[8]==1)):
print("player 1 win")
return False
if((state[0]==0 and state[1]==0 and state[2]==0) or (state[3]==0 and state[4]==0 and state[5]==0) or (state[6]==0 and state[7]==0 and state[8]==0) or (state[0]==0 and state[4]==0 and state[8]==0) or (state[2]==0 and state[4]==0 and state[6]==0) or (state[0]==0 and state[3]==0 and state[6]==0) or (state[1]==0 and state[4]==0 and state[7]==0) or (state[2]==0 and state[5]==0 and state[8]==0)):
print("System win")
return False
if player1==True:
v=int(input("Enter player 1 position "))
state[v-1]=1
player1=False
vv.append(v-1)
if(player1==False):
v=checkPos(state,vv)
#print("%i---"%v)
state[v]=0
vv.append(v)
player1=True
i=i-1
display_board(state)
return True
starting_state=[2,2,2,2,2,2,2,2,2]
print("start state:")
display_board(starting_state)
tic_toe_toe_sys( starting_state )