-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiles.py
171 lines (158 loc) · 6.43 KB
/
tiles.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
import flet as ft
import random
tiles_placed=[]
class Tiles:
def __init__(self):
self.player_tile_size = 45
self.player_tile_text_size = 20
self.tiles = ["A", "A", "A", "A", "A", "A", "A", "A", "A",
"B", "B",
"C", "C", "C", "C",
"D", "D", "D", "D",
"E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E",
"F", "F",
"G", "G", "G",
"H", "H",
"I", "I", "I", "I", "I", "I", "I", "I", "I",
"J",
"K",
"L", "L", "L", "L",
"M", "M",
"N", "N", "N", "N", "N", "N",
"O", "O", "O", "O", "O", "O", "O", "O",
"P", "P",
"Q",
"R", "R", "R", "R", "R", "R",
"S", "S", "S", "S",
"T", "T", "T", "T", "T", "T", "T",
"U", "U", "U", "U",
"V", "V",
"W", "W",
"X",
"Y", "Y",
"Z"
" ", " "]
self.tile_bag = []
self.player_tiles = []
#last tile checked
self.last_tile_checked = None
#row for player letters direction left or right/col for player letters direction up or down
self.row_col = None
#array of tiles played
self.lettersPlayed = []
#array to hold word score modifiers
self.wordScoreModifier = []
def pick_seven_letters(self):
self.player_tiles
self.tile_bag
player_tiles = random.sample(self.tiles, 7)
tile_bag = self.tiles[:-7]
#print(player_tiles)
#print(tile_bag)
return player_tiles
def player_drag_target(self, dc):
return ft.DragTarget(
group="available",
content=ft.Container(
#content=ft.Text(letter, color="black", size=self.player_tile_text_size),
width=self.player_tile_size,
height=self.player_tile_size,
bgcolor=ft.colors.TRANSPARENT,
alignment=ft.alignment.center,
margin=.1,
border_radius=5,
border=ft.Border(
left=ft.BorderSide(1, "black"),
top=ft.BorderSide(1, "black"),
right=ft.BorderSide(1, "black"),
bottom=ft.BorderSide(1, "black"),
),
),
on_will_accept=dc.drag_will_accept,
on_accept=lambda e: dc.drag_accept(e),
on_leave=dc.drag_leave,
data=""
)
def player_tile(self, letter, points, dc):
return ft.Draggable(
group="available",
content=ft.Container(
content=ft.Text(letter, color="black", size=self.player_tile_text_size),
width=self.player_tile_size,
height=self.player_tile_size,
bgcolor=ft.colors.YELLOW,
alignment=ft.alignment.center,
margin=.1,
border_radius=5,
border=ft.Border(
left=ft.BorderSide(1, "black"),
top=ft.BorderSide(1, "black"),
right=ft.BorderSide(1, "black"),
bottom=ft.BorderSide(1, "black"),
),
),
on_drag_start=lambda e: dc.drag_start(e),
on_drag_complete=lambda e: dc.drag_complete(e),
data={"value":letter, "points":points}
)
def tiles_placed(self):
if len(tiles_placed) == 0:
return False
def check_pos_available(self, board, index):
space = board.controls[index]
print(space.controls[0].group)
if len(space.controls) > 1:
return False
else:
return True
def get_available_spaces(self, board):
available_spaces = []
for index, space in enumerate(board.controls):
if space.controls[0].group == "available":
available_spaces.append(index)
return available_spaces
def play(self, board, alert):
self.lettersPlayed = []
isValidPlacement = False
for item in board.controls:
index = item.controls[0].data
if index==112:
if len(item.controls) < 2:
return "no tile played on start position"
if len(item.controls) > 1 and item.controls[1].group == "available":
space_value = item.controls[0].content.content.value
if space_value == "TW" or space_value == "DW":
if space_value=="TW":
self.wordScoreModifier.append(3)
if space_value=="DW":
self.wordScoreModifier.append(2)
#if self.last_tile_checked is not None:
#isValidPlacement = self.check_valid_placement(board, index)
tile_data=dict(item.controls[1].data)
tile_data['index']=index
self.lettersPlayed.append(tile_data)
#print(space_value, index)
#self.last_tile_checked=index
validArray = self.check_valid_placement(board)
isValid = validArray[0]
validText = validArray[1]
print(isValid, validText)
if isValid==False:
alert.alert(validText)
else:
alert.alert(validText)
return self.lettersPlayed
def check_valid_placement(self, board):
add_tiles=[-15, 15, -1, 1]
for t in self.lettersPlayed:
invalidCount=0
if t["index"]==112:
pass
else:
for num in add_tiles:
num = t["index"] + num
if len(board.controls[num].controls)<2:
invalidCount+=1
if invalidCount==4:
return [False, "Invalid word placement"]
return [True, "Valid word placement"]