boardgame 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +35 -0
- data/boardgame.gemspec +15 -0
- data/coverage/.last_run.json +1 -1
- data/coverage/.resultset.json +1 -1
- data/coverage/index.html +617 -151
- data/lib/boardgame.rb +5 -0
- data/lib/{map.rb → boardgame/map.rb} +3 -3
- data/lib/{piece.rb → boardgame/piece.rb} +2 -2
- data/lib/{tile.rb → boardgame/tile.rb} +1 -1
- data/readme.md +36 -0
- data/test/test_helper.rb +1 -1
- data/test/test_map.rb +18 -12
- data/test/test_piece.rb +24 -0
- data/test/test_tile.rb +45 -4
- metadata +10 -6
- data/lib/game.rb +0 -3
data/lib/boardgame.rb
ADDED
@@ -4,7 +4,7 @@
|
|
4
4
|
#| 1..#. | with a piece
|
5
5
|
#| Y | placed in (2, 1)
|
6
6
|
#`---------'
|
7
|
-
class Map
|
7
|
+
class BoardGame::Map
|
8
8
|
|
9
9
|
attr_reader :tiles, :height, :width
|
10
10
|
|
@@ -22,7 +22,7 @@ class Map
|
|
22
22
|
# Sets a tile for a given X/Y coord.
|
23
23
|
def []=(x, y, tile)
|
24
24
|
validate_coords(x, y)
|
25
|
-
raise 'needs to be a tile' unless tile.is_a? Tile
|
25
|
+
raise 'needs to be a tile' unless tile.is_a? BoardGame::Tile
|
26
26
|
@tiles[x][y] = tile.move_to(x, y, self)
|
27
27
|
end
|
28
28
|
|
@@ -49,7 +49,7 @@ private
|
|
49
49
|
0.upto(max_x) do |wd|
|
50
50
|
@tiles[wd] = {}
|
51
51
|
0.upto(max_y) do |ht|
|
52
|
-
self[wd, ht] = Tile.new({})
|
52
|
+
self[wd, ht] = BoardGame::Tile.new({})
|
53
53
|
end
|
54
54
|
end
|
55
55
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# A piece on the board.
|
2
|
-
class Piece
|
2
|
+
class BoardGame::Piece
|
3
3
|
attr_accessor :tile
|
4
4
|
|
5
5
|
def move_to(new_tile)
|
6
|
-
raise 'that isnt a tile' unless new_tile.is_a? Tile
|
6
|
+
raise 'that isnt a tile' unless new_tile.is_a? BoardGame::Tile
|
7
7
|
@tile.remove self if @tile
|
8
8
|
@tile = new_tile
|
9
9
|
@tile << self
|
data/readme.md
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Ruby 'boardgame' Gem
|
2
|
+
|
3
|
+
This library provides three classes for controlling logic in board games.
|
4
|
+
|
5
|
+
* Create, move and track game pieces across tiles on a board
|
6
|
+
* Automatic validation and boundary checking
|
7
|
+
|
8
|
+
# Installation
|
9
|
+
|
10
|
+
1. `gem install boardgame`
|
11
|
+
2. `require 'boardgame'`
|
12
|
+
3. See usage instructions below
|
13
|
+
|
14
|
+
##Creating boards, pieces and tiles
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
require 'boardgame'
|
18
|
+
# Create a 5x5 map.
|
19
|
+
map = BoardGame::Map.new(5, 5)
|
20
|
+
|
21
|
+
# Get tiles
|
22
|
+
home_tile = map[2, 2]
|
23
|
+
# => #<BoardGame::Tile 0x123>
|
24
|
+
|
25
|
+
# Make a game piece
|
26
|
+
knight = BoardGame::Piece.new
|
27
|
+
|
28
|
+
#Put a piece on the board
|
29
|
+
home_tile << knight
|
30
|
+
|
31
|
+
#Move the game piece
|
32
|
+
knight.move_to map[0, 0]
|
33
|
+
|
34
|
+
#remove from the board
|
35
|
+
home_tile.remove(knight)
|
36
|
+
```
|
data/test/test_helper.rb
CHANGED
data/test/test_map.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
|
1
|
+
require_relative "test_helper"
|
2
2
|
|
3
3
|
class TestMap < Minitest::Test
|
4
4
|
|
5
5
|
def map
|
6
|
-
@map ||= Map.new(4, 4)
|
6
|
+
@map ||= BoardGame::Map.new(4, 4)
|
7
7
|
end
|
8
8
|
|
9
9
|
def test_new
|
@@ -25,34 +25,34 @@ class TestMap < Minitest::Test
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_set_tile
|
28
|
-
map[1, 2] = Tile.new()
|
29
|
-
assert map[1,2].is_a?(Tile),
|
28
|
+
map[1, 2] = BoardGame::Tile.new()
|
29
|
+
assert map[1,2].is_a?(BoardGame::Tile),
|
30
30
|
"Could not set tile on map."
|
31
31
|
assert_raises RuntimeError do
|
32
32
|
map[1, 2] = "Not a tile"
|
33
33
|
end
|
34
34
|
assert_raises RuntimeError do
|
35
|
-
map[11, 4] = Tile.new
|
35
|
+
map[11, 4] = BoardGame::Tile.new
|
36
36
|
end
|
37
37
|
assert_raises RuntimeError do
|
38
|
-
map[4, 11] = Tile.new
|
38
|
+
map[4, 11] = BoardGame::Tile.new
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
42
|
def test_get_tile
|
43
|
-
assert map[0, 1].is_a?(Tile),
|
43
|
+
assert map[0, 1].is_a?(BoardGame::Tile),
|
44
44
|
"Lookups of tiles should return Tile."
|
45
|
-
map[1, 2] = Tile.new
|
46
|
-
assert map[1,2].is_a?(Tile),
|
45
|
+
map[1, 2] = BoardGame::Tile.new
|
46
|
+
assert map[1,2].is_a?(BoardGame::Tile),
|
47
47
|
"Could not set tile on map."
|
48
48
|
assert_equal map[0,0].map, map,
|
49
49
|
"Tile did not know where that it was owned by map `map`."
|
50
50
|
end
|
51
51
|
|
52
52
|
def test_inspect
|
53
|
-
map[0, 0] = Tile.new
|
54
|
-
map[3, 3] = Tile.new
|
55
|
-
map[2, 1]
|
53
|
+
map[0, 0] = BoardGame::Tile.new
|
54
|
+
map[3, 3] = BoardGame::Tile.new
|
55
|
+
map[2, 1] << BoardGame::Piece.new
|
56
56
|
expectation = "\n"\
|
57
57
|
"□□□□\n"\
|
58
58
|
"□□+□\n"\
|
@@ -61,4 +61,10 @@ class TestMap < Minitest::Test
|
|
61
61
|
assert_equal map.inspect, expectation,
|
62
62
|
"Maps dont look right in console."
|
63
63
|
end
|
64
|
+
|
65
|
+
def test_validate_height
|
66
|
+
assert_raises RuntimeError do
|
67
|
+
BoardGame::Map.new "1", 1
|
68
|
+
end
|
69
|
+
end
|
64
70
|
end
|
data/test/test_piece.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class TestPiece < Minitest::Test
|
4
|
+
def setup
|
5
|
+
@map = BoardGame::Map.new(5, 5)
|
6
|
+
@piece = BoardGame::Piece.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_inspect
|
10
|
+
assert @piece.inspect == "+"
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_move_to
|
14
|
+
assert_raises RuntimeError do
|
15
|
+
@piece.move_to [2, 2]
|
16
|
+
end
|
17
|
+
result = @piece.move_to @map[2,2]
|
18
|
+
assert @piece.tile == @map[2, 2]
|
19
|
+
assert @map[2,2].content.first == @piece
|
20
|
+
assert result == @piece
|
21
|
+
@piece.move_to @map[2,2]
|
22
|
+
assert @piece.tile == @map[2,2]
|
23
|
+
end
|
24
|
+
end
|
data/test/test_tile.rb
CHANGED
@@ -1,16 +1,57 @@
|
|
1
|
-
|
1
|
+
require_relative "test_helper"
|
2
2
|
|
3
3
|
class TestTile < Minitest::Test
|
4
4
|
def test_new
|
5
|
-
assert Tile.new.is_a?(Tile),
|
5
|
+
assert BoardGame::Tile.new.is_a?(BoardGame::Tile),
|
6
6
|
"Expected new tile to be."
|
7
7
|
end
|
8
8
|
|
9
9
|
def test_insertion
|
10
|
-
tile = Tile.new
|
11
|
-
piece = Piece.new
|
10
|
+
tile = BoardGame::Tile.new
|
11
|
+
piece = BoardGame::Piece.new
|
12
12
|
tile << piece
|
13
13
|
assert tile.content.first == piece,
|
14
14
|
"Did not put piece on tile."
|
15
15
|
end
|
16
|
+
|
17
|
+
def test_removal
|
18
|
+
tile = BoardGame::Tile.new
|
19
|
+
piece = BoardGame::Piece.new
|
20
|
+
tile << piece
|
21
|
+
assert tile.content.first == piece,
|
22
|
+
"Did not put piece on tile."
|
23
|
+
tile.remove(piece)
|
24
|
+
assert tile.content.length == 0,
|
25
|
+
"Unable to remove pieces from tiles."
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_inspect
|
29
|
+
tile = BoardGame::Tile.new
|
30
|
+
assert tile.inspect == "□",
|
31
|
+
"Expected empty tile to show □"
|
32
|
+
piece = BoardGame::Piece.new
|
33
|
+
tile << piece
|
34
|
+
assert tile.inspect == piece.inspect,
|
35
|
+
"Expected occupied tiles to show top piece when inspected."
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_move_to
|
39
|
+
map = BoardGame::Map.new(2, 2)
|
40
|
+
tile = map[0, 0]
|
41
|
+
piece = BoardGame::Piece.new
|
42
|
+
tile << piece
|
43
|
+
assert piece.tile == tile
|
44
|
+
assert tile.x == 0
|
45
|
+
assert tile.y == 0
|
46
|
+
assert tile.map == map
|
47
|
+
new_map = BoardGame::Map.new(2, 2)
|
48
|
+
tile.move_to(1, 1, new_map)
|
49
|
+
assert piece.tile == tile
|
50
|
+
assert tile.x == 1
|
51
|
+
assert tile.y == 1
|
52
|
+
assert tile.map == new_map
|
53
|
+
assert_raises RuntimeError do
|
54
|
+
tile.move_to(2, 2)
|
55
|
+
end
|
56
|
+
end
|
16
57
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: boardgame
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rick Carlino
|
@@ -17,7 +17,9 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- .DS_Store
|
20
|
+
- .gitignore
|
20
21
|
- Rakefile
|
22
|
+
- boardgame.gemspec
|
21
23
|
- coverage/.last_run.json
|
22
24
|
- coverage/.resultset.json
|
23
25
|
- coverage/assets/0.8.0/application.css
|
@@ -45,13 +47,14 @@ files:
|
|
45
47
|
- coverage/assets/0.8.0/smoothness/images/ui-icons_888888_256x240.png
|
46
48
|
- coverage/assets/0.8.0/smoothness/images/ui-icons_cd0a0a_256x240.png
|
47
49
|
- coverage/index.html
|
48
|
-
- lib/
|
49
|
-
- lib/map.rb
|
50
|
-
- lib/piece.rb
|
51
|
-
- lib/tile.rb
|
50
|
+
- lib/boardgame.rb
|
51
|
+
- lib/boardgame/map.rb
|
52
|
+
- lib/boardgame/piece.rb
|
53
|
+
- lib/boardgame/tile.rb
|
52
54
|
- readme.md
|
53
55
|
- test/test_helper.rb
|
54
56
|
- test/test_map.rb
|
57
|
+
- test/test_piece.rb
|
55
58
|
- test/test_tile.rb
|
56
59
|
homepage: http://github.com/rickcarlino/shiny-octo-nemesis
|
57
60
|
licenses:
|
@@ -76,8 +79,9 @@ rubyforge_project:
|
|
76
79
|
rubygems_version: 2.2.2
|
77
80
|
signing_key:
|
78
81
|
specification_version: 4
|
79
|
-
summary:
|
82
|
+
summary: Control logic for board and tile based games using Ruby.
|
80
83
|
test_files:
|
81
84
|
- test/test_helper.rb
|
82
85
|
- test/test_map.rb
|
86
|
+
- test/test_piece.rb
|
83
87
|
- test/test_tile.rb
|
data/lib/game.rb
DELETED