Skip to content

Commit

Permalink
Added GameSetup class for game configuration validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lisahamm committed Feb 26, 2015
1 parent e9bd162 commit b94f4ec
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 15 deletions.
62 changes: 62 additions & 0 deletions lib/game_setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class GameSetup
attr_reader :errors

def initialize(setup)
@setup = setup
@errors = {}
end

def valid?
run_validations
errors.empty?
end

def invalid?
!valid?
end

private

def run_validations
validate_player
validate_opponent
validate_player_order
end

def validate_player_mark
validate(:player_mark)
end

def validate_opponent
validate(:opponent)
end

def validate_player_order
validate(:player_order)
end

def validate(key)
if valid_options[key].include?(@setup[key])
true
else
self.errors[key] = message_options[key]
false
end
end

def valid_options
{
player_mark: ["X", "O"],
opponent: ["yes", "no"],
player_order: ["first", "second"]
}
end

def message_options
{
player_mark: "Please select a mark to continue",
opponent: "Please indicate if you would like to play against the computer",
player_order: "Please select if you would like to go first or second"
}
end
end
30 changes: 30 additions & 0 deletions spec/game_setup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'
require 'game_setup'

describe 'GameSetup' do
valid_params = {player_mark: 'X', opponent: 'yes', player_order: 'first'}
invalid_params = {opponent: 'yes', player_order: 'third'}

let(:valid_game_setup) {GameSetup.new(valid_params)}
let(:invalid_game_setup) {GameSetup.new(invalid_params)}

context 'valid game setup' do
it 'validates the game configurations' do
expect(valid_game_setup.valid?).to eq true
end

it 'does not produce error messages' do
expect(valid_game_setup.errors.empty?).to eq true
end
end

context 'invalid_game_setup' do
it 'validates the game configurations' do
expect(invalid_game_setup.valid?).to eq false
end

it 'does not produce error messages' do
expect(invalid_game_setup.errors.empty?).to eq false
end
end
end
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
require 'rspec'
require 'rack/test'

root = File.expand_path(File.dirname(__FILE__) + '/../')

lib = root + '/lib'
spec = root + '/spec'

$:.unshift(lib) unless $:.include?(lib)
$:.unshift(spec) unless $:.include?(spec)


RSpec.configure do |conf|
conf.include Rack::Test::Methods
end
21 changes: 10 additions & 11 deletions tic_tac_toe_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'sinatra/base'
require 'tic_tac_toe'
require './lib/game_setup'

class TicTacToeController < Sinatra::Base
include Validation
enable :sessions

get '/' do
Expand All @@ -10,19 +12,16 @@ class TicTacToeController < Sinatra::Base
end

post '/setup' do
if params[:player_mark] || params[:opponent] == nil
session[:invalid_input_message] = "Please select an option to continue"
redirect to('/invalid_setup')
else
session[:mark] = params[:player_mark]
session[:opponent] = params[:opponent]
redirect to('/game')

if !setup_valid?(params)
@invalid_input_messages = error_messages
erb :index
end
end

get '/invalid_setup' do
@invalid_input_message = session[:invalid_input_message]
erb :index
session[:mark] = params[:player_mark]
session[:opponent] = params[:opponent]
session[:player_order] = params[:player_order]
redirect to('/game')
end

get '/game' do
Expand Down
28 changes: 28 additions & 0 deletions tic_tac_toe_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
ENV['RACK_ENV'] = 'test'


require './../tic_tac_toe_controller'
require 'rspec'
require 'rack/test'

describe 'The HelloWorld App' do
include Rack::Test::Methods

def app
TicTacToeController
end

it "says hello" do
get '/'
expect(last_response).to be_ok
expect(last_response.status).to eq 200
expect(last_response.body).to include "Player 1, select your mark"
end

it "starts the game" do
post "/setup", player_mark: "X", opponent: "yes"
expect(last_response).to be_redirect

#expect(session[:mark]).to eq "O"
end
end
18 changes: 14 additions & 4 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<form id="setup-form" action="/setup" method="POST">

<div class="form-section">
Player 1, select your mark:
Select your mark:
<input type="radio" name="player_mark" value="X" />X
<input type="radio" name="player_mark" value="O" />O
</div>

<% if params[:player_mark] == nil %>
<p><%= @invalid_input_message %></p>
<% if params[:player_mark] == {} %>
<p><%= @invalid_input_messages[:player_mark] %></p>
<% end %>

<div class="form-section">
Expand All @@ -18,7 +18,17 @@
</div>

<% if params[:opponent] == nil %>
<p><%= @invalid_input_message %></p>
<p><%= @invalid_input_messages[:opponent] %></p>
<% end %>

<div class="form-section">
Would you like to go first or second?:
<input type="radio" name="player_order" value="first" />First
<input type="radio" name="player_order" value="second" />Second
</div>

<% if params[:player_order] == nil %>
<p><%= @invalid_input_messages[:player_order] %></p>
<% end %>

<div class="form-section">
Expand Down

0 comments on commit b94f4ec

Please sign in to comment.