forked from zense/Hackathon-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquare_Checker.py
276 lines (218 loc) · 10.1 KB
/
Square_Checker.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# This is a game in which a player with highest number of squares made wins at last
from tkinter import *
import numpy as np
# Defining Basic Heights And Widths Of Things
size_of_board = 800
number_of_dots = 8
symbol_size = (size_of_board / 3 - size_of_board / 8) / 2
symbol_thickness = 50
dot_color = '#7BC043' # Green
player1_color = '#0492CF' # Darkish Blue
player1_box_light = '#FF8100' # Orange
player2_color = '#FF0000' # Red
player2_box_light = '#FFFF00' # Yellow
Green_color = '#7BC043'
dot_width = 0.25*size_of_board/number_of_dots
edge_width = 0.1*size_of_board/number_of_dots
distance_between_dots = size_of_board / (number_of_dots)
# Defining Class named with Game Name
class Square_Maker():
# Initialization functions
def __init__(self):
self.window = Tk()
self.window.title('Square_Maker')
self.canvas = Canvas(self.window, width=size_of_board, height=size_of_board)
self.canvas.pack()
self.window.bind('<Button-1>', self.click)
self.player1_starts = True
self.refresh_board()
self.play_again()
# Defining Play Again Option
def play_again(self):
self.refresh_board()
self.board_status = np.zeros(shape=(number_of_dots - 1, number_of_dots - 1))
self.row_status = np.zeros(shape=(number_of_dots, number_of_dots - 1))
self.col_status = np.zeros(shape=(number_of_dots - 1, number_of_dots))
# Input from user in form of clicks of Mouse
self.player1_starts = not self.player1_starts
self.player1_turn = not self.player1_starts
self.reset_board = False
self.turntext_handle = []
self.already_marked_boxes = []
self.display_turn_text()
# Running Event Loop by defining function for it
def mainloop(self):
self.window.mainloop()
# Logical Functions:
# The modules required to carry out game logic
# Function To Check Whether Grid was coloured or not
def is_grid_occupied(self, logical_position, type):
r = logical_position[0]
c = logical_position[1]
occupied = True
if type == 'row' and self.row_status[c][r] == 0:
occupied = False
if type == 'col' and self.col_status[c][r] == 0:
occupied = False
return occupied
def convert_grid_to_logical_position(self, grid_position):
grid_position = np.array(grid_position)
position = (grid_position-distance_between_dots/4)//(distance_between_dots/2)
type = False
logical_position = []
if position[1] % 2 == 0 and (position[0] - 1) % 2 == 0:
r = int((position[0]-1)//2)
c = int(position[1]//2)
logical_position = [r, c]
type = 'row'
# self.row_status[c][r]=1
elif position[0] % 2 == 0 and (position[1] - 1) % 2 == 0:
c = int((position[1] - 1) // 2)
r = int(position[0] // 2)
logical_position = [r, c]
type = 'col'
return logical_position, type
# Defining Function To Shade Box With Respective Colour of player's by checking if square is completed or not
def mark_box(self):
boxes = np.argwhere(self.board_status == -4)
for box in boxes:
if list(box) not in self.already_marked_boxes and list(box) !=[]:
self.already_marked_boxes.append(list(box))
color = player1_box_light
self.shade_box(box, color)
boxes = np.argwhere(self.board_status == 4)
for box in boxes:
if list(box) not in self.already_marked_boxes and list(box) !=[]:
self.already_marked_boxes.append(list(box))
color = player2_box_light
self.shade_box(box, color)
def update_board(self, type, logical_position):
r = logical_position[0]
c = logical_position[1]
val = 1
if self.player1_turn:
val =- 1
if c < (number_of_dots-1) and r < (number_of_dots-1):
self.board_status[c][r] += val
if type == 'row':
self.row_status[c][r] = 1
if c >= 1:
self.board_status[c-1][r] += val
elif type == 'col':
self.col_status[c][r] = 1
if r >= 1:
self.board_status[c][r-1] += val
# To Check Whether Game is over or not
def is_gameover(self):
return (self.row_status == 1).all() and (self.col_status == 1).all()
# Drawing Functions:
# The modules required to draw required game based object on canvas
def make_edge(self, type, logical_position):
if type == 'row':
start_x = distance_between_dots/2 + logical_position[0]*distance_between_dots
end_x = start_x+distance_between_dots
start_y = distance_between_dots/2 + logical_position[1]*distance_between_dots
end_y = start_y
elif type == 'col':
start_y = distance_between_dots / 2 + logical_position[1] * distance_between_dots
end_y = start_y + distance_between_dots
start_x = distance_between_dots / 2 + logical_position[0] * distance_between_dots
end_x = start_x
if self.player1_turn:
color = player1_color
else:
color = player2_color
self.canvas.create_line(start_x, start_y, end_x, end_y, fill=color, width=edge_width)
def display_gameover(self):
player1_score = len(np.argwhere(self.board_status == -4))
player2_score = len(np.argwhere(self.board_status == 4))
if player1_score > player2_score:
# Player 1 wins
text = 'Winner: Player 1 '
color = player1_color
elif player2_score > player1_score:
text = 'Winner: Player 2 '
color = player2_color
else:
text = 'Its a tie'
color = 'gray'
self.canvas.delete("all")
self.canvas.create_text(size_of_board / 2, size_of_board / 3, font="cmr 60 bold", fill=color, text=text)
score_text = 'Scores \n'
self.canvas.create_text(size_of_board / 2, 5 * size_of_board / 8, font="cmr 40 bold", fill=Green_color,
text=score_text)
score_text = 'Player 1 : ' + str(player1_score) + '\n'
score_text += 'Player 2 : ' + str(player2_score) + '\n'
# score_text += 'Tie : ' + str(self.tie_score)
self.canvas.create_text(size_of_board / 2, 3 * size_of_board / 4, font="cmr 30 bold", fill=Green_color,
text=score_text)
self.reset_board = True
score_text = 'Click to play again \n'
self.canvas.create_text(size_of_board / 2, 15 * size_of_board / 16, font="cmr 20 bold", fill="gray",
text=score_text)
def refresh_board(self):
for i in range(number_of_dots):
x = i*distance_between_dots+distance_between_dots/2
self.canvas.create_line(x, distance_between_dots/2, x,
size_of_board-distance_between_dots/2,
fill='gray', dash = (2, 2))
self.canvas.create_line(distance_between_dots/2, x,
size_of_board-distance_between_dots/2, x,
fill='gray', dash=(2, 2))
for i in range(number_of_dots):
for j in range(number_of_dots):
start_x = i*distance_between_dots+distance_between_dots/2
end_x = j*distance_between_dots+distance_between_dots/2
self.canvas.create_oval(start_x-dot_width/2, end_x-dot_width/2, start_x+dot_width/2,
end_x+dot_width/2, fill=dot_color,
outline=dot_color)
def display_turn_text(self):
text = 'Next turn: '
if self.player1_turn:
text += 'Player1'
color = player1_color
else:
text += 'Player2'
color = player2_color
self.canvas.delete(self.turntext_handle)
self.turntext_handle = self.canvas.create_text(size_of_board - 5*len(text),size_of_board-distance_between_dots/8,font="cmr 15 bold", text=text, fill=color)
def shade_box(self, box, color):
start_x = distance_between_dots / 2 + box[1] * distance_between_dots + edge_width/2
start_y = distance_between_dots / 2 + box[0] * distance_between_dots + edge_width/2
end_x = start_x + distance_between_dots - edge_width
end_y = start_y + distance_between_dots - edge_width
self.canvas.create_rectangle(start_x, start_y, end_x, end_y, fill=color, outline='')
def display_turn_text(self):
text = 'Next turn: '
if self.player1_turn:
text += 'Player1'
color = player1_color
else:
text += 'Player2'
color = player2_color
self.canvas.delete(self.turntext_handle)
self.turntext_handle = self.canvas.create_text(size_of_board - 5*len(text),
size_of_board-distance_between_dots/8,
font="cmr 15 bold",text=text, fill=color)
def click(self, event):
if not self.reset_board:
grid_position = [event.x, event.y]
logical_positon, valid_input = self.convert_grid_to_logical_position(grid_position)
if valid_input and not self.is_grid_occupied(logical_positon, valid_input):
self.update_board(valid_input, logical_positon)
self.make_edge(valid_input, logical_positon)
self.mark_box()
self.refresh_board()
self.player1_turn = not self.player1_turn
if self.is_gameover():
# self.canvas.delete("all")
self.display_gameover()
else:
self.display_turn_text()
else:
self.canvas.delete("all")
self.play_again()
self.reset_board = False
# Defining Game Instance As object of class Of Our Main Game
game_instance = Square_Maker()
game_instance.mainloop()