purple_shoes 0.0.101

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. data/README.md +56 -0
  2. data/VERSION +1 -0
  3. data/bin/pshoes +23 -0
  4. data/bin/pshoes.bat +2 -0
  5. data/lib/plugins/video.rb +88 -0
  6. data/lib/purple_shoes.rb +51 -0
  7. data/lib/shoes/anim.rb +47 -0
  8. data/lib/shoes/app.rb +688 -0
  9. data/lib/shoes/basic.rb +254 -0
  10. data/lib/shoes/colors.rb +149 -0
  11. data/lib/shoes/download.rb +26 -0
  12. data/lib/shoes/help.rb +473 -0
  13. data/lib/shoes/helper_methods.rb +295 -0
  14. data/lib/shoes/main.rb +96 -0
  15. data/lib/shoes/manual.rb +6 -0
  16. data/lib/shoes/ruby.rb +61 -0
  17. data/lib/shoes/slot.rb +104 -0
  18. data/lib/shoes/style.rb +41 -0
  19. data/lib/shoes/text.rb +46 -0
  20. data/lib/shoes/url.rb +14 -0
  21. data/lib/shoes/widget.rb +23 -0
  22. data/samples/class-book.yaml +387 -0
  23. data/samples/cy.png +0 -0
  24. data/samples/loogink.png +0 -0
  25. data/samples/potato_chopping/1258_s001.gif +0 -0
  26. data/samples/potato_chopping/1258_s002.gif +0 -0
  27. data/samples/potato_chopping/1258_s003.gif +0 -0
  28. data/samples/potato_chopping/1258_s004.gif +0 -0
  29. data/samples/potato_chopping/1258_s005.gif +0 -0
  30. data/samples/potato_chopping/1258_s006.gif +0 -0
  31. data/samples/potato_chopping/1258_s007.gif +0 -0
  32. data/samples/potato_chopping/1258_s008.gif +0 -0
  33. data/samples/potato_chopping/1258_s009.gif +0 -0
  34. data/samples/potato_chopping/1258_s010.gif +0 -0
  35. data/samples/potato_chopping/1258_s011.gif +0 -0
  36. data/samples/potato_chopping/1258_s012.gif +0 -0
  37. data/samples/potato_chopping/1258_s013.gif +0 -0
  38. data/samples/potato_chopping/1258_s014.gif +0 -0
  39. data/samples/potato_chopping/1258_s015.gif +0 -0
  40. data/samples/potato_chopping/1258_s016.gif +0 -0
  41. data/samples/potato_chopping/1258_s017.gif +0 -0
  42. data/samples/potato_chopping/1258_s018.gif +0 -0
  43. data/samples/potato_chopping/1258_s019.gif +0 -0
  44. data/samples/potato_chopping/1258_s020.gif +0 -0
  45. data/samples/potato_chopping/1258_s021.gif +0 -0
  46. data/samples/potato_chopping/1258_s022.gif +0 -0
  47. data/samples/potato_chopping/1258_s023.gif +0 -0
  48. data/samples/potato_chopping/1258_s024.gif +0 -0
  49. data/samples/potato_chopping/1258_s025.gif +0 -0
  50. data/samples/potato_chopping/1258_s026.gif +0 -0
  51. data/samples/potato_chopping/1258_s027.gif +0 -0
  52. data/samples/potato_chopping/1258_s028.gif +0 -0
  53. data/samples/potato_chopping/1258_s029.gif +0 -0
  54. data/samples/potato_chopping/1258_s030.gif +0 -0
  55. data/samples/potato_chopping/1258_s031.gif +0 -0
  56. data/samples/potato_chopping/1258_s032.gif +0 -0
  57. data/samples/potato_chopping/1258_s033.gif +0 -0
  58. data/samples/potato_chopping/1258_s034.gif +0 -0
  59. data/samples/potato_chopping/1258_s035.gif +0 -0
  60. data/samples/potato_chopping/1258_s036.gif +0 -0
  61. data/samples/potato_chopping/1258_s037.gif +0 -0
  62. data/samples/potato_chopping/1258_s038.gif +0 -0
  63. data/samples/potato_chopping/1258_s039.gif +0 -0
  64. data/samples/potato_chopping/1258_s040.gif +0 -0
  65. data/samples/potato_chopping/1258_s041.gif +0 -0
  66. data/samples/potato_chopping/1258_s042.gif +0 -0
  67. data/samples/potato_chopping/1258_s043.gif +0 -0
  68. data/samples/potato_chopping/1258_s044.gif +0 -0
  69. data/samples/potato_chopping/1258_s045.gif +0 -0
  70. data/samples/potato_chopping/1258_s046.gif +0 -0
  71. data/samples/potato_chopping/1258_s047.gif +0 -0
  72. data/samples/potato_chopping/1258_s048.gif +0 -0
  73. data/samples/potato_chopping/1258_s049.gif +0 -0
  74. data/samples/potato_chopping/1258_s050.gif +0 -0
  75. data/samples/potato_chopping/1258_s051.gif +0 -0
  76. data/samples/potato_chopping/1258_s052.gif +0 -0
  77. data/samples/potato_chopping/1258_s053.gif +0 -0
  78. data/samples/potato_chopping/1258_s054.gif +0 -0
  79. data/samples/potato_chopping/1258_s055.gif +0 -0
  80. data/samples/potato_chopping/1258_s056.gif +0 -0
  81. data/samples/potato_chopping/1258_s057.gif +0 -0
  82. data/samples/potato_chopping/1258_s058.gif +0 -0
  83. data/samples/potato_chopping/1258_s059.gif +0 -0
  84. data/samples/sample1.rb +30 -0
  85. data/samples/sample10.rb +11 -0
  86. data/samples/sample11.rb +12 -0
  87. data/samples/sample12.rb +22 -0
  88. data/samples/sample13.rb +15 -0
  89. data/samples/sample14.rb +75 -0
  90. data/samples/sample15.rb +20 -0
  91. data/samples/sample16.rb +12 -0
  92. data/samples/sample17.rb +12 -0
  93. data/samples/sample18.rb +24 -0
  94. data/samples/sample19.rb +39 -0
  95. data/samples/sample2.rb +16 -0
  96. data/samples/sample20.rb +26 -0
  97. data/samples/sample21.rb +7 -0
  98. data/samples/sample22.rb +13 -0
  99. data/samples/sample23.rb +10 -0
  100. data/samples/sample24.rb +22 -0
  101. data/samples/sample25.rb +22 -0
  102. data/samples/sample27.rb +19 -0
  103. data/samples/sample28.rb +64 -0
  104. data/samples/sample29.rb +12 -0
  105. data/samples/sample3.rb +10 -0
  106. data/samples/sample30.rb +30 -0
  107. data/samples/sample31.rb +24 -0
  108. data/samples/sample34.rb +29 -0
  109. data/samples/sample35.rb +33 -0
  110. data/samples/sample36.rb +48 -0
  111. data/samples/sample37.rb +10 -0
  112. data/samples/sample38.rb +14 -0
  113. data/samples/sample4.rb +9 -0
  114. data/samples/sample40.rb +19 -0
  115. data/samples/sample42.rb +15 -0
  116. data/samples/sample43.rb +26 -0
  117. data/samples/sample44.rb +57 -0
  118. data/samples/sample45.rb +12 -0
  119. data/samples/sample46.rb +35 -0
  120. data/samples/sample47.rb +20 -0
  121. data/samples/sample48.rb +33 -0
  122. data/samples/sample49.rb +33 -0
  123. data/samples/sample5.rb +8 -0
  124. data/samples/sample50.rb +326 -0
  125. data/samples/sample58.rb +19 -0
  126. data/samples/sample6.rb +11 -0
  127. data/samples/sample7.rb +5 -0
  128. data/samples/sample8.rb +9 -0
  129. data/samples/sample9.rb +12 -0
  130. data/samples/sample99.rb +4 -0
  131. data/samples/sounds/102719__sarge4267__explosion.mp3 +0 -0
  132. data/samples/sounds/145622__andybrannan__train-fog-horn-long-wyomming.aiff +0 -0
  133. data/samples/sounds/46492__phreaksaccount__shields1.ogg +0 -0
  134. data/samples/sounds/61847__simon-rue__boink-v3.wav +0 -0
  135. data/snapshots/sample1.png +0 -0
  136. data/snapshots/sample10.png +0 -0
  137. data/snapshots/sample11.png +0 -0
  138. data/snapshots/sample12.png +0 -0
  139. data/snapshots/sample13.png +0 -0
  140. data/snapshots/sample14.png +0 -0
  141. data/snapshots/sample15.png +0 -0
  142. data/snapshots/sample16.png +0 -0
  143. data/snapshots/sample17.png +0 -0
  144. data/snapshots/sample18.png +0 -0
  145. data/snapshots/sample19.png +0 -0
  146. data/snapshots/sample2.png +0 -0
  147. data/snapshots/sample20.png +0 -0
  148. data/snapshots/sample21.png +0 -0
  149. data/snapshots/sample22.png +0 -0
  150. data/snapshots/sample23.png +0 -0
  151. data/snapshots/sample24.png +0 -0
  152. data/snapshots/sample25.png +0 -0
  153. data/snapshots/sample27.png +0 -0
  154. data/snapshots/sample28.png +0 -0
  155. data/snapshots/sample29.png +0 -0
  156. data/snapshots/sample3-osx.png +0 -0
  157. data/snapshots/sample3.png +0 -0
  158. data/snapshots/sample30.png +0 -0
  159. data/snapshots/sample31.png +0 -0
  160. data/snapshots/sample33.png +0 -0
  161. data/snapshots/sample34.png +0 -0
  162. data/snapshots/sample35.png +0 -0
  163. data/snapshots/sample36.png +0 -0
  164. data/snapshots/sample37.png +0 -0
  165. data/snapshots/sample38.png +0 -0
  166. data/snapshots/sample4.png +0 -0
  167. data/snapshots/sample40.png +0 -0
  168. data/snapshots/sample42.png +0 -0
  169. data/snapshots/sample43.png +0 -0
  170. data/snapshots/sample44-linux.png +0 -0
  171. data/snapshots/sample44.png +0 -0
  172. data/snapshots/sample45.png +0 -0
  173. data/snapshots/sample46.png +0 -0
  174. data/snapshots/sample47.png +0 -0
  175. data/snapshots/sample48.png +0 -0
  176. data/snapshots/sample49.png +0 -0
  177. data/snapshots/sample5.png +0 -0
  178. data/snapshots/sample50.png +0 -0
  179. data/snapshots/sample58.png +0 -0
  180. data/snapshots/sample6.png +0 -0
  181. data/snapshots/sample7.png +0 -0
  182. data/snapshots/sample8.png +0 -0
  183. data/snapshots/sample9.png +0 -0
  184. data/snapshots/sample99.png +0 -0
  185. data/static/code_highlighter.js +188 -0
  186. data/static/code_highlighter_ruby.js +26 -0
  187. data/static/man-editor-notepad.png +0 -0
  188. data/static/man-editor-osx.png +0 -0
  189. data/static/man-ele-background.png +0 -0
  190. data/static/man-ele-border.png +0 -0
  191. data/static/man-ele-button.png +0 -0
  192. data/static/man-ele-check.png +0 -0
  193. data/static/man-ele-editbox.png +0 -0
  194. data/static/man-ele-editline.png +0 -0
  195. data/static/man-ele-image.png +0 -0
  196. data/static/man-ele-listbox.png +0 -0
  197. data/static/man-ele-progress.png +0 -0
  198. data/static/man-ele-radio.png +0 -0
  199. data/static/man-ele-shape.png +0 -0
  200. data/static/man-ele-textblock.png +0 -0
  201. data/static/man-ele-video.png +0 -0
  202. data/static/man-shot1.png +0 -0
  203. data/static/manual-en.txt +3969 -0
  204. data/static/manual-ja.txt +3882 -0
  205. data/static/manual.css +184 -0
  206. data/static/purple_shoes-icon.png +0 -0
  207. data/static/rshoes-icon.png +0 -0
  208. data/static/shoes-manual-apps.png +0 -0
  209. data/static/sound_jars/jl1.0.1.jar +0 -0
  210. data/static/sound_jars/jogg-0.0.7.jar +0 -0
  211. data/static/sound_jars/jorbis-0.0.15.jar +0 -0
  212. data/static/sound_jars/mp3spi1.9.5.jar +0 -0
  213. data/static/sound_jars/tritonus_share.jar +0 -0
  214. data/static/sound_jars/vorbisspi1.0.3.jar +0 -0
  215. metadata +271 -0
@@ -0,0 +1,15 @@
1
+ require 'purple_shoes'
2
+
3
+ Shoes.app height: 200 do
4
+ stack margin_left: 10 do
5
+ title 'Progress Example'
6
+ @p = para '0%'
7
+ end
8
+
9
+ pg = progress left: 10, top: 100, width: width - 20
10
+ animate do |i|
11
+ j = i % 100 + 1
12
+ pg.fraction = j / 100.0
13
+ @p.text = "%2d%" % (pg.fraction * 100)
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ require 'purple_shoes'
2
+
3
+ Shoes.app do
4
+ stack margin_left: 10 do
5
+ title "Downloading Astronomy Picture of the Day", size: 16
6
+ status = para "One moment..."
7
+ msg = para '0%'
8
+ dl = download "http://antwrp.gsfc.nasa.gov/apod/image/1009/venusmoon_pascual_big.jpg",
9
+ save: "venusmoon_pascual_big.jpg"
10
+
11
+ pg = progress left: 10, top: 100, width: width - 20
12
+ a = animate do
13
+ if dl.started?
14
+ pg.fraction = dl.progress.to_f / dl.content_length
15
+ msg.text = "%2d%" % (pg.fraction * 100)
16
+ end
17
+ if dl.finished?
18
+ status.text = "Okay, is downloaded."
19
+ status.style stroke: orangered
20
+ image("venusmoon_pascual_big.jpg", width: 580, height: 380).move 10, 100
21
+ pg.hide
22
+ a.stop
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,57 @@
1
+ #
2
+ # Shoes Clock by Thomas Bell
3
+ # posted to the Shoes mailing list on 04 Dec 2007
4
+ # Original code for Red Shoes is: https://github.com/shoes/shoes/blob/master/samples/good-clock.rb
5
+ # The following is a snippet modified for Purple Shoes, using rotate instead of clear.
6
+ #
7
+ require 'purple_shoes'
8
+
9
+ Shoes.app height: 260, width: 250 do
10
+ def draw_background
11
+ background rgb(230, 240, 200)
12
+
13
+ fill white
14
+ stroke black
15
+ strokewidth 4
16
+ oval @centerx - 102, @centery - 102, 204, 204
17
+
18
+ fill black
19
+ nostroke
20
+ oval @centerx - 5, @centery - 5, 10, 10
21
+
22
+ stroke black
23
+ strokewidth 1
24
+ line(@centerx, @centery - 102, @centerx, @centery - 95)
25
+ line(@centerx - 102, @centery, @centerx - 95, @centery)
26
+ line(@centerx + 95, @centery, @centerx + 102, @centery)
27
+ line(@centerx, @centery + 95, @centerx, @centery + 102)
28
+ end
29
+
30
+ def clock_hand(sw, color=black)
31
+ stroke color
32
+ strokewidth sw
33
+ line @centerx, @centery, @centerx + @radius, @centery
34
+ end
35
+
36
+ @radius, @centerx, @centery = 90, 126, 140
37
+ draw_background
38
+ stack do
39
+ background black
40
+ @msg = para(fg(' '*3, tr_color("#666")), fg(' '*14, tr_color("#ccc")),
41
+ strong(fg(' '*5, white)), fg(' '*3, tr_color("#666")), margin: 4, align: 'center')
42
+ end
43
+
44
+ ch1 = clock_hand 2, red
45
+ ch2 = clock_hand 5
46
+ ch3 = clock_hand 8
47
+
48
+ animate 8 do |i|
49
+ t = Time.new
50
+ h, m, s, u = t.hour, t.min, t.sec, t.usec
51
+ @msg.text = t.strftime("%a") + t.strftime(" %b %d, %Y ") +
52
+ t.strftime("%I:%M") + t.strftime(".%S")
53
+ ch1.rotate = [(t.sec + t.usec * 0.000001) * 6 - 90, @centerx, @centery]
54
+ ch2.rotate = [(t.min + t.sec / 60.0) * 6 - 90, @centerx, @centery]
55
+ ch3.rotate = [(t.hour + t.min / 60.0) * 30 - 90, @centerx, @centery]
56
+ end
57
+ end
@@ -0,0 +1,12 @@
1
+ require 'purple_shoes'
2
+
3
+ Shoes.app height: 200 do
4
+ title 'Hello Green Shoes'
5
+ font 'Coolvetica'
6
+ title 'Hello Green Shoes'
7
+ title 'Hello Green Shoes', font: 'Lucida console'
8
+ font nil
9
+ para 'sans (default)', left: 400, top: 25
10
+ para 'Coolvetica', left: 400, top: 70
11
+ para 'Lucida Console', left: 470, top: 120
12
+ end
@@ -0,0 +1,35 @@
1
+ require 'purple_shoes'
2
+
3
+ Shoes.app height: 250, width: 198 do
4
+ def do_calc
5
+ @number = @previous.send(@op, @number) if @op
6
+ @op = nil
7
+ end
8
+ @previous, @number, @op = 0, 0, nil
9
+ background rgb(235, 235, 210)
10
+ flow margin: 5 do
11
+ flow height: 240, width: 190, margin: [2, 5, 0, 0] do
12
+ background tr_color("#996")[0, 3]..tr_color("#333")[0, 3], curve: 5
13
+ number_field = para strong(@number, ' '*20), stroke: white, margin: 8
14
+ flow width: 218 do
15
+ %w(7 8 9 / 4 5 6 * 1 2 3 - 0 Clr = +).each do |btn|
16
+ button btn, width: 46, height: 46 do
17
+ case btn
18
+ when /[0-9]/
19
+ @number = @number.to_i * 10 + btn.to_i
20
+ when 'Clr'
21
+ @previous, @number, @op = 0, 0, nil
22
+ when '='
23
+ do_calc
24
+ else
25
+ do_calc
26
+ @previous, @number = @number, nil
27
+ @op = btn
28
+ end
29
+ number_field.text = @number.to_s
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ require 'purple_shoes'
2
+
3
+ Shoes.app do
4
+ flow width: 0.3, height: 0.5 do
5
+ background lawngreen
6
+ edit_line width: 0.5, margin: 10, text: 'edit_line'
7
+ end
8
+ flow width: 0.4, height: 0.5 do
9
+ background goldenrod
10
+ edit_box width: 1.0, height: 1.0, margin: 20, text: "edit_box\n" * 3
11
+ end
12
+ flow width: 0.3, height: 0.5 do
13
+ background salmon
14
+ button 'Shoes', width: 1.0, height: 1.0, margin: 50
15
+ end
16
+ flow width: 1.0, height: 0.5 do
17
+ background plum
18
+ list_box items: %w[edit_box edit_line list_box button], width: 0.5 , margin: 20, choose: 'list_box'
19
+ end
20
+ end
@@ -0,0 +1,33 @@
1
+ # Original code was written by Cecil Coupe
2
+ # He shared that in Shoes ML: http://librelist.com/browser//shoes/2011/2/17/radio-checked-method-broken/#1197b650c7dfdae2bc7c76bd9dad7e0e
3
+
4
+ require 'purple_shoes'
5
+
6
+ Shoes.app width: 300, height: 300 do
7
+ background gold..deeppink, angle: 45
8
+ @opt = 'none'
9
+ r1 = radio do
10
+ @opt = "First"
11
+ end
12
+ para "Radio 1", width: 70
13
+ r2 = radio do
14
+ @opt = "Second"
15
+ end
16
+ para "Radio 2", width: 70
17
+ r3 = radio do
18
+ @opt = "Third"
19
+ end
20
+ para "Radio 3", width: 70
21
+
22
+ stack do
23
+ para "Default is #{@opt}"
24
+ # Set default for Radio Group :grp
25
+ r3.checked = true
26
+ button "Do My Bidding" do
27
+ @p.text += "Aye, Aye. Using #{@opt}\n"
28
+ flush
29
+ end
30
+ para "Choices follow\n"
31
+ @p = para
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'purple_shoes'
2
+
3
+ Shoes.app do
4
+ popup = proc{alert 'Testing style method...'}
5
+
6
+ s1 = stack width: 0.5, height: 1.0 do
7
+ background cornflowerblue
8
+ @star = star 120, 120, 10, 100, 50
9
+ @star.click &popup
10
+ @msg = para fg('Wait 5 seconds...', yellow), left: 10, top: 450
11
+ end
12
+
13
+ s2 = stack width: 0.5, height: 1.0 do
14
+ background coral
15
+ para 'Purple Shoes is one of colorful Shoes. It is written in JRuby with SWT.'
16
+ @para = para 'Testing, ', link('test', &popup), ', test. ',
17
+ strong('Breadsticks. '),
18
+ em('Breadsticks. '),
19
+ code('Breadsticks. '),
20
+ bg(fg(strong(ins('EVEN BETTER.')), white), rgb(255, 0, 192)),
21
+ sub('fine!')
22
+ para 'Yah! Yah! Yah!'
23
+ end
24
+
25
+ timer 5 do
26
+ @star.style width: 140, height: 140, left: 90, top: 90, fill: gold..deeppink, outer: 70, inner: 50
27
+ @para.style align: 'center', size: 24, stroke: green
28
+ s1.style width: 0.3
29
+ s2.style width: 0.7
30
+ @msg.text = 'Looks good to me!'
31
+ aflush
32
+ end
33
+ end
@@ -0,0 +1,8 @@
1
+ require 'purple_shoes'
2
+
3
+ Shoes.app width: 320, height: 32 do
4
+ 5.times do |i|
5
+ i+=1
6
+ button("sample#{i}"){load File.join(DIR, "../samples/sample#{i}.rb")}
7
+ end
8
+ end
@@ -0,0 +1,326 @@
1
+ require 'purple_shoes'
2
+ # The original is Red Shoes built-in sample: expert-othello.rb
3
+ # A little bit edited for Green Shoes.
4
+
5
+ # Othello
6
+ # by tieg
7
+ # 1/13/08
8
+ # with help: DeeJay, Ryan M. from mailing list
9
+ #
10
+ # FIXME bug if it memorizes it but it's not a valid move
11
+ #
12
+ module Othello
13
+
14
+ PIECE_WIDTH = 62
15
+ PIECE_HEIGHT = 62
16
+ TOP_OFFSET = 47
17
+ LEFT_OFFSET = 12
18
+ SPACE = ' ' * 57
19
+
20
+ class Game
21
+ BOARD_SIZE = [8,8]
22
+
23
+ attr_accessor :p1, :p2, :board, :board_history
24
+
25
+ def initialize
26
+ @board_history = []
27
+ @p1 = Player.new(:black, pieces_per_player)
28
+ @p2 = Player.new(:white, pieces_per_player)
29
+ @board = new_board
30
+ lay_initial_pieces
31
+ end
32
+
33
+ def next_turn(check_available_moves=true)
34
+ @current_player = next_player
35
+ if check_available_moves && skip_turn?
36
+ # FIXME Possible infinite loop if neither player has a good move?
37
+ next_turn
38
+ raise "Player #{@current_player.piece} (#{@current_player.color}) has no available moves. Player #{next_player.piece}'s (#{next_player.color}) turn."
39
+ end
40
+ end
41
+
42
+ def current_player
43
+ @current_player ||= @p1
44
+ end
45
+
46
+ def next_player
47
+ current_player == @p1 ? @p2 : @p1
48
+ end
49
+
50
+ # Build the array for the board, with zero-based arrays.
51
+ def new_board
52
+ Array.new(BOARD_SIZE[0]) do # build each cols L to R
53
+ Array.new(BOARD_SIZE[1]) do # insert cells in each col
54
+ 0
55
+ end
56
+ end
57
+ end
58
+
59
+ # Lay the initial 4 pieces in the middle
60
+ def lay_initial_pieces
61
+ lay_piece([4, 3], false)
62
+ next_turn(false)
63
+ lay_piece([3, 3], false)
64
+ next_turn(false)
65
+ lay_piece([3, 4], false)
66
+ next_turn(false)
67
+ lay_piece([4, 4], false)
68
+ next_turn(false)
69
+ end
70
+
71
+ def lay_piece(c=[0,0], check_adjacent_pieces=true)
72
+ memorize_board
73
+ piece = current_player.piece
74
+ opp_piece = current_player.opp_piece
75
+ raise "Spot already taken." if board_at(c) != 0
76
+ if check_adjacent_pieces
77
+ pieces_to_change = []
78
+ pieces_to_change << check_direction(c, [ 0, 1], piece, opp_piece) # N
79
+ pieces_to_change << check_direction(c, [ 1, 1], piece, opp_piece) # NE
80
+ pieces_to_change << check_direction(c, [ 1, 0], piece, opp_piece) # E
81
+ pieces_to_change << check_direction(c, [ 1,-1], piece, opp_piece) # SE
82
+ pieces_to_change << check_direction(c, [ 0,-1], piece, opp_piece) # S
83
+ pieces_to_change << check_direction(c, [-1,-1], piece, opp_piece) # SW
84
+ pieces_to_change << check_direction(c, [-1, 0], piece, opp_piece) # W
85
+ pieces_to_change << check_direction(c, [-1, 1], piece, opp_piece) # NW
86
+ raise "You must move to a spot that will turn your opponent's piece." if pieces_to_change.compact.all? { |a| a.empty? }
87
+ pieces_to_change.compact.each { |direction| direction.each { |i| @board[i[0]][i[1]] = piece } }
88
+ end
89
+ current_player.pieces -= 1
90
+ @board[c[0]][c[1]] = piece
91
+ current_winner = calculate_current_winner
92
+ raise "Game over. Player #{current_winner.piece} wins with #{current_winner.pieces_on_board} pieces!" if @p1.pieces + @p2.pieces == 0
93
+ end
94
+
95
+ def skip_turn?
96
+ possibles = []
97
+ @board.each_with_index { |col,col_index|
98
+ col.each_with_index { |cell,row_index|
99
+ return false if possible_move?([col_index,row_index])
100
+ }
101
+ }
102
+ true
103
+ end
104
+
105
+ def possible_move?(c=[0,0])
106
+ return nil if board_at(c) != 0
107
+ possible_moves = []
108
+ piece = current_player.piece
109
+ opp_piece = current_player.opp_piece
110
+ pieces_to_change = []
111
+ pieces_to_change << check_direction(c, [ 0, 1], piece, opp_piece) # N
112
+ pieces_to_change << check_direction(c, [ 1, 1], piece, opp_piece) # NE
113
+ pieces_to_change << check_direction(c, [ 1, 0], piece, opp_piece) # E
114
+ pieces_to_change << check_direction(c, [ 1,-1], piece, opp_piece) # SE
115
+ pieces_to_change << check_direction(c, [ 0,-1], piece, opp_piece) # S
116
+ pieces_to_change << check_direction(c, [-1,-1], piece, opp_piece) # SW
117
+ pieces_to_change << check_direction(c, [-1, 0], piece, opp_piece) # W
118
+ pieces_to_change << check_direction(c, [-1, 1], piece, opp_piece) # NW
119
+ return nil if pieces_to_change.compact.all? { |a| a.empty? }
120
+ true
121
+ end
122
+
123
+ def memorize_board
124
+ dup_board = new_board
125
+ dup_board = []
126
+ @board.each do |col|
127
+ dup_board << col.dup
128
+ end
129
+ @board_history << { :player => current_player, :board => dup_board }
130
+ end
131
+
132
+ def undo!
133
+ last_move = @board_history.pop
134
+ @board = last_move[:board]
135
+ @current_player = last_move[:player]
136
+ @current_player.pieces += 1
137
+ end
138
+
139
+ def calculate_current_winner
140
+ @p1.pieces_on_board, @p2.pieces_on_board = 0, 0
141
+ @board.each { |row|
142
+ row.each { |cell|
143
+ if cell == 1
144
+ @p1.pieces_on_board += 1
145
+ else
146
+ @p2.pieces_on_board += 1
147
+ end
148
+ }
149
+ }
150
+ @p1.pieces_on_board > @p2.pieces_on_board ? @p1 : @p2
151
+ end
152
+
153
+ def check_direction(c, dir, piece, opp_piece)
154
+ c_adjacent = next_in_direction(c.dup, dir)
155
+ c_last = nil
156
+ pieces_in_between = []
157
+ # Find the last piece if there is one.
158
+ while(valid_location?(c_adjacent))
159
+ if board_at(c_adjacent) == opp_piece
160
+ pieces_in_between << c_adjacent.dup
161
+ elsif board_at(c_adjacent) == piece && pieces_in_between.size > 0
162
+ c_last = c_adjacent
163
+ break
164
+ else
165
+ break
166
+ end
167
+ c_adjacent = next_in_direction(c_adjacent, dir)
168
+ end
169
+
170
+ pieces_in_between.empty? || c_last.nil? ? nil : pieces_in_between
171
+ end
172
+
173
+ # Find the value of the board at the given coordinate.
174
+ def board_at(c)
175
+ @board[c[0]][c[1]]
176
+ end
177
+
178
+ # Is this a valid location on board?
179
+ def valid_location?(c=[1,1])
180
+ c[0] >= 0 && c[1] >= 0 && c[0] < BOARD_SIZE[0] && c[1] < BOARD_SIZE[1]
181
+ end
182
+
183
+ # Perform the operations to get the next spot in the appropriate direction
184
+ def next_in_direction(c, dir)
185
+ c[0] += dir[0]
186
+ c[1] += dir[1]
187
+ c
188
+ end
189
+
190
+ private
191
+ def pieces_per_player
192
+ total_squares / 2
193
+ end
194
+
195
+ # The total number of squares
196
+ def total_squares
197
+ BOARD_SIZE[0] * BOARD_SIZE[1]
198
+ end
199
+
200
+ class Player
201
+ attr_accessor :pieces, :color, :pieces_on_board
202
+
203
+ def initialize(color=:black,pieces=0)
204
+ @pieces = pieces
205
+ @pieces_on_board = 0 # used only in calculating winner
206
+ @color = color
207
+ end
208
+
209
+ def piece
210
+ color == :black ? 1 : 2
211
+ end
212
+
213
+ def opp_piece
214
+ color == :black ? 2 : 1
215
+ end
216
+ end
217
+ end
218
+
219
+ def draw_player_1(first_turn=false)
220
+ stack width: width-5, height: 50 do
221
+ if GAME.current_player==GAME.p1
222
+ background yellow
223
+ border black, strokewidth: 10
224
+ para strong("Player 1 (#{GAME.current_player.color}) turn"), margin: [20, 15, 0, 0]
225
+ else
226
+ background white
227
+ border black, strokewidth: 10
228
+ undo = GAME.board_history.empty? ? nil : link("Undo last move"){ GAME.undo!; draw_board }
229
+ para strong("Player 1"), SPACE, undo, margin: [20, 15, 0, 0]
230
+ end
231
+ end
232
+ end
233
+
234
+ def draw_player_2(first_turn=false)
235
+ stack width: width-5, height: 50 do
236
+ if GAME.current_player==GAME.p2
237
+ background yellow
238
+ border black, strokewidth: 10
239
+ para strong("Player 2 (#{GAME.current_player.color}) turn"), margin: [20, 15, 0, 0]
240
+ else
241
+ background white
242
+ border black, strokewidth: 10
243
+ undo = GAME.board_history.empty? ? nil : link("Undo last move"){ GAME.undo!; draw_board }
244
+ para strong("Player 2"), SPACE, undo, margin: [20, 15, 0, 0]
245
+ end
246
+ end
247
+ end
248
+
249
+
250
+ def draw_board
251
+ clear do
252
+ background black
253
+ draw_player_1
254
+
255
+ stack width: width, height: 500 do
256
+ fill rgb(0, 190, 0)
257
+ #rect :left => 0, :top => 0, :width => 495, :height => 495
258
+
259
+ GAME.board.each_with_index do |col, col_index|
260
+ col.each_with_index do |cell, row_index|
261
+ left, top = left_top_corner_of_piece(col_index, row_index)
262
+ left = left - LEFT_OFFSET + 10
263
+ top = top - TOP_OFFSET + 50
264
+ fill rgb(0, 255, 0)
265
+ strokewidth 1
266
+ stroke rgb(0, 100, 0)
267
+ rect :left => left, :top => top, :width => PIECE_WIDTH, :height => PIECE_HEIGHT
268
+
269
+ if cell != 0
270
+ strokewidth 0
271
+ fill (cell == 1 ? rgb(100,100,100) : rgb(155,155,155))
272
+ oval(left+3, top+4, PIECE_WIDTH-10, PIECE_HEIGHT-10)
273
+
274
+ fill (cell == 1 ? black : white)
275
+ oval(left+5, top+5, PIECE_WIDTH-10, PIECE_HEIGHT-10)
276
+ end
277
+ end
278
+ end
279
+ end
280
+
281
+ draw_player_2
282
+ end
283
+ end
284
+
285
+ def left_top_corner_of_piece(a,b)
286
+ [(a*PIECE_WIDTH+LEFT_OFFSET), (b*PIECE_HEIGHT+TOP_OFFSET)]
287
+ end
288
+
289
+ def right_bottom_corner_of_piece(a,b)
290
+ left_top_corner_of_piece(a,b).map { |coord| coord + PIECE_WIDTH }
291
+ end
292
+
293
+ def find_piece(x,y)
294
+ GAME.board.each_with_index { |row_array, row|
295
+ row_array.each_with_index { |col_array, col|
296
+ left, top = left_top_corner_of_piece(col, row).map { |i| i - 5}
297
+ right, bottom = right_bottom_corner_of_piece(col, row).map { |i| i -5 }
298
+ return [col, row] if x >= left && x <= right && y >= top && y <= bottom
299
+ }
300
+ }
301
+ return false
302
+ end
303
+ end
304
+
305
+
306
+ Shoes.app :width => 520, :height => 600 do
307
+ extend Othello
308
+ GAME = Othello::Game.new
309
+
310
+ draw_board
311
+
312
+ click { |button, x, y|
313
+ if coords = find_piece(x,y)
314
+ begin
315
+ GAME.lay_piece(coords)
316
+ GAME.next_turn
317
+ draw_board
318
+ rescue => e
319
+ #draw_board
320
+ alert(e.message)
321
+ end
322
+ else
323
+ # alert("Not a piece.")
324
+ end
325
+ }
326
+ end