aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2015-04-14 17:27:31 -0400
committerTeddy Wing2015-04-14 17:27:31 -0400
commiteba2547a194b801596b43f6ba37b7d1b867a321d (patch)
treee0a845af0cac64fa3e17752cf14a5cbeb988825e
parent1863abc09ec0af8a0026d4c170bfd50b11032b76 (diff)
downloadtic-tac-toe-eba2547a194b801596b43f6ba37b7d1b867a321d.tar.bz2
Board: Method to unintelligently convert string coordinates to array
Take string coordinates in the form ":integer:,:integer:" and convert them to [integer, integer]. We'll be using these converted coordinate values to place a piece on the board.
-rw-r--r--board.rb13
-rw-r--r--spec/board_spec.rb13
2 files changed, 26 insertions, 0 deletions
diff --git a/board.rb b/board.rb
index 93b726a..c990218 100644
--- a/board.rb
+++ b/board.rb
@@ -10,4 +10,17 @@ class Board
def render
"...\n" * 3
end
+
+ # Raises an ArgumentError if integer conversion fails
+ def transform_coordinates(str)
+ coordinates = str.split(',')
+
+ begin
+ coordinates[0] = Integer(coordinates[0])
+ coordinates[1] = Integer(coordinates[1])
+
+ coordinates if coordinates.length > 1
+ rescue
+ end
+ end
end
diff --git a/spec/board_spec.rb b/spec/board_spec.rb
index 86b4276..8d2b8da 100644
--- a/spec/board_spec.rb
+++ b/spec/board_spec.rb
@@ -22,4 +22,17 @@ EOF
]
end
end
+
+ describe '#transform_coordinates' do
+ it 'converts string coordinates to an array' do
+ @board.transform_coordinates('0,4').must_equal [0, 4]
+ end
+
+ it "returns nil if coordinates don't match the format" do
+ @board.transform_coordinates('4').must_be_nil
+ @board.transform_coordinates('4 2').must_be_nil
+ @board.transform_coordinates('booyakacha').must_be_nil
+ @board.transform_coordinates('booya,kacha').must_be_nil
+ end
+ end
end