-
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
5 changed files
with
86 additions
and
0 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,2 @@ | ||
*.DS_Store | ||
Gemfile.lock |
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,2 @@ | ||
require './tic_tac_toe_controller' | ||
run TicTacToeController |
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,6 @@ | ||
require 'rspec' | ||
require 'rack/test' | ||
|
||
RSpec.configure do |conf| | ||
conf.include Rack::Test::Methods | ||
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
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 |
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,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 |