Skip to content

Commit

Permalink
Added and refined tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lisahamm committed Mar 12, 2015
1 parent 714e061 commit 6ac0389
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/game_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def computer_opponent(params, game)
nil
end

def check_for_computer_turn(game)
if game.current_player_mark == session[:computer_opponent]
def check_for_computer_turn(game, computer_opponent)
if game.current_player_mark == computer_opponent
game.take_turn(game.generate_ai_move)
end
end
Expand Down
30 changes: 21 additions & 9 deletions spec/game_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
describe 'GameHelpers' do
include GameHelpers
let(:params) {{player_mark: "X", computer_opponent: "no", player_order: "first"}}
let(:game) {TicTacToe::Game.new("X", "0", "X", board=nil)}

xdescribe '#create_game' do
describe '#create_game' do
it "configures the game based on the params" do
game = create_game(params)
expect(game.player1.mark).to eq "X"
expect(game.player1).to eq "X"
end
end

describe '#player_settings' do
it "creates settings for the first and second player" do
player_settings = player_settings(params)
expect(player_settings[0].fetch(:mark)).to eq "X"
expect(player_settings[1].fetch(:mark)).to eq "O"
describe '#player_marks' do
it "creates player marks for the first and second player based on the params" do
player_marks = player_marks(params)
expect(player_marks[0]).to eq "X"
expect(player_marks[1]).to eq "O"
end
end

Expand All @@ -27,9 +28,20 @@
end

describe '#computer_opponent' do
it "provides the player number of the computer_opponent if elected in the game setup" do
expect(computer_opponent(params)).to eq nil
it "provides the mark of the computer_opponent if elected in the game setup" do
expect(computer_opponent(params, game)).to eq nil
end
end

it "executes computer's move if it is the computer's turn" do
computer_opponent = "X"
check_for_computer_turn(game, computer_opponent)
expect(game.board.empty?).to eq false
end

it "does not update the board if it is not the computer's turn" do
computer_opponent = "O"
check_for_computer_turn(game, computer_opponent)
expect(game.board.empty?).to eq true
end
end
2 changes: 1 addition & 1 deletion spec/tic_tac_toe_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def app
# post "/setup", player_mark: "X", computer_opponent: "yes"
# expect(last_response).to be_redirect

# #expect(session[:mark]).to eq "O"
#expect(session[:mark]).to eq "O"
# end
end
7 changes: 2 additions & 5 deletions tic_tac_toe_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ class TicTacToeController < Sinatra::Base
erb :index
else
game = create_game(params)

session[:computer_opponent] = computer_opponent(params, game)

check_for_computer_turn(game)

check_for_computer_turn(game, session[:computer_opponent])
session[:player1_mark] = player_marks(params)[0]
session[:player2_mark] = player_marks(params)[1]
session[:current_player_mark] = game.current_player_mark
Expand All @@ -52,7 +49,7 @@ class TicTacToeController < Sinatra::Base
redirect to('/game_over')
end

check_for_computer_turn(game)
check_for_computer_turn(game, session[:computer_opponent])

session[:moves] = game.board_to_array
session[:current_player_mark] = game.current_player_mark
Expand Down

0 comments on commit 6ac0389

Please sign in to comment.