Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix `draw`, which was broken when we switched @board to a hash

  • Loading branch information...
commit fff24d945b29a584ad6b113456d48a3766bc2421 1 parent e9716fd
@jaredbeck authored
Showing with 68 additions and 14 deletions.
  1. +45 −13 lib/hivegame/board.rb
  2. +23 −1 spec/board_spec.rb
View
58 lib/hivegame/board.rb
@@ -37,20 +37,24 @@ def add(point, bug)
return true
end
- # `draw` writes the board to stdout
- def draw
- @rows.times do |row|
- line = "%03d:" % row
- (@cols - row).times {line << ' '}
-
- @cols.times do |col|
- line << (@board[row][col].bug || '.').to_s
- line << ' '
- end
+ # `to_ascii` returns a textual representation (aka. ascii art)
+ def to_ascii
+ lines = []
+ cols = col_count
+ min_row.upto(max_row) do |row|
+ line = "%03d:" % row # left-padded row number
+ (cols - row).times {line << ' '}
+
+ min_col.upto(max_col) do |col|
+ line << (hex([row,col,0]).bug || '.').to_s
+ line << ' '
+ end
+
+ lines << line
+ end
- puts line
- end
- end
+ lines.join "\n"
+ end
# `each` enumerates occupied hexes
def each
@@ -85,6 +89,14 @@ def occupied_hexes
private
+ def col_count
+ max_col - min_col
+ end
+
+ def col_numbers
+ @board.map{|k,v| k[1]}
+ end
+
# `supported_point?` returns true if `point` is resting on
# the table or if the hex below `point` is occupied.
def supported_point? point
@@ -92,5 +104,25 @@ def supported_point? point
h == 0 || hex([r,c,h-1]).occupied?
end
+ def min_row
+ row_numbers.min
+ end
+
+ def max_row
+ row_numbers.max
+ end
+
+ def min_col
+ col_numbers.min
+ end
+
+ def max_col
+ col_numbers.max
+ end
+
+ def row_numbers
+ @board.map{|k,v| k[0]}
+ end
+
end
end
View
24 spec/board_spec.rb
@@ -1,6 +1,7 @@
require "spec_helper"
describe Hivegame::Board do
+ let(:origin) { [0,0,0] }
context "when initialized" do
it("is empty") { should be_empty }
@@ -9,7 +10,6 @@
describe "#add" do
let(:bug) { double("Bug") }
- let(:origin) { [0,0,0] }
it "adds a piece to the given position" do
subject.add(origin, bug).should be_true
end
@@ -29,6 +29,28 @@
end
end
+ describe "#to_ascii" do
+ context "empty board" do
+ it "should output only spaces and dots" do
+ board = remove_line_nums(subject.to_ascii)
+ distincts = Set.new(board.split('')).to_a
+ distincts.should =~ " .".split('')
+ end
+ end
+
+ it "shows bugs instead of a dot" do
+ bug = 'X'
+ subject.add([42,42,0], bug)
+ board = remove_line_nums(subject.to_ascii)
+ distincts = Set.new(board.split('')).to_a
+ distincts.should =~ "X .\n".split('')
+ end
+
+ def remove_line_nums ascii_board
+ ascii_board.split("\n").map{|n| n.sub(/^\d{3}\:/, '')}.join("\n")
+ end
+ end
+
describe "#neighbors" do
it "returns eight for any point" do
r = rand(10000)
Please sign in to comment.
Something went wrong with that request. Please try again.