Jump to content




Recent Status Updates

View All Updates

Developed by Kemal Taskin
Photo
- - - - -

using a hash to keep track of wins

ruby hash

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 frostdoji

frostdoji

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 30 January 2013 - 12:09 PM

Ok i made a rock paper and scissors game i got the game to work but i need to keep track of win,losses and ties and print them im using a hash to do this my big problem is if statement to track the wins,loss.ties
i also used elsif just cant think of what to put to keep track so any help or advice you can give would be appreciated

puts "Would you like to play a game of Rock, Paper, Scissors?"
puts "Y/N"
player_input = gets.chomp.downcase
while true  #loops forever till quit is typed
if player_input == 'y'
  puts "Lets Play Rock, Paper, Scissors!"
  puts "Press r for Rock"
  puts "Press s for Scissors"
  puts "Press p for Paper"
  puts "Press Enter when you select r,s,p"
  puts "Oh if you want to quit at any time just type quit"
  computer = "rsp"[rand(3)].chr
  player = gets.chomp.downcase

case [player, computer]
  when ['p','r'], ['s','p'], ['r','s']
  puts "You Win! Congrats"
  when ['r','r'], ['s','s'], ['p','p']
  puts "You Tied! Bummer, like they say its just like kissing your sister"
  else
  puts "You Lose! Suck It Chump "
end
puts "The computer chose: #{computer.upcase}"
    tracking = {
		 :wins => 0,
		 :loss => 0,
		 :ties => 0,
    }
	 if 
	    tracking[:wins] += 1
	  elsif 
		  tracking[:loss] += 1
	  elsif 
	    tracking[:ties] +=1
	  end

else player_input == "n"
  puts "Oh you don't want to lose do you, CHICKEN"
  exit
end
  puts tracking[:wins]
  puts tracking[:loss]
  puts tracking[:ties]
break if player == "quit"
end


#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,574 posts

Posted 30 January 2013 - 01:05 PM

Simplest solution: just declare 3 global variables: wins, losses, and ties, and initialize them all to zero at the start of the program. Increment the appropriate variable by one in your win/loss/tie checking logic.

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid