Skip to content

Commit

Permalink
Merge pull request #4 from randallreedjr/bug/updated-math24-syntax
Browse files Browse the repository at this point in the history
Bug/updated math24 syntax
  • Loading branch information
randallreedjr committed Jan 24, 2017
2 parents 0da5993 + db1e1d7 commit 095e529
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 518 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,2 +1,4 @@
/.bundle/
/log/
math24-*.gem
.byebug_history
1 change: 1 addition & 0 deletions .rspec
@@ -1,3 +1,4 @@
--color
--warnings
--require spec_helper
--format documentation
14 changes: 11 additions & 3 deletions Gemfile
Expand Up @@ -4,9 +4,17 @@ gem 'sinatra', '1.4.5'
gem 'tilt', '1.4.1'
gem 'rack', '1.5.2'
gem 'rack-protection', '1.5.0'
gem 'rspec', '3.0.0'
gem 'rspec-core', '3.0.1'
gem 'math24', '~>2.0.0'
gem 'require_all', '~>1.4.0'
gem 'thin', '~>1.7.0'
gem 'newrelic_rpm', '~>3.17.1'

group :development, :test do
gem 'rspec', '3.0.0'
gem 'rspec-core', '3.0.1'
gem 'rack-test'
gem 'byebug'
end

group :production do
gem 'newrelic_rpm', '~>3.17.1'
end
15 changes: 10 additions & 5 deletions Gemfile.lock
@@ -1,27 +1,30 @@
GEM
remote: https://rubygems.org/
specs:
byebug (9.0.6)
daemons (1.2.4)
diff-lcs (1.2.5)
diff-lcs (1.3)
eventmachine (1.2.1)
math24 (2.0.1)
newrelic_rpm (3.17.1.326)
newrelic_rpm (3.17.2.327)
rack (1.5.2)
rack-protection (1.5.0)
rack
rack-test (0.6.3)
rack (>= 1.0)
require_all (1.4.0)
rspec (3.0.0)
rspec-core (~> 3.0.0)
rspec-expectations (~> 3.0.0)
rspec-mocks (~> 3.0.0)
rspec-core (3.0.1)
rspec-support (~> 3.0.0)
rspec-expectations (3.0.1)
rspec-expectations (3.0.4)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.0.0)
rspec-mocks (3.0.1)
rspec-mocks (3.0.4)
rspec-support (~> 3.0.0)
rspec-support (3.0.0)
rspec-support (3.0.4)
sinatra (1.4.5)
rack (~> 1.4)
rack-protection (~> 1.4)
Expand All @@ -36,10 +39,12 @@ PLATFORMS
ruby

DEPENDENCIES
byebug
math24 (~> 2.0.0)
newrelic_rpm (~> 3.17.1)
rack (= 1.5.2)
rack-protection (= 1.5.0)
rack-test
require_all (~> 1.4.0)
rspec (= 3.0.0)
rspec-core (= 3.0.1)
Expand Down
58 changes: 33 additions & 25 deletions app/controllers/math24_controller.rb
Expand Up @@ -7,15 +7,8 @@ class Math24Controller < ApplicationController
erb :'math24/about'
end

get '/problem' do
math24 = Math24.new
problem = math24.generate_problem.join(" ")
erb :'math24/problem', :locals => {:problem => problem}
end

post '/problem' do
math24 = Math24.new
problem = "#{params[:problem] || ""}"
get '/problem' do
problem = Math24.generate_problem.join(" ")
erb :'math24/problem', :locals => {:problem => problem}
end

Expand All @@ -24,20 +17,22 @@ class Math24Controller < ApplicationController
end

post '/solution' do
math24solver = Math24Solver.new
problem = "#{params[:problem] || ""}"
if problem.include?(",")
numbers = problem.gsub(" ","").split(",")
numbers = problem.delete(" ").split(",")
else
numbers = problem.squeeze(" ").split(" ")
numbers = problem.squeeze(" ").split
end

valid = numbers.all? do |number|
number.to_i > 0 && number.to_i < 10
end

if valid
solution = math24solver.solve(numbers)
erb :'math24/solution', :locals => {:problem => numbers.join(" "),
:solution => solution,
solution = Math24.solve(numbers)
message = solution ? "#{solution} = 24" : "No solution found"
erb :'math24/solution', :locals => {:problem => numbers.join(" "),
:message => message,
:last_answer => 24}
else
erb :'math24/solve', :locals => {:invalid => true}
Expand All @@ -47,16 +42,29 @@ class Math24Controller < ApplicationController
post '/check' do
problem = "#{params[:problem] || ""}"
solution = "#{params[:solution] || ""}"
math24 = Math24.new
math24.numbers = problem.split(" ")
if math24.solution?(solution)
erb :'math24/correct', :locals => {:problem => problem,
:solution => solution,
:last_answer => math24.last_answer}
numbers = problem.split

begin
valid_solution = Math24.check(numbers, solution)
rescue ArgumentError
error = true
valid_solution = false
end

if valid_solution
erb :'math24/correct',
:locals => {
:problem => problem,
:solution => solution
}
elsif error
erb :'math24/incorrect', :locals => {:problem => problem,
:solution => solution,
:last_answer => '???'}
else
erb :'math24/incorrect', :locals => {:problem => problem,
:solution => solution,
:last_answer => math24.last_answer}
erb :'math24/incorrect', :locals => {:problem => problem,
:solution => solution,
:last_answer => instance_eval(solution)}
end
end
end
end
4 changes: 2 additions & 2 deletions app/views/math24/correct.erb
Expand Up @@ -12,7 +12,7 @@
<div class="container">
<div class="jumbotron">
<div class="numbers"><%= problem %></div>
<div class="numbers correct"><%= solution %> = <%= last_answer %>!</div>
<div class="numbers correct"><%= solution %> = 24!</div>
<!--<div class="status green">CORRECT!</div>-->
<div class="green mybutton"><a href="/problem">Give me another!</a></div>
<%= erb :'layouts/_footer' %>
<%= erb :'layouts/_footer' %>
4 changes: 2 additions & 2 deletions app/views/math24/solution.erb
Expand Up @@ -12,9 +12,9 @@
<div class="container">
<div class="jumbotron">
<div class="numbers"><%= problem %></div>
<div class="numbers"><%= solution %>!</div>
<div class="numbers"><%= message %>!</div>
<div>
<div class="green mybutton"><a href="/problem">Give me problems</a></div>
<div class="red mybutton"><a href="/solve">Show me solutions</a></div>
</div>
<%= erb :'layouts/_footer' %>
<%= erb :'layouts/_footer' %>
Empty file added bin/.keep
Empty file.
8 changes: 0 additions & 8 deletions bin/math24

This file was deleted.

8 changes: 0 additions & 8 deletions bin/math24solver

This file was deleted.

12 changes: 2 additions & 10 deletions config/environment.rb
@@ -1,14 +1,6 @@
require_relative "../lib/math24solvercli.rb"
#require "math24"

ENV['SINATRA_ENV'] ||= "development"
ENV['SINATRA_ENV'] ||= "development"

require 'bundler/setup'
Bundler.require(:default, ENV['SINATRA_ENV'])

# ActiveRecord::Base.establish_connection(
# :adapter => "sqlite3",
# :database => "db/math24#{ENV['SINATRA_ENV']}.sqlite"
# )

require_all 'app'
require_all 'app'
Empty file added lib/.keep
Empty file.
35 changes: 0 additions & 35 deletions lib/math24.rb

This file was deleted.

24 changes: 0 additions & 24 deletions lib/math24gamecli.rb

This file was deleted.

68 changes: 0 additions & 68 deletions lib/math24solver.rb

This file was deleted.

0 comments on commit 095e529

Please sign in to comment.