-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
103 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module GameHelpers | ||
def create_game(params) | ||
player_marks = configure_players(params) | ||
TicTacToe::Game.new(TicTacToe::Board.new, player_marks[:player1], player_marks[:player2]) | ||
end | ||
|
||
def configure_players(params) | ||
player_marks = {} | ||
if params[:player_order] = 'first' | ||
player_marks[:player1] = params[:player_mark] | ||
player_marks[:player2] = opponent_mark(player_marks[:player1]) | ||
else | ||
player_marks[:player2] = params[:player_mark] | ||
player_marks[:player1] = opponent_mark(player_marks[:player2]) | ||
end | ||
player_marks | ||
end | ||
|
||
def opponent_mark(mark) | ||
mark == 'X' ? 'O' : 'X' | ||
end | ||
|
||
def computer_opponent(params) | ||
if params[:computer_opponent] == 'yes' | ||
params[:player_order] == 'first' ? 'player2' : 'player1' | ||
end | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'spec_helper' | ||
require 'game_helpers' | ||
|
||
describe 'GameHelpers' do | ||
include GameHelpers | ||
let(:params) {{player_mark: "X", computer_opponent: "no", player_order: "first"}} | ||
|
||
describe '#create_game' do | ||
it "configures the game based on the params" do | ||
game = create_game(params) | ||
expect(game.player1.mark).to eq 'X' | ||
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" | ||
end | ||
end | ||
|
||
describe '#opponent_mark' do | ||
it "returns the opponent's mark" do | ||
expect(opponent_mark('X')).to eq 'O' | ||
end | ||
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 | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters