glimmer-dsl-swt 4.20.13.13 → 4.20.13.17

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 73c5a7ba90a7c91df55ac716b815e08e71b9ff13294ebf44e4c6587aa67080f2
4
- data.tar.gz: 889670d6eba956aaf4cc267c8a70cf36283379369a51bed85b5b5dd2aadd2762
3
+ metadata.gz: 1cc6c92bf35a9a28f26f3a34a735f5e900f294b70ffcd7ee7dc3ff9cd4a6b360
4
+ data.tar.gz: 900a03b90b78756a42a8407438ab380a2e856194ee1346651780049bc678bced
5
5
  SHA512:
6
- metadata.gz: 455e0b8626d4782e9ec122de48eae8ffec9688b2a724de11c858c4833925389ca3e2a46285e47e30b50e0fe414ccbe74f5dd869bbcbe03986e682dd5a1aad991
7
- data.tar.gz: 4ae13e2fedaa551c03ff3b8919bfe9ffa4faf31b7b6b393ba37b91e1fb54c179703da2c3d5283446122a7867eb970373ed191289ad2ace0e92880058b633f573
6
+ metadata.gz: d5cd116baea369efa9131cf6ee45b44b40e6ac7c4e1a5a79f6572b4d69f61885edd291c38b2b83b5cdbdabbb17d660559025891649bbb580acff4d30bdae1758
7
+ data.tar.gz: 99c8aa855ec1a72923c7c9725a92f81bd5d5c5c568b7defc9a02ac3bcfc26fa82c6233cc0832ec578400b2131c13a679d8a0e04a0ec10ff37aaca1bc44d41086
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Change Log
2
2
 
3
+ ### 4.20.13.17
4
+
5
+ - Hello, Slider! sample
6
+
7
+ ### 4.20.13.16
8
+
9
+ - Fix issue with setting app name (via `Display.app_name=`) when app is not packaged (always gets set to Glimmer)
10
+ - Fix Hello, Custom Shell! sample
11
+
12
+ ### 4.20.13.15
13
+
14
+ - Battleship elaborate sample
15
+
16
+ ### 4.20.13.14
17
+
18
+ - Connect 4 elaborate sample
19
+
3
20
  ### 4.20.13.13
4
21
 
5
22
  - Parking elaborate sample
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for SWT 4.20.13.13
1
+ # [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for SWT 4.20.13.17
2
2
  ## JRuby Desktop Development GUI Framework
3
3
  [![Gem Version](https://badge.fury.io/rb/glimmer-dsl-swt.svg)](http://badge.fury.io/rb/glimmer-dsl-swt)
4
4
  [![Travis CI](https://travis-ci.com/AndyObtiva/glimmer-dsl-swt.svg?branch=master)](https://travis-ci.com/github/AndyObtiva/glimmer-dsl-swt)
@@ -15,7 +15,7 @@
15
15
  [<img src="https://covers.oreillystatic.com/images/9780596519650/lrg.jpg" width=105 /><br />
16
16
  Featured in JRuby Cookbook](http://shop.oreilly.com/product/9780596519650.do) and [Chalmers/Gothenburg University Software Engineering Master's Lecture Material](http://www.cse.chalmers.se/~bergert/slides/guest_lecture_DSLs.pdf)
17
17
 
18
- [Glimmer DSL for SWT](https://rubygems.org/gems/glimmer-dsl-swt) 4.20.13.13 includes [SWT 4.20](https://download.eclipse.org/eclipse/downloads/drops4/R-4.20-202106111600/), which was released on June 11, 2021. Gem version numbers are in sync with the SWT library versions. The first two digits represent the SWT version number. The last two digits represent the minor and patch versions of Glimmer DSL for SWT. Note that SWT now supports AARCH64 on Mac and Linux, but it is not fully tested in Glimmer DSL for SWT yet, so deem its support experimental for the time being without guarantees for functionality until declared otherwise (report any issues you may encounter).
18
+ [Glimmer DSL for SWT](https://rubygems.org/gems/glimmer-dsl-swt) 4.20.13.17 includes [SWT 4.20](https://download.eclipse.org/eclipse/downloads/drops4/R-4.20-202106111600/), which was released on June 11, 2021. Gem version numbers are in sync with the SWT library versions. The first two digits represent the SWT version number. The last two digits represent the minor and patch versions of Glimmer DSL for SWT. Note that SWT now supports AARCH64 on Mac and Linux, but it is not fully tested in Glimmer DSL for SWT yet, so deem its support experimental for the time being without guarantees for functionality until declared otherwise (please report any issues you may encounter).
19
19
 
20
20
  **Starting in version 4.20.0.0, [Glimmer DSL for SWT](https://rubygems.org/gems/glimmer-dsl-swt) comes with the new [***Shine***](/docs/reference/GLIMMER_GUI_DSL_SYNTAX.md#shine) syntax** for highly intuitive and visually expressive View/Model Attribute Mapping, relying on `<=>` for bidirectional (two-way) data-binding and `<=` for unidirectional (one-way) data-binding, providing an alternative to the `bind` keyword (keep in mind that it is still a beta, so default back to `bind` whenever needed).
21
21
 
@@ -199,11 +199,6 @@ Glimmer app:
199
199
 
200
200
  [![Math Bowling App Screenshot](https://raw.githubusercontent.com/AndyObtiva/MathBowling/master/Math-Bowling-Screenshot.png)](https://github.com/AndyObtiva/MathBowling)
201
201
 
202
- **Note:** I offer Glimmer DSL for SWT as a free and open-source [Ruby Gem](https://rubygems.org/gems/glimmer-dsl-swt) that represents my interests in Ruby Programming, Desktop GUI application development with SWT, Object Oriented Design, Design Patterns, and Software Architecture.
203
- Additionally, I am sharing my professional experience and expertise in Eclipse SWT given that I am an [EclipseCon](http://andymaleh.blogspot.com/2007/03/eclipsecon-2007-day-3.html)/[EclipseWorld](http://andymaleh.blogspot.com/2008/11/eclipseworld-2008-highlights.html) presenter and have built professional applications in SWT/JFace/RCP for Obtiva and the Pampered Chef in the past. This is also done in the hopes that it would indirectly bring me work in a field I am extremely passionate about.
204
- That said, please keep in mind that I myself am learning topics in Software Engineering too everyday, including newer editions of SWT and JRuby, which seem to pop up every quarter.
205
- If you see anything that needs to be improved, please do not hesitate to contact me on [Gitter](https://gitter.im/AndyObtiva/glimmer) or submit [Issues](https://github.com/AndyObtiva/glimmer-dsl-swt/issues)/[Pull-Requests](https://github.com/AndyObtiva/glimmer-dsl-swt/pulls).
206
-
207
202
  ## Table of contents
208
203
 
209
204
  - [Glimmer (JRuby Desktop Development GUI Framework)](#jruby-desktop-development-gui-framework)
@@ -326,7 +321,7 @@ jgem install glimmer-dsl-swt
326
321
 
327
322
  Or this command if you want a specific version:
328
323
  ```
329
- jgem install glimmer-dsl-swt -v 4.20.13.13
324
+ jgem install glimmer-dsl-swt -v 4.20.13.17
330
325
  ```
331
326
 
332
327
  `jgem` is JRuby's version of `gem` command.
@@ -354,7 +349,7 @@ Note: if you're using activerecord or activesupport, keep in mind that Glimmer u
354
349
 
355
350
  Add the following to `Gemfile`:
356
351
  ```
357
- gem 'glimmer-dsl-swt', '~> 4.20.13.13'
352
+ gem 'glimmer-dsl-swt', '~> 4.20.13.17'
358
353
  ```
359
354
 
360
355
  And, then run:
@@ -375,7 +370,7 @@ glimmer
375
370
  ```
376
371
 
377
372
  ```
378
- Glimmer (JRuby Desktop Development GUI Framework) - JRuby Gem: glimmer-dsl-swt v4.20.13.13
373
+ Glimmer (JRuby Desktop Development GUI Framework) - JRuby Gem: glimmer-dsl-swt v4.20.13.17
379
374
 
380
375
  Usage: glimmer [--bundler] [--pd] [--quiet] [--debug] [--log-level=VALUE] [[ENV_VAR=VALUE]...] [[-jruby-option]...] (application.rb or task[task_args]) [[application2.rb]...]
381
376
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.20.13.13
1
+ 4.20.13.17
@@ -60,10 +60,12 @@
60
60
  - [User Profile](#user-profile)
61
61
  - [Login](#login)
62
62
  - [Tic Tac Toe](#tic-tac-toe)
63
+ - [Connect 4](#connect-4)
63
64
  - [Contact Manager](#contact-manager)
64
65
  - [Game of Life](#game-of-life)
65
66
  - [Glimmer Tetris](#glimmer-tetris)
66
67
  - [Klondike Solitaire](#klondike-solitaire)
68
+ - [Battleship](#battleship)
67
69
  - [Mandelbrot Fractal](#mandelbrot-fractal)
68
70
  - [Parking](#parking)
69
71
  - [Stock Ticker](#stock-ticker)
@@ -73,6 +75,7 @@
73
75
  - [Glimmer Calculator](#glimmer-calculator)
74
76
  - [Gladiator](#gladiator)
75
77
  - [Timer](#timer)
78
+ - [Glimmer Klondike Solitaire](#glimmer-klondike-solitaire)
76
79
  - [License](#license)
77
80
 
78
81
  ## Samples
@@ -1045,6 +1048,34 @@ Code:
1045
1048
  ![Tic Tac Toe In Progress](/images/glimmer-tic-tac-toe-in-progress.png)
1046
1049
  ![Tic Tac Toe Game Over](/images/glimmer-tic-tac-toe-game-over.png)
1047
1050
 
1051
+ #### Connect 4
1052
+
1053
+ This sample demonstrates a widget/shape hybrid MVC application, including GUI layout and data-binding.
1054
+
1055
+ Code:
1056
+
1057
+ [samples/elaborate/connect4.rb](/samples/elaborate/connect4.rb)
1058
+
1059
+ Connect 4
1060
+
1061
+ ![Connect 4](/images/glimmer-connect4.png)
1062
+
1063
+ Connect 4 - About To Drop
1064
+
1065
+ ![Connect 4 About To Drop](/images/glimmer-connect4-about-to-drop.png)
1066
+
1067
+ Connect 4 - Dropped Coin
1068
+
1069
+ ![Connect 4 Dropped Coin](/images/glimmer-connect4-dropped-coin.png)
1070
+
1071
+ Connect 4 - Player 1 Wins (keeps the coin about to drop visual cue)
1072
+
1073
+ ![Connect 4 Player 1 Wins](/images/glimmer-connect4-player1-wins.png)
1074
+
1075
+ Connect 4 - Game Over Message Box
1076
+
1077
+ ![Connect 4 Game Over Message Box](/images/glimmer-connect4-game-over-dialog.png)
1078
+
1048
1079
  #### Contact Manager
1049
1080
 
1050
1081
  This sample demonstrates table data-binding, sorting, filtering, GUI layout, MVP pattern, and test-driven development (has [specs](https://github.com/AndyObtiva/glimmer-dsl-swt/blob/master/spec/samples/elaborate/contact_manager/contact_manager_presenter_spec.rb)).
@@ -1075,7 +1106,7 @@ Contact Manager - Edit Done
1075
1106
 
1076
1107
  #### Game of Life
1077
1108
 
1078
- This sample demonstrates how to build an interactive canvas-based visualization of Conway's Game of Life (test-first), taking advantage of data-binding and multi-threading.
1109
+ This sample demonstrates how to build an interactive canvas-based visualization of Conway's Game of Life ([test-first](https://github.com/AndyObtiva/glimmer-dsl-swt/blob/master/spec/samples/elaborate/game_of_life/model/grid_spec.rb)), taking advantage of data-binding and multi-threading.
1079
1110
 
1080
1111
  Code:
1081
1112
 
@@ -1113,7 +1144,7 @@ Code:
1113
1144
 
1114
1145
  #### Klondike Solitaire
1115
1146
 
1116
- This sample demonstrates how to build an interactive card game with MVC architecture, canvas, custom-shapes, data-binding, observers, and drag & drop.
1147
+ This sample demonstrates how to build an interactive card game with MVC architecture, canvas, custom-shapes, data-binding, observers, and canvas shape drag & drop.
1117
1148
 
1118
1149
  Code:
1119
1150
 
@@ -1123,6 +1154,26 @@ Code:
1123
1154
 
1124
1155
  ![Klondike Solitaire Played](/images/glimmer-klondike-solitaire-played.png)
1125
1156
 
1157
+ Check out a souped up large-card-size packaged version of the game in the [Glimmer Klondike Solitaire](#glimmer-klondike-solitaire) external sample.
1158
+
1159
+ #### Battleship
1160
+
1161
+ This sample demonstrates how to build an interactive board game with A.I., MVC architecture, hybrid canvas widget/shape approach, custom-widgets, data-binding, observers, and widget drag & drop.
1162
+
1163
+ Code:
1164
+
1165
+ [samples/elaborate/battleship.rb](/samples/elaborate/battleship.rb)
1166
+
1167
+ ![Battleship](/images/glimmer-battleship.png)
1168
+
1169
+ ![Battleship Placement](/images/glimmer-battleship-placement.png)
1170
+
1171
+ ![Battleship Ready for Battle](/images/glimmer-battleship-ready-for-battle.png)
1172
+
1173
+ ![Battleship Won](/images/glimmer-battleship-won.png)
1174
+
1175
+ ![Battleship Game Over](/images/glimmer-battleship-game-over.png)
1176
+
1126
1177
  #### Mandelbrot Fractal
1127
1178
 
1128
1179
  This sample demonstrates how to render canvas graphics with multi-threaded processing taking advantage of all CPU cores and doing background processing of images.
@@ -1246,6 +1297,14 @@ Gladiator is a good demonstration of:
1246
1297
 
1247
1298
  [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer-cs-timer/master/glimmer-timer-screenshot.png" />](https://github.com/AndyObtiva/glimmer-cs-timer)
1248
1299
 
1300
+ #### Glimmer Klondike Solitaire
1301
+
1302
+ This is a souped up version of the Klondike Solitaire elaborate sample, which is built as an external application to enable packaging as a native executable installer. Enjoy!
1303
+
1304
+ [<img alt="Glimmer Klondike Solitaire Icon" src="https://raw.githubusercontent.com/AndyObtiva/glimmer_klondike_solitaire/master/package/linux/Glimmer%20Klondike%20Solitaire.png" height=40 /> Glimmer Klondike Solitaire](https://github.com/AndyObtiva/glimmer_klondike_solitaire)
1305
+
1306
+ ![Glimmer Klondike Solitaire](https://raw.githubusercontent.com/AndyObtiva/glimmer_klondike_solitaire/master/images/glimmer-klondike-solitaire.png)
1307
+
1249
1308
  ## License
1250
1309
 
1251
1310
  [MIT](LICENSE.txt)
Binary file
@@ -200,12 +200,14 @@ module Glimmer
200
200
  elsif observation_request.start_with?('on_')
201
201
  event_name = observation_request.sub(/^on_/, '')
202
202
  if OBSERVED_MENU_ITEMS.include?(event_name) && OS.mac?
203
- system_menu = swt_display.getSystemMenu
204
- menu_item = system_menu.getItems.find {|menu_item| menu_item.getID == SWTProxy["ID_#{event_name.upcase}"]}
205
- listener = ConcreteListener.new(&block)
206
- display_mac_event_registration = menu_item.addListener(SWTProxy[:Selection], listener)
207
- Glimmer::UI::CustomWidget.current_custom_widgets.last&.observer_registrations&.push(display_mac_event_registration)
208
- display_mac_event_registration
203
+ auto_exec do
204
+ system_menu = swt_display.getSystemMenu
205
+ menu_item = system_menu.getItems.find {|menu_item| menu_item.getID == SWTProxy["ID_#{event_name.upcase}"]}
206
+ listener = ConcreteListener.new(&block)
207
+ display_mac_event_registration = menu_item.addListener(SWTProxy[:Selection], listener)
208
+ Glimmer::UI::CustomWidget.current_custom_widgets.last&.observer_registrations&.push(display_mac_event_registration)
209
+ display_mac_event_registration
210
+ end
209
211
  end
210
212
  end
211
213
  end
@@ -30,8 +30,8 @@ module Glimmer
30
30
 
31
31
  class << self
32
32
  def launch(*args, &content)
33
+ launched_custom_shell = send(keyword, *args, &content)
33
34
  auto_exec do
34
- launched_custom_shell = send(keyword, *args, &content)
35
35
  launched_custom_shell.swt_widget.set_data('launched', true)
36
36
  launched_custom_shell.open
37
37
  end
@@ -176,16 +176,16 @@ module Glimmer
176
176
  options ||= {}
177
177
  @options = self.class.options.merge(options)
178
178
  @content = Util::ProcTracker.new(content) if content
179
- execute_hook('before_body')
179
+ auto_exec { execute_hook('before_body') }
180
180
  body_block = self.class.instance_variable_get("@body_block")
181
181
  raise Glimmer::Error, 'Invalid custom widget for having no body! Please define body block!' if body_block.nil?
182
- @body_root = instance_exec(&body_block)
182
+ @body_root = auto_exec { instance_exec(&body_block) }
183
183
  raise Glimmer::Error, 'Invalid custom widget for having an empty body! Please fill body block!' if @body_root.nil?
184
184
  @swt_widget = @body_root.swt_widget
185
185
  auto_exec do
186
186
  @swt_widget.set_data('custom_widget', self)
187
187
  end
188
- execute_hook('after_body')
188
+ auto_exec { execute_hook('after_body') }
189
189
  auto_exec do
190
190
  @dispose_listener_registration = @body_root.on_widget_disposed do
191
191
  observer_registrations.compact.each(&:deregister)
@@ -0,0 +1,79 @@
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 'glimmer-dsl-swt'
23
+ require 'facets/string/titlecase'
24
+ require 'facets/string/underscore'
25
+
26
+ require_relative 'battleship/model/game'
27
+
28
+ require_relative 'battleship/view/grid'
29
+ require_relative 'battleship/view/ship_collection'
30
+ require_relative 'battleship/view/action_panel'
31
+
32
+ class Battleship
33
+ include Glimmer::UI::CustomShell
34
+
35
+ COLOR_WATER = rgb(156, 211, 219)
36
+ COLOR_SHIP = :dark_gray
37
+
38
+ before_body do
39
+ @game = Model::Game.new
40
+ end
41
+
42
+ after_body do
43
+ observe(@game, :over) do |game_over_value|
44
+ if game_over_value
45
+ game_over_message = if game_over_value == :you
46
+ "Game over!\nYou Won!"
47
+ else
48
+ "Game over!\nYou Lost!"
49
+ end
50
+
51
+ message_box {
52
+ text 'Game Over!'
53
+ message game_over_message
54
+ }.open
55
+ end
56
+ end
57
+ end
58
+
59
+ body {
60
+ shell(:no_resize) {
61
+ grid_layout(2, false) {
62
+ horizontal_spacing 15
63
+ vertical_spacing 15
64
+ }
65
+
66
+ text 'Glimmer Battleship'
67
+
68
+ @enemy_grid = grid(game: @game, player: :enemy)
69
+ @enemy_ship_collection = ship_collection(game: @game, player: :enemy)
70
+
71
+ @player_grid = grid(game: @game, player: :you)
72
+ @player_ship_collection = ship_collection(game: @game, player: :you)
73
+
74
+ action_panel(game: @game)
75
+ }
76
+ }
77
+ end
78
+
79
+ Battleship.launch
@@ -0,0 +1,84 @@
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 Battleship
23
+ module Model
24
+ class Cell
25
+ attr_reader :grid, :row_index, :column_index
26
+ attr_accessor :hit, :ship, :ship_index
27
+ alias hit? hit
28
+
29
+ def initialize(grid, row_index, column_index)
30
+ @grid = grid
31
+ @row_index = row_index
32
+ @column_index = column_index
33
+ end
34
+
35
+ def reset!
36
+ self.hit = nil
37
+ self.ship = nil
38
+ self.ship_index = nil
39
+ end
40
+
41
+ # Places ship horizontally so that its top left cell is self,
42
+ # automatically figuring out the rest of the cells
43
+ def place_ship!(ship)
44
+ begin
45
+ old_ship_top_left_cell = ship.top_left_cell
46
+ old_ship_orientation = ship.orientation
47
+ ship.top_left_cell = self
48
+ if old_ship_top_left_cell
49
+ ship.cells(old_ship_orientation).each(&:reset!)
50
+ ship.length.times do |index|
51
+ if ship.orientation == :horizontal
52
+ old_cell = grid.cell_rows[old_ship_top_left_cell.row_index][old_ship_top_left_cell.column_index + index]
53
+ else
54
+ old_cell = grid.cell_rows[old_ship_top_left_cell.row_index + index][old_ship_top_left_cell.column_index]
55
+ end
56
+ old_cell.reset!
57
+ end
58
+ end
59
+ ship.length.times do |index|
60
+ if ship.orientation == :horizontal
61
+ cell = grid.cell_rows[row_index][column_index + index]
62
+ else
63
+ cell = grid.cell_rows[row_index + index][column_index]
64
+ end
65
+ cell.ship = ship
66
+ cell.ship_index = index
67
+ end
68
+ rescue => e
69
+ Glimmer::Config.logger.debug(e.full_message)
70
+ end
71
+ end
72
+
73
+ def hit=(value)
74
+ @hit = value
75
+ ship&.sunk = true if ship&.all_cells_hit?
76
+ grid.game.over = grid.game.opposite_player(grid.player) if grid.game.started? && grid.game.ship_collections[grid.player].ships.values.map(&:sunk).all?
77
+ end
78
+
79
+ def to_s
80
+ "#{(Grid::ROW_ALPHABETS[row_index])}#{(column_index + 1)}"
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,143 @@
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 'grid'
23
+ require_relative 'ship'
24
+
25
+ class Battleship
26
+ module Model
27
+ class Game
28
+ BATTLESHIPS = {
29
+ aircraft_carrier: 5,
30
+ battleship: 4,
31
+ submarine: 3,
32
+ cruiser: 3,
33
+ destroyer: 2
34
+ }
35
+ PLAYERS = [:enemy, :you]
36
+
37
+ attr_reader :grids, :ship_collections
38
+ attr_accessor :started, :current_player, :over
39
+ alias started? started
40
+ alias over? over
41
+
42
+ def initialize
43
+ @grids = PLAYERS.reduce({}) { |hash, player| hash.merge(player => Grid.new(self, player)) }
44
+ @ship_collections = PLAYERS.reduce({}) { |hash, player| hash.merge(player => ShipCollection.new(self, player)) }
45
+ @started = false
46
+ @current_player = :enemy
47
+ @enemy_moves = []
48
+ end
49
+
50
+ def battle!
51
+ self.started = true
52
+ place_enemy_ships!
53
+ enemy_attack!
54
+ end
55
+
56
+ def reset!
57
+ self.started = false
58
+ self.over = false
59
+ self.current_player = :enemy
60
+ @grids.values.each(&:reset!)
61
+ @ship_collections.values.each(&:reset!)
62
+ @enemy_moves = []
63
+ end
64
+
65
+ def attack!(row_index, column_index)
66
+ return unless started?
67
+ cell = opposite_grid.cell_rows[row_index][column_index]
68
+ return unless cell.hit.nil?
69
+ cell.hit = !!cell.ship
70
+ switch_player!
71
+ enemy_attack! if current_player == :enemy
72
+ end
73
+
74
+ # Enemy attack artificial intelligence
75
+ def enemy_attack!
76
+ begin
77
+ cell = nil
78
+ if @enemy_moves.any? && @enemy_moves.last.hit? && !@enemy_moves.last.ship.sunk?
79
+ if @enemy_moves[-2].nil? || !@enemy_moves[-2].hit?
80
+ orientation = Ship::ORIENTATIONS[(rand * 2).to_i]
81
+ else
82
+ orientation = @enemy_moves.last.row_index == @enemy_moves[-2].row_index ? :horizontal : :vertical
83
+ end
84
+ offset = 1 * ((rand * 2).to_i == 1 ? 1 : -1)
85
+ if orientation == :horizontal
86
+ offset *= -1 if [-1, Grid::WIDTH].include?(@enemy_moves.last.column_index + offset)
87
+ cell = opposite_grid.cell_rows[@enemy_moves.last.row_index][@enemy_moves.last.column_index + offset]
88
+ else
89
+ offset *= -1 if [-1, Grid::HEIGHT].include?(@enemy_moves.last.row_index + offset)
90
+ cell = opposite_grid.cell_rows[@enemy_moves.last.row_index + offset][@enemy_moves.last.column_index]
91
+ end
92
+ cell = nil if cell.hit?
93
+ end
94
+ if cell.nil?
95
+ random_row_index = (rand * Grid::HEIGHT).to_i
96
+ random_column_index = (rand * Grid::WIDTH).to_i
97
+ cell = opposite_grid.cell_rows[random_row_index][random_column_index]
98
+ end
99
+ end until cell.hit.nil?
100
+ attack!(cell.row_index, cell.column_index)
101
+ @enemy_moves << cell
102
+ end
103
+
104
+ def opposite_grid
105
+ grids[opposite_player]
106
+ end
107
+
108
+ def opposite_player(player = nil)
109
+ player ||= current_player
110
+ PLAYERS[(PLAYERS.index(player) + 1) % PLAYERS.size]
111
+ end
112
+
113
+ def switch_player!
114
+ self.current_player = opposite_player
115
+ end
116
+
117
+ def over=(value)
118
+ @over = value
119
+ self.started = false
120
+ end
121
+
122
+ private
123
+
124
+ def place_enemy_ships!
125
+ ship_collection = @ship_collections[:enemy]
126
+ ship_collection.ships.values.each do |ship|
127
+ until ship.top_left_cell
128
+ random_row_index = (rand * Grid::HEIGHT).to_i
129
+ random_column_index = (rand * Grid::WIDTH).to_i
130
+ enemy_grid = @grids[:enemy]
131
+ top_left_cell = enemy_grid.cell_rows[random_row_index][random_column_index]
132
+ top_left_cell.place_ship!(ship)
133
+ begin
134
+ ship.toggle_orientation! if (rand * 2).to_i == 1
135
+ rescue => e
136
+ Glimmer::Config.logger.debug(e.full_message)
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end