Skip to content

Commit

Permalink
Switched to modular style
Browse files Browse the repository at this point in the history
  • Loading branch information
lisahamm committed Feb 24, 2015
1 parent e55cc75 commit 3b81ac5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.DS_Store
Gemfile.lock
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require './tic_tac_toe_controller'
run TicTacToeController
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'rspec'
require 'rack/test'

RSpec.configure do |conf|
conf.include Rack::Test::Methods
end
28 changes: 28 additions & 0 deletions spec/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
48 changes: 48 additions & 0 deletions tic_tac_toe_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'sinatra/base'
require 'tic_tac_toe'

class TicTacToeController < Sinatra::Base
enable :sessions

get '/' do
session.clear
erb :index
end

post '/setup' do
session[:mark] = params[:player_mark]
session[:opponent] = params[:opponent]
redirect to('/game')
end

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

post '/make_move' do
move = params[:move].to_i
board = TicTacToe::Board.new(cells: session[:moves])
board.set_cell(move, session[:mark])
session[:moves] = board.to_array
redirect to('/game_over') if board.winner? || board.tie_game?
player_mark = session[:mark] == 'X' ? 'O' : 'X'
session[:mark] = player_mark
if session[:opponent] == 'yes'
@computer_player = TicTacToe::ComputerPlayer.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])
erb :game_over
end

run! if app_file == $0
end

0 comments on commit 3b81ac5

Please sign in to comment.