Skip to content

Commit

Permalink
Added #check_for_computer_turn method
Browse files Browse the repository at this point in the history
  • Loading branch information
lisahamm committed Mar 11, 2015
1 parent 513a476 commit 714e061
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
10 changes: 8 additions & 2 deletions lib/game_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ def opponent_mark(mark)
mark == "X" ? "O" : "X"
end

def computer_opponent(params)
def computer_opponent(params, game)
player_order = params[:player_order]
if params[:computer_opponent] == "yes"
return player_order == "first" ? "player2" : "player1"
return player_order == "first" ? game.player2 : game.player1
end
nil
end

def check_for_computer_turn(game)
if game.current_player_mark == session[:computer_opponent]
game.take_turn(game.generate_ai_move)
end
end

def array_to_board(array)
TicTacToe::Board.new(cells: array)
end
Expand Down
11 changes: 4 additions & 7 deletions tic_tac_toe_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ class TicTacToeController < Sinatra::Base
else
game = create_game(params)

if computer_opponent(params) == "player1"
game.take_turn(game.generate_ai_move)
end
session[:computer_opponent] = computer_opponent(params, game)

check_for_computer_turn(game)

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

if session[:computer_opponent] != nil
game.take_turn(game.generate_ai_move)
end
check_for_computer_turn(game)

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

0 comments on commit 714e061

Please sign in to comment.