Week 3, day 6

Woke up a new man today! Totally rested and regenerated, and I think it shows. Have managed to finish the barebones of the weekend Rock-Paper-Scissors challenge today and intend to spend tomorrow styling it nicely with CSS.
Looking at it now it's hard for me to believe that I was staring at a blank page this morning and I'm pretty proud of what I've achieved. It still needs lots of extra features but the foundations are there and I'm confident they're strong.

 1 require 'sinatra/base'
 2 require_relative 'lib/cpu'
 3 require_relative 'lib/decide'
 4 
 5 class RPS < Sinatra::Base
 6   enable :sessions
 7   cpu = CPU.new
 8   decide = Decide.new
 9   set :views, Proc.new { File.join(root, "views") }
10 
11   get '/' do
12     erb :index
13   end
14 
15   get '/game' do
16     redirect '/'
17   end
18 
19   post '/game' do
20     if params[:Choice]
21       @p1_choice = params[:Choice]
22       @p2_choice = cpu.choice
23       @decision = decide.make @p1_choice, @p2_choice
24       erb :result
25     else
26       session[:Name] = params[:Name] if params[:Name]
27       erb :game
28     end
29   end
30 
31   run! if app_file == $0
32 end

Above is my server file, really simple but does the job. Join me after the break for some more snippets.

index.erb

1 Play Rock-Paper-Scissors!
2 
3 <form action="/game" method="post">
4   Enter your name: <input type="text" name="Name" value=<%= session[:Name] %>>
5   <input type="submit" value="Play">
6 </form>

game.erb

 1 Welcome <%= session[:Name] %>
 2 
 3 <form action="/game" method="post">
 4   Make your choice
 5   <input type="submit" name="Choice" value="Rock">
 6   <input type="submit" name="Choice" value="Paper">
 7   <input type="submit" name="Choice" value="Scissors">
 8 </form>
 9 
10 <a href="/">Homepage</a>

result.erb

 1 You <%= @decision %>
 2 You chose <%= @p1_choice %> and your opponent chose <%= @p2_choice %>
 3 
 4 <form action="/game" method="post">
 5   Make your choice
 6   <input type="submit" name="Choice" value="Rock">
 7   <input type="submit" name="Choice" value="Paper">
 8   <input type="submit" name="Choice" value="Scissors">
 9 </form>
10 
11 <a href="/">Homepage</a>
Written on April 4, 2015