aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2015-04-14 18:40:47 -0400
committerTeddy Wing2015-04-14 18:40:47 -0400
commitda8707f50f40b325df86efaa26f821142c89662c (patch)
tree828b54e083323275af828d9377a9ded23408b2a5
parent07885399cb161c38244a9c8a564939c7b98bf4ba (diff)
downloadtic-tac-toe-da8707f50f40b325df86efaa26f821142c89662c.tar.bz2
Flesh out the game loop
* Use a new, currently empty `Board#winner?` method as our loop condition * Add `Player#insignia` so we can print the correct player insignia depending on who the current player is * Print the board and a prompt for coordinates * Switch the current player after a player finishes moving
-rw-r--r--board.rb5
-rw-r--r--main.rb10
-rw-r--r--player.rb2
-rw-r--r--spec/board_spec.rb3
-rw-r--r--spec/player_spec.rb8
5 files changed, 27 insertions, 1 deletions
diff --git a/board.rb b/board.rb
index 5db93fd..df66a8d 100644
--- a/board.rb
+++ b/board.rb
@@ -1,4 +1,6 @@
class Board
+ attr_accessor :current_player
+
def initialize
@board = [
['.', '.', '.'],
@@ -29,4 +31,7 @@ class Board
def update_cell(row_index, column_index, value)
@board[row_index][column_index] = value
end
+
+ def winner?
+ end
end
diff --git a/main.rb b/main.rb
index dedf9f7..4a3c10f 100644
--- a/main.rb
+++ b/main.rb
@@ -6,9 +6,17 @@ player_1 = Player.new(Player::INSIGNIAS[:x], board)
player_2 = Player.new(Player::INSIGNIAS[:o], board)
board.current_player = player_1
-until board.winner
+until board.winner?
+ puts board.render
+ puts
+
+ print "Player #{board.current_player.insignia} move - " \
+ "Enter coordinates (e.g. 0,2): "
+
coordinates = gets.chomp
coordinates = board.transform_coordinates(coordinates)
board.current_player.move(coordinates)
+
+ board.current_player = board.current_player == player_1 ? player_2 : player_1
end
diff --git a/player.rb b/player.rb
index 8146220..07301f8 100644
--- a/player.rb
+++ b/player.rb
@@ -4,6 +4,8 @@ class Player
:o => 'O'
}
+ attr_reader :insignia
+
def initialize(insignia, board)
@insignia = insignia
@board = board
diff --git a/spec/board_spec.rb b/spec/board_spec.rb
index ae1bdd0..09f50e1 100644
--- a/spec/board_spec.rb
+++ b/spec/board_spec.rb
@@ -57,4 +57,7 @@ EOF
@board.instance_variable_get(:@board)[1][2].must_equal value
end
end
+
+ describe '#winner?' do
+ end
end
diff --git a/spec/player_spec.rb b/spec/player_spec.rb
index d33ac05..11bfd35 100644
--- a/spec/player_spec.rb
+++ b/spec/player_spec.rb
@@ -13,6 +13,14 @@ describe Player do
end
end
+ describe '#insignia' do
+ it 'must be the correct insignia' do
+ insignia = Player::INSIGNIAS[:o]
+ player = Player.new(insignia, Board.new)
+ player.insignia.must_equal insignia
+ end
+ end
+
describe '#move' do
before do
@board = Board.new