-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_mech.py
230 lines (212 loc) · 6.23 KB
/
game_mech.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#Game Mechanics
import random
def make_move(turn,col,row, m):
if m[row][col] != 0:
pass
elif turn==1:
m[row][col]= 1
turn=2
else:
m[row][col]=2
turn=1
return m,turn
def checkifwon(m):
for x in range(3):
#check horizontal
if (m[x][0]==m[x][1]==m[x][2]) and (m[x][0] != 0):
highlight = [[x,0],[x,1],[x,2]]
return "Win",highlight
elif (m[0][x]==m[1][x]==m[2][x]) and (m[2][x] != 0):
highlight = [[0,x],[1,x],[2,x]]
#checking vertical
return "Win",highlight
#Checks diagonals
if (m[0][0]==m[1][1]==m[2][2]) and (m[2][2] != 0 ):
highlight = [[0,0],[1,1],[2,2]]
return "Win",highlight
elif (m[2][0]==m[1][1]==m[0][2]) and (m[1][1]!= 0):
highlight = [[2,0],[1,1],[0,2]]
return "Win",highlight
if 0 not in m[0] and 0 not in m[1] and 0 not in m[2]:
return "Tie",""
return None,""
def ConverttoDict(m):
board = {}
for row in range(1,4):
for col in range(1,4):
if m[row-1][col-1] == 0:
board['c'+str(col)+'r'+str(row)] = "None.png"
elif m[row-1][col-1] == 1:
board['c'+str(col)+'r'+str(row)] = "CircleBig.png"
elif m[row-1][col-1] == 2:
board['c'+str(col)+'r'+str(row)] = "CrossBig.png"
return board
def ConverttoString(big_board,turn,win):
board_s = ""
for row in range(3):
for col in range(3):
board_s += str(big_board[row][col])
if col < 2:
board_s += "."
if row < 2:
board_s += "/"
board_s += "/"+str(turn)
board_s += "/"+win
return board_s
def ConverttoMatrix(board_s):
board_split = board_s.split("/")
turn = int(board_split[-2])
win = board_split[-1]
board_split.pop(-1)
big_board = []
for x in range(3):
big_board.append(board_split[x].split("."))
for y in range(3):
big_board[x][y] = int(big_board[x][y])
return big_board, turn, win
def compMove(m):
count=0
for x in range(2):
for y in range(3):
if (m[y][0]==m[y][1] and m[y][2]==0 and m[y][0]==(2-x) and count==0):
m[y][2]=2
count+=1
elif (m[y][2]==m[y][1] and m[y][0]==0 and m[y][2]==(2-x)and count==0):
m[y][0]=2
count+=1
elif (m[y][0]==m[y][2] and m[y][1]==0 and m[y][2]==(2-x)and count==0):
m[y][1]=2
count+=1
elif (m[0][y]==m[1][y] and m[2][y]==0 and m[0][y]==(2-x)and count==0):
m[2][y]=2
count+=1
elif (m[0][y]==m[2][y] and m[1][y]==0 and m[0][y]==(2-x)and count==0):
m[1][y]=2
count+=1
elif (m[2][y]==m[1][y] and m[0][y]==0 and m[1][y]==(2-x)and count==0):
m[0][y]=2
count+=1
elif (m[1][1]==m[0][0] and m[2][2]==0 and m[1][1]==(2-x)and count==0):
m[2][2]=2
count+=1
elif (m[1][1]==m[2][2] and m[0][0]==0 and m[1][1]==(2-x)and count==0):
m[0][0]=2
count+=1
elif (m[2][2]==m[0][0] and m[1][1]==0 and m[2][2]==(2-x)and count==0):
m[1][1]=2
count+=1
elif (m[2][0]==m[0][2] and m[1][1]==0 and m[0][2]==(2-x)and count==0):
m[1][1]=2
count+=1
elif (m[0][2]==m[1][1] and m[2][0]==0 and m[1][1]==(2-x)and count==0):
m[2][0]=2
count+=1
elif (m[1][1]==m[2][0] and m[0][2]==0 and m[1][1]==(2-x)and count==0):
m[0][2]=2
count+=1
if (count==0) and(0 in m[0] or 0 in m[1] or 0 in m[2]):
randomMove(m)
return m
def randomMove(m):
row=random.randint(0,2)
col=random.randint(0,2)
if m[row][col]>0:
randomMove(m)
else:
m[row][col]=2
return m
#-----------Done--------------------------
def make_move_crazy(m,row,col,turn):
if m[row][col]>0:
pass
else:
if turn==1:
m[row][col]=1
turn=2
else:
m[row][col]=2
turn=1
return m,turn
#-----------Done---------------------------
#-----------------Done (I think)-----------
def checkifwon_crazy(m,win):
for x in range(3):
#check horizontal
if (m[x][0]==m[x][1]==m[x][2]) and (m[x][0]>0) and win==0:
return "Win", m[x][0]
elif (m[0][x]==m[1][x]==m[2][x]) and (m[2][x]>0)and win==0:
#checking vertical
return "Win",m[0][x]
#Checks diagonals
if (m[0][0]==m[1][1]==m[2][2]) and (m[2][2]>0)and win==0:
return "Win",m[1][1]
elif (m[2][0]==m[1][1]==m[0][2]) and (m[1][1]>0)and win==0:
return "Win", m[1][1]
if 0 not in m[0] and 0 not in m[1] and 0 not in m[2]:
return "Tie", 3
return None, 0
#------------------Done-------------------------------
def checkifwon_crazy_f(win_t):
if (win_t[0]==win_t[1]==win_t[2]) and (win_t[1]>0):
return "Win",win_t[0]
elif (win_t[3]==win_t[4]==win_t[5]) and (win_t[4]>0):
return "Win",win_t[4]
elif (win_t[6]==win_t[7]==win_t[8]) and (win_t[7]>0):
return "Win",win_t[7]
elif (win_t[0]==win_t[3]==win_t[6]) and (win_t[0]>0):
return "Win",win_t[0]
elif (win_t[1]==win_t[4]==win_t[7]) and (win_t[4]>0):
return "Win",win_t[4]
elif (win_t[2]==win_t[5]==win_t[8]) and (win_t[5]>0):
return "Win",win_t[5]
#Checks diagonals
elif win_t[0]==win_t[4]==win_t[8] and (win_t[4]>0):
return "Win",win_t[4]
elif win_t[6]==win_t[4]==win_t[2] and (win_t[4]>0):
return "Win", win_t[4]
elif 0 not in win_t:
return "Tie", 3
return " ", 0
def ConverttoDict_crazy(m):
board = {}
for table in range(1,10):
for row in range(1,4):
for col in range(1,4):
if m[table-1][row-1][col-1] == 0:
board['t'+str(table)+'c'+str(col)+'r'+str(row)] = "NoneSmall.png"
elif m[table-1][row-1][col-1] == 1:
board['t'+str(table)+'c'+str(col)+'r'+str(row)] = "CircleSmall.png"
elif m[table-1][row-1][col-1] == 2:
board['t'+str(table)+'c'+str(col)+'r'+str(row)] = "CrossSmall.png"
return board
def ConverttoString_crazy(big_board,turn,win_t,win):
board_s = ""
for game in range(9):
for row in range(3):
for col in range(3):
board_s += str(big_board[game][row][col])
if col < 2:
board_s += "."
if row < 2:
board_s += "/"
board_s += "/"+str(win_t[game])
if game < 8:
board_s += ","
board_s += ","+str(turn)
board_s += ","+win
return board_s
def ConverttoMatrix_crazy(board_s):
board_split = board_s.split(",")
win_t = []
win = board_split[-1]
turn = int(board_split[-2])
big_board = []
for z in range(9):
big_board.append(board_split[z].split("/"))
win_t.append(int(big_board[z][-1]))
big_board[z].pop(-1)
for x in range(3):
big_board[z][x]=big_board[z][x].split(".")
for y in range(3):
big_board[z][x][y] = int(big_board[z][x][y])
return big_board, turn, win_t,win