This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initial_play.py
178 lines (149 loc) · 5.96 KB
/
initial_play.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
import itertools
def initial_play(hand):
# find jokers in hand
jokers_count = hand.count((0, 0))
# get all possible substitutions for jokers
jokers_combinations = combinations([(1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1),
(11, 1), (12, 1), (13, 1), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2),
(7, 2), (8, 2), (9, 2), (10, 2), (11, 2), (12, 2), (13, 2), (1, 3), (2, 3),
(3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3), (10, 3), (11, 3),
(12, 3), (13, 3), (1, 4), (2, 4), (3, 4), (4, 4), (5, 4), (6, 4), (7, 4),
(8, 4), (9, 4), (10, 4), (11, 4), (12, 4), (13, 4)], jokers_count)
print(jokers_count)
result = 0, [], []
for c in jokers_combinations:
new_hand = [x for x in hand]
# replace jokers for combination tiles
for tile in list(c):
new_hand.pop(new_hand.index((0, 0)))
new_hand.append(tile)
# play
current_result = initial_play_rec(new_hand, [])
# update the best play
if current_result[0] > result[0]:
result = current_result
# re-insert jokers in result
for tile in list(c):
result_hand = result[1]
for x in result_hand:
if x == tile:
result_hand.remove(tile)
result_hand.append((0, 0))
break
found = False
result_comb = result[2]
for x in result_comb:
for y in x:
if y == tile:
x.remove(tile)
x.append((0, 0))
found = True
break
if found:
break
result = result[0], result_hand, result_comb
return result
def initial_play_rec(hand, current_result):
result_points = 0
result_hand = hand
result = []
# determine series
_series = series(hand)
# determine stairs
_stairs = stairs(hand)
# base_case
if len(_series) == 0 and len(_stairs) == 0:
return 0, hand, [x for x in current_result]
for serie in _series:
t_points = serie[0][1][0] * len(serie)
# remove serie from hand
new_hand = [x for x in hand]
for tile in list(map(lambda x: x[1], serie)):
new_hand.remove(tile)
# initial_play form result hand
current_result.append(list(map(lambda x: x[1], serie)))
pts, final_hand, final_result = initial_play_rec(new_hand, current_result)
current_result.pop(-1)
if pts + t_points > 30 and (len(final_hand) < len(result_hand) or points(final_hand) > points(result_hand)):
result_points = pts + t_points
result_hand = final_hand
result = final_result
for stair in _stairs:
e_points = sum(map(lambda x: x[1][0], stair))
# remove stair from hand
new_hand = [x for x in hand]
for tile in list(map(lambda x: x[1], stair)):
new_hand.remove(tile)
# initial_play from result hand
current_result.append(list(map(lambda x: x[1], stair)))
pts, final_hand, final_result = initial_play_rec(new_hand, current_result)
current_result.pop(-1)
if pts + e_points > 30 and (len(final_hand) < len(result_hand) or points(final_hand) > points(result_hand)):
result_points = pts + e_points
result_hand = final_hand
result = final_result
return result_points, result_hand, result
def series(hand):
# create index
index = list(enumerate(hand))
# sort by value, color
index.sort(key=lambda x: (x[1][0], x[1][1]))
# create list of list of series
result = []
current_serie = [index[0]]
current_color = index[0][1][1]
current_value = index[0][1][0]
for tile in index[1:]:
if tile[1][0] == current_value and tile[1][1] == current_color:
continue
elif tile[1][0] == current_value:
current_serie.append(tile)
current_color = tile[1][1]
else:
current_value = tile[1][0]
current_color = tile[1][1]
if len(current_serie) >= 3:
result.append(current_serie)
current_serie = [tile]
if len(current_serie) >= 3:
result.append(current_serie)
return result
def stairs(hand):
# create index
index = list(enumerate(hand))
# sort by color, value
index.sort(key=lambda x: (x[1][1], x[1][0]))
# create list of list of stair
result = []
current_stair = [index[0]]
current_color = index[0][1][1]
current_value = index[0][1][0]
for tile in index[1:]:
if tile[1][0] == current_value and tile[1][1] == current_color:
continue
elif tile[1][1] == current_color:
if tile[1][0] == current_value + 1:
current_stair.append(tile)
else:
if len(current_stair) >= 3:
result.append(current_stair)
current_stair = [tile]
current_value = tile[1][0]
else:
current_value = tile[1][0]
current_color = tile[1][1]
if len(current_stair) >= 3:
result.append(current_stair)
current_stair = [tile]
if len(current_stair) >= 3:
result.append(current_stair)
return result
def points(hand):
return sum([tile[0] for tile in hand])
def combinations(data, count):
return itertools.combinations(data, count)
test_hand = [(0, 0), (3, 2), (4, 1), (13, 1), (13, 3), (1, 1), (2, 2), (10, 4), (13, 2), (5, 1), (13, 4), (6, 1),
(7, 1), (8, 2), (1, 2)]
print(series(test_hand))
print(stairs(test_hand))
print(initial_play(test_hand))