-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
124 lines (97 loc) · 3.32 KB
/
database.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
import random
from PIL import Image, ImageTk
IMAGE_PATH = r"./images/"
#the pile
LETTERS = {"a": 13, "b": 3, "c": 3, "d": 6, "e": 18, "f": 3, "g": 4,
"h": 3, "i": 12, "j": 2, "k": 2, "l": 5, "m": 3, "n": 8,
"o": 11, "p": 3, "q": 2, "r": 9, "s": 6, "t": 9, "u": 6,
"v": 3, "w": 3, "x": 2, "y": 3, "z": 2}
# the class for creating images
class CreateCanvasObject(object):
def __init__(self, canvas, letter, xpos, ypos):
self.canvas = canvas
self.xpos, self.ypos = xpos, ypos
self.tk_image = ImageTk.PhotoImage(
Image.open(IMAGE_PATH + letter + ".jpg").resize((32,32)))
self.image_obj = canvas.create_image(
xpos, ypos, image=self.tk_image)
canvas.tag_bind(self.image_obj, '<Button1-Motion>', self.move)
canvas.tag_bind(self.image_obj, '<ButtonRelease-1>', self.release)
self.move_flag = False
def move(self, event):
if self.move_flag:
new_xpos, new_ypos = event.x, event.y
self.canvas.move(self.image_obj,
new_xpos - self.mouse_xpos, new_ypos - self.mouse_ypos)
self.mouse_xpos = new_xpos
self.mouse_ypos = new_ypos
else:
self.move_flag = True
self.canvas.tag_raise(self.image_obj)
self.mouse_xpos = event.x
self.mouse_ypos = event.y
def release(self, event):
self.move_flag = False
class Game:
def __init__(self, name):
self.name = name
self.players = []
self.letters = LETTERS
def add_player(self, player):
self.players.append(player)
def distribute(self):
if len(self.players) <= 4:
n = 21
elif len(self.players) <= 6:
n = 15
else:
n = 11
while n > 0:
n -= 1
for player in self.players:
player.draw(self.letters)
def peel(self):
# all players draw one card
if sum(self.letters.values()) > len(self.players):
for player in self.players:
player.draw(self.letters)
# the player
class Player:
def __init__(self, name):
self.name = name
self.tiles = {}
def draw(self, letters):
letter = random.choice(list(letters.keys()))
if letters[letter] > 0:
letters[letter] -= 1
if letter not in self.tiles.keys():
self.tiles[letter] = 1
else:
self.tiles[letter] += 1
def dump(self, letters):
# return 1
# draw 3
n = 3
while n > 0:
n -= 1
self.draw(letters)
chosen_letter = choose_letter(self.tiles)
self.tiles[chosen_letter] -= 1
letters[chosen_letter] += 1
def choose_letter(letters): #letters that you have in hand
#insert routine here to choose a letter
print(letters)
letter = str(input("select a letter to dump"))
for let, value in letters.items():
if let == letter and value > 0:
return letter
else:
print("error, you have no such letter! try again!")
pass
if __name__ == '__main__':
# draw a board
new_game = Game("new_game")
Lyndon = Player("Lyndon")
new_game.add_player(Lyndon)
new_game.distribute()
Lyndon.dump(new_game.letters)