glimmer-dsl-swt 4.18.4.4 → 4.18.4.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -0
  3. data/LICENSE.txt +20 -20
  4. data/README.md +43 -5058
  5. data/RUBY_VERSION +1 -1
  6. data/VERSION +1 -1
  7. data/bin/girb +31 -31
  8. data/bin/girb_runner.rb +34 -34
  9. data/bin/glimmer +26 -26
  10. data/docs/reference/GLIMMER_COMMAND.md +591 -0
  11. data/docs/reference/GLIMMER_CONFIGURATION.md +183 -0
  12. data/docs/reference/GLIMMER_GIRB.md +30 -0
  13. data/docs/reference/GLIMMER_GUI_DSL_SYNTAX.md +3367 -0
  14. data/docs/reference/GLIMMER_PACKAGING_AND_DISTRIBUTION.md +202 -0
  15. data/docs/reference/GLIMMER_SAMPLES.md +755 -0
  16. data/docs/reference/GLIMMER_STYLE_GUIDE.md +14 -0
  17. data/glimmer-dsl-swt.gemspec +22 -7
  18. data/lib/ext/glimmer.rb +41 -41
  19. data/lib/ext/glimmer/config.rb +167 -167
  20. data/lib/ext/rouge/themes/glimmer.rb +29 -29
  21. data/lib/glimmer-dsl-swt.rb +44 -44
  22. data/lib/glimmer/Rakefile +26 -26
  23. data/lib/glimmer/data_binding/list_selection_binding.rb +72 -72
  24. data/lib/glimmer/data_binding/observable_widget.rb +38 -38
  25. data/lib/glimmer/data_binding/shine.rb +44 -44
  26. data/lib/glimmer/data_binding/table_items_binding.rb +89 -89
  27. data/lib/glimmer/data_binding/tree_items_binding.rb +108 -108
  28. data/lib/glimmer/data_binding/widget_binding.rb +73 -73
  29. data/lib/glimmer/dsl/swt/animation_expression.rb +43 -43
  30. data/lib/glimmer/dsl/swt/async_exec_expression.rb +35 -35
  31. data/lib/glimmer/dsl/swt/bind_expression.rb +58 -58
  32. data/lib/glimmer/dsl/swt/block_property_expression.rb +41 -41
  33. data/lib/glimmer/dsl/swt/checkbox_group_selection_data_binding_expression.rb +61 -61
  34. data/lib/glimmer/dsl/swt/color_expression.rb +40 -40
  35. data/lib/glimmer/dsl/swt/column_properties_expression.rb +45 -45
  36. data/lib/glimmer/dsl/swt/combo_selection_data_binding_expression.rb +67 -67
  37. data/lib/glimmer/dsl/swt/cursor_expression.rb +44 -44
  38. data/lib/glimmer/dsl/swt/custom_widget_expression.rb +63 -63
  39. data/lib/glimmer/dsl/swt/data_binding_expression.rb +55 -55
  40. data/lib/glimmer/dsl/swt/dialog_expression.rb +48 -48
  41. data/lib/glimmer/dsl/swt/directory_dialog_expression.rb +48 -48
  42. data/lib/glimmer/dsl/swt/display_expression.rb +40 -40
  43. data/lib/glimmer/dsl/swt/dnd_expression.rb +46 -46
  44. data/lib/glimmer/dsl/swt/dsl.rb +63 -63
  45. data/lib/glimmer/dsl/swt/exec_expression.rb +55 -55
  46. data/lib/glimmer/dsl/swt/expand_item_expression.rb +60 -60
  47. data/lib/glimmer/dsl/swt/file_dialog_expression.rb +48 -48
  48. data/lib/glimmer/dsl/swt/font_expression.rb +49 -49
  49. data/lib/glimmer/dsl/swt/image_expression.rb +54 -50
  50. data/lib/glimmer/dsl/swt/layout_data_expression.rb +46 -46
  51. data/lib/glimmer/dsl/swt/layout_expression.rb +50 -50
  52. data/lib/glimmer/dsl/swt/list_selection_data_binding_expression.rb +65 -65
  53. data/lib/glimmer/dsl/swt/menu_bar_expression.rb +54 -54
  54. data/lib/glimmer/dsl/swt/menu_expression.rb +53 -53
  55. data/lib/glimmer/dsl/swt/message_box_expression.rb +54 -54
  56. data/lib/glimmer/dsl/swt/multiply_expression.rb +53 -53
  57. data/lib/glimmer/dsl/swt/observe_expression.rb +56 -53
  58. data/lib/glimmer/dsl/swt/pixel_expression.rb +38 -0
  59. data/lib/glimmer/dsl/swt/property_expression.rb +46 -46
  60. data/lib/glimmer/dsl/swt/radio_group_selection_data_binding_expression.rb +62 -61
  61. data/lib/glimmer/dsl/swt/rgb_expression.rb +33 -33
  62. data/lib/glimmer/dsl/swt/rgba_expression.rb +33 -33
  63. data/lib/glimmer/dsl/swt/shape_expression.rb +54 -54
  64. data/lib/glimmer/dsl/swt/shell_expression.rb +46 -46
  65. data/lib/glimmer/dsl/swt/swt_expression.rb +46 -46
  66. data/lib/glimmer/dsl/swt/sync_exec_expression.rb +36 -36
  67. data/lib/glimmer/dsl/swt/tab_item_expression.rb +54 -54
  68. data/lib/glimmer/dsl/swt/table_items_data_binding_expression.rb +52 -52
  69. data/lib/glimmer/dsl/swt/transform_expression.rb +55 -55
  70. data/lib/glimmer/dsl/swt/tree_items_data_binding_expression.rb +52 -52
  71. data/lib/glimmer/dsl/swt/tree_properties_expression.rb +47 -47
  72. data/lib/glimmer/dsl/swt/widget_expression.rb +67 -66
  73. data/lib/glimmer/dsl/swt/widget_listener_expression.rb +53 -53
  74. data/lib/glimmer/rake_task.rb +220 -220
  75. data/lib/glimmer/rake_task/list.rb +97 -97
  76. data/lib/glimmer/rake_task/package.rb +139 -139
  77. data/lib/glimmer/rake_task/scaffold.rb +765 -765
  78. data/lib/glimmer/swt/color_proxy.rb +108 -107
  79. data/lib/glimmer/swt/cursor_proxy.rb +66 -66
  80. data/lib/glimmer/swt/custom/animation.rb +245 -243
  81. data/lib/glimmer/swt/custom/checkbox_group.rb +181 -181
  82. data/lib/glimmer/swt/custom/code_text.rb +1 -0
  83. data/lib/glimmer/swt/custom/drawable.rb +97 -49
  84. data/lib/glimmer/swt/custom/radio_group.rb +177 -176
  85. data/lib/glimmer/swt/custom/shape.rb +332 -297
  86. data/lib/glimmer/swt/date_time_proxy.rb +85 -85
  87. data/lib/glimmer/swt/directory_dialog_proxy.rb +65 -65
  88. data/lib/glimmer/swt/display_proxy.rb +167 -166
  89. data/lib/glimmer/swt/dnd_proxy.rb +51 -51
  90. data/lib/glimmer/swt/expand_item_proxy.rb +97 -97
  91. data/lib/glimmer/swt/file_dialog_proxy.rb +66 -66
  92. data/lib/glimmer/swt/font_proxy.rb +94 -94
  93. data/lib/glimmer/swt/image_proxy.rb +195 -184
  94. data/lib/glimmer/swt/layout_data_proxy.rb +105 -105
  95. data/lib/glimmer/swt/layout_proxy.rb +112 -112
  96. data/lib/glimmer/swt/menu_proxy.rb +126 -126
  97. data/lib/glimmer/swt/message_box_proxy.rb +89 -89
  98. data/lib/glimmer/swt/packages.rb +37 -37
  99. data/lib/glimmer/swt/properties.rb +74 -49
  100. data/lib/glimmer/swt/sash_form_proxy.rb +53 -53
  101. data/lib/glimmer/swt/scrolled_composite_proxy.rb +46 -37
  102. data/lib/glimmer/swt/style_constantizable.rb +157 -157
  103. data/lib/glimmer/swt/styled_text_proxy.rb +38 -38
  104. data/lib/glimmer/swt/swt_proxy.rb +59 -59
  105. data/lib/glimmer/swt/tab_item_proxy.rb +92 -91
  106. data/lib/glimmer/swt/table_column_proxy.rb +57 -57
  107. data/lib/glimmer/swt/transform_proxy.rb +109 -109
  108. data/lib/glimmer/swt/tree_proxy.rb +145 -145
  109. data/lib/glimmer/swt/widget_listener_proxy.rb +64 -64
  110. data/lib/glimmer/swt/widget_proxy.rb +977 -957
  111. data/lib/glimmer/ui/custom_shell.rb +82 -82
  112. data/lib/glimmer/ui/custom_widget.rb +332 -315
  113. data/lib/glimmer/util/proc_tracker.rb +39 -39
  114. data/samples/elaborate/contact_manager.rb +142 -142
  115. data/samples/elaborate/contact_manager/contact.rb +32 -32
  116. data/samples/elaborate/contact_manager/contact_manager_presenter.rb +47 -47
  117. data/samples/elaborate/contact_manager/contact_repository.rb +169 -169
  118. data/samples/elaborate/login.rb +123 -123
  119. data/samples/elaborate/mandelbrot_fractal.rb +354 -0
  120. data/samples/elaborate/tetris.rb +5 -18
  121. data/samples/elaborate/tetris/model/block.rb +48 -48
  122. data/samples/elaborate/tetris/model/past_game.rb +39 -39
  123. data/samples/elaborate/tetris/view/block.rb +1 -1
  124. data/samples/elaborate/tetris/view/playfield.rb +1 -1
  125. data/samples/elaborate/tic_tac_toe.rb +76 -76
  126. data/samples/elaborate/tic_tac_toe/board.rb +145 -145
  127. data/samples/elaborate/tic_tac_toe/cell.rb +48 -48
  128. data/samples/elaborate/user_profile.rb +76 -76
  129. data/samples/hello/hello_browser.rb +31 -31
  130. data/samples/hello/hello_button.rb +46 -46
  131. data/samples/hello/hello_canvas.rb +64 -64
  132. data/samples/hello/hello_canvas_animation.rb +1 -1
  133. data/samples/hello/hello_checkbox.rb +85 -85
  134. data/samples/hello/hello_checkbox_group.rb +71 -71
  135. data/samples/hello/hello_code_text.rb +104 -104
  136. data/samples/hello/hello_combo.rb +63 -63
  137. data/samples/hello/hello_computed.rb +96 -96
  138. data/samples/hello/hello_computed/contact.rb +42 -42
  139. data/samples/hello/hello_cursor.rb +57 -0
  140. data/samples/hello/hello_custom_shell.rb +155 -155
  141. data/samples/hello/hello_custom_widget.rb +86 -86
  142. data/samples/hello/hello_date_time.rb +63 -63
  143. data/samples/hello/hello_dialog.rb +78 -78
  144. data/samples/hello/hello_directory_dialog.rb +60 -60
  145. data/samples/hello/hello_drag_and_drop.rb +50 -50
  146. data/samples/hello/hello_expand_bar.rb +110 -110
  147. data/samples/hello/hello_file_dialog.rb +60 -60
  148. data/samples/hello/hello_group.rb +104 -104
  149. data/samples/hello/hello_link.rb +80 -80
  150. data/samples/hello/hello_list_multi_selection.rb +74 -74
  151. data/samples/hello/hello_list_single_selection.rb +59 -59
  152. data/samples/hello/hello_menu_bar.rb +241 -241
  153. data/samples/hello/hello_message_box.rb +37 -37
  154. data/samples/hello/hello_pop_up_context_menu.rb +84 -84
  155. data/samples/hello/hello_radio.rb +108 -108
  156. data/samples/hello/hello_radio_group.rb +87 -87
  157. data/samples/hello/hello_sash_form.rb +137 -137
  158. data/samples/hello/hello_spinner.rb +69 -69
  159. data/samples/hello/hello_styled_text.rb +138 -138
  160. data/samples/hello/hello_tab.rb +50 -50
  161. data/samples/hello/hello_table.rb +1 -1
  162. data/samples/hello/hello_table/baseball_park.png +0 -0
  163. data/samples/hello/hello_world.rb +29 -29
  164. metadata +19 -4
@@ -1,39 +1,39 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining
4
- # a copy of this software and associated documentation files (the
5
- # "Software"), to deal in the Software without restriction, including
6
- # without limitation the rights to use, copy, modify, merge, publish,
7
- # distribute, sublicense, and/or sell copies of the Software, and to
8
- # permit persons to whom the Software is furnished to do so, subject to
9
- # the following conditions:
10
- #
11
- # The above copyright notice and this permission notice shall be
12
- # included in all copies or substantial portions of the Software.
13
- #
14
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- class Tetris
23
- module Model
24
- class PastGame
25
- attr_accessor :name, :score, :lines, :level
26
-
27
- def initialize(name, score, lines, level)
28
- @name = name
29
- @score = score.to_i
30
- @lines = lines.to_i
31
- @level = level.to_i
32
- end
33
-
34
- def to_a
35
- [@name, @score, @lines, @level]
36
- end
37
- end
38
- end
39
- end
1
+ # Copyright (c) 2007-2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ class Tetris
23
+ module Model
24
+ class PastGame
25
+ attr_accessor :name, :score, :lines, :level
26
+
27
+ def initialize(name, score, lines, level)
28
+ @name = name
29
+ @score = score.to_i
30
+ @lines = lines.to_i
31
+ @level = level.to_i
32
+ end
33
+
34
+ def to_a
35
+ [@name, @score, @lines, @level]
36
+ end
37
+ end
38
+ end
39
+ end
@@ -27,7 +27,7 @@ class Tetris
27
27
  options :game_playfield, :block_size, :row, :column
28
28
 
29
29
  body {
30
- canvas(:double_buffered) {
30
+ canvas {
31
31
  background bind(game_playfield[row][column], :color)
32
32
  polygon(0, 0, block_size, 0, block_size - 4, 4, 4, 4) {
33
33
  background bind(game_playfield[row][column], :color) { |color_value|
@@ -29,7 +29,7 @@ class Tetris
29
29
  options :game_playfield, :playfield_width, :playfield_height, :block_size
30
30
 
31
31
  body {
32
- composite(:double_buffered) {
32
+ composite((:double_buffered unless OS.mac?)) {
33
33
  grid_layout {
34
34
  num_columns playfield_width
35
35
  make_columns_equal_width true
@@ -1,76 +1,76 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining
4
- # a copy of this software and associated documentation files (the
5
- # "Software"), to deal in the Software without restriction, including
6
- # without limitation the rights to use, copy, modify, merge, publish,
7
- # distribute, sublicense, and/or sell copies of the Software, and to
8
- # permit persons to whom the Software is furnished to do so, subject to
9
- # the following conditions:
10
- #
11
- # The above copyright notice and this permission notice shall be
12
- # included in all copies or substantial portions of the Software.
13
- #
14
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- require_relative "tic_tac_toe/board"
23
-
24
- class TicTacToe
25
- include Glimmer
26
-
27
- def initialize
28
- @tic_tac_toe_board = Board.new
29
- @shell = shell {
30
- text "Tic-Tac-Toe"
31
- minimum_size 176, 200
32
- composite {
33
- grid_layout 3, true
34
- (1..3).each { |row|
35
- (1..3).each { |column|
36
- button {
37
- layout_data :fill, :fill, true, true
38
- text bind(@tic_tac_toe_board[row, column], :sign)
39
- enabled bind(@tic_tac_toe_board[row, column], :empty)
40
- font style: :bold, height: 20
41
- on_widget_selected {
42
- @tic_tac_toe_board.mark(row, column)
43
- }
44
- }
45
- }
46
- }
47
- }
48
- }
49
- observe(@tic_tac_toe_board, :game_status) { |game_status|
50
- display_win_message if game_status == Board::WIN
51
- display_draw_message if game_status == Board::DRAW
52
- }
53
- end
54
-
55
- def display_win_message
56
- display_game_over_message("Player #{@tic_tac_toe_board.winning_sign} has won!")
57
- end
58
-
59
- def display_draw_message
60
- display_game_over_message("Draw!")
61
- end
62
-
63
- def display_game_over_message(message_text)
64
- message_box {
65
- text 'Game Over'
66
- message message_text
67
- }.open
68
- @tic_tac_toe_board.reset
69
- end
70
-
71
- def open
72
- @shell.open
73
- end
74
- end
75
-
76
- TicTacToe.new.open
1
+ # Copyright (c) 2007-2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require_relative "tic_tac_toe/board"
23
+
24
+ class TicTacToe
25
+ include Glimmer
26
+
27
+ def initialize
28
+ @tic_tac_toe_board = Board.new
29
+ @shell = shell {
30
+ text "Tic-Tac-Toe"
31
+ minimum_size 176, 200
32
+ composite {
33
+ grid_layout 3, true
34
+ (1..3).each { |row|
35
+ (1..3).each { |column|
36
+ button {
37
+ layout_data :fill, :fill, true, true
38
+ text bind(@tic_tac_toe_board[row, column], :sign)
39
+ enabled bind(@tic_tac_toe_board[row, column], :empty)
40
+ font style: :bold, height: 20
41
+ on_widget_selected {
42
+ @tic_tac_toe_board.mark(row, column)
43
+ }
44
+ }
45
+ }
46
+ }
47
+ }
48
+ }
49
+ observe(@tic_tac_toe_board, :game_status) { |game_status|
50
+ display_win_message if game_status == Board::WIN
51
+ display_draw_message if game_status == Board::DRAW
52
+ }
53
+ end
54
+
55
+ def display_win_message
56
+ display_game_over_message("Player #{@tic_tac_toe_board.winning_sign} has won!")
57
+ end
58
+
59
+ def display_draw_message
60
+ display_game_over_message("Draw!")
61
+ end
62
+
63
+ def display_game_over_message(message_text)
64
+ message_box {
65
+ text 'Game Over'
66
+ message message_text
67
+ }.open
68
+ @tic_tac_toe_board.reset
69
+ end
70
+
71
+ def open
72
+ @shell.open
73
+ end
74
+ end
75
+
76
+ TicTacToe.new.open
@@ -1,145 +1,145 @@
1
- # Copyright (c) 2007-2021 Andy Maleh
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining
4
- # a copy of this software and associated documentation files (the
5
- # "Software"), to deal in the Software without restriction, including
6
- # without limitation the rights to use, copy, modify, merge, publish,
7
- # distribute, sublicense, and/or sell copies of the Software, and to
8
- # permit persons to whom the Software is furnished to do so, subject to
9
- # the following conditions:
10
- #
11
- # The above copyright notice and this permission notice shall be
12
- # included in all copies or substantial portions of the Software.
13
- #
14
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- require_relative 'cell'
23
-
24
- class TicTacToe
25
- class Board
26
- DRAW = :draw
27
- IN_PROGRESS = :in_progress
28
- WIN = :win
29
- attr :winning_sign
30
- attr_accessor :game_status
31
-
32
- def initialize
33
- @sign_state_machine = {nil => "X", "X" => "O", "O" => "X"}
34
- build_grid
35
- @winning_sign = Cell::EMPTY
36
- @game_status = IN_PROGRESS
37
- end
38
-
39
- #row and column numbers are 1-based
40
- def mark(row, column)
41
- self[row, column].mark(current_sign)
42
- game_over? #updates winning sign
43
- end
44
-
45
- def current_sign
46
- @current_sign = @sign_state_machine[@current_sign]
47
- end
48
-
49
- def [](row, column)
50
- @grid[row-1][column-1]
51
- end
52
-
53
- def game_over?
54
- win? or draw?
55
- end
56
-
57
- def win?
58
- win = (row_win? or column_win? or diagonal_win?)
59
- self.game_status=WIN if win
60
- win
61
- end
62
-
63
- def reset
64
- (1..3).each do |row|
65
- (1..3).each do |column|
66
- self[row, column].reset
67
- end
68
- end
69
- @winning_sign = Cell::EMPTY
70
- @current_sign = nil
71
- self.game_status=IN_PROGRESS
72
- end
73
-
74
- private
75
-
76
- def build_grid
77
- @grid = []
78
- 3.times do |row_index| #0-based
79
- @grid << []
80
- 3.times { @grid[row_index] << Cell.new }
81
- end
82
- end
83
-
84
- def row_win?
85
- (1..3).each do |row|
86
- if row_has_same_sign(row)
87
- @winning_sign = self[row, 1].sign
88
- return true
89
- end
90
- end
91
- false
92
- end
93
-
94
- def column_win?
95
- (1..3).each do |column|
96
- if column_has_same_sign(column)
97
- @winning_sign = self[1, column].sign
98
- return true
99
- end
100
- end
101
- false
102
- end
103
-
104
- #needs refactoring if we ever decide to make the board size dynamic
105
- def diagonal_win?
106
- if (self[1, 1].sign == self[2, 2].sign) and (self[2, 2].sign == self[3, 3].sign) and self[1, 1].marked
107
- @winning_sign = self[1, 1].sign
108
- return true
109
- end
110
- if (self[3, 1].sign == self[2, 2].sign) and (self[2, 2].sign == self[1, 3].sign) and self[3, 1].marked
111
- @winning_sign = self[3, 1].sign
112
- return true
113
- end
114
- false
115
- end
116
-
117
- def draw?
118
- @board_full = true
119
- 3.times do |x|
120
- 3.times do |y|
121
- @board_full = false if self[x, y].empty
122
- end
123
- end
124
- self.game_status = DRAW if @board_full
125
- @board_full
126
- end
127
-
128
- def row_has_same_sign(number)
129
- row_sign = self[number, 1].sign
130
- [2, 3].each do |column|
131
- return false unless row_sign == (self[number, column].sign)
132
- end
133
- true if self[number, 1].marked
134
- end
135
-
136
- def column_has_same_sign(number)
137
- column_sign = self[1, number].sign
138
- [2, 3].each do |row|
139
- return false unless column_sign == (self[row, number].sign)
140
- end
141
- true if self[1, number].marked
142
- end
143
-
144
- end
145
- end
1
+ # Copyright (c) 2007-2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require_relative 'cell'
23
+
24
+ class TicTacToe
25
+ class Board
26
+ DRAW = :draw
27
+ IN_PROGRESS = :in_progress
28
+ WIN = :win
29
+ attr :winning_sign
30
+ attr_accessor :game_status
31
+
32
+ def initialize
33
+ @sign_state_machine = {nil => "X", "X" => "O", "O" => "X"}
34
+ build_grid
35
+ @winning_sign = Cell::EMPTY
36
+ @game_status = IN_PROGRESS
37
+ end
38
+
39
+ #row and column numbers are 1-based
40
+ def mark(row, column)
41
+ self[row, column].mark(current_sign)
42
+ game_over? #updates winning sign
43
+ end
44
+
45
+ def current_sign
46
+ @current_sign = @sign_state_machine[@current_sign]
47
+ end
48
+
49
+ def [](row, column)
50
+ @grid[row-1][column-1]
51
+ end
52
+
53
+ def game_over?
54
+ win? or draw?
55
+ end
56
+
57
+ def win?
58
+ win = (row_win? or column_win? or diagonal_win?)
59
+ self.game_status=WIN if win
60
+ win
61
+ end
62
+
63
+ def reset
64
+ (1..3).each do |row|
65
+ (1..3).each do |column|
66
+ self[row, column].reset
67
+ end
68
+ end
69
+ @winning_sign = Cell::EMPTY
70
+ @current_sign = nil
71
+ self.game_status=IN_PROGRESS
72
+ end
73
+
74
+ private
75
+
76
+ def build_grid
77
+ @grid = []
78
+ 3.times do |row_index| #0-based
79
+ @grid << []
80
+ 3.times { @grid[row_index] << Cell.new }
81
+ end
82
+ end
83
+
84
+ def row_win?
85
+ (1..3).each do |row|
86
+ if row_has_same_sign(row)
87
+ @winning_sign = self[row, 1].sign
88
+ return true
89
+ end
90
+ end
91
+ false
92
+ end
93
+
94
+ def column_win?
95
+ (1..3).each do |column|
96
+ if column_has_same_sign(column)
97
+ @winning_sign = self[1, column].sign
98
+ return true
99
+ end
100
+ end
101
+ false
102
+ end
103
+
104
+ #needs refactoring if we ever decide to make the board size dynamic
105
+ def diagonal_win?
106
+ if (self[1, 1].sign == self[2, 2].sign) and (self[2, 2].sign == self[3, 3].sign) and self[1, 1].marked
107
+ @winning_sign = self[1, 1].sign
108
+ return true
109
+ end
110
+ if (self[3, 1].sign == self[2, 2].sign) and (self[2, 2].sign == self[1, 3].sign) and self[3, 1].marked
111
+ @winning_sign = self[3, 1].sign
112
+ return true
113
+ end
114
+ false
115
+ end
116
+
117
+ def draw?
118
+ @board_full = true
119
+ 3.times do |x|
120
+ 3.times do |y|
121
+ @board_full = false if self[x, y].empty
122
+ end
123
+ end
124
+ self.game_status = DRAW if @board_full
125
+ @board_full
126
+ end
127
+
128
+ def row_has_same_sign(number)
129
+ row_sign = self[number, 1].sign
130
+ [2, 3].each do |column|
131
+ return false unless row_sign == (self[number, column].sign)
132
+ end
133
+ true if self[number, 1].marked
134
+ end
135
+
136
+ def column_has_same_sign(number)
137
+ column_sign = self[1, number].sign
138
+ [2, 3].each do |row|
139
+ return false unless column_sign == (self[row, number].sign)
140
+ end
141
+ true if self[1, number].marked
142
+ end
143
+
144
+ end
145
+ end