Skip to content

Commit

Permalink
Removed Board initialization from Game params
Browse files Browse the repository at this point in the history
  • Loading branch information
lisahamm committed Mar 10, 2015
1 parent e27aab3 commit a981307
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: git://github.com/lisahamm/tic_tac_toe.git
revision: 0a4da6836ab891e2d3743f29187669133f868df9
remote: git://github.com/lisahamm/ruby_ttt.git
revision: a96705779a65b9d7202498ac91f06df2266850fa
specs:
tic_tac_toe (0.0.1)

Expand Down
20 changes: 9 additions & 11 deletions lib/game_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module GameHelpers
def create_game(params)
player_marks = configure_players(params)
TicTacToe::Game.new(TicTacToe::Board.new, player_marks[:player1], player_marks[:player2])
TicTacToe::Game.new(player_settings(params))
end

def configure_players(params)
player_marks = {}
def player_settings(params)
player_settings = []
ai = computer_opponent(params) != nil
if params[:player_order] = 'first'
player_marks[:player1] = params[:player_mark]
player_marks[:player2] = opponent_mark(player_marks[:player1])
player_settings[0] = {mark: params[:player_mark], ai: false}
player_settings[1] = {mark: opponent_mark(params[:player_mark]), ai: ai}
else
player_marks[:player2] = params[:player_mark]
player_marks[:player1] = opponent_mark(player_marks[:player2])
player_settings[0] = {mark: opponent_mark(params[:player_mark]), ai: ai}
player_settings[1] = {mark: params[:player_mark], ai: false}
end
player_marks
player_settings
end

def opponent_mark(mark)
Expand All @@ -25,6 +25,4 @@ def computer_opponent(params)
params[:player_order] == 'first' ? 'player2' : 'player1'
end
end


end
10 changes: 5 additions & 5 deletions spec/game_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
end
end

describe '#configure_players' do
it "sets marks for the first and second player" do
player_marks = configure_players(params)
expect(player_marks[:player1]).to eq "X"
expect(player_marks[:player2]).to eq "O"
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"
end
end

Expand Down
16 changes: 4 additions & 12 deletions tic_tac_toe_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,29 @@ class TicTacToeController < Sinatra::Base
erb :index
else
session[:game] = create_game(params)
session[:computer_opponent] = params[:computer_opponent]
redirect to('/game')
end
end

get '/game' do
@board = TicTacToe::Board.new(cells: session[:moves])
session[:moves] = @board.to_array
game = session[:game]
@board = game.board
erb :board
end

post '/make_move' do
move = params[:move].to_i
game = session[:game]
move = params[:move].to_i
game.take_turn(move)
game.switch_turn
session[:game] = game
session[:moves] = game.board.to_array
redirect to('/game_over') if !game.in_progress?
# if session[:computer_opponent] == 'yes'
# @computer_player = TicTacToe::AI.new(player_mark)
# @computer_player.take_turn(board)
# session[:moves] = board.to_array
# player_mark = session[:mark] == 'X' ? 'O' : 'X'
# session[:mark] = player_mark
# end
redirect to('/game')
end

get '/game_over' do
@board = TicTacToe::Board.new(cells: session[:moves])
@board = session[:game].board
erb :game_over
end

Expand Down

0 comments on commit a981307

Please sign in to comment.