-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
161 lines (142 loc) · 4.46 KB
/
utils.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
import random
from typing import List
from solver import Tile
ALL_TILES = ( ["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
)
SCORES ={"a": 1,
"b": 3,
"c": 3,
"d": 2,
"e": 1,
"f": 4,
"g": 2,
"h": 4,
"i": 1,
"j": 8,
"k": 5,
"l": 1,
"m": 3,
"n": 1,
"o": 1,
"p": 3,
"q": 10,
"r": 1,
"s": 1,
"t": 1,
"u": 1,
"v": 4,
"w": 4,
"x": 8,
"y": 4,
"z": 10
}
def chooseLetters(n):
tiles = random.sample(ALL_TILES, n)
return tiles
def displayTiles(tiles: List[Tile]) -> None:
"""Function to print tiles as an ascii grid based on their positions.
Edges of the board automatically resize based on max width
and height of board
Args:
tiles (List[Tile]): list of tiles as Tile objects containing information about letter and also position
"""
positions = [t.position for t in tiles]
letters = [t.letter for t in tiles]
print("-------------------")
print(letters)
print(positions)
x_min = min(list(zip(*positions))[0])
x_max = max(list(zip(*positions))[0])
y_min = min(list(zip(*positions))[1])
y_max = max(list(zip(*positions))[1])
print(f"x_min: {x_min}")
print(f"x_max: {x_max}")
print(f"y_min: {y_min}")
print(f"y_max: {y_max}")
# Print the tiles
print("".join(["_"]*(2*(x_max-(x_min-1))+3)))
for y in range(y_max, y_min-1, -1):
print("| ", end="")
for x in range(x_min, x_max+1):
if (x,y) in positions:
print(letters[positions.index((x,y))], end=" ")
else:
print(" ", end="")
print("|")
print("|" + "".join(["_"]*(2*(x_max-(x_min-1))+1)) + "|")
def displayConnectivity(tiles: List[Tile]) -> None:
"""Function to print tiles as an ascii grid, showing both their positions and connections with neighbouring tiles.
Edges of the board automatically resize based on max width and height
Args:
tiles (List[Tile]): list of tiles as Tile objects containing information about letter and also position
"""
positions = [t.position for t in tiles]
letters = [t.letter for t in tiles]
print("-------------------")
print(letters)
print(positions)
x_min = min(list(zip(*positions))[0])
x_max = max(list(zip(*positions))[0])
y_min = min(list(zip(*positions))[1])
y_max = max(list(zip(*positions))[1])
# Print the tiles
# print(" ".join([str(i) for i in range(2*(x_max-(x_min-1))+3)]))
print(" ","".join(["_"]*(2*(x_max-(x_min-1))+3)))
for y in range(y_max, y_min-1, -1):
print(f"{y} " if y<10 else y,"| ", end="")
for x in range(x_min, x_max+1):
if (x,y) in positions:
tile = tiles[positions.index((x,y))]
print(tile, end=" ")
else:
print(" ", end="")
print("|")
print(" |" + "".join(["_"]*(2*(x_max-(x_min-1))+1)) + "|")
def randomPlaceTiles(letters: List[str]) -> List[Tile]:
"""Function to generate tiles with random positions for testing.
Args:
letters (List[str]): input list of letters to place as tiles
Returns:
List[Tile]: output list of tiles with positions set
"""
tiles: List[Tile] = []
positions = []
for i in range(len(letters)):
successful = False
while not successful:
x = random.choice(range(0,15))
y = random.choice(range(0,15))
if (x,y) not in positions:
positions.append((x,y))
tiles.append(Tile(letters[i], x, y))
successful = True
# plt.scatter(list(zip(*positions))[0], list(zip(*positions))[1])
# plt.show()
# print(positions)
# print(list(zip(*positions))[0])
return tiles