All Files
(98.19%
covered at
12.26
hits/line)
7 files in total.
166 relevant lines.
163 lines covered and
3 lines missed
-
1
module BoardGame
-
end
-
1
require_relative 'boardgame/piece'
-
1
require_relative 'boardgame/tile'
-
1
require_relative 'boardgame/map'
-
#._________. A simple board / map of tiles
-
#| 0123 X | =============================
-
#| 0.... | Example 4X2 map,
-
#| 1..#. | with a piece
-
#| Y | placed in (2, 1)
-
#`---------'
-
1
class BoardGame::Map
-
-
1
attr_reader :tiles, :height, :width
-
-
1
def initialize(height, width)
-
10
@height, @width = height, width
-
10
validate_height
-
9
initialize_map_tiles
-
end
-
-
# Retrieves a tile for a given X/Y coord.
-
1
def [](x, y)
-
27
return @tiles[x][y] if within? x, y
-
end
-
-
# Sets a tile for a given X/Y coord.
-
1
def []=(x, y, tile)
-
145
validate_coords(x, y)
-
143
raise 'needs to be a tile' unless tile.is_a? BoardGame::Tile
-
142
@tiles[x][y] = tile.move_to(x, y, self)
-
end
-
-
# Determine if x and y values are within the limits of the map.
-
1
def within?(x, y)
-
321
(x >= 0) && (x < height) && (y >= 0) && (y < height)
-
end
-
-
1
def inspect
-
1
buffer = "\n"
-
1
0.upto(max_y) do |ht|
-
4
0.upto(max_x) do |wd|
-
16
buffer += self[wd, ht].inspect
-
end
-
4
buffer += "\n"
-
end
-
1
return buffer
-
end
-
-
1
def as_json(*args)
-
tiles.as_json
-
end
-
-
1
private
-
-
1
def initialize_map_tiles
-
9
@tiles = []
-
9
0.upto(max_x) do |wd|
-
34
@tiles[wd] = []
-
34
0.upto(max_y) do |ht|
-
138
self[wd, ht] = BoardGame::Tile.new({})
-
end
-
end
-
end
-
-
1
def max_x
-
13
width - 1
-
end
-
-
1
def max_y
-
35
height - 1
-
end
-
-
1
def validate_coords(x, y)
-
145
raise "Coords out of range for #{height}x#{width} map." unless within? x, y
-
end
-
-
1
def validate_height
-
10
unless height.is_a?(Fixnum) && width.is_a?(Fixnum)
-
1
raise 'Expected height and width to be Fixnums'
-
end
-
end
-
end
-
# A piece on the board.
-
1
class BoardGame::Piece
-
1
attr_accessor :tile
-
-
1
def move_to(new_tile)
-
3
raise 'that isnt a tile' unless new_tile.is_a? BoardGame::Tile
-
2
@tile.remove self if @tile
-
2
@tile = new_tile
-
2
@tile << self
-
2
self
-
end
-
-
1
def inspect
-
4
"+"
-
end
-
-
1
def as_json(*args)
-
{}
-
end
-
end
-
# A single tile on the map/board.
-
1
class BoardGame::Tile
-
-
1
attr_accessor :content, :x, :y, :map
-
-
1
def initialize(x: nil , y: nil, map: nil, content: [])
-
148
@content, @x, @y, @map = content, x, y, map
-
end
-
-
1
def move_to(x, y, map = @map)
-
144
if map.within? x, y
-
143
@x, @y, @map = x, y, map
-
else
-
1
raise 'invalid move'
-
end
-
143
self
-
end
-
-
1
def inspect
-
18
content.empty? ? "□" : content.last.inspect
-
end
-
-
1
def <<(piece)
-
7
piece.tile = self
-
7
@content << piece
-
end
-
-
1
def remove(piece)
-
2
piece.tile = nil
-
2
@content.delete piece
-
end
-
-
1
def as_json(*args)
-
{x: @x,
-
y: @y,
-
content: @content}
-
end
-
end
-
1
require_relative "test_helper"
-
-
1
class TestMap < Minitest::Test
-
-
1
def map
-
20
@map ||= BoardGame::Map.new(4, 4)
-
end
-
-
1
def test_new
-
1
assert map.tiles.is_a?(Array),
-
"Expected new maps to have a tiles array."
-
end
-
-
1
def test_within
-
1
assert map.within?(2, 2),
-
"Within did not identify coords within bounds"
-
1
assert map.within?(0, 0),
-
"Within did not identify coords within bounds"
-
1
refute map.within?(-1, 0),
-
"Within did not identify coords within bounds"
-
1
refute map.within?(4, 4),
-
"Within did not identify coords outside bounds"
-
1
refute map.within?(5, 5),
-
"Within did not identify coords outside bounds"
-
end
-
-
1
def test_set_tile
-
1
map[1, 2] = BoardGame::Tile.new()
-
1
assert map[1,2].is_a?(BoardGame::Tile),
-
"Could not set tile on map."
-
1
assert_raises RuntimeError do
-
1
map[1, 2] = "Not a tile"
-
end
-
1
assert_raises RuntimeError do
-
1
map[11, 4] = BoardGame::Tile.new
-
end
-
1
assert_raises RuntimeError do
-
1
map[4, 11] = BoardGame::Tile.new
-
end
-
end
-
-
1
def test_get_tile
-
1
assert map[0, 1].is_a?(BoardGame::Tile),
-
"Lookups of tiles should return Tile."
-
1
map[1, 2] = BoardGame::Tile.new
-
1
assert map[1,2].is_a?(BoardGame::Tile),
-
"Could not set tile on map."
-
1
assert_equal map[0,0].map, map,
-
"Tile did not know where that it was owned by map `map`."
-
end
-
-
1
def test_inspect
-
1
map[0, 0] = BoardGame::Tile.new
-
1
map[3, 3] = BoardGame::Tile.new
-
1
map[2, 1] << BoardGame::Piece.new
-
1
expectation = "\n"\
-
"□□□□\n"\
-
"□□+□\n"\
-
"□□□□\n"\
-
"□□□□\n"
-
1
assert_equal map.inspect, expectation,
-
"Maps dont look right in console."
-
end
-
-
1
def test_validate_height
-
1
assert_raises RuntimeError do
-
1
BoardGame::Map.new "1", 1
-
end
-
end
-
-
1
def test_serialization
-
1
binding.pry
-
end
-
end
-
1
require_relative "test_helper"
-
-
1
class TestPiece < Minitest::Test
-
1
def setup
-
2
@map = BoardGame::Map.new(5, 5)
-
2
@piece = BoardGame::Piece.new
-
end
-
-
1
def test_inspect
-
1
assert @piece.inspect == "+"
-
end
-
-
1
def test_move_to
-
1
assert_raises RuntimeError do
-
1
@piece.move_to [2, 2]
-
end
-
1
result = @piece.move_to @map[2,2]
-
1
assert @piece.tile == @map[2, 2]
-
1
assert @map[2,2].content.first == @piece
-
1
assert result == @piece
-
1
@piece.move_to @map[2,2]
-
1
assert @piece.tile == @map[2,2]
-
end
-
end
-
1
require_relative "test_helper"
-
-
1
class TestTile < Minitest::Test
-
1
def test_new
-
1
assert BoardGame::Tile.new.is_a?(BoardGame::Tile),
-
"Expected new tile to be."
-
end
-
-
1
def test_insertion
-
1
tile = BoardGame::Tile.new
-
1
piece = BoardGame::Piece.new
-
1
tile << piece
-
1
assert tile.content.first == piece,
-
"Did not put piece on tile."
-
end
-
-
1
def test_removal
-
1
tile = BoardGame::Tile.new
-
1
piece = BoardGame::Piece.new
-
1
tile << piece
-
1
assert tile.content.first == piece,
-
"Did not put piece on tile."
-
1
tile.remove(piece)
-
1
assert tile.content.length == 0,
-
"Unable to remove pieces from tiles."
-
end
-
-
1
def test_inspect
-
1
tile = BoardGame::Tile.new
-
1
assert tile.inspect == "□",
-
"Expected empty tile to show □"
-
1
piece = BoardGame::Piece.new
-
1
tile << piece
-
1
assert tile.inspect == piece.inspect,
-
"Expected occupied tiles to show top piece when inspected."
-
end
-
-
1
def test_move_to
-
1
map = BoardGame::Map.new(2, 2)
-
1
tile = map[0, 0]
-
1
piece = BoardGame::Piece.new
-
1
tile << piece
-
1
assert piece.tile == tile
-
1
assert tile.x == 0
-
1
assert tile.y == 0
-
1
assert tile.map == map
-
1
new_map = BoardGame::Map.new(2, 2)
-
1
tile.move_to(1, 1, new_map)
-
1
assert piece.tile == tile
-
1
assert tile.x == 1
-
1
assert tile.y == 1
-
1
assert tile.map == new_map
-
1
assert_raises RuntimeError do
-
1
tile.move_to(2, 2)
-
end
-
end
-
end