glimmer-dsl-swt 4.20.13.14 → 4.20.13.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +5 -5
- data/VERSION +1 -1
- data/docs/reference/GLIMMER_SAMPLES.md +32 -2
- data/glimmer-dsl-swt.gemspec +0 -0
- data/samples/elaborate/battleship.rb +79 -0
- data/samples/elaborate/battleship/model/cell.rb +84 -0
- data/samples/elaborate/battleship/model/game.rb +143 -0
- data/samples/elaborate/battleship/model/grid.rb +54 -0
- data/samples/elaborate/battleship/model/ship.rb +114 -0
- data/samples/elaborate/battleship/model/ship_collection.rb +56 -0
- data/samples/elaborate/battleship/view/action_panel.rb +59 -0
- data/samples/elaborate/battleship/view/cell.rb +163 -0
- data/samples/elaborate/battleship/view/grid.rb +77 -0
- data/samples/elaborate/battleship/view/ship.rb +65 -0
- data/samples/elaborate/battleship/view/ship_collection.rb +49 -0
- data/samples/elaborate/connect4.rb +4 -6
- data/samples/elaborate/connect4/model/grid.rb +11 -10
- data/samples/elaborate/klondike_solitaire/view/column_pile.rb +0 -1
- data/samples/elaborate/klondike_solitaire/view/foundation_pile.rb +0 -2
- metadata +13 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6f16ea275e3ce37889ed445d6cc3ab42bbcb7068eb1e57f51017d1625ac2582
|
4
|
+
data.tar.gz: d5dc4c387caa917f1c20db29cbf27c06148736b52bc7bd4cfb63d75854e866f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e9ab4f42471a424a335d5f39a395639601b88c4694eeb016f4d5b3b2449114eea07e3395058892b22b1c4e5b9b11c1e1b3e24a0fc7b687393cfb8218c548142
|
7
|
+
data.tar.gz: 472f326d596fbcb2e551549bb6bb6411839acb86437ad789e4ecc29841facbd2a27c32a696ac4c2a2c68d8b754517a35fff7570a05441c18db5033f1774c671b
|
data/CHANGELOG.md
CHANGED
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.
|
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.15
|
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.
|
18
|
+
[Glimmer DSL for SWT](https://rubygems.org/gems/glimmer-dsl-swt) 4.20.13.15 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).
|
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
|
|
@@ -326,7 +326,7 @@ jgem install glimmer-dsl-swt
|
|
326
326
|
|
327
327
|
Or this command if you want a specific version:
|
328
328
|
```
|
329
|
-
jgem install glimmer-dsl-swt -v 4.20.13.
|
329
|
+
jgem install glimmer-dsl-swt -v 4.20.13.15
|
330
330
|
```
|
331
331
|
|
332
332
|
`jgem` is JRuby's version of `gem` command.
|
@@ -354,7 +354,7 @@ Note: if you're using activerecord or activesupport, keep in mind that Glimmer u
|
|
354
354
|
|
355
355
|
Add the following to `Gemfile`:
|
356
356
|
```
|
357
|
-
gem 'glimmer-dsl-swt', '~> 4.20.13.
|
357
|
+
gem 'glimmer-dsl-swt', '~> 4.20.13.15'
|
358
358
|
```
|
359
359
|
|
360
360
|
And, then run:
|
@@ -375,7 +375,7 @@ glimmer
|
|
375
375
|
```
|
376
376
|
|
377
377
|
```
|
378
|
-
Glimmer (JRuby Desktop Development GUI Framework) - JRuby Gem: glimmer-dsl-swt v4.20.13.
|
378
|
+
Glimmer (JRuby Desktop Development GUI Framework) - JRuby Gem: glimmer-dsl-swt v4.20.13.15
|
379
379
|
|
380
380
|
Usage: glimmer [--bundler] [--pd] [--quiet] [--debug] [--log-level=VALUE] [[ENV_VAR=VALUE]...] [[-jruby-option]...] (application.rb or task[task_args]) [[application2.rb]...]
|
381
381
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
4.20.13.
|
1
|
+
4.20.13.15
|
@@ -65,6 +65,7 @@
|
|
65
65
|
- [Game of Life](#game-of-life)
|
66
66
|
- [Glimmer Tetris](#glimmer-tetris)
|
67
67
|
- [Klondike Solitaire](#klondike-solitaire)
|
68
|
+
- [Battleship](#battleship)
|
68
69
|
- [Mandelbrot Fractal](#mandelbrot-fractal)
|
69
70
|
- [Parking](#parking)
|
70
71
|
- [Stock Ticker](#stock-ticker)
|
@@ -74,6 +75,7 @@
|
|
74
75
|
- [Glimmer Calculator](#glimmer-calculator)
|
75
76
|
- [Gladiator](#gladiator)
|
76
77
|
- [Timer](#timer)
|
78
|
+
- [Glimmer Klondike Solitaire](#glimmer-klondike-solitaire)
|
77
79
|
- [License](#license)
|
78
80
|
|
79
81
|
## Samples
|
@@ -1066,7 +1068,7 @@ Connect 4 - Dropped Coin
|
|
1066
1068
|
|
1067
1069
|
![Connect 4 Dropped Coin](/images/glimmer-connect4-dropped-coin.png)
|
1068
1070
|
|
1069
|
-
Connect 4 - Player 1 Wins
|
1071
|
+
Connect 4 - Player 1 Wins (keeps the coin about to drop visual cue)
|
1070
1072
|
|
1071
1073
|
![Connect 4 Player 1 Wins](/images/glimmer-connect4-player1-wins.png)
|
1072
1074
|
|
@@ -1142,7 +1144,7 @@ Code:
|
|
1142
1144
|
|
1143
1145
|
#### Klondike Solitaire
|
1144
1146
|
|
1145
|
-
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.
|
1146
1148
|
|
1147
1149
|
Code:
|
1148
1150
|
|
@@ -1152,6 +1154,26 @@ Code:
|
|
1152
1154
|
|
1153
1155
|
![Klondike Solitaire Played](/images/glimmer-klondike-solitaire-played.png)
|
1154
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
|
+
|
1155
1177
|
#### Mandelbrot Fractal
|
1156
1178
|
|
1157
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.
|
@@ -1275,6 +1297,14 @@ Gladiator is a good demonstration of:
|
|
1275
1297
|
|
1276
1298
|
[<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer-cs-timer/master/glimmer-timer-screenshot.png" />](https://github.com/AndyObtiva/glimmer-cs-timer)
|
1277
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
|
+
|
1278
1308
|
## License
|
1279
1309
|
|
1280
1310
|
[MIT](LICENSE.txt)
|
data/glimmer-dsl-swt.gemspec
CHANGED
Binary file
|
@@ -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
|
@@ -0,0 +1,54 @@
|
|
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 Battleship
|
25
|
+
module Model
|
26
|
+
class Grid
|
27
|
+
HEIGHT = 10
|
28
|
+
WIDTH = 10
|
29
|
+
ROW_ALPHABETS = %w[A B C D E F G H I J]
|
30
|
+
|
31
|
+
attr_reader :game, :player, :cell_rows
|
32
|
+
|
33
|
+
def initialize(game, player)
|
34
|
+
@game = game
|
35
|
+
@player = player
|
36
|
+
build
|
37
|
+
end
|
38
|
+
|
39
|
+
def reset!
|
40
|
+
@cell_rows.flatten.each(&:reset!)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def build
|
46
|
+
@cell_rows = HEIGHT.times.map do |row_index|
|
47
|
+
WIDTH.times.map do |column_index|
|
48
|
+
Cell.new(self, row_index, column_index)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,114 @@
|
|
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 Battleship
|
25
|
+
module Model
|
26
|
+
class Ship
|
27
|
+
ORIENTATIONS = [:horizontal, :vertical]
|
28
|
+
|
29
|
+
attr_reader :ship_collection, :name, :length
|
30
|
+
attr_accessor :orientation, :top_left_cell, :sunk
|
31
|
+
alias sunk? sunk
|
32
|
+
|
33
|
+
def initialize(ship_collection, name, length)
|
34
|
+
@ship_collection = ship_collection
|
35
|
+
@name = name
|
36
|
+
@length = length
|
37
|
+
@orientation = :horizontal
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_s
|
41
|
+
"#{name.titlecase} (#{length} cells)"
|
42
|
+
end
|
43
|
+
|
44
|
+
def top_left_cell=(a_cell)
|
45
|
+
if a_cell
|
46
|
+
raise "Top left cell #{a_cell} already occupied by ship #{a_cell.ship.name}" if a_cell.ship && a_cell.ship != self
|
47
|
+
raise "Top left cell #{a_cell} cannot fit ship #{name}" if a_cell.column_index + length > Grid::WIDTH
|
48
|
+
grid = ship_collection.game.grids[ship_collection.player]
|
49
|
+
1.upto(length - 1).each do |index|
|
50
|
+
if orientation == :horizontal
|
51
|
+
other_cell = grid.cell_rows[a_cell.row_index][a_cell.column_index + index]
|
52
|
+
else
|
53
|
+
other_cell = grid.cell_rows[a_cell.row_index + index][a_cell.column_index]
|
54
|
+
end
|
55
|
+
raise "Cell #{other_cell} already occupied by ship #{other_cell.ship.name}" if other_cell.ship && other_cell.ship != self
|
56
|
+
end
|
57
|
+
end
|
58
|
+
@top_left_cell = a_cell
|
59
|
+
end
|
60
|
+
|
61
|
+
def orientation=(value)
|
62
|
+
if top_left_cell
|
63
|
+
if value == :horizontal
|
64
|
+
if top_left_cell.column_index + length > Grid::WIDTH
|
65
|
+
raise "Top left cell #{top_left_cell} cannot fit ship #{name}"
|
66
|
+
end
|
67
|
+
else
|
68
|
+
if top_left_cell.row_index + length > Grid::HEIGHT
|
69
|
+
raise "Top left cell #{top_left_cell} cannot fit ship #{name}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
if cells(value)[1..-1].map(&:ship).reject {|s| s == self}.any?
|
73
|
+
raise 'Orientation #{value} cells occupied by another ship'
|
74
|
+
end
|
75
|
+
if value != @orientation
|
76
|
+
cells.each(&:reset!)
|
77
|
+
@orientation = value
|
78
|
+
cells.each_with_index do |cell, index|
|
79
|
+
cell.ship = self
|
80
|
+
cell.ship_index = index
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def cells(for_orientation = nil)
|
87
|
+
for_orientation ||= orientation
|
88
|
+
if top_left_cell
|
89
|
+
length.times.map do |index|
|
90
|
+
if for_orientation == :horizontal
|
91
|
+
top_left_cell.grid.cell_rows[top_left_cell.row_index][top_left_cell.column_index + index]
|
92
|
+
else
|
93
|
+
top_left_cell.grid.cell_rows[top_left_cell.row_index + index][top_left_cell.column_index]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def all_cells_hit?
|
100
|
+
cells&.map(&:hit)&.all? {|h| h == true}
|
101
|
+
end
|
102
|
+
|
103
|
+
def reset!
|
104
|
+
self.sunk = nil
|
105
|
+
self.top_left_cell = nil
|
106
|
+
self.orientation = :horizontal
|
107
|
+
end
|
108
|
+
|
109
|
+
def toggle_orientation!
|
110
|
+
self.orientation = ORIENTATIONS[(ORIENTATIONS.index(orientation) + 1) % ORIENTATIONS.length]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,56 @@
|
|
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 'ship'
|
23
|
+
|
24
|
+
class Battleship
|
25
|
+
module Model
|
26
|
+
class ShipCollection
|
27
|
+
BATTLESHIPS = {
|
28
|
+
aircraft_carrier: 5,
|
29
|
+
battleship: 4,
|
30
|
+
submarine: 3,
|
31
|
+
cruiser: 3,
|
32
|
+
destroyer: 2
|
33
|
+
}
|
34
|
+
|
35
|
+
attr_reader :game, :player, :ships
|
36
|
+
attr_accessor :placed_count
|
37
|
+
|
38
|
+
def initialize(game, player)
|
39
|
+
@game = game
|
40
|
+
@player = player
|
41
|
+
@ships = BATTLESHIPS.reduce({}) do |hash, pair|
|
42
|
+
name, width = pair
|
43
|
+
ship = Ship.new(self, name, width)
|
44
|
+
Glimmer::DataBinding::Observer.proc do |cell_value|
|
45
|
+
self.placed_count = ships.values.map(&:top_left_cell).compact.size
|
46
|
+
end.observe(ship, :top_left_cell)
|
47
|
+
hash.merge(name => ship)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def reset!
|
52
|
+
ships.values.each(&:reset!)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,59 @@
|
|
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 View
|
24
|
+
class ActionPanel
|
25
|
+
include Glimmer::UI::CustomWidget
|
26
|
+
|
27
|
+
options :game
|
28
|
+
|
29
|
+
body {
|
30
|
+
composite {
|
31
|
+
row_layout(:horizontal) {
|
32
|
+
margin_width 0
|
33
|
+
margin_height 0
|
34
|
+
}
|
35
|
+
|
36
|
+
layout_data(:center, :center, true, false)
|
37
|
+
|
38
|
+
@battle_button = button {
|
39
|
+
text 'Battle!'
|
40
|
+
enabled <= [game.ship_collections[:you], :placed_count, on_read: ->(c) {c == 5}]
|
41
|
+
|
42
|
+
on_widget_selected do
|
43
|
+
game.battle!
|
44
|
+
@battle_button.enabled = false
|
45
|
+
end
|
46
|
+
}
|
47
|
+
|
48
|
+
button {
|
49
|
+
text 'Restart'
|
50
|
+
|
51
|
+
on_widget_selected do
|
52
|
+
game.reset!
|
53
|
+
end
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,163 @@
|
|
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 View
|
24
|
+
class Cell
|
25
|
+
include Glimmer::UI::CustomWidget
|
26
|
+
|
27
|
+
class << self
|
28
|
+
attr_accessor :dragging
|
29
|
+
alias dragging? dragging
|
30
|
+
end
|
31
|
+
|
32
|
+
COLOR_WATER = rgb(156, 211, 219)
|
33
|
+
COLOR_SHIP = :gray
|
34
|
+
COLOR_PLACED = :white
|
35
|
+
COLOR_EMPTY = :black
|
36
|
+
COLOR_NO_HIT = :white
|
37
|
+
COLOR_HIT = :red
|
38
|
+
|
39
|
+
options :game, :player, :row_index, :column_index, :ship
|
40
|
+
option :type, default: :grid # other type is :ship
|
41
|
+
|
42
|
+
body {
|
43
|
+
canvas {
|
44
|
+
if type == :grid
|
45
|
+
if player == :you
|
46
|
+
background <= [model, :ship, on_read: ->(s) {s ? COLOR_SHIP : COLOR_WATER}]
|
47
|
+
else
|
48
|
+
background COLOR_WATER
|
49
|
+
end
|
50
|
+
else
|
51
|
+
background <= [ship, :sunk, on_read: ->(s) {s ? COLOR_HIT : COLOR_PLACED}]
|
52
|
+
background <= [ship, :top_left_cell, on_read: ->(c) {c ? COLOR_PLACED : COLOR_SHIP}]
|
53
|
+
end
|
54
|
+
|
55
|
+
rectangle(0, 0, [:max, -1], [:max, -1])
|
56
|
+
oval(:default, :default, 10, 10) {
|
57
|
+
foreground <= [model, :hit, on_read: ->(h) {h == nil ? COLOR_EMPTY : (h ? COLOR_HIT : COLOR_NO_HIT)}]
|
58
|
+
}
|
59
|
+
oval(:default, :default, 5, 5) {
|
60
|
+
background <= [model, :hit, on_read: ->(h) {h == nil ? COLOR_EMPTY : (h ? COLOR_HIT : COLOR_NO_HIT)}]
|
61
|
+
}
|
62
|
+
|
63
|
+
on_mouse_move do |event|
|
64
|
+
if game.started?
|
65
|
+
if type == :grid
|
66
|
+
if player == :enemy
|
67
|
+
body_root.cursor = :cross
|
68
|
+
else
|
69
|
+
body_root.cursor = :arrow
|
70
|
+
end
|
71
|
+
else
|
72
|
+
body_root.cursor = :arrow
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
if player == :enemy
|
78
|
+
on_mouse_up do
|
79
|
+
game.attack!(row_index, column_index)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
if player == :you
|
84
|
+
on_drag_detected do |event|
|
85
|
+
unless game.started? || game.over?
|
86
|
+
Cell.dragging = true
|
87
|
+
body_root.cursor = :hand if type == :grid
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
on_drag_set_data do |event|
|
92
|
+
the_ship = ship || model&.ship
|
93
|
+
if the_ship && !game.started? && !game.over? && !(type == :ship && the_ship.top_left_cell)
|
94
|
+
event.data = the_ship.name.to_s
|
95
|
+
else
|
96
|
+
event.doit = false
|
97
|
+
Cell.dragging = false
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
on_mouse_up do
|
102
|
+
unless game.started? || game.over?
|
103
|
+
Cell.dragging = false
|
104
|
+
change_cursor
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
if type == :grid
|
109
|
+
on_mouse_move do |event|
|
110
|
+
unless game.started? || game.over?
|
111
|
+
change_cursor
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
on_mouse_hover do |event|
|
116
|
+
unless game.started? || game.over?
|
117
|
+
change_cursor
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
on_mouse_up do |event|
|
122
|
+
unless game.started? || game.over?
|
123
|
+
begin
|
124
|
+
model.ship&.toggle_orientation!
|
125
|
+
rescue => e
|
126
|
+
Glimmer::Config.logger.debug e.full_message
|
127
|
+
end
|
128
|
+
change_cursor
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
on_drop do |event|
|
133
|
+
unless game.started? || game.over?
|
134
|
+
ship_name = event.data
|
135
|
+
place_ship(ship_name.to_s.to_sym) if ship_name
|
136
|
+
Cell.dragging = false
|
137
|
+
change_cursor
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
}
|
143
|
+
}
|
144
|
+
|
145
|
+
def model
|
146
|
+
game.grids[player].cell_rows[row_index][column_index] if type == :grid
|
147
|
+
end
|
148
|
+
|
149
|
+
def place_ship(ship_name)
|
150
|
+
ship = game.ship_collections[player].ships[ship_name]
|
151
|
+
model.place_ship!(ship)
|
152
|
+
end
|
153
|
+
|
154
|
+
def change_cursor
|
155
|
+
if type == :grid && model.ship && !Cell.dragging?
|
156
|
+
body_root.cursor = model.ship.orientation == :horizontal ? :sizens : :sizewe
|
157
|
+
elsif !Cell.dragging?
|
158
|
+
body_root.cursor = :arrow
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,77 @@
|
|
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 '../model/grid'
|
23
|
+
|
24
|
+
require_relative 'cell'
|
25
|
+
|
26
|
+
class Battleship
|
27
|
+
module View
|
28
|
+
class Grid
|
29
|
+
include Glimmer::UI::CustomWidget
|
30
|
+
|
31
|
+
options :game, :player
|
32
|
+
|
33
|
+
body {
|
34
|
+
composite {
|
35
|
+
grid_layout(Model::Grid::WIDTH + 1, true) {
|
36
|
+
margin_width 0
|
37
|
+
margin_height 0
|
38
|
+
horizontal_spacing 0
|
39
|
+
vertical_spacing 0
|
40
|
+
}
|
41
|
+
|
42
|
+
label(:center) {
|
43
|
+
layout_data(:fill, :center, true, false) {
|
44
|
+
horizontal_span (Model::Grid::WIDTH + 1)
|
45
|
+
}
|
46
|
+
|
47
|
+
text player.to_s.capitalize
|
48
|
+
font height: 20, style: :bold
|
49
|
+
}
|
50
|
+
|
51
|
+
label # filler
|
52
|
+
Model::Grid::WIDTH.times do |column_index|
|
53
|
+
label {
|
54
|
+
text (column_index + 1).to_s
|
55
|
+
font height: 16
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
Model::Grid::HEIGHT.times do |row_index|
|
60
|
+
label {
|
61
|
+
text Model::Grid::ROW_ALPHABETS[row_index]
|
62
|
+
font height: 16
|
63
|
+
}
|
64
|
+
Model::Grid::WIDTH.times do |column_index|
|
65
|
+
cell(game: game, player: player, row_index: row_index, column_index: column_index) {
|
66
|
+
layout_data {
|
67
|
+
width_hint 25
|
68
|
+
height_hint 25
|
69
|
+
}
|
70
|
+
}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
}
|
74
|
+
}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,65 @@
|
|
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 Battleship
|
25
|
+
module View
|
26
|
+
class Ship
|
27
|
+
include Glimmer::UI::CustomWidget
|
28
|
+
|
29
|
+
options :game, :player, :ship_name, :ship_length
|
30
|
+
|
31
|
+
body {
|
32
|
+
composite {
|
33
|
+
row_layout(:vertical) {
|
34
|
+
fill true
|
35
|
+
margin_width 0
|
36
|
+
margin_height 0
|
37
|
+
}
|
38
|
+
|
39
|
+
label {
|
40
|
+
text ship_name.to_s.titlecase
|
41
|
+
font height: 16
|
42
|
+
}
|
43
|
+
|
44
|
+
composite {
|
45
|
+
grid_layout(ship_length, true) {
|
46
|
+
margin_width 0
|
47
|
+
margin_height 0
|
48
|
+
horizontal_spacing 0
|
49
|
+
vertical_spacing 0
|
50
|
+
}
|
51
|
+
|
52
|
+
ship_length.times do |column_index|
|
53
|
+
cell(game: game, player: player, type: :ship, column_index: column_index, ship: game.ship_collections[player].ships[ship_name]) {
|
54
|
+
layout_data {
|
55
|
+
width_hint 25
|
56
|
+
height_hint 25
|
57
|
+
}
|
58
|
+
}
|
59
|
+
end
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,49 @@
|
|
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 '../model/game'
|
23
|
+
require_relative '../model/ship_collection'
|
24
|
+
|
25
|
+
require_relative 'ship'
|
26
|
+
|
27
|
+
class Battleship
|
28
|
+
module View
|
29
|
+
class ShipCollection
|
30
|
+
include Glimmer::UI::CustomWidget
|
31
|
+
|
32
|
+
options :game, :player
|
33
|
+
|
34
|
+
body {
|
35
|
+
composite {
|
36
|
+
row_layout(:vertical) {
|
37
|
+
fill true
|
38
|
+
margin_width 0
|
39
|
+
margin_height 0
|
40
|
+
}
|
41
|
+
|
42
|
+
Model::ShipCollection::BATTLESHIPS.each do |ship_name, ship_length|
|
43
|
+
ship(game: game, player: player, ship_name: ship_name, ship_length: ship_length)
|
44
|
+
end
|
45
|
+
}
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -26,8 +26,6 @@ require_relative 'connect4/model/grid'
|
|
26
26
|
class Connect4
|
27
27
|
include Glimmer::UI::CustomShell
|
28
28
|
|
29
|
-
WIDTH = 7
|
30
|
-
HEIGHT = 6
|
31
29
|
COLOR_BACKGROUND = rgb(63, 104, 212)
|
32
30
|
COLOR_EMPTY_SLOT = :white
|
33
31
|
COLOR_COIN1 = rgb(236, 223, 56)
|
@@ -36,7 +34,7 @@ class Connect4
|
|
36
34
|
attr_accessor :current_player_color
|
37
35
|
|
38
36
|
before_body do
|
39
|
-
@grid = Model::Grid.new
|
37
|
+
@grid = Model::Grid.new
|
40
38
|
select_player_color
|
41
39
|
end
|
42
40
|
|
@@ -68,13 +66,13 @@ class Connect4
|
|
68
66
|
|
69
67
|
body {
|
70
68
|
shell(:no_resize) {
|
71
|
-
grid_layout(
|
69
|
+
grid_layout(Model::Grid::WIDTH, true)
|
72
70
|
|
73
71
|
text 'Glimmer Connect 4'
|
74
72
|
background COLOR_BACKGROUND
|
75
73
|
|
76
|
-
HEIGHT.times do |row_index|
|
77
|
-
WIDTH.times do |column_index|
|
74
|
+
Model::Grid::HEIGHT.times do |row_index|
|
75
|
+
Model::Grid::WIDTH.times do |column_index|
|
78
76
|
canvas {
|
79
77
|
layout_data {
|
80
78
|
width_hint 50
|
@@ -24,12 +24,13 @@ require_relative 'slot'
|
|
24
24
|
class Connect4
|
25
25
|
module Model
|
26
26
|
class Grid
|
27
|
-
|
27
|
+
WIDTH = 7
|
28
|
+
HEIGHT = 6
|
29
|
+
|
30
|
+
attr_reader :slot_rows
|
28
31
|
attr_accessor :current_player, :game_over
|
29
32
|
|
30
|
-
def initialize
|
31
|
-
@width = width
|
32
|
-
@height = height
|
33
|
+
def initialize
|
33
34
|
build_slots
|
34
35
|
start!
|
35
36
|
end
|
@@ -60,8 +61,8 @@ class Connect4
|
|
60
61
|
private
|
61
62
|
|
62
63
|
def build_slots
|
63
|
-
@slot_rows =
|
64
|
-
|
64
|
+
@slot_rows = HEIGHT.times.map do |row_index|
|
65
|
+
WIDTH.times.map do |column_index|
|
65
66
|
Slot.new(self)
|
66
67
|
end
|
67
68
|
end
|
@@ -82,10 +83,10 @@ class Connect4
|
|
82
83
|
def evaluate_horizontal_win
|
83
84
|
connections = nil
|
84
85
|
last_slot_value = nil
|
85
|
-
|
86
|
+
HEIGHT.times do |row_index|
|
86
87
|
connections = nil
|
87
88
|
last_slot_value = nil
|
88
|
-
|
89
|
+
WIDTH.times do |column_index|
|
89
90
|
slot = @slot_rows[row_index][column_index]
|
90
91
|
if slot.value.to_i > 0 && slot.value == last_slot_value
|
91
92
|
connections += 1
|
@@ -103,10 +104,10 @@ class Connect4
|
|
103
104
|
def evaluate_vertical_win
|
104
105
|
connections = nil
|
105
106
|
last_slot_value = nil
|
106
|
-
|
107
|
+
WIDTH.times do |column_index|
|
107
108
|
connections = nil
|
108
109
|
last_slot_value = nil
|
109
|
-
|
110
|
+
HEIGHT.times do |row_index|
|
110
111
|
slot = @slot_rows[row_index][column_index]
|
111
112
|
if slot.value.to_i > 0 && slot.value == last_slot_value
|
112
113
|
connections += 1
|
@@ -36,7 +36,6 @@ class KlondikeSolitaire
|
|
36
36
|
|
37
37
|
on_drop do |drop_event|
|
38
38
|
begin
|
39
|
-
# TODO make sure one cannot drag a column pile of cards here
|
40
39
|
card_shape = drop_event.dragged_shape.get_data('custom_shape')
|
41
40
|
card = card_shape.model
|
42
41
|
card_parent_pile = card_shape.parent_pile
|
@@ -46,7 +45,6 @@ class KlondikeSolitaire
|
|
46
45
|
card_source_model.remove!(card)
|
47
46
|
drop_event.dragged_shape.dispose
|
48
47
|
rescue => e
|
49
|
-
# pd e
|
50
48
|
drop_event.doit = false
|
51
49
|
end
|
52
50
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: glimmer-dsl-swt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.20.13.
|
4
|
+
version: 4.20.13.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Maleh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-08-
|
11
|
+
date: 2021-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
@@ -528,6 +528,17 @@ files:
|
|
528
528
|
- lib/glimmer/ui/custom_shell.rb
|
529
529
|
- lib/glimmer/ui/custom_widget.rb
|
530
530
|
- lib/glimmer/util/proc_tracker.rb
|
531
|
+
- samples/elaborate/battleship.rb
|
532
|
+
- samples/elaborate/battleship/model/cell.rb
|
533
|
+
- samples/elaborate/battleship/model/game.rb
|
534
|
+
- samples/elaborate/battleship/model/grid.rb
|
535
|
+
- samples/elaborate/battleship/model/ship.rb
|
536
|
+
- samples/elaborate/battleship/model/ship_collection.rb
|
537
|
+
- samples/elaborate/battleship/view/action_panel.rb
|
538
|
+
- samples/elaborate/battleship/view/cell.rb
|
539
|
+
- samples/elaborate/battleship/view/grid.rb
|
540
|
+
- samples/elaborate/battleship/view/ship.rb
|
541
|
+
- samples/elaborate/battleship/view/ship_collection.rb
|
531
542
|
- samples/elaborate/calculator.rb
|
532
543
|
- samples/elaborate/calculator/model/command.rb
|
533
544
|
- samples/elaborate/calculator/model/command/all_clear.rb
|