Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
enagled guess full word. added hints
  • Loading branch information
allonmj committed Mar 26, 2012
1 parent a178734 commit 24eb2d3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
22 changes: 15 additions & 7 deletions app/controllers/guesses_controller.rb
@@ -1,18 +1,26 @@
class GuessesController < ApplicationController
def create
@game = current_user.games.find(params[:game_id])
@game.guess(current_user, params[:guess][:letter])
if @game.guesses.count >=3
@game.guess_word(current_user, params[:guess][:letter])
else
@game.guess(current_user, params[:guess][:letter])
end
win
end


def win
if @game.complete?
if @game.guesses.wrong.count < 7


if @game.winner?
flash.notice = "You won! You're pretty much a pro."
elsif @game.guesses.wrong.count < 12
flash.notice = "You won! You're decent."
elsif @game.guesses.wrong.count < 16
flash.notice = "You won! You kinda suck, with #{@game.guesses.wrong.count} wrong guesses, but you made it."
else
flash.notice = "You finished the game! Not sure if I'd call it winning since you took #{@game.guesses.wrong.count} wrong guesses; you basically pound on the keyboard like a gorilla."
flash.notice = "You lost! --with #{@game.guesses.wrong.count} wrong guesses"
end
end
redirect_to @game
end

end
16 changes: 14 additions & 2 deletions app/models/game.rb
@@ -1,7 +1,7 @@
class Game < ActiveRecord::Base
belongs_to :word
has_and_belongs_to_many :users
has_many :guesses
has_many :guesses

validates_presence_of :word
before_validation :choose_word, :on => :create
Expand All @@ -13,7 +13,7 @@ def choose_word

def generate_masked_word
positions = []
positions = (positions + [rand(word.text.length)]).uniq until positions.length == 2
positions = (positions + [rand(word.text.length)]).uniq until positions.length == 4
masked_word = word.text.gsub(/./, '*')
positions.each {|n| masked_word[n] = word.text[n] }
self.masked_word = masked_word
Expand All @@ -33,7 +33,19 @@ def guess(user, letter)
guesses.create! :user => user, :letter => letter, :hit => hit
end

def guess_word(user, w)
if w == word.text
update_attribute :masked_word, w
end
end


def complete?
guesses.count >= 4
end

def winner?
masked_word == word.text
end

end
12 changes: 12 additions & 0 deletions app/views/games/show.html.erb
Expand Up @@ -4,10 +4,22 @@
<%= g.text_field :letter %>
<% end %>

<p>
<b>Hint:</b>
<%= @game.word.meaning %>
<% if @game.guesses.count >=3 %>
<br/>
<b>Guess the word!</b>
<% else %>
<% end %>
</p>

<p>
<b>Guesses:</b>
<ul>
<% @game.guesses.each do |guess| %>
<li><%= guess.letter %></li>
<% end %>
</ul>
</p>
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -58,7 +58,7 @@

# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => 'games#show'
root :to => 'welcome#index'

# See how all your routes lay out with "rake routes"
end

0 comments on commit 24eb2d3

Please sign in to comment.