scrabble_laubedan 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/lib/board.rb +551 -0
  3. data/lib/button.rb +44 -0
  4. data/lib/database/vocabulary.db +0 -0
  5. data/lib/field.rb +31 -0
  6. data/lib/media/allStones.png +0 -0
  7. data/lib/media/boardNew.png +0 -0
  8. data/lib/media/changeStone.png +0 -0
  9. data/lib/media/checkSpellingButtonOff.png +0 -0
  10. data/lib/media/checkSpellingButtonOn.png +0 -0
  11. data/lib/media/checkSpellingButtonPressedOff.png +0 -0
  12. data/lib/media/checkSpellingButtonPressedOn.png +0 -0
  13. data/lib/media/cursor.png +0 -0
  14. data/lib/media/dot.png +0 -0
  15. data/lib/media/nextTurnButton.png +0 -0
  16. data/lib/media/nextTurnButtonPressed.png +0 -0
  17. data/lib/media/quitButton.png +0 -0
  18. data/lib/media/quitButtonPressed.png +0 -0
  19. data/lib/media/startButton.png +0 -0
  20. data/lib/media/startButtonPressed.png +0 -0
  21. data/lib/media/stone0.png +0 -0
  22. data/lib/media/stone1.png +0 -0
  23. data/lib/media/stone10.png +0 -0
  24. data/lib/media/stone2.png +0 -0
  25. data/lib/media/stone3.png +0 -0
  26. data/lib/media/stone4.png +0 -0
  27. data/lib/media/stone5.png +0 -0
  28. data/lib/media/stone6.png +0 -0
  29. data/lib/media/stone7.png +0 -0
  30. data/lib/media/stone8.png +0 -0
  31. data/lib/media/stone9.png +0 -0
  32. data/lib/media/stoneA.png +0 -0
  33. data/lib/media/stoneB.png +0 -0
  34. data/lib/media/stoneC.png +0 -0
  35. data/lib/media/stoneD.png +0 -0
  36. data/lib/media/stoneE.png +0 -0
  37. data/lib/media/stoneF.png +0 -0
  38. data/lib/media/stoneG.png +0 -0
  39. data/lib/media/stoneH.png +0 -0
  40. data/lib/media/stoneI.png +0 -0
  41. data/lib/media/stoneJ.png +0 -0
  42. data/lib/media/stoneK.png +0 -0
  43. data/lib/media/stoneL.png +0 -0
  44. data/lib/media/stoneM.png +0 -0
  45. data/lib/media/stoneN.png +0 -0
  46. data/lib/media/stoneO.png +0 -0
  47. data/lib/media/stoneP.png +0 -0
  48. data/lib/media/stoneQ.png +0 -0
  49. data/lib/media/stoneR.png +0 -0
  50. data/lib/media/stoneS.png +0 -0
  51. data/lib/media/stoneT.png +0 -0
  52. data/lib/media/stoneU.png +0 -0
  53. data/lib/media/stoneV.png +0 -0
  54. data/lib/media/stoneW.png +0 -0
  55. data/lib/media/stoneX.png +0 -0
  56. data/lib/media/stoneY.png +0 -0
  57. data/lib/media/stoneZ.png +0 -0
  58. data/lib/media/stone_.png +0 -0
  59. data/lib/player.rb +21 -0
  60. data/lib/scrabble_laubedan.rb +124 -0
  61. data/lib/stone.rb +65 -0
  62. metadata +107 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b698ef4e137cfb48640e7394201aa4fd5e060b2
4
+ data.tar.gz: e3568f0be975620730008742b22132252b06cdbd
5
+ SHA512:
6
+ metadata.gz: 75118dd93e9f9bbe29484b2bf75cabbeb759f1cfc15c63ed435c288bee69063361e181587744cec0657e5e0cc0d3ca988946c5957cc9a178e9ec194f9f302ca1
7
+ data.tar.gz: 55c65a427763dc948a23472e9d1bc391e7a705c3cc1b4a9a5c46f899f7b55e0cdd6bb8d89385b2054002a04da10f20843be88d7a90dc1ddda1c821edf8dab2b2
data/lib/board.rb ADDED
@@ -0,0 +1,551 @@
1
+ require_relative 'field'
2
+ require_relative 'player'
3
+ require_relative 'stone'
4
+ require 'set'
5
+ require 'gosu'
6
+ require 'sqlite3'
7
+
8
+ #Board main class storing, displaying and controling game
9
+ class Board
10
+ attr_accessor :fields, :player1, :player2, :player1Stack, :player2Stack
11
+ def initialize (g, field,sCh,p)
12
+ @turn = 0
13
+ @game = g #Gosu window
14
+ @activeObjects = Set.new #active objects to be drawn, updated and clicked
15
+ @path = p
16
+ initFields(field)
17
+ initPlayers(field)
18
+ initStones
19
+ addStonesToStack
20
+ freezeStones
21
+ markAvailableFields
22
+ @newWordsCoords = Set.new #set storing coordinates in arrays [x_from,y_from,x_to,y_to]
23
+ @chooseLetter = false #shoud button for choosing letter for joker be active
24
+ @checkSpelling = sCh #check spelling
25
+ @db = SQLite3::Database.new (@path + "/database/vocabulary.db") #database with vocabulary for spell check
26
+ end
27
+
28
+ def initPlayers(field)
29
+ @player1Stack = Array.new(7) #stack of player 1
30
+ @player2Stack = Array.new(7) #stack of player 2
31
+
32
+ for i in 0..6 #prepare stacks of players
33
+ @player1Stack[i] = Field.new( 25, 50 + 50*i,0,0,field)
34
+ @player2Stack[i] = Field.new( @game.width - 75 , @game.height - 50 - 50*i,0,0, field)
35
+ @activeObjects << @player1Stack[i]
36
+ @activeObjects << @player2Stack[i]
37
+ end
38
+
39
+ @player1 = Player.new(10,10,Gosu::Font.new(20),"Player1") #create player 1
40
+ @player2 = Player.new(@game.width-80,500,Gosu::Font.new(20),"Player2") #create player 1
41
+
42
+ @activeObjects << @player1
43
+ @activeObjects << @player2
44
+ end
45
+
46
+ def initFields(field) #initializes board fields - assignes bonuses, fields for stacks and field for changing stones
47
+ # puts @path + "/media/changeStone.png"
48
+ @changeField = Field.new(@game.width-60, 10,0,1,Gosu::Image.new(@path + "/media/changeStone.png", :tileable => true)) #field for changing stones
49
+ @changeField.allowed = true #sets change field to be visible
50
+ @activeObjects << @changeField
51
+
52
+
53
+ @fields = Array.new(15) { Array.new(15, nil) } #2d array containing all fields on board except stacks and change field
54
+
55
+ for i in 0..14
56
+ for j in 0..14
57
+ @fields[i][j] = Field.new(113+50*i, 27 + 53*j,0,1,field) #create fields on board
58
+ @activeObjects << @fields[i][j]
59
+ end
60
+ end
61
+
62
+ #assigning bonuses to fields
63
+ @fields[0][0].wordValue = 3
64
+ @fields[0][7].wordValue = 3
65
+ @fields[0][14].wordValue = 3
66
+ @fields[7][0].wordValue = 3
67
+ @fields[7][14].wordValue = 3
68
+ @fields[14][0].wordValue = 3
69
+ @fields[14][7].wordValue = 3
70
+ @fields[14][14].wordValue = 3
71
+
72
+ @fields[1][1].wordValue = 2
73
+ @fields[2][2].wordValue = 2
74
+ @fields[3][3].wordValue = 2
75
+ @fields[4][4].wordValue = 2
76
+ @fields[1][13].wordValue = 2
77
+ @fields[2][12].wordValue = 2
78
+ @fields[3][11].wordValue = 2
79
+ @fields[4][10].wordValue = 2
80
+ @fields[10][4].wordValue = 2
81
+ @fields[11][3].wordValue = 2
82
+ @fields[12][2].wordValue = 2
83
+ @fields[13][1].wordValue = 2
84
+ @fields[10][10].wordValue = 2
85
+ @fields[11][11].wordValue = 2
86
+ @fields[12][12].wordValue = 2
87
+ @fields[13][13].wordValue = 2
88
+
89
+ @fields[0][3].letterValue = 2
90
+ @fields[0][11].letterValue = 2
91
+ @fields[2][6].letterValue = 2
92
+ @fields[2][8].letterValue = 2
93
+ @fields[3][0].letterValue = 2
94
+ @fields[3][7].letterValue = 2
95
+ @fields[3][14].letterValue = 2
96
+ @fields[6][2].letterValue = 2
97
+ @fields[6][6].letterValue = 2
98
+ @fields[6][8].letterValue = 2
99
+ @fields[6][12].letterValue = 2
100
+ @fields[7][3].letterValue = 2
101
+ @fields[7][11].letterValue = 2
102
+ @fields[8][2].letterValue = 2
103
+ @fields[8][6].letterValue = 2
104
+ @fields[8][8].letterValue = 2
105
+ @fields[8][12].letterValue = 2
106
+ @fields[11][0].letterValue = 2
107
+ @fields[11][7].letterValue = 2
108
+ @fields[11][14].letterValue = 2
109
+ @fields[12][6].letterValue = 2
110
+ @fields[12][8].letterValue = 2
111
+ @fields[14][3].letterValue = 2
112
+ @fields[14][11].letterValue = 2
113
+
114
+ @fields[1][5].letterValue = 3
115
+ @fields[1][9].letterValue = 3
116
+ @fields[5][1].letterValue = 3
117
+ @fields[5][5].letterValue = 3
118
+ @fields[5][9].letterValue = 3
119
+ @fields[5][13].letterValue = 3
120
+ @fields[9][1].letterValue = 3
121
+ @fields[9][5].letterValue = 3
122
+ @fields[9][9].letterValue = 3
123
+ @fields[9][13].letterValue = 3
124
+ @fields[13][5].letterValue = 3
125
+ @fields[13][9].letterValue = 3
126
+ end
127
+
128
+ def initStones() #initializes all stones and table for choosing joker replacement letter
129
+ @allStonesTable = Gosu::Image.new(@path + "/media/allStones.png", :tileable => true) #image of table for choosing replacement letter for joker
130
+ @stones = Array.new # "pouch" with all stones
131
+ #initialization of stones
132
+ scr = 0
133
+ for i in 0..1
134
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stone_.png", false), @stones, scr, "_", @game, Gosu::Image.new(@game, @path + "/media/stone0.png", false) )
135
+ end
136
+ scr = 1
137
+ for i in 0..11
138
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneE.png", false), @stones, scr, "e", @game, Gosu::Image.new(@game, @path + "/media/stone1.png", false) )
139
+ end
140
+ for i in 0..8
141
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneA.png", false), @stones, scr, "a", @game, Gosu::Image.new(@game, @path + "/media/stone1.png", false) )
142
+ end
143
+ for i in 0..8
144
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneI.png", false), @stones, scr, "i", @game, Gosu::Image.new(@game, @path + "/media/stone1.png", false) )
145
+ end
146
+ for i in 0..7
147
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneO.png", false), @stones, scr, "o", @game, Gosu::Image.new(@game, @path + "/media/stone1.png", false) )
148
+ end
149
+ for i in 0..5
150
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneN.png", false), @stones, scr, "n", @game, Gosu::Image.new(@game, @path + "/media/stone1.png", false) )
151
+ end
152
+ for i in 0..5
153
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneR.png", false), @stones, scr, "r", @game, Gosu::Image.new(@game, @path + "/media/stone1.png", false) )
154
+ end
155
+ for i in 0..5
156
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneT.png", false), @stones, scr, "t", @game, Gosu::Image.new(@game, @path + "/media/stone1.png", false) )
157
+ end
158
+ for i in 0..3
159
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneL.png", false), @stones, scr, "l", @game, Gosu::Image.new(@game, @path + "/media/stone1.png", false) )
160
+ end
161
+ for i in 0..3
162
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneS.png", false), @stones, scr, "s", @game, Gosu::Image.new(@game, @path + "/media/stone1.png", false) )
163
+ end
164
+ for i in 0..3
165
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneU.png", false), @stones, scr, "u", @game, Gosu::Image.new(@game, @path + "/media/stone1.png", false) )
166
+ end
167
+ scr = 2
168
+ for i in 0..3
169
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneD.png", false), @stones, scr, "d", @game, Gosu::Image.new(@game, @path + "/media/stone2.png", false) )
170
+ end
171
+ for i in 0..2
172
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneG.png", false), @stones, scr, "g", @game, Gosu::Image.new(@game, @path + "/media/stone2.png", false) )
173
+ end
174
+ scr=3
175
+ for i in 0..1
176
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneB.png", false), @stones, scr, "b", @game, Gosu::Image.new(@game, @path + "/media/stone3.png", false) )
177
+ end
178
+ for i in 0..1
179
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneC.png", false), @stones, scr, "c", @game, Gosu::Image.new(@game, @path + "/media/stone3.png", false) )
180
+ end
181
+ for i in 0..1
182
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneM.png", false), @stones, scr, "m", @game, Gosu::Image.new(@game, @path + "/media/stone3.png", false) )
183
+ end
184
+ for i in 0..1
185
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneP.png", false), @stones, scr, "p", @game, Gosu::Image.new(@game, @path + "/media/stone3.png", false) )
186
+ end
187
+ scr=4
188
+ for i in 0..1
189
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneF.png", false), @stones, scr, "f", @game, Gosu::Image.new(@game, @path + "/media/stone4.png", false) )
190
+ end
191
+ for i in 0..1
192
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneH.png", false), @stones, scr, "h", @game, Gosu::Image.new(@game, @path + "/media/stone4.png", false) )
193
+ end
194
+ for i in 0..1
195
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneV.png", false), @stones, scr, "v", @game, Gosu::Image.new(@game, @path + "/media/stone4.png", false) )
196
+ end
197
+ for i in 0..1
198
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneW.png", false), @stones, scr, "w", @game, Gosu::Image.new(@game, @path + "/media/stone4.png", false) )
199
+ end
200
+ for i in 0..1
201
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneY.png", false), @stones, scr, "y", @game, Gosu::Image.new(@game, @path + "/media/stone4.png", false) )
202
+ end
203
+
204
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneK.png", false), @stones, 5, "k", @game, Gosu::Image.new(@game, @path + "/media/stone5.png", false) )
205
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneJ.png", false), @stones, 8, "j", @game, Gosu::Image.new(@game, @path + "/media/stone8.png", false) )
206
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneX.png", false), @stones, 8, "x", @game, Gosu::Image.new(@game, @path + "/media/stone8.png", false) )
207
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneQ.png", false), @stones, 10, "q", @game, Gosu::Image.new(@game, @path + "/media/stone10.png", false) )
208
+ @stones << Stone.new(0,0, Gosu::Image.new(@game, @path + "/media/stoneZ.png", false), @stones, 10, "z", @game, Gosu::Image.new(@game, @path + "/media/stone10.png", false) )
209
+ @stones.shuffle! #shuffle stones in "pouch"
210
+ end
211
+
212
+ def returnStonesFromTable #returns all stones which player placed in current turn
213
+ for i in 0..14
214
+ for j in 0..14
215
+ if @fields[i][j].stone != nil && !@fields[i][j].stone.static
216
+ @fields[i][j].stone.myField = nil
217
+ @fields[i][j].stone.posX = 0
218
+ @fields[i][j].stone.posY = 0
219
+ placeStone(@fields[i][j].stone)
220
+ @fields[i][j].stone = nil
221
+ end
222
+ end
223
+ end
224
+
225
+ end
226
+
227
+ def findWordInDirection(x,y,dir) #find words in direction dir (0/1) containing letter at coords [x,y]
228
+ a = dir == 1 ? 1 : 0
229
+ b = dir == 0 ? 1 : 0
230
+ border1 = dir == 1 ? x : y
231
+ border2 = dir == 1 ? x : y
232
+
233
+ for i in 1..14
234
+ if border1 < 14 && @fields[i*a+x][i*b+y].stone != nil #search in positive direction from [x,y]
235
+ border1+=1
236
+ else
237
+ break
238
+ end
239
+ end
240
+ for i in 1..14
241
+ if border2 > 0 && @fields[i*-a+x][i*-b+y].stone != nil #search in negative direction from [x,y]
242
+ border2-=1
243
+ else
244
+ break
245
+ end
246
+ end
247
+ if dir == 1 #save coords of found word in set @newWordsCoords
248
+ #since direction is never diagonal, one coord is same for both positions
249
+ #larger changed coord is always in first part, smaller in second
250
+ @newWordsCoords << [ y, border1 > border2 ? border2 : border1, y, border1 < border2 ? border2 : border1]
251
+ else
252
+ @newWordsCoords << [ border1 > border2 ? border2 : border1,x, border1 < border2 ? border2 : border1, x]
253
+ end
254
+ end
255
+
256
+ def findWords #finds newly created words on board
257
+ @newWordsCoords.clear() #clear set of words found in previous turn
258
+ for i in 0..14
259
+ for j in 0..14
260
+ if @fields[i][j].stone != nil && @fields[i][j].stone.static == false
261
+ if (j+1 < 15 && @fields[i][j+1].stone != nil) || ( j-1 >=0 && @fields[i][j-1].stone != nil)
262
+ findWordInDirection(i,j,0) #explore in direction 0
263
+ end
264
+ if (i+1 < 15 && @fields[i+1][j].stone != nil) || ( i-1 >=0 && @fields[i-1][j].stone != nil)
265
+ findWordInDirection(i,j,1) #explore in direction 1
266
+ end
267
+ end
268
+ end
269
+ end
270
+ #exception if only one stone is placed on starting field, otherwise stone has to have always a neighbour
271
+ if @fields[7][7].stone != nil && @fields[7][8].stone == nil && @fields[8][7].stone == nil && @fields[6][7].stone == nil && @fields[7][6].stone == nil
272
+ @newWordsCoords << [7,7,7,7]
273
+ end
274
+ end
275
+
276
+ def isInDb(word) #tryies to find word in database and returns true/false if it was found/not found
277
+ isItThere = false
278
+ res = @db.execute( "select * from vocabulary where word= '#{word}' COLLATE NOCASE" ) do |row|
279
+ isItThere = true
280
+ end
281
+ isItThere
282
+ end
283
+
284
+ def spellCheck #extracts words from board and checks if are present in vocabulary
285
+ @newWordsCoords.each do |i|
286
+ length = (i[2]-i[0] + i[1]-i[3]).abs #lenght of word
287
+ word = "" #word
288
+ dirX = i[2]-i[0] > 0 ? 1 : ((i[2]-i[0]) < 0 ? -1 : 0)
289
+ dirY = i[3]-i[1] > 0 ? 1 : ((i[3]-i[1]) < 0 ? -1 : 0)
290
+ for j in 0..length #contructs the word from stones
291
+ y = i[0]+j*dirX
292
+ x = i[1]+j*dirY
293
+ word << @fields[x][y].stone.letter
294
+ end
295
+ if !isInDb(word) #checks if word is in vocabulary
296
+ return false
297
+ end
298
+ end
299
+ true
300
+ end
301
+
302
+ def countPoints
303
+ totalPoints = 0
304
+ @newWordsCoords.each do |i|
305
+ wordPoints = 0
306
+ wordMultiplicator = 1 #product of multiplicators for whole word
307
+ length = (i[2]-i[0] + i[1]-i[3]).abs #word lenght
308
+ dirX = i[2]-i[0] > 0 ? 1 : ((i[2]-i[0]) < 0 ? -1 : 0)
309
+ dirY = i[3]-i[1] > 0 ? 1 : ((i[3]-i[1]) < 0 ? -1 : 0)
310
+ for j in 0..length #counts points and bonuses for word
311
+ y = i[0]+j*dirX
312
+ x = i[1]+j*dirY
313
+ wordPoints += @fields[x][y].stone.score * (@fields[x][y].stone.static ? 1 : @fields[x][y].letterValue) #points for word with letter bonues considered
314
+ wordMultiplicator *= @fields[x][y].wordValue > 0 && !@fields[x][y].stone.static ? @fields[x][y].wordValue : 1 #update of word multiplicator bonus
315
+ end
316
+ totalPoints+= wordPoints * wordMultiplicator #update of total points by points of word with word multiplicator bonuses
317
+ end
318
+
319
+ bonus = 0 #check if player used all stones and can get 50 points bonus
320
+ if @turn%2 == 1
321
+ @player1Stack.each do |i|
322
+ bonus += i.stone == nil ? 0 : 1
323
+ end
324
+ else
325
+ @player2Stack.each do |i|
326
+ bonus += i.stone == nil ? 0 : 1
327
+ end
328
+ end
329
+ totalPoints += bonus == 0 ? 50 : 0
330
+
331
+ #assigne points
332
+ if @turn%2 == 1
333
+ @player1.score += totalPoints
334
+ else
335
+ @player2.score += totalPoints
336
+ end
337
+ end
338
+
339
+ def checkTable #check if rules are kept
340
+ checkConsistency(7,7) #mark separated "islands" of stones
341
+ repairInconsistency #remove marked separated stones
342
+ markAvailableFields #mark available fields for placing stones
343
+ findWords #search for newly added words
344
+ if @checkSpelling && !spellCheck #check spelling if is required
345
+ returnStonesFromTable #if spelling is not right return stones to stack
346
+ return false
347
+ end
348
+ countPoints #count and add points for newly added words
349
+ true
350
+ end
351
+
352
+ def repairInconsistency #remove marked separated stones
353
+ for i in 0..14
354
+ for j in 0..14
355
+ if @fields[i][j].stone != nil && @fields[i][j].stone.checked == false
356
+ @fields[i][j].stone.myField = nil
357
+ @fields[i][j].stone.posX = 0
358
+ @fields[i][j].stone.posY = 0
359
+ placeStone(@fields[i][j].stone)
360
+ @fields[i][j].stone = nil
361
+ end
362
+ if @fields[i][j].stone != nil
363
+ @fields[i][j].stone.checked = false
364
+ end
365
+ end
366
+ end
367
+ end
368
+
369
+ def checkConsistency (indexX,indexY) #finds all stones properly connected to starting stone and marks them as ok
370
+ if @fields[indexX][indexY].stone != nil && @fields[indexX][indexY].stone.checked != true
371
+ @fields[indexX][indexY].stone.checked = true #sets stone checked as connected to starting stone
372
+
373
+ 0 <= indexX-1 && 14 >= indexX-1 && 0 <= indexY && 14 >= indexY && checkConsistency(indexX-1, indexY)
374
+ 0 <= indexX && 14 >= indexX && 0 <= indexY-1 && 14 >= indexY-1 && checkConsistency(indexX , indexY-1)
375
+ 0 <= indexX && 14 >= indexX && 0 <= indexY+1 && 14 >= indexY+1 && checkConsistency(indexX , indexY+1)
376
+ 0 <= indexX+1 && 14 >= indexX+1 && 0 <= indexY && 14 >= indexY && checkConsistency(indexX+1, indexY)
377
+
378
+ else
379
+ return
380
+ end
381
+ true
382
+ end
383
+
384
+ def freezeStones #marks all stones on table as static and (un)freezes players stacks depending on turn
385
+ for i in 0..14
386
+ for j in 0..14
387
+ if @fields[i][j].stone != nil
388
+ @fields[i][j].stone.static = true
389
+ end
390
+ end
391
+ end
392
+
393
+ val1 = @turn % 2 == 1 ? false : true
394
+
395
+ @player1Stack.each do |i|
396
+ if i.stone != nil
397
+ i.stone.static = val1
398
+ end
399
+ end
400
+ @player2Stack.each do |i|
401
+ if i.stone != nil
402
+ i.stone.static = !val1
403
+ end
404
+ end
405
+ end
406
+
407
+ def addStonesToStack #fills player stack with new stones if there is space for them
408
+ if @turn % 2 == 1
409
+ for i in 0..6
410
+ if @player1Stack[i].stone == nil && @stones.size > 0
411
+ @player1Stack[i].assingStone(@stones[0])
412
+ @activeObjects << @stones[0]
413
+ @stones.delete_at(0)
414
+ end
415
+ end
416
+ else
417
+ for i in 0..6
418
+ if @player2Stack[i].stone == nil && @stones.size > 0
419
+ @player2Stack[i].assingStone(@stones[0])
420
+ @activeObjects << @stones[0]
421
+ @stones.delete_at(0)
422
+ end
423
+ end
424
+ end
425
+ end
426
+
427
+ def markAvailableFields #marks fields which can be used to place stones
428
+ for i in 0..14
429
+ for j in 0..14
430
+ @fields[i][j].allowed = availableField(i,j)
431
+ end
432
+ end
433
+ end
434
+
435
+ def nextTurn #proceed to next turn if checkTable is ok
436
+ if !checkTable
437
+
438
+ else
439
+ if @stones.size > 0 #are there stones in pouch to play with?
440
+ @turn+=1 #increment turn
441
+ addStonesToStack #refill player stack
442
+ freezeStones #freeze stones
443
+ @stoneChanged = false #reset stoneChanged to default value
444
+ else
445
+ @game.gameFinished #end game
446
+ end
447
+ end
448
+ end
449
+
450
+ def availableField (indexX,indexY)
451
+ isFree = @fields[indexX][indexY].stone == nil
452
+ if !isFree || @stoneChanged #field is occupied and can not be used
453
+ return false
454
+ end
455
+
456
+ if @fields[7][7].stone == nil #is starting field empty? if so its free and no other can be used
457
+ if indexX == 7 && indexY == 7
458
+ return true
459
+ else
460
+ return false
461
+ end
462
+ end
463
+ #check in possible directions if has neighbour, if has at least one then it can be used
464
+ hasNeighbour = false
465
+ hasNeighbour = hasNeighbour || ( 0 <= indexX-1 && 14 >= indexX-1 && 0 <= indexY && 14 >= indexY && @fields[indexX-1][indexY].stone != nil )
466
+ hasNeighbour = hasNeighbour || ( 0 <= indexX && 14 >= indexX && 0 <= indexY-1 && 14 >= indexY-1 && @fields[indexX] [indexY-1].stone != nil )
467
+ hasNeighbour = hasNeighbour || ( 0 <= indexX && 14 >= indexX && 0 <= indexY+1 && 14 >= indexY+1 && @fields[indexX] [indexY+1].stone != nil )
468
+ hasNeighbour = hasNeighbour || ( 0 <= indexX+1 && 14 >= indexX+1 && 0 <= indexY && 14 >= indexY && @fields[indexX+1][indexY].stone != nil )
469
+
470
+ hasNeighbour
471
+ end
472
+
473
+ def changeStone(stone) #returns stone to pouch
474
+ @activeObjects.delete(stone) #remove stone from @activeObjects
475
+ @stoneChanged = true #player changed stone -> can not create words
476
+ returnStonesFromTable #if player already placed stones on board, then remove them
477
+ @stones << stone #put stone in pouch
478
+ @stones.shuffle! #shuffle pouch!
479
+ end
480
+
481
+ def placeStone (stone) #assigne stone to proper field based on its current position in window
482
+
483
+ #potential field coords on board
484
+ indexX = ((stone.posX-113)/50).to_i
485
+ indexY = ((stone.posY-27)/53).to_i
486
+
487
+ if stone.posX >= (@game.width-60) && stone.posX <= (@game.width-10) && stone.posY >= 10 && stone.posY <= 60 #if is stone over change stone button -> change stone
488
+ changeStone(stone)
489
+ elsif stone.posX >= 113 && stone.posX <= 863 && stone.posY >= 27 && stone.posY <= 796 && availableField(indexX,indexY) && !@stoneChanged #stone is over field on board and player can place stones, place stone
490
+ @fields[indexX][indexY].assingStone(stone)
491
+ if stone.score == 0 #if placed stone is joker, then it needs to have assigned letter it's supposed to take place of
492
+ @chooseLetter = true
493
+ @jokerStone = stone
494
+ end
495
+ else #if stone is not on any field, or over stack -> return stone to players stack
496
+ if @turn%2 == 1 #player1Stack
497
+ for i in 0..6
498
+ if @player1Stack[i].stone == nil
499
+ @player1Stack[i].assingStone(stone)
500
+ break
501
+ end
502
+ end
503
+ else #player2Stack
504
+ for i in 0..6
505
+ if @player2Stack[i].stone == nil
506
+ @player2Stack[i].assingStone(stone)
507
+ break
508
+ end
509
+ end
510
+ end
511
+ end
512
+ markAvailableFields #mark available fields after last move
513
+ end
514
+
515
+ def choosingLetter #evaulate which letter was chosen for joker stone
516
+ if @game.mouse_x >= 25 && @game.mouse_x <= 115 && @game.mouse_y >= 25 && @game.mouse_y <= 610
517
+ x = ((@game.mouse_x-25)/45).to_i
518
+ y = ((@game.mouse_y-25)/45).to_i
519
+ @chooseLetter = false
520
+ chosenLetter = 65 + x*13 + y
521
+ @jokerStone.imageDown = Gosu::Image.new(@path + "/media/stone" + chosenLetter.chr() + ".png", :tileable => true)
522
+ @jokerStone = nil
523
+ end
524
+ end
525
+
526
+ def update #send update signal to active objects
527
+ @activeObjects.each do |i|
528
+ i.update
529
+ end
530
+ end
531
+
532
+ def draw #send draw signal to active objects
533
+ @activeObjects.each do |i|
534
+ i.draw
535
+ end
536
+ if @chooseLetter
537
+ @allStonesTable.draw(25, 25, 3)
538
+ end
539
+ end
540
+
541
+ def clicked #send clicked signal to active objects
542
+ if @chooseLetter
543
+ choosingLetter
544
+ else
545
+ @activeObjects.each do |i|
546
+ i.clicked
547
+ end
548
+ end
549
+ end
550
+
551
+ end
data/lib/button.rb ADDED
@@ -0,0 +1,44 @@
1
+ class Button #Class for buttons in menu contains position, image and lambda with action
2
+ attr_accessor :posX, :posY, :buttonHeight, :buttonWidth, :image, :imageOver
3
+ def initialize (x,y,img,imgOver,act,g)
4
+ #position
5
+ @posX = x
6
+ @posY = y
7
+ @image = img #image
8
+ @actualImage = img #image to show
9
+ @imageOver = imgOver #hover image
10
+ @action = act #lambda with action
11
+ @buttonWidth = @image.width.to_f #button image width
12
+ @buttonHeight = @image.height.to_f #button image height
13
+ @game = g #gosu game window
14
+ end
15
+
16
+ def isHovering #is cursor hovering over this stone
17
+ x = @game.mouse_x
18
+ y = @game.mouse_y
19
+ if x >= @posX && x <= (@posX + @buttonWidth) && y >= @posY && y <= @posY + @buttonHeight
20
+ true
21
+ else
22
+ false
23
+ end
24
+ end
25
+
26
+ def clicked #if is cursor over button make action in lamdba
27
+ if isHovering
28
+ @action.call
29
+ end
30
+ end
31
+
32
+ def update #set image based on if is cursor hovering over it
33
+ if isHovering
34
+ @actualImage = @imageOver
35
+ else
36
+ @actualImage = @image
37
+ end
38
+ end
39
+
40
+ def draw #draw button
41
+ @actualImage.draw(@posX, @posY, 2)
42
+ end
43
+
44
+ end
Binary file
data/lib/field.rb ADDED
@@ -0,0 +1,31 @@
1
+ class Field #Field class for containing stone, bonueses for placing stone on field, and showing field
2
+ attr_accessor :posX, :posY, :wordValue, :letterValue, :stone, :allowed
3
+ def initialize(x, y, wVal,lVal,img)
4
+ @posX = x
5
+ @posY = y
6
+ @wordValue = wVal #word multiplicator bonus
7
+ @letterValue = lVal #letter multiplicator bonus
8
+ @stone = nil #stone on field
9
+ @image = img #image shown if is field available for placing stone
10
+ @allowed = false #should field be shown
11
+ end
12
+
13
+ def assingStone(stone) #assigne stone to this field
14
+ @stone = stone
15
+ @stone.posX = @posX
16
+ @stone.posY = @posY
17
+ @stone.myField = self
18
+ end
19
+
20
+ def draw
21
+ if @stone == nil && @allowed
22
+ @image.draw(@posX, @posY, 2)
23
+ end
24
+ end
25
+
26
+ def clicked #because field is placed in @activeObjects
27
+ end
28
+
29
+ def update #because field is placed in @activeObjects
30
+ end
31
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
data/lib/media/dot.png ADDED
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
data/lib/player.rb ADDED
@@ -0,0 +1,21 @@
1
+ class Player #Player class keeping and showing players score
2
+ attr_accessor :score
3
+ def initialize (x,y,sb,pn)
4
+ @score = 0
5
+ @posX = x
6
+ @posY = y
7
+ @scoreBoard = sb
8
+ @playerName = pn
9
+ end
10
+
11
+ def draw
12
+ @scoreBoard.draw("#{@playerName}", @posX, @posY, 3, 1.0, 1.0, Gosu::Color::YELLOW) #gosu text can not containg new lines so it has to be separated :/
13
+ @scoreBoard.draw("score: #{@score}", @posX, @posY+20, 3, 1.0, 1.0, Gosu::Color::YELLOW)
14
+ end
15
+
16
+ def clicked #because player is placed in @activeObjects
17
+ end
18
+
19
+ def update #because player is placed in @activeObjects
20
+ end
21
+ end
@@ -0,0 +1,124 @@
1
+ require 'gosu'
2
+ require_relative 'board'
3
+ require_relative 'button'
4
+
5
+ class Scrabble_laubedan < Gosu::Window
6
+ attr_accessor :board
7
+ def initialize
8
+ super 975, 900, false #window size
9
+ @path = File.expand_path(File.dirname(__FILE__))
10
+ @path = @path.to_s
11
+ self.caption = "Scrabble" #window label
12
+ @background_image = Gosu::Image.new(@path+"/media/boardNew.png", :tileable => true) #background image (board)
13
+ @cursor = Gosu::Image.new(self, @path+"/media/cursor.png", false) #cursor
14
+ initButtons
15
+ @gameEnded = false #is game still going on
16
+
17
+ end
18
+
19
+ def startGame
20
+ @board = Board.new(self,Gosu::Image.new(@path+"/media/dot.png", :tileable => true), @checkSpelling, @path)
21
+ @activeButtons.delete(@startButton) #remove start button
22
+ @activeButtons.delete(@checkSpellingButton) #remove check spelling button
23
+ @quitButton.posY = self.height-10-@quitButton.buttonHeight #reposition quit button
24
+ @quitButton.posX = 10
25
+ @activeButtons << @nextTurnButton #allow next turn button
26
+
27
+ end
28
+
29
+ def exitGame
30
+ self.close #close gosu window
31
+ end
32
+
33
+ def setSpellingButtonImage #spelling button controler, setting @checkSpelling and button images
34
+ @checkSpelling = !@checkSpelling
35
+ @checkSpellingButton.image = @checkSpelling ? @checkSpellingButtonOnImg : @checkSpellingButtonOffImg
36
+ @checkSpellingButton.imageOver = @checkSpelling ? @checkSpellingButtonOnImgPressed : @checkSpellingButtonOffImgPressed
37
+ end
38
+
39
+ def initButtons #initialization of menu buttons
40
+ @checkSpelling = false
41
+
42
+ @activeButtons = Array.new #array containing active buttons to be drawn, updated, clicked
43
+
44
+ @startButton = Button.new(self.width/2.0 - 75, self.height/2.0 - 100 ,
45
+ Gosu::Image.new(@path+"/media/startButton.png", :tileable => true),
46
+ Gosu::Image.new(@path+"/media/startButtonPressed.png", :tileable => true),
47
+ lambda { startGame },
48
+ self)
49
+ @quitButton = Button.new(self.width/2.0 - 75, self.height/2.0 ,
50
+ Gosu::Image.new(@path+"/media/quitButton.png", :tileable => true),
51
+ Gosu::Image.new(@path+"/media/quitButtonPressed.png", :tileable => true) ,
52
+ lambda{ exitGame },
53
+ self)
54
+ @nextTurnButton = Button.new(@quitButton.buttonWidth+20, self.height-10-@quitButton.buttonHeight ,
55
+ Gosu::Image.new(@path+"/media/nextTurnButton.png", :tileable => true),
56
+ Gosu::Image.new(@path+"/media/nextTurnButtonPressed.png", :tileable => true) ,
57
+ lambda{ @board.nextTurn },
58
+ self)
59
+
60
+ @checkSpellingButtonOnImg = Gosu::Image.new(@path+"/media/checkSpellingButtonOn.png", :tileable => true)
61
+ @checkSpellingButtonOffImg = Gosu::Image.new(@path+"/media/checkSpellingButtonOff.png", :tileable => true)
62
+ @checkSpellingButtonOffImgPressed = Gosu::Image.new(@path+"/media/checkSpellingButtonPressedOff.png", :tileable => true)
63
+ @checkSpellingButtonOnImgPressed = Gosu::Image.new(@path+"/media/checkSpellingButtonPressedOn.png", :tileable => true)
64
+ @checkSpellingButton = Button.new(self.width - 300, self.height-5-@quitButton.buttonHeight,
65
+ @checkSpellingButtonOffImg,
66
+ @checkSpellingButtonOffImgPressed,
67
+ lambda{ setSpellingButtonImage },
68
+ self)
69
+
70
+ @activeButtons << @startButton
71
+ @activeButtons << @quitButton
72
+ @activeButtons << @checkSpellingButton
73
+ end
74
+
75
+ def button_down (id) #if button down send signal to buttons and find which one was it
76
+ if id == Gosu::MsLeft then
77
+ if@board != nil
78
+ @board.clicked
79
+ end
80
+ end
81
+ @activeButtons.each do |i|
82
+ i.clicked
83
+ end
84
+ end
85
+
86
+ def gameFinished #end game and show which player won or if it was tie
87
+ @gameOver = Gosu::Font.new(150)
88
+ @activeButtons.delete(@nextTurnButton)
89
+ @gameOverText = @board.player1.score == @board.player2.score ? "Tie!" : @board.player1.score > @board.player2.score ? "Player1 win!" : "Player2 wins!"
90
+ @gameEnded = true
91
+ end
92
+
93
+ def update #call update on board and active buttons
94
+ if @board != nil
95
+ @board.update
96
+ end
97
+ @activeButtons.each do |i|
98
+ i.update
99
+ end
100
+ end
101
+
102
+ def draw #call draw on board and active buttons and other visible elements
103
+ @cursor.draw(self.mouse_x, self.mouse_y, 10, 0.1, 0.1)
104
+ @background_image.draw(0,0,0, (self.width/@background_image.width.to_f), (self.height/@background_image.height.to_f) )
105
+ if @board != nil && !@gameEnded
106
+ @board.draw
107
+ end
108
+
109
+ @activeButtons.each do |i|
110
+ i.draw
111
+ end
112
+
113
+ if @gameEnded == true
114
+ @gameOver.draw("#{@gameOverText}", self.width/2.0 - (@board.player1.score == @board.player2.score ? 75 : 350) , self.height/2.0-50, 3, 1.0, 1.0, Gosu::Color::YELLOW)
115
+ end
116
+
117
+ end
118
+ end
119
+
120
+ # window = Scrabble_laubedan.new
121
+
122
+ # window.show
123
+
124
+
data/lib/stone.rb ADDED
@@ -0,0 +1,65 @@
1
+ require_relative 'field'
2
+ class Stone #Stone class contains letter, value, possition and other variables needed
3
+ attr_accessor :letter, :score, :posX, :posY, :static, :followCursor, :myField, :checked, :imageDown
4
+ def initialize (pos_x,pos_y, img, stn, scr, ltr, gme, imgDown)
5
+ #stone position
6
+ @posX = pos_x
7
+ @posY = pos_y
8
+ @image = img #stone image
9
+ @score = scr #stone score
10
+ @letter = ltr #letter
11
+ @actualImage = img #actual image to show
12
+ @game = gme #gosu game window
13
+ @imageDown = imgDown #hover image
14
+ @buttonWidth = @image.width.to_f #stone image width
15
+ @buttonHeight = @image.height.to_f #stone image height
16
+ @static = false #can stone be moved
17
+ @followCursor = false #is stone attached to cursor
18
+ @myField = nil #fild stone is on
19
+ @checked = false #is stone properly connected to starting field/stone
20
+ end
21
+
22
+ def draw #draw stone
23
+ @actualImage.draw(@posX, @posY, 2)
24
+ end
25
+
26
+ def isHovering #is cursor hovering over this stone
27
+ x = @game.mouse_x
28
+ y = @game.mouse_y
29
+ if x >= @posX && x <= (@posX + @buttonWidth) && y >= @posY && y <= @posY + @buttonHeight
30
+ true
31
+ else
32
+ false
33
+ end
34
+ end
35
+
36
+ def update
37
+ if @followCursor #if is attached to cursor, then set it's possition to it
38
+ @posX = @game.mouse_x
39
+ @posY = @game.mouse_y
40
+ end
41
+ if isHovering #set image based on if is cursor hovering over it
42
+ @actualImage = @imageDown
43
+ else
44
+ @actualImage = @image
45
+ end
46
+ end
47
+
48
+ def clicked #evaluate click
49
+ isOver = isHovering
50
+ if @followCursor #&& isOver #if is attached to cursor then detach it and try to place it on board
51
+ @followCursor = false
52
+ @game.board.placeStone(self)
53
+ return
54
+ elsif !(@followCursor) && !(@static) && isOver #if is not static and not following and cursor is over it, then attach it to cursor and detach it from field
55
+ @followCursor = true
56
+ @game.board.markAvailableFields
57
+ if @myField != nil
58
+ @myField.stone = nil
59
+ @myField = nil
60
+ end
61
+ true
62
+ end
63
+ end
64
+
65
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scrabble_laubedan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - laubedan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'Implementation of board game scrabble with standard rules and optional
14
+ english spell check. Vocabulary for spell check contains over 100 000 words which
15
+ are stored in sqlite3 db. Requires gosu (0.10.8) and sqlite3. Usage: just require
16
+ ''scrabble_laubedan'', create new instance of scrabble = Scrabble_laubedan and then
17
+ call scrabble.show'
18
+ email: laubedan@fit.cvut.cz
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/board.rb
24
+ - lib/button.rb
25
+ - lib/database/vocabulary.db
26
+ - lib/field.rb
27
+ - lib/media/allStones.png
28
+ - lib/media/boardNew.png
29
+ - lib/media/changeStone.png
30
+ - lib/media/checkSpellingButtonOff.png
31
+ - lib/media/checkSpellingButtonOn.png
32
+ - lib/media/checkSpellingButtonPressedOff.png
33
+ - lib/media/checkSpellingButtonPressedOn.png
34
+ - lib/media/cursor.png
35
+ - lib/media/dot.png
36
+ - lib/media/nextTurnButton.png
37
+ - lib/media/nextTurnButtonPressed.png
38
+ - lib/media/quitButton.png
39
+ - lib/media/quitButtonPressed.png
40
+ - lib/media/startButton.png
41
+ - lib/media/startButtonPressed.png
42
+ - lib/media/stone0.png
43
+ - lib/media/stone1.png
44
+ - lib/media/stone10.png
45
+ - lib/media/stone2.png
46
+ - lib/media/stone3.png
47
+ - lib/media/stone4.png
48
+ - lib/media/stone5.png
49
+ - lib/media/stone6.png
50
+ - lib/media/stone7.png
51
+ - lib/media/stone8.png
52
+ - lib/media/stone9.png
53
+ - lib/media/stoneA.png
54
+ - lib/media/stoneB.png
55
+ - lib/media/stoneC.png
56
+ - lib/media/stoneD.png
57
+ - lib/media/stoneE.png
58
+ - lib/media/stoneF.png
59
+ - lib/media/stoneG.png
60
+ - lib/media/stoneH.png
61
+ - lib/media/stoneI.png
62
+ - lib/media/stoneJ.png
63
+ - lib/media/stoneK.png
64
+ - lib/media/stoneL.png
65
+ - lib/media/stoneM.png
66
+ - lib/media/stoneN.png
67
+ - lib/media/stoneO.png
68
+ - lib/media/stoneP.png
69
+ - lib/media/stoneQ.png
70
+ - lib/media/stoneR.png
71
+ - lib/media/stoneS.png
72
+ - lib/media/stoneT.png
73
+ - lib/media/stoneU.png
74
+ - lib/media/stoneV.png
75
+ - lib/media/stoneW.png
76
+ - lib/media/stoneX.png
77
+ - lib/media/stoneY.png
78
+ - lib/media/stoneZ.png
79
+ - lib/media/stone_.png
80
+ - lib/player.rb
81
+ - lib/scrabble_laubedan.rb
82
+ - lib/stone.rb
83
+ homepage: http://rubygems.org/gems/scrabble_laubedan
84
+ licenses:
85
+ - Nonstandard
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.6.10
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Scrabble with optional english spell check
107
+ test_files: []