From 6ac03890ade3cb378dbbe183ea63c93913c75779 Mon Sep 17 00:00:00 2001 From: Lisa Hamm Date: Thu, 12 Mar 2015 10:36:52 -0500 Subject: [PATCH] Added and refined tests --- lib/game_helpers.rb | 4 ++-- spec/game_helpers_spec.rb | 30 ++++++++++++++++++++--------- spec/tic_tac_toe_controller_spec.rb | 2 +- tic_tac_toe_controller.rb | 7 ++----- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/lib/game_helpers.rb b/lib/game_helpers.rb index 7051e96..e40f4ea 100644 --- a/lib/game_helpers.rb +++ b/lib/game_helpers.rb @@ -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 diff --git a/spec/game_helpers_spec.rb b/spec/game_helpers_spec.rb index e1d520a..d9fc502 100644 --- a/spec/game_helpers_spec.rb +++ b/spec/game_helpers_spec.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/spec/tic_tac_toe_controller_spec.rb b/spec/tic_tac_toe_controller_spec.rb index aaaf579..3544280 100644 --- a/spec/tic_tac_toe_controller_spec.rb +++ b/spec/tic_tac_toe_controller_spec.rb @@ -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 \ No newline at end of file diff --git a/tic_tac_toe_controller.rb b/tic_tac_toe_controller.rb index e92cda1..99d6954 100644 --- a/tic_tac_toe_controller.rb +++ b/tic_tac_toe_controller.rb @@ -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 @@ -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