-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRummikub.py
124 lines (95 loc) · 2.82 KB
/
Rummikub.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
from random import shuffle
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
PINK = (255, 192, 203)
ORANGE = (255, 165, 0)
LIGHT_BLUE = (135, 206, 235)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
class Game:
bg_color = LIGHT_BLUE
def __init__(self, players):
self.pile = Pile()
self.sets = []
self.hands = [Rack(self.pile) for i in range(players)]
self.in_progress = True
self.game_loop()
def game_loop(self):
while self.in_progress:
for hand in self.hands:
self.play_round(hand)
if hand.get_length() == 0: # if the player has gotten rid of all the tiles
self.in_progress = False
break
def play_round(self, hand):
pass
def valid_gamestate(self):
for set in self.sets:
if not set.is_valid():
return False
return True
class Tile:
width = None
height = None
bg_color = WHITE
def __init__(self, val, color):
self.val = val
self.color = color
def draw(self, x, y):
pass
class Rack:
start_amount = 13
def __init__(self, pile):
self.the_pile = pile
self.tiles = [pile.retrieve_tile() for i in range(self.start_amount)]
def draw(self):
pass
def get_tile(self):
pass
def get_length(self):
return len(self.tiles)
def score(self):
return sum([tile.val for tile in self.tiles])
class Set:
def __init__(self, tiles):
self.tiles = tiles
def is_valid(self):
if len(self.tiles) < 2: # must be at least 3 long
return False
colors = set([tile.color for tile in self.tiles])
vals = sorted([tile.val for tile in self.tiles])
if len(colors) != len(self.tiles) and len(colors) != 1: # must have all dif or all same color
return False
all_same_color = len(colors) == 1
if len(set(vals)) == 1: # if they share one number
ascending = False
elif [i for i in range(vals[0], vals[-1])] == [vals]:
ascending = True
else:
return False
return ascending == all_same_color
def split(self, index):
set1 = Set(self.tiles[:index])
set2 = Set(self.tiles[index:])
del self
return set1, set2
def fuse(self):
pass
def draw(self):
pass
def get_open_location(self):
pass
class Pile:
def __init__(self):
self.pile = []
for iteration in range(2):
for val in range(1, 14):
for color in [RED, BLUE, BLACK, YELLOW]:
self.pile.append(Tile(val, color))
shuffle(self.pile)
def retrieve_tile(self):
return self.pile.pop()
def draw(self):
pass