shoes 4.0.0.pre11 → 4.0.0.pre12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. checksums.yaml +4 -4
  2. data/bin/shoes-stub +8 -0
  3. data/lib/shoes/samples.rb +8 -0
  4. data/samples/README +122 -0
  5. data/samples/avatar.png +0 -0
  6. data/samples/blue-box.png +0 -0
  7. data/samples/class-book.yaml +387 -0
  8. data/samples/class_book.rb +46 -0
  9. data/samples/cy.png +0 -0
  10. data/samples/expert_definr.rb +24 -0
  11. data/samples/expert_funnies.rb +60 -0
  12. data/samples/expert_game_of_life.rb +224 -0
  13. data/samples/expert_irb.rb +109 -0
  14. data/samples/expert_minesweeper.rb +272 -0
  15. data/samples/expert_othello.rb +323 -0
  16. data/samples/expert_pong.rb +69 -0
  17. data/samples/expert_snake.rb +88 -0
  18. data/samples/expert_tankspank.rb +425 -0
  19. data/samples/expert_tetris.rb +322 -0
  20. data/samples/good_arc.rb +51 -0
  21. data/samples/good_bounce.rb +34 -0
  22. data/samples/good_clock.rb +55 -0
  23. data/samples/good_displace.rb +99 -0
  24. data/samples/good_follow.rb +27 -0
  25. data/samples/good_image_dl.rb +13 -0
  26. data/samples/good_potato_chopping.rb +23 -0
  27. data/samples/good_psychidelic_circles.rb +73 -0
  28. data/samples/good_reminder.rb +163 -0
  29. data/samples/good_vjot.rb +65 -0
  30. data/samples/lib/require_me.rb +6 -0
  31. data/samples/loogink.png +0 -0
  32. data/samples/menu-corner1.png +0 -0
  33. data/samples/menu-corner2.png +0 -0
  34. data/samples/menu-gray.png +0 -0
  35. data/samples/menu-left.png +0 -0
  36. data/samples/menu-right.png +0 -0
  37. data/samples/menu-top.png +0 -0
  38. data/samples/nks_booklist.rb +25 -0
  39. data/samples/nks_breadsticks.rb +8 -0
  40. data/samples/nks_dancing_circle.rb +14 -0
  41. data/samples/nks_dictionary.rb +24 -0
  42. data/samples/nks_edit_box.rb +5 -0
  43. data/samples/nks_edit_line.rb +7 -0
  44. data/samples/nks_notes.rb +16 -0
  45. data/samples/nks_poem.rb +23 -0
  46. data/samples/nks_self.rb +7 -0
  47. data/samples/nks_text_sizes.rb +11 -0
  48. data/samples/nks_trurl.rb +6 -0
  49. data/samples/potato_chopping/1258_s001.gif +0 -0
  50. data/samples/potato_chopping/1258_s002.gif +0 -0
  51. data/samples/potato_chopping/1258_s003.gif +0 -0
  52. data/samples/potato_chopping/1258_s004.gif +0 -0
  53. data/samples/potato_chopping/1258_s005.gif +0 -0
  54. data/samples/potato_chopping/1258_s006.gif +0 -0
  55. data/samples/potato_chopping/1258_s007.gif +0 -0
  56. data/samples/potato_chopping/1258_s008.gif +0 -0
  57. data/samples/potato_chopping/1258_s009.gif +0 -0
  58. data/samples/potato_chopping/1258_s010.gif +0 -0
  59. data/samples/potato_chopping/1258_s011.gif +0 -0
  60. data/samples/potato_chopping/1258_s012.gif +0 -0
  61. data/samples/potato_chopping/1258_s013.gif +0 -0
  62. data/samples/potato_chopping/1258_s014.gif +0 -0
  63. data/samples/potato_chopping/1258_s015.gif +0 -0
  64. data/samples/potato_chopping/1258_s016.gif +0 -0
  65. data/samples/potato_chopping/1258_s017.gif +0 -0
  66. data/samples/potato_chopping/1258_s018.gif +0 -0
  67. data/samples/potato_chopping/1258_s019.gif +0 -0
  68. data/samples/potato_chopping/1258_s020.gif +0 -0
  69. data/samples/potato_chopping/1258_s021.gif +0 -0
  70. data/samples/potato_chopping/1258_s022.gif +0 -0
  71. data/samples/potato_chopping/1258_s023.gif +0 -0
  72. data/samples/potato_chopping/1258_s024.gif +0 -0
  73. data/samples/potato_chopping/1258_s025.gif +0 -0
  74. data/samples/potato_chopping/1258_s026.gif +0 -0
  75. data/samples/potato_chopping/1258_s027.gif +0 -0
  76. data/samples/potato_chopping/1258_s028.gif +0 -0
  77. data/samples/potato_chopping/1258_s029.gif +0 -0
  78. data/samples/potato_chopping/1258_s030.gif +0 -0
  79. data/samples/potato_chopping/1258_s031.gif +0 -0
  80. data/samples/potato_chopping/1258_s032.gif +0 -0
  81. data/samples/potato_chopping/1258_s033.gif +0 -0
  82. data/samples/potato_chopping/1258_s034.gif +0 -0
  83. data/samples/potato_chopping/1258_s035.gif +0 -0
  84. data/samples/potato_chopping/1258_s036.gif +0 -0
  85. data/samples/potato_chopping/1258_s037.gif +0 -0
  86. data/samples/potato_chopping/1258_s038.gif +0 -0
  87. data/samples/potato_chopping/1258_s039.gif +0 -0
  88. data/samples/potato_chopping/1258_s040.gif +0 -0
  89. data/samples/potato_chopping/1258_s041.gif +0 -0
  90. data/samples/potato_chopping/1258_s042.gif +0 -0
  91. data/samples/potato_chopping/1258_s043.gif +0 -0
  92. data/samples/potato_chopping/1258_s044.gif +0 -0
  93. data/samples/potato_chopping/1258_s045.gif +0 -0
  94. data/samples/potato_chopping/1258_s046.gif +0 -0
  95. data/samples/potato_chopping/1258_s047.gif +0 -0
  96. data/samples/potato_chopping/1258_s048.gif +0 -0
  97. data/samples/potato_chopping/1258_s049.gif +0 -0
  98. data/samples/potato_chopping/1258_s050.gif +0 -0
  99. data/samples/potato_chopping/1258_s051.gif +0 -0
  100. data/samples/potato_chopping/1258_s052.gif +0 -0
  101. data/samples/potato_chopping/1258_s053.gif +0 -0
  102. data/samples/potato_chopping/1258_s054.gif +0 -0
  103. data/samples/potato_chopping/1258_s055.gif +0 -0
  104. data/samples/potato_chopping/1258_s056.gif +0 -0
  105. data/samples/potato_chopping/1258_s057.gif +0 -0
  106. data/samples/potato_chopping/1258_s058.gif +0 -0
  107. data/samples/potato_chopping/1258_s059.gif +0 -0
  108. data/samples/red-box.png +0 -0
  109. data/samples/shape_arc_to.rb +10 -0
  110. data/samples/simple-form.shy +0 -0
  111. data/samples/simple_accordion.rb +98 -0
  112. data/samples/simple_alert.rb +10 -0
  113. data/samples/simple_altered_para.rb +11 -0
  114. data/samples/simple_anim_shapes.rb +18 -0
  115. data/samples/simple_anim_text.rb +14 -0
  116. data/samples/simple_animate.rb +12 -0
  117. data/samples/simple_arc.rb +25 -0
  118. data/samples/simple_attach.rb +26 -0
  119. data/samples/simple_border_image.rb +9 -0
  120. data/samples/simple_borderless.rb +4 -0
  121. data/samples/simple_bounce.rb +27 -0
  122. data/samples/simple_breadsticks.rb +11 -0
  123. data/samples/simple_breadsticks2.rb +11 -0
  124. data/samples/simple_brightness_transitions.rb +16 -0
  125. data/samples/simple_button_animate.rb +17 -0
  126. data/samples/simple_buttons.rb +4 -0
  127. data/samples/simple_calc.rb +67 -0
  128. data/samples/simple_calc_2.rb +49 -0
  129. data/samples/simple_clipboard.rb +17 -0
  130. data/samples/simple_color_selector.rb +11 -0
  131. data/samples/simple_color_transitions.rb +12 -0
  132. data/samples/simple_concentric_circles.rb +9 -0
  133. data/samples/simple_console.rb +12 -0
  134. data/samples/simple_control_sizes.rb +25 -0
  135. data/samples/simple_count_and_draw.rb +19 -0
  136. data/samples/simple_curve.rb +34 -0
  137. data/samples/simple_dialogs.rb +33 -0
  138. data/samples/simple_dialogs_outside.rb +17 -0
  139. data/samples/simple_displace.rb +16 -0
  140. data/samples/simple_downloader.rb +29 -0
  141. data/samples/simple_draw.rb +16 -0
  142. data/samples/simple_editor.rb +30 -0
  143. data/samples/simple_face.rb +15 -0
  144. data/samples/simple_flashing.rb +22 -0
  145. data/samples/simple_flow_wrap.rb +12 -0
  146. data/samples/simple_font.rb +18 -0
  147. data/samples/simple_form.rb +30 -0
  148. data/samples/simple_fullscreen.rb +5 -0
  149. data/samples/simple_gradient_shapes.rb +23 -0
  150. data/samples/simple_guess_game.rb +30 -0
  151. data/samples/simple_image_as_stroke.rb +22 -0
  152. data/samples/simple_image_fill.rb +13 -0
  153. data/samples/simple_image_stroke.rb +13 -0
  154. data/samples/simple_info.rb +10 -0
  155. data/samples/simple_iterated_content.rb +8 -0
  156. data/samples/simple_keypress.rb +15 -0
  157. data/samples/simple_logo_display.rb +15 -0
  158. data/samples/simple_loogink_cy.rb +33 -0
  159. data/samples/simple_lorem_ipsum.rb +19 -0
  160. data/samples/simple_manual.rb +5 -0
  161. data/samples/simple_menu.rb +42 -0
  162. data/samples/simple_mouse_follow.rb +9 -0
  163. data/samples/simple_oval.rb +6 -0
  164. data/samples/simple_polygon_line.rb +19 -0
  165. data/samples/simple_position_as_we_go.rb +10 -0
  166. data/samples/simple_progress_bar.rb +15 -0
  167. data/samples/simple_random_bubbles.rb +15 -0
  168. data/samples/simple_require.rb +9 -0
  169. data/samples/simple_sample_executor.rb +7 -0
  170. data/samples/simple_sample_executor_all.rb +14 -0
  171. data/samples/simple_sesame_street_shoes.rb +6 -0
  172. data/samples/simple_shoes_intro.rb +22 -0
  173. data/samples/simple_slide.rb +57 -0
  174. data/samples/simple_stack_flow_buttons.rb +16 -0
  175. data/samples/simple_stripes.rb +9 -0
  176. data/samples/simple_system_background.rb +5 -0
  177. data/samples/simple_text_movement.rb +8 -0
  178. data/samples/simple_tictactoe.rb +224 -0
  179. data/samples/simple_timer.rb +16 -0
  180. data/samples/simple_translate.rb +10 -0
  181. data/samples/simple_visibility.rb +20 -0
  182. metadata +186 -7
@@ -0,0 +1,272 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Shoes Minesweeper by que/varyform
4
+ #
5
+ LEVELS = { beginner: [9, 9, 10], intermediate: [16, 16, 40], expert: [30, 16, 99] }.freeze
6
+
7
+ class Field
8
+ CELL_SIZE = 20
9
+ COLORS = %w(#00A #0A0 #A00 #004 #040 #400 #000).freeze
10
+
11
+ class Cell
12
+ attr_accessor :flag
13
+ def initialize(aflag = false)
14
+ @flag = aflag
15
+ end
16
+ end
17
+
18
+ class Bomb < Cell
19
+ attr_accessor :exploded
20
+ def initialize(exploded = false)
21
+ @exploded = exploded
22
+ end
23
+ end
24
+
25
+ class OpenCell < Cell
26
+ attr_accessor :number
27
+ def initialize(bombs_around = 0)
28
+ @number = bombs_around
29
+ end
30
+ end
31
+
32
+ class EmptyCell < Cell; end
33
+
34
+ attr_reader :cell_size, :offset
35
+
36
+ def initialize(app, level = :beginner)
37
+ @app = app
38
+ @field = []
39
+ @w = LEVELS[level][0]
40
+ @h = LEVELS[level][1]
41
+ @bombs = LEVELS[level][2]
42
+ @h.times { @field << Array.new(@w) { EmptyCell.new } }
43
+ @game_over = false
44
+ @width = @w * CELL_SIZE
45
+ @height = @h * CELL_SIZE
46
+ @cell_size = CELL_SIZE
47
+ @offset = [(@app.width - @width.to_i) / 2, (@app.height - @height.to_i) / 2]
48
+ plant_bombs
49
+ @start_time = Time.now
50
+ end
51
+
52
+ def total_time
53
+ @latest_time = Time.now - @start_time unless game_over? || all_found?
54
+ @latest_time
55
+ end
56
+
57
+ def click!(x, y)
58
+ return unless cell_exists?(x, y)
59
+ return if flagged?(x, y)
60
+ return die!(x, y) if bomb?(x, y)
61
+ open(x, y)
62
+ discover(x, y) if bombs_around(x, y).zero?
63
+ end
64
+
65
+ def flag!(x, y)
66
+ return unless cell_exists?(x, y)
67
+ self[x, y].flag = !self[x, y].flag unless self[x, y].is_a?(OpenCell)
68
+ end
69
+
70
+ def game_over?
71
+ @game_over
72
+ end
73
+
74
+ def render_cell(x, y, color = "#AAA", stroke = true)
75
+ @app.stroke "#666" if stroke
76
+ @app.fill color
77
+ @app.rect x * cell_size, y * cell_size, cell_size - 1, cell_size - 1
78
+ @app.stroke "#BBB" if stroke
79
+ @app.line x * cell_size + 1, y * cell_size + 1, x * cell_size + cell_size - 1, y * cell_size
80
+ @app.line x * cell_size + 1, y * cell_size + 1, x * cell_size, y * cell_size + cell_size - 1
81
+ end
82
+
83
+ def render_flag(x, y)
84
+ @app.stroke "#000"
85
+ @app.line(x * cell_size + cell_size / 4 + 1, y * cell_size + cell_size / 5, x * cell_size + cell_size / 4 + 1, y * cell_size + cell_size / 5 * 4)
86
+ @app.fill "#A00"
87
+ @app.rect(x * cell_size + cell_size / 4 + 2, y * cell_size + cell_size / 5,
88
+ cell_size / 3, cell_size / 4)
89
+ end
90
+
91
+ def render_bomb(x, y)
92
+ render_cell(x, y)
93
+ if game_over? || all_found? # draw bomb
94
+ render_cell(x, y, @app.rgb(0xFF, 0, 0, 0.5)) if self[x, y].exploded
95
+ @app.nostroke
96
+ @app.fill @app.rgb(0, 0, 0, 0.8)
97
+ @app.oval(x * cell_size + 3, y * cell_size + 3, 13)
98
+ @app.fill "#333"
99
+ @app.oval(x * cell_size + 5, y * cell_size + 5, 7)
100
+ @app.fill "#AAA"
101
+ @app.oval(x * cell_size + 6, y * cell_size + 6, 3)
102
+ @app.fill @app.rgb(0, 0, 0, 0.8)
103
+ @app.stroke "#222"
104
+ @app.strokewidth 2
105
+ @app.oval(x * cell_size + cell_size / 2 + 2, y * cell_size + cell_size / 4 - 2, 2)
106
+ @app.oval(x * cell_size + cell_size / 2 + 4, y * cell_size + cell_size / 4 - 2, 1)
107
+ @app.strokewidth 1
108
+ end
109
+ end
110
+
111
+ def render_number(x, y)
112
+ render_cell(x, y, "#999", false)
113
+ if self[x, y].number.nonzero?
114
+ @app.nostroke
115
+ @app.para self[x, y].number.to_s, left: x * cell_size + 3, top: y * cell_size,
116
+ font: '13px', stroke: COLORS[self[x, y].number - 1]
117
+ end
118
+ end
119
+
120
+ def paint
121
+ 0.upto @h - 1 do |y|
122
+ 0.upto @w - 1 do |x|
123
+ @app.nostroke
124
+ case self[x, y]
125
+ when EmptyCell then render_cell(x, y)
126
+ when Bomb then render_bomb(x, y)
127
+ when OpenCell then render_number(x, y)
128
+ end
129
+ render_flag(x, y) if flagged?(x, y) && !(game_over? && bomb?(x, y))
130
+ end
131
+ end
132
+ end
133
+
134
+ def bombs_left
135
+ @bombs - @field.flatten.compact.select(&:flag).size
136
+ end
137
+
138
+ def all_found?
139
+ @field.flatten.compact.select { |e| e.is_a?(OpenCell) }.size + @bombs == @w * @h
140
+ end
141
+
142
+ def reveal!(x, y)
143
+ return unless cell_exists?(x, y)
144
+ return unless self[x, y].is_a?(Field::OpenCell)
145
+ if flags_around(x, y) >= self[x, y].number
146
+ (-1..1).each do |v|
147
+ (-1..1).each { |h| click!(x + h, y + v) unless (v.zero? && h.zero?) || flagged?(x + h, y + v) }
148
+ end
149
+ end
150
+ end
151
+
152
+ private
153
+
154
+ def cell_exists?(x, y)
155
+ ((0...@w).cover? x) && ((0...@h).cover? y)
156
+ end
157
+
158
+ def flagged?(x, y)
159
+ return false unless cell_exists?(x, y)
160
+ self[x, y].flag
161
+ end
162
+
163
+ def bomb?(x, y)
164
+ cell_exists?(x, y) && (self[x, y].is_a? Bomb)
165
+ end
166
+
167
+ def can_be_discovered?(x, y)
168
+ return false unless cell_exists?(x, y)
169
+ return false if self[x, y].flag
170
+ cell_exists?(x, y) && (self[x, y].is_a? EmptyCell) && !bomb?(x, y) && bombs_around(x, y).zero?
171
+ end
172
+
173
+ def open(x, y)
174
+ self[x, y] = OpenCell.new(bombs_around(x, y)) unless (self[x, y].is_a? OpenCell) || flagged?(x, y)
175
+ end
176
+
177
+ def neighbors
178
+ (-1..1).each do |col|
179
+ (-1..1).each { |row| yield row, col unless col.zero? && row.zero? }
180
+ end
181
+ end
182
+
183
+ def discover(x, y)
184
+ open(x, y)
185
+ neighbors do |col, row|
186
+ cx = x + row
187
+ cy = y + col
188
+ next unless cell_exists?(cx, cy)
189
+ discover(cx, cy) if can_be_discovered?(cx, cy)
190
+ open(cx, cy)
191
+ end
192
+ end
193
+
194
+ def count_neighbors
195
+ return 0 unless block_given?
196
+ count = 0
197
+ neighbors { |h, v| count += 1 if yield(h, v) }
198
+ count
199
+ end
200
+
201
+ def bombs_around(x, y)
202
+ count_neighbors { |v, h| bomb?(x + h, y + v) }
203
+ end
204
+
205
+ def flags_around(x, y)
206
+ count_neighbors { |v, h| flagged?(x + h, y + v) }
207
+ end
208
+
209
+ def die!(x, y)
210
+ self[x, y].exploded = true
211
+ @game_over = true
212
+ end
213
+
214
+ def plant_bomb(x, y)
215
+ self[x, y].is_a?(EmptyCell) ? self[x, y] = Bomb.new : false
216
+ end
217
+
218
+ def plant_bombs
219
+ @bombs.times { redo unless plant_bomb(rand(@w), rand(@h)) }
220
+ end
221
+
222
+ def [](*args)
223
+ x, y = args
224
+ raise "Cell #{x}:#{y} does not exists!" unless cell_exists?(x, y)
225
+ @field[y][x]
226
+ end
227
+
228
+ def []=(*args)
229
+ x, y, v = args
230
+ cell_exists?(x, y) ? @field[y][x] = v : false
231
+ end
232
+ end
233
+
234
+ Shoes.app width: 730, height: 450, title: 'Minesweeper' do
235
+ def render_field
236
+ clear do
237
+ background rgb(50, 50, 90, 0.7)
238
+ flow margin: 4 do
239
+ button("Beginner") { new_game :beginner }
240
+ button("Intermediate") { new_game :intermediate }
241
+ button("Expert") { new_game :expert }
242
+ end
243
+ stack { @status = para stroke: white }
244
+ @field.paint
245
+ para "Left click - open cell, right click - put flag, middle click - reveal empty cells", top: 270, left: -100, stroke: white, font: "11px"
246
+ end
247
+ end
248
+
249
+ def new_game(level)
250
+ @field = Field.new self, level
251
+ translate(-@old_offset.first, -@old_offset.last) unless @old_offset.nil?
252
+ translate(@field.offset.first, @field.offset.last)
253
+ @old_offset = @field.offset
254
+ render_field
255
+ end
256
+
257
+ new_game :beginner
258
+ animate(5) { @status.replace "Time: #{@field.total_time.to_i} Bombs left: #{@field.bombs_left}" }
259
+
260
+ click do |button, x, y|
261
+ next if @field.game_over? || @field.all_found?
262
+ fx = ((x - @field.offset.first) / @field.cell_size).to_i
263
+ fy = ((y - @field.offset.last) / @field.cell_size).to_i
264
+ @field.click!(fx, fy) if button == 1
265
+ @field.reveal!(fx, fy) if button == 2
266
+ @field.flag!(fx, fy) if button == 3
267
+
268
+ render_field
269
+ alert("Winner!\nTotal time: #{@field.total_time}") if @field.all_found?
270
+ alert("Bang!\nYou loose.") if @field.game_over?
271
+ end
272
+ end
@@ -0,0 +1,323 @@
1
+ # frozen_string_literal: true
2
+ # Othello
3
+ # by tieg
4
+ # 1/13/08
5
+ # with help: DeeJay, Ryan M. from mailing list
6
+ #
7
+ # FIXME bug if it memorizes it but it's not a valid move
8
+ #
9
+ module Othello
10
+ PIECE_WIDTH = 62
11
+ PIECE_HEIGHT = 62
12
+ TOP_OFFSET = 47
13
+ LEFT_OFFSET = 12
14
+
15
+ class Game
16
+ BOARD_SIZE = [8, 8].freeze
17
+
18
+ attr_accessor :p1, :p2, :board, :board_history
19
+
20
+ def initialize
21
+ @board_history = []
22
+ @p1 = Player.new(:black, pieces_per_player)
23
+ @p2 = Player.new(:white, pieces_per_player)
24
+ @board = new_board
25
+ lay_initial_pieces
26
+ end
27
+
28
+ def next_turn(check_available_moves = true)
29
+ @current_player = next_player
30
+ if check_available_moves && skip_turn?
31
+ # FIXME: Possible infinite loop if neither player has a good move?
32
+ next_turn
33
+ raise "Player #{@current_player.piece} (#{@current_player.color}) has no available moves. Player #{next_player.piece}'s (#{next_player.color}) turn."
34
+ end
35
+ end
36
+
37
+ def current_player
38
+ @current_player ||= @p1
39
+ end
40
+
41
+ def next_player
42
+ current_player == @p1 ? @p2 : @p1
43
+ end
44
+
45
+ # Build the array for the board, with zero-based arrays.
46
+ def new_board
47
+ Array.new(BOARD_SIZE[0]) do # build each cols L to R
48
+ Array.new(BOARD_SIZE[1]) do # insert cells in each col
49
+ 0
50
+ end
51
+ end
52
+ end
53
+
54
+ # Lay the initial 4 pieces in the middle
55
+ def lay_initial_pieces
56
+ lay_piece([4, 3], false)
57
+ next_turn(false)
58
+ lay_piece([3, 3], false)
59
+ next_turn(false)
60
+ lay_piece([3, 4], false)
61
+ next_turn(false)
62
+ lay_piece([4, 4], false)
63
+ next_turn(false)
64
+
65
+ @board_history.clear
66
+ end
67
+
68
+ def lay_piece(c = [0, 0], check_adjacent_pieces = true)
69
+ memorize_board
70
+ piece = current_player.piece
71
+ opp_piece = current_player.opp_piece
72
+ raise "Spot already taken." if board_at(c).nonzero?
73
+ if check_adjacent_pieces
74
+ pieces_to_change = []
75
+ pieces_to_change << check_direction(c, [0, 1], piece, opp_piece) # N
76
+ pieces_to_change << check_direction(c, [1, 1], piece, opp_piece) # NE
77
+ pieces_to_change << check_direction(c, [1, 0], piece, opp_piece) # E
78
+ pieces_to_change << check_direction(c, [1, -1], piece, opp_piece) # SE
79
+ pieces_to_change << check_direction(c, [0, -1], piece, opp_piece) # S
80
+ pieces_to_change << check_direction(c, [-1, -1], piece, opp_piece) # SW
81
+ pieces_to_change << check_direction(c, [-1, 0], piece, opp_piece) # W
82
+ pieces_to_change << check_direction(c, [-1, 1], piece, opp_piece) # NW
83
+ raise "You must move to a spot that will turn your opponent's piece." if pieces_to_change.compact.all?(&:empty?)
84
+ pieces_to_change.compact.each { |direction| direction.each { |i| @board[i[0]][i[1]] = piece } }
85
+ end
86
+ current_player.pieces -= 1
87
+ @board[c[0]][c[1]] = piece
88
+ current_winner = calculate_current_winner
89
+ if (@p1.pieces + @p2.pieces).zero?
90
+ raise "Game over. Player #{current_winner.piece} wins with #{current_winner.pieces_on_board} pieces!"
91
+ end
92
+ end
93
+
94
+ def skip_turn?
95
+ @board.each_with_index do |col, col_index|
96
+ col.each_with_index do |_cell, row_index|
97
+ return false if possible_move?([col_index, row_index])
98
+ end
99
+ end
100
+ true
101
+ end
102
+
103
+ def possible_move?(c = [0, 0])
104
+ return nil if board_at(c).nonzero?
105
+ piece = current_player.piece
106
+ opp_piece = current_player.opp_piece
107
+ pieces_to_change = []
108
+ pieces_to_change << check_direction(c, [0, 1], piece, opp_piece) # N
109
+ pieces_to_change << check_direction(c, [1, 1], piece, opp_piece) # NE
110
+ pieces_to_change << check_direction(c, [1, 0], piece, opp_piece) # E
111
+ pieces_to_change << check_direction(c, [1, -1], piece, opp_piece) # SE
112
+ pieces_to_change << check_direction(c, [0, -1], piece, opp_piece) # S
113
+ pieces_to_change << check_direction(c, [-1, -1], piece, opp_piece) # SW
114
+ pieces_to_change << check_direction(c, [-1, 0], piece, opp_piece) # W
115
+ pieces_to_change << check_direction(c, [-1, 1], piece, opp_piece) # NW
116
+ return nil if pieces_to_change.compact.all?(&:empty?)
117
+ true
118
+ end
119
+
120
+ def memorize_board
121
+ dup_board = new_board
122
+ dup_board = []
123
+ @board.each do |col|
124
+ dup_board << col.dup
125
+ end
126
+ @board_history << { player: current_player, board: dup_board }
127
+ end
128
+
129
+ def undo!
130
+ last_move = @board_history.pop
131
+ @board = last_move[:board]
132
+ @current_player = last_move[:player]
133
+ @current_player.pieces += 1
134
+ end
135
+
136
+ def calculate_current_winner
137
+ @p1.pieces_on_board = 0
138
+ @p2.pieces_on_board = 0
139
+ @board.each do |row|
140
+ row.each do |cell|
141
+ if cell == 1
142
+ @p1.pieces_on_board += 1
143
+ else
144
+ @p2.pieces_on_board += 1
145
+ end
146
+ end
147
+ end
148
+ @p1.pieces_on_board > @p2.pieces_on_board ? @p1 : @p2
149
+ end
150
+
151
+ def check_direction(c, dir, piece, opp_piece)
152
+ c_adjacent = next_in_direction(c.dup, dir)
153
+ c_last = nil
154
+ pieces_in_between = []
155
+ # Find the last piece if there is one.
156
+ while valid_location?(c_adjacent)
157
+ if board_at(c_adjacent) == opp_piece
158
+ pieces_in_between << c_adjacent.dup
159
+ elsif board_at(c_adjacent) == piece && !pieces_in_between.empty?
160
+ c_last = c_adjacent
161
+ break
162
+ else
163
+ break
164
+ end
165
+ c_adjacent = next_in_direction(c_adjacent, dir)
166
+ end
167
+
168
+ pieces_in_between.empty? || c_last.nil? ? nil : pieces_in_between
169
+ end
170
+
171
+ # Find the value of the board at the given coordinate.
172
+ def board_at(c)
173
+ @board[c[0]][c[1]]
174
+ end
175
+
176
+ # Is this a valid location on board?
177
+ def valid_location?(c = [1, 1])
178
+ c[0] >= 0 && c[1] >= 0 && c[0] < BOARD_SIZE[0] && c[1] < BOARD_SIZE[1]
179
+ end
180
+
181
+ # Perform the operations to get the next spot in the appropriate direction
182
+ def next_in_direction(c, dir)
183
+ c[0] += dir[0]
184
+ c[1] += dir[1]
185
+ c
186
+ end
187
+
188
+ private
189
+
190
+ def pieces_per_player
191
+ total_squares / 2
192
+ end
193
+
194
+ # The total number of squares
195
+ def total_squares
196
+ BOARD_SIZE[0] * BOARD_SIZE[1]
197
+ end
198
+
199
+ class Player
200
+ attr_accessor :pieces, :color, :pieces_on_board
201
+
202
+ def initialize(color = :black, pieces = 0)
203
+ @pieces = pieces
204
+ @pieces_on_board = 0 # used only in calculating winner
205
+ @color = color
206
+ end
207
+
208
+ def piece
209
+ color == :black ? 1 : 2
210
+ end
211
+
212
+ def opp_piece
213
+ color == :black ? 2 : 1
214
+ end
215
+ end
216
+ end
217
+
218
+ def draw_player_1(_first_turn = false)
219
+ stack height: 50, margin: 10 do
220
+ if GAME.current_player == GAME.p1
221
+ background yellow
222
+ para span("Player 1 (#{GAME.current_player.color}) turn", stroke: black, font: "Trebuchet 20px bold"), margin: 4
223
+ else
224
+ background white
225
+ para span("Player 1", stroke: black, font: "Trebuchet 10px bold"), margin: 4
226
+ end
227
+ end
228
+ end
229
+
230
+ def draw_player_2(_first_turn = false)
231
+ stack top: 550, left: 0, height: 50, margin: 10 do
232
+ if GAME.current_player == GAME.p2
233
+ background yellow
234
+ para span("Player 2's (#{GAME.current_player.color}) turn", stroke: black, font: "Trebuchet 20px bold"), margin: 4
235
+ else
236
+ background white
237
+ para span("Player 2", stroke: black, font: "Trebuchet 10px bold"), margin: 4
238
+ end
239
+ end
240
+ end
241
+
242
+ def draw_board
243
+ clear do
244
+ background black
245
+ draw_player_1
246
+ stack margin: 10 do
247
+ fill rgb(0, 190, 0)
248
+ rect left: 0, top: 0, width: 495, height: 495
249
+
250
+ GAME.board.each_with_index do |col, col_index|
251
+ col.each_with_index do |cell, row_index|
252
+ left, top = left_top_corner_of_piece(col_index, row_index)
253
+ left -= LEFT_OFFSET
254
+ top -= TOP_OFFSET
255
+ fill rgb(0, 440, 0, 90)
256
+ strokewidth 1
257
+ stroke rgb(0, 100, 0)
258
+ rect left: left, top: top, width: PIECE_WIDTH, height: PIECE_HEIGHT
259
+
260
+ if cell.nonzero?
261
+ strokewidth 0
262
+ fill(cell == 1 ? rgb(100, 100, 100) : rgb(155, 155, 155))
263
+ oval(left + 3, top + 4, PIECE_WIDTH - 10, PIECE_HEIGHT - 10)
264
+
265
+ fill(cell == 1 ? black : white)
266
+ oval(left + 5, top + 5, PIECE_WIDTH - 10, PIECE_HEIGHT - 10)
267
+ end
268
+ end
269
+ end
270
+ end
271
+ draw_player_2
272
+
273
+ unless GAME.board_history.empty?
274
+ button("Undo last move", top: 10, right: 10) do
275
+ GAME.undo!
276
+ draw_board
277
+ end
278
+ end
279
+ end
280
+ end
281
+
282
+ def left_top_corner_of_piece(a, b)
283
+ [(a * PIECE_WIDTH + LEFT_OFFSET), (b * PIECE_HEIGHT + TOP_OFFSET)]
284
+ end
285
+
286
+ def right_bottom_corner_of_piece(a, b)
287
+ left_top_corner_of_piece(a, b).map { |coord| coord + PIECE_WIDTH }
288
+ end
289
+
290
+ def find_piece(x, y)
291
+ GAME.board.each_with_index do |row_array, row|
292
+ row_array.each_with_index do |_col_array, col|
293
+ left, top = left_top_corner_of_piece(col, row).map { |i| i - 5 }
294
+ right, bottom = right_bottom_corner_of_piece(col, row).map { |i| i - 5 }
295
+ return [col, row] if x >= left && x <= right && y >= top && y <= bottom
296
+ end
297
+ end
298
+ false
299
+ end
300
+ end
301
+
302
+ GAME = Othello::Game.new
303
+
304
+ Shoes.app width: 520, height: 600 do
305
+ extend Othello
306
+
307
+ draw_board
308
+
309
+ click do |_button, x, y|
310
+ coords = find_piece(x, y)
311
+ if coords
312
+ begin
313
+ GAME.lay_piece(coords)
314
+ GAME.next_turn
315
+ draw_board
316
+ rescue => e
317
+ alert(e.message)
318
+ end
319
+ else
320
+ alert("Not a piece.")
321
+ end
322
+ end
323
+ end