-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
476 lines (399 loc) · 15.4 KB
/
main.py
File metadata and controls
476 lines (399 loc) · 15.4 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
from tkinter import *
import datetime
import math
import enum
import numpy as np
import random
import time
import tkinter.messagebox
np.random.seed(0)
random.seed(0)
from PIL import Image
from PIL import ImageTk
class Suit(enum.Enum):
SPADES = 1
HEARTS = 2
CLUBS = 3
DIAMONDS = 4
no_of_ranks = 13
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
self.tag_id = None
self.canvas = None
def set_canvas(self, canvas):
self.canvas = canvas
def set_tag_id(self, tag_id):
self.tag_id = tag_id
dummy_arr_for_imgs = []
stacks_ = None
class Stack:
def __init__(self, cards, no_of_hidden_cards):
self.cards = cards
self.no_of_hidden_cards = no_of_hidden_cards
self.no_of_draggable_cards = 0
self.init_no_of_draggable_cards()
def init_no_of_draggable_cards(self):
global stacks_
n = len(self.cards)
if n==0:
self.no_of_draggable_cards = 0
return
flag = False
for i in reversed(range( n-1 )):
print('ranks: {} {}'.format(self.cards[i].rank, self.cards[i+1].rank))
if self.cards[i].rank != self.cards[i+1].rank+1:
flag = True
print('marking no_of_draggable_cards: ', n-(i+1))
self.no_of_draggable_cards = n - (i+1)
break
if not flag: self.no_of_draggable_cards = n
try:
print('iterating from 0 to {} and setting them non-draggable'.format(n-self.no_of_draggable_cards))
for i in range(n-self.no_of_draggable_cards):
make_non_draggable(self.cards[i].tag_id)
print('iterating from {} to {} and setting draggable'.format(n-self.no_of_draggable_cards, n))
for i in range(n-self.no_of_draggable_cards, n):
print('*')
print('{}, {}'.format(self.cards[i].tag_id, self.cards[i].canvas))
print('&')
make_draggable(self.cards[i].tag_id, self.cards[i].canvas, stacks_)
except Exception as e:
print(e)
pass
def remove_cards_from_stack_from_index(self, j):
#j is the no_of_cards_to_be_removed
ret = self.cards[-j:]
for i in range(j): self.cards.pop()
n = len(self.cards)
global dummy_arr_for_imgs
if n!=0 and n == self.no_of_hidden_cards:
#print(n)
card = self.cards[n-1]
obj_id = card.tag_id
image = get_preprocessed_image(card)
dummy_arr_for_imgs.append(image)
#print(image)
canvas.itemconfig(obj_id, image = image)
canvas.tag_raise(obj_id)
#time.sleep(2)
self.no_of_hidden_cards -= 1
make_draggable(obj_id, canvas, stacks)
self.init_no_of_draggable_cards()
return ret
def add_cards_to_stack(self, new_cards, i):
n = len(self.cards)
for idx, new_card in enumerate(new_cards):
obj_id = new_card.tag_id
j = n+idx
print('updating for: rank: {} from {}, {} to {}, {}'.format(new_card.rank, d_obj_id_to_card[obj_id][0], d_obj_id_to_card[obj_id][1], i, j))
d_obj_id_to_card[obj_id] = (i, j)
self.cards.extend(new_cards)
self.init_no_of_draggable_cards()
class DrawCardManager:
def __init__(self, total_no_of_full_sequences):
self.total_no_of_full_sequences = total_no_of_full_sequences
self.all_cards = []
self.cards_used = []
self.all_cards_random_perm = []
self.index_into_all_cards_random_perm = 0
self.no_of_full_sequences_completed_by_user = 0
#draw_initial_cards(no_of_initial_cards)
def draw_initial_cards(self, no_of_initial_cards):
# Initialize all cards
global no_of_ranks
for i in range(self.total_no_of_full_sequences):
for rank in range(1, no_of_ranks+1):
for suit in [Suit.SPADES]:
self.all_cards.append(Card(rank, suit))
#print(len(self.all_cards))
# Initialize cards_used
self.all_cards_random_perm = list(np.random.permutation(self.all_cards))
self.index_into_all_cards_random_perm = no_of_initial_cards
#print(no_of_initial_cards)
return self.all_cards_random_perm[:no_of_initial_cards]
def getNextRandomCards(self, no_of_cards):
if self.index_into_all_cards_random_perm+no_of_cards > self.total_no_of_full_sequences*13:
tkinter.messagebox.showerror("Error", "No more cards!")
return None
ret = []
ret = self.all_cards_random_perm[self.index_into_all_cards_random_perm:self.index_into_all_cards_random_perm+no_of_cards]
self.index_into_all_cards_random_perm += no_of_cards
return ret
all_cards_file_path = 'images/'
hidden_card_file_name = 'card_back.jpg'
correction = {
1:'ace',
11:'jack',
12:'queen',
13:'king'
}
stacks = None
def my_find(canvas, obj_id):
arr = canvas.find_all()
x,y = canvas.coords(obj_id)
min_dist = 1000000
min_id = None
for arr_item in arr:
#if arr_item in d_obj_id_to_card:
# if d_obj_id_to_card[arr_item][1] == -1: continue
if obj_id == arr_item: continue
x0, y0, x1, y1 = canvas.bbox(arr_item)
if(x0<x and x<x1 and y0<y and y<y1):
dist = math.sqrt((x-x0)**2+(y-y0)**2)
if min_dist > dist:
min_dist = dist
min_id = arr_item
return min_id
def get_obj_ids_of_all_subsequent_elements(obj_id ,stacks):
i, j = d_obj_id_to_card[obj_id]
n = len(stacks[i].cards)
arr = []
for j2 in range(j, n): arr.append(stacks[i].cards[j2].tag_id)
return arr
frame2_arr, no_of_frame2_arr_items_done = None, None
def handle_stack_done(stack, canvas):
global frame2_arr, no_of_frame2_arr_items_done
try: x1, y1 = canvas.coords(frame2_arr[no_of_frame2_arr_items_done])
except: x1, y1, _, _ = canvas.coords(frame2_arr[no_of_frame2_arr_items_done])
for card in stack.cards:
obj_id = card.tag_id
animate(obj_id, x1, y1, canvas)
canvas.tag_raise(obj_id)
time.sleep(0.1)
for card in stack.cards:
obj_id = card.tag_id
if card.rank==1:
make_non_draggable(obj_id)
continue
canvas.delete(obj_id)
no_of_frame2_arr_items_done += 1
if no_of_frame2_arr_items_done == 6:
messagebox.showinfo("Congratulations","----You won!----")
def on_drop(event, obj_id, canvas, stacks):
global d, d_obj_id_to_card
obj_ids = get_obj_ids_of_all_subsequent_elements(obj_id, stacks)
x, y = canvas.coords(obj_id)
droppable = False
ovlp_id_valid = False
#decide whether overlap id is a valid id!
#try:
ovlp_id = my_find(canvas, obj_id)
print('ovlp_id', ovlp_id)
if d_obj_id_to_card.get(ovlp_id, None)!=None:
ovlp_id_valid = True
#except Exception as e:
# print(e)
empty = False
#Decide whether droppable
if ovlp_id_valid:
i, j = d_obj_id_to_card[ovlp_id]
# Note: if stack is empty, then it is droppable:
if j==-1:
droppable = True
empty = True
else:
i1, j1 = d_obj_id_to_card[obj_id]
card1 = stacks[i1].cards[j1]
i, j = d_obj_id_to_card[ovlp_id]
print('i, j: {}, {}'.format(i,j))
card = stacks[i].cards[j]
print('card_rank: ', card.rank)
print('card1_rank', card1.rank)
print('ovlp_id', ovlp_id)
if i!=i1 or i1 == len(stacks[i1].cards)-1:
if card.rank == card1.rank+1:
droppable = True
else: droppable = False
if droppable:
#start the dropping process
#i, j = d_obj_id_to_card[ovlp_id]
print(canvas.coords(ovlp_id))
try: x, y = canvas.coords(ovlp_id)
except: x, y, _, _ = canvas.coords(ovlp_id)
obj_id = None
y_offset = None
for idx, obj_id in enumerate(obj_ids):
i_, j_ = d_obj_id_to_card[obj_id]
y_offset = idx+1
if empty: y_offset = idx
canvas.coords(obj_id, x, y+y_offset*20)
#i1, j1 = d_obj_id_to_card[obj_id]
#print(stacks[i].cards[j].rank)
no_of_cards_to_remove = len(obj_ids)
i1, j1 = d_obj_id_to_card[obj_id]
cards = stacks[i1].remove_cards_from_stack_from_index(no_of_cards_to_remove)
stacks[i].add_cards_to_stack(cards, i)
#Now if stack is done, full!, we will animate it to the next frame2 item and empty it
if stacks[i].no_of_draggable_cards==13:
handle_stack_done(stacks[i], canvas)
stacks[i].remove_cards_from_stack_from_index(13)
else:
#xy_arr = d[obj_id]
for idx, obj_id in enumerate(obj_ids):
x, y = d[obj_id]
canvas.coords(obj_id, x, y)
d = {}
def on_drag_start(event, obj_id, canvas, stacks):
global d
obj_ids = get_obj_ids_of_all_subsequent_elements(obj_id, stacks)
for obj_id in obj_ids:
x, y = canvas.coords(obj_id)
d[obj_id] = (x,y)
#if obj_id not in d: d[obj_id] = []
#d[obj_id].append((x, y))
def on_drag_motion(event, obj_id, canvas, stacks):
global d
obj_ids = get_obj_ids_of_all_subsequent_elements(obj_id, stacks)
#print(len(obj_ids))
for i, obj_id in enumerate(obj_ids):
x,y = canvas.coords(obj_id)
x2, y2 = event.x, event.y
canvas.coords(obj_id, x2, y2+20*i)
canvas.tag_raise(obj_id)
def get_card_file_name(card):
global all_cards_file_path
#print('Called get_card_file_name for card: {}, {}'.format(card.rank, card.suit.name))
rank, suit = card.rank, card.suit
if(rank==1 or rank>10): rank = correction[rank]
rank = str(rank)
suit_string = suit.name.lower()
ret = all_cards_file_path+rank+'_'+suit_string+'.png'
#print('returning : {}'.format(ret))
return ret
def do_nothing(event):
pass
def make_non_draggable(obj_id):
canvas.tag_bind(obj_id, "<Button-1>", do_nothing)
canvas.tag_bind(obj_id, "<B1-Motion>", do_nothing)
canvas.tag_bind(obj_id, "<ButtonRelease-1>", do_nothing)
def make_draggable(obj_id, canvas, stacks):
canvas.tag_bind(obj_id, "<Button-1>", lambda event: on_drag_start(event, obj_id, canvas, stacks))
canvas.tag_bind(obj_id, "<B1-Motion>", lambda event: on_drag_motion(event, obj_id, canvas, stacks))
canvas.tag_bind(obj_id, "<ButtonRelease-1>", lambda event: on_drop(event, obj_id, canvas, stacks))
width, height = 75, 95
def get_preprocessed_image(card=None):
global all_cards_file_path, hidden_card_file_name, width, height
if card==None:
filename = all_cards_file_path+hidden_card_file_name
else:
filename = get_card_file_name(card)
img = Image.open(filename)
img = img.resize((width,height), Image.ANTIALIAS)
image = ImageTk.PhotoImage(img)
return image
padx, pady = 5, 10
x0 = 20; y0 = 20
shown_ht = 20
def almost_reached(a, b, c, d):
return math.sqrt((a-c)**2+(b-d)**2)<0.01
def animate(obj_id, x1, y1, canvas):
try:
x0, y0 = canvas.coords(obj_id)
except:
return
if almost_reached(x0, y0, x1, y1):
return
canvas.tag_raise(obj_id)
canvas.coords(obj_id, x0+(x1-x0)/10, y0+(y1-y0)/10)
canvas.after(10, animate, obj_id, x1, y1, canvas)
dummy_arr_for_imgs = []
def on_click(event, stacks, canvas, drawCardManager):
for stack in stacks:
if len(stack.cards) == 0:
tkinter.messagebox.showerror("Error", "There can't be any empty stacks!")
return
cards = drawCardManager.getNextRandomCards(10)
if cards is None: return
images = []
for card in cards:
image = get_preprocessed_image(card)
dummy_arr_for_imgs.append(image)
images.append(image)
for i, stack in enumerate(stacks):
obj = canvas.create_image( (20,20), anchor=NW, image=images[i])
n = len(stack.cards)
d_obj_id_to_card[obj] = (i, n-1)
cards[i].tag_id = obj
cards[i].canvas = canvas
tag_id = stack.cards[n-1].tag_id
x, y = canvas.coords(tag_id)
y+=20
animate(obj, x, y, canvas)
stack.add_cards_to_stack([cards[i]], i)
d_obj_id_to_card = None
def main():
global width, height, padx, pady, shown_ht, canvas, stacks, d_obj_id_to_card, stacks_, frame2_arr, no_of_frame2_arr_items_done
root = Tk()
root.geometry("800x600")
canvas = Canvas(root, width=800, height=600)
canvas.config(bg = "green")
canvas.pack(fill="both", expand=True)
#create frame2_arr
frame2_arr = []
no_of_frame2_arr_items_done = 0
for i in range(6):
x1, y1 = x0+(i+2)*(width+padx), y0
x2, y2 = x1+width, y1+height
obj_id = canvas.create_rectangle(x1, y1, x2, y2, fill='pale green')
frame2_arr.append(obj_id)
stacks = []
no_of_stacks = 10
hidden, visible = 0, 0
total_no_of_full_sequences = 6
hidden_arr, visible_arr = [], []
initial_no_cards = 0
for i in range(no_of_stacks):
hidden = random.randrange(1,3)
visible = random.randrange(1,3)
initial_no_cards += (hidden+visible)
hidden_arr.append(hidden); visible_arr.append(visible)
stacks_ = stacks
#create frame3
drawCardManager = DrawCardManager(total_no_of_full_sequences)
initial_cards = drawCardManager.draw_initial_cards(initial_no_cards)
initial_cards_i = 0
'''
#remove this after testing
hidden_arr[0] = 0; visible_arr[0] = 12
cards = []
for i in range(12):
cards.append(Card(13-i, Suit.SPADES))
stacks.append(Stack(cards, 0))
for i in range(1, no_of_stacks):
hidden = hidden_arr[i]; visible = visible_arr[i]
cards = initial_cards[initial_cards_i:initial_cards_i+hidden+visible]
initial_cards_i += (hidden + visible)
stack = Stack(cards, hidden)
stacks.append(stack)
'''
for i in range(no_of_stacks):
hidden = hidden_arr[i]; visible = visible_arr[i]
cards = initial_cards[initial_cards_i:initial_cards_i+hidden+visible]
initial_cards_i += (hidden + visible)
stack = Stack(cards, hidden)
stacks.append(stack)
dummy_arr = []; dummy_img_arr = []
d_obj_id_to_card = {}
for i, stack in enumerate(stacks):
x=i*80+20; y=160
empty_card_obj_id = canvas.create_rectangle(x, y, x+width, y+height, fill='pale green')
d_obj_id_to_card[empty_card_obj_id] = (i, -1)
for j, card in enumerate(stack.cards):
if j<stack.no_of_hidden_cards:
image = get_preprocessed_image()
else:
image = get_preprocessed_image(card)
x=i*80+20; y=160+j*20
obj_id = canvas.create_image( (x, y), anchor=NW, image=image)
d_obj_id_to_card[obj_id] = (i, j)
n = len(stack.cards)
if j>=n-stack.no_of_draggable_cards: make_draggable(obj_id, canvas, stacks)
dummy_img_arr.append(image)
card.tag_id = obj_id
card.canvas = canvas
frame1_obj_id = canvas.create_rectangle(x0, y0, x0+width, y0+height, fill='pale green')
canvas.tag_bind(frame1_obj_id, "<Button-1>", lambda event, canvas=canvas, stacks=stacks: on_click(event, stacks, canvas, drawCardManager))
root.mainloop()
main()