This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrabble.py
336 lines (285 loc) · 12.1 KB
/
scrabble.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import os
import copy
import math
import random
import arcade
import arcade.gui
from arcade.gui import UIManager
from letters import *
from wordchecker import *
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 900
BOARD_WIDTH = 900
BOARD_HEIGHT = 900
SLOT_WIDTH = 60
SLOT_HEIGHT = 60
SLOT_COUNT_X = 15
SLOT_COUNT_Y = 15
# SCREEN_HEIGHT = 720
SCREEN_TITLE = "Snakes and Scrabbles"
# Dictionary for storing all letters and their respective point values
# (https://www.thewordfinder.com/scrabble-point-values.php)
LETTER_DECK = ["a" for i in range(9)] + \
["b" for i in range(2)] + \
["c" for i in range(2)] + \
["d" for i in range(4)] + \
["e" for i in range(12)] + \
["f" for i in range(2)] + \
["g" for i in range(3)] + \
["h" for i in range(2)] + \
["i" for i in range(9)] + \
["j" for i in range(1)] + \
["k" for i in range(1)] + \
["l" for i in range(4)] + \
["m" for i in range(2)] + \
["n" for i in range(6)] + \
["o" for i in range(8)] + \
["p" for i in range(2)] + \
["q" for i in range(1)] + \
["r" for i in range(6)] + \
["s" for i in range(4)] + \
["t" for i in range(6)] + \
["u" for i in range(4)] + \
["v" for i in range(2)] + \
["w" for i in range(2)] + \
["x" for i in range(1)] + \
["y" for i in range(2)] + \
["z" for i in range(1)]
STARTING_HAND = ["a" for i in range(2)] + \
["c"] +\
["e"] +\
["k"] +\
["n"] + \
["s"]
PLAYER_SLOTS = [[1100, 300 + SLOT_HEIGHT * i] for i in range(7)]
class SubmitButton(arcade.gui.UIFlatButton):
def __init__(self, game, *args, **kwargs):
self.game = game
super(SubmitButton, self).__init__(*args, **kwargs)
def on_click(self):
self.game.check_board()
class HelpButton(arcade.gui.UIFlatButton):
def __init__(self, scrab_view, *args, **kwargs):
super().__init__(*args, **kwargs)
self.scrabView = scrab_view
def on_click(self):
instruction_view = HelpView(self.scrabView)
window.show_view(instruction_view)
class GameViewButton(arcade.gui.UIFlatButton):
def on_click(self):
game_view = ScrabbleGame()
game_view.setup()
window.show_view(game_view)
class MainView(arcade.View):
def __init__(self):
super().__init__()
def on_draw(self):
arcade.start_render()
arcade.draw_text("Scrabble", 500, 500, arcade.color.BLACK, font_size=80)
def on_show_view(self):
self.setup()
arcade.set_background_color(arcade.color.WHITE)
def setup(self):
ui_manager.purge_ui_elements()
arcade.start_render()
button = GameViewButton('Play', center_x=650, center_y=400, width=250)
ui_manager.add_ui_element(button)
class HelpView(arcade.View):
def __init__(self, scrab_view):
super().__init__()
self.scrabView = scrab_view
self.help = arcade.load_texture(os.path.join("images", "help.png"))
def on_draw(self):
arcade.start_render()
arcade.draw_text("Point Values", 650, 750,
arcade.color.BLACK, font_size=50, anchor_x="center")
arcade.draw_text("Made by Shreya, Rauno and Ricky.", 650, 700,
arcade.color.BLACK, font_size=20, anchor_x="center")
arcade.draw_text("Press esc to return to game", 650, 650,
arcade.color.BLACK, font_size=20, anchor_x="center")
arcade.draw_lrwh_rectangle_textured(500, 25,
291, 601,
self.help)
def on_show_view(self):
ui_manager.purge_ui_elements()
arcade.set_background_color(arcade.color.WHITE)
def on_key_press(self, key, _modifiers):
if key == arcade.key.ESCAPE:
arcade.set_background_color(arcade.color.GREEN)
self.scrabView.setup_ui()
self.window.show_view(self.scrabView)
class ScrabbleGame(arcade.View):
"""
Attribute tile: A SpriteList that stores all the letters a player starts their game round with
"""
def __init__(self):
super().__init__()
arcade.set_background_color(arcade.color.GREEN)
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)
self.background = None
# Selectable and movable letter blocks
self.active_blocks = arcade.SpriteList()
# Homeward bound and temporarily unselectable letter blocks
self.moving_blocks = arcade.SpriteList()
# Temporarily placed and unselectable blocks
self.pending_blocks = arcade.SpriteList()
# Unselectable and unmovable letter blocks
self.inactive_blocks = arcade.SpriteList()
self.heldLetter = None
# Transposed board matrix with rows as tuples: [*zip(*self.board)]
# Transposed board matrix with rows as lists: list(map(list, [*zip(*self.board)]))
self.board = [[None for j in range(SLOT_COUNT_X)] for i in range(SLOT_COUNT_Y)]
self.board_temp = copy.deepcopy(self.board)
# Wordchecker object attribute for word checking functionality
self.word_checker = WordChecker()
self.score = 0
def setup_ui(self):
ui_manager.purge_ui_elements()
self.background = arcade.load_texture(os.path.join("images", "table.png"))
submit_button = SubmitButton(self, 'Submit move', center_x=1100, center_y=150, width=250)
info_button = HelpButton(self, 'i', center_x=1250, center_y=875, width=50)
ui_manager.add_ui_element(submit_button)
ui_manager.add_ui_element(info_button)
# Call to restart game
def setup(self):
self.setup_ui()
for i in range(len(PLAYER_SLOTS)):
self.create_letter(STARTING_HAND[i],
PLAYER_SLOTS[i][0], PLAYER_SLOTS[i][1],
i)
self.heldLetter = None
# Called to calculate new frame
def on_update(self, delta_time):
self.active_blocks.on_update(delta_time)
self.inactive_blocks.on_update(delta_time)
self.moving_blocks.on_update(delta_time)
for block in self.moving_blocks:
if not block.movement:
self.moving_blocks.remove(block)
if block.is_home():
self.active_blocks.append(block)
else:
self.pending_blocks.append(block)
# Called on frame draw
def on_draw(self):
arcade.start_render()
arcade.draw_lrwh_rectangle_textured(0, 0,
BOARD_WIDTH, BOARD_HEIGHT,
self.background)
self.active_blocks.draw()
self.inactive_blocks.draw()
self.pending_blocks.draw()
self.moving_blocks.draw()
score_text = "Score: " + str(self.score)
arcade.draw_text(score_text, 1100, 100,
arcade.color.BLACK, 20, width=500, align="center",
anchor_x="center", anchor_y="center")
# Checks board when the RETURN (ENTER) key is pressed
def on_key_press(self, symbol: int, modifiers: int):
if symbol == arcade.key.RETURN:
if len(self.pending_blocks) > 0:
self.check_board()
def on_mouse_press(self, x, y, button, key_modifiers):
""" Called when the user presses a mouse button. """
self.heldLetter = (arcade.get_sprites_at_point((x, y), self.active_blocks) or [None])[0]
def on_mouse_release(self, x: float, y: float, button: int, modifiers: int):
""" Called when the user presses a mouse button. """
if self.heldLetter is not None:
self.active_blocks.remove(self.heldLetter)
self.moving_blocks.append(self.heldLetter)
if len(arcade.get_sprites_at_point((x, y), self.inactive_blocks)) == 0 and x < BOARD_WIDTH:
letter_x, letter_y = self.nearest_cell(x, y)
self.heldLetter.place(letter_x, letter_y)
self.board_temp[int((letter_x-SLOT_WIDTH/2)/SLOT_WIDTH)][int((letter_y - SLOT_HEIGHT/2)/SLOT_HEIGHT)] = self.heldLetter
else:
self.heldLetter.return_home()
self.heldLetter = None
def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
""" User moves mouse """
if self.heldLetter is not None:
self.heldLetter.center_x += dx
self.heldLetter.center_y += dy
def nearest_cell(self, x, y):
return [SLOT_WIDTH * math.floor(x/SLOT_WIDTH) + SLOT_WIDTH / 2,
SLOT_HEIGHT * math.floor(y/SLOT_HEIGHT) + SLOT_HEIGHT / 2]
def get_strings(self):
strings = []
for row in self.board_temp:
string_builder = ""
for letter in row:
if letter is not None:
string_builder += letter.letter_string
elif len(string_builder) > 0:
# Only remember strings longer than one letter
if len(string_builder) > 1:
strings.append(string_builder)
string_builder = ""
for col in [*zip(*self.board_temp)]:
string_builder = ""
for letter in col:
if letter is not None:
string_builder += letter.letter_string
elif len(string_builder) > 0:
if len(string_builder) > 1:
strings.append(string_builder)
string_builder = ""
return strings
def check_board(self):
# Check if placement is valid
strings = self.get_strings()
for s in strings:
# If not, revert last move
if not (self.word_checker.check(s) or self.word_checker.check(s[::-1])):
for block in self.pending_blocks:
block.return_home()
self.moving_blocks.append(block)
self.board_temp = copy.deepcopy(self.board)
self.pending_blocks = arcade.SpriteList()
return
# If all checks passed, place the letter blocks
empty_slots = []
for block in self.pending_blocks:
self.inactive_blocks.append(block)
empty_slots.append(block.home_slot_index)
self.update_score()
self.populate(empty_slots)
self.pending_blocks = arcade.SpriteList()
def populate(self, slots):
new = self.new_letters(len(slots))
for i in range(len(slots)):
self.create_letter(new[i],
PLAYER_SLOTS[slots[i]][0], PLAYER_SLOTS[slots[i]][1],
slots[i])
def create_letter(self, character, x, y, home_slot_index):
new_letter = Letter(character)
new_letter.center_x = x
new_letter.center_y = y
new_letter.home_slot_index = home_slot_index
self.active_blocks.append(new_letter)
return new_letter
def update_score(self):
for i in range(SLOT_COUNT_X):
for j in range(SLOT_COUNT_Y):
if self.board_temp[i][j] != self.board[i][j]:
self.score += self.letter_score(self.board_temp[i][j])
self.board = []
for i in range(SLOT_COUNT_X):
self.board.append(copy.copy(self.board_temp[i]))
def letter_score(self, letter_to_score):
return LETTERS_DICTIONARY[letter_to_score.letter_string]
def new_letters(self, tiles):
# tiles is the number of new letters to generate(initially 7)
letters = []
for i in range(tiles):
choice = random.choice(LETTER_DECK)
letters.append(choice)
LETTER_DECK.remove(choice)
return letters
if __name__ == "__main__":
window = arcade.Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
ui_manager = UIManager()
mainView = MainView()
mainView.setup()
window.show_view(mainView)
arcade.run()