Jump to content

Recent Status Updates

  • Photo
      16 Sep

    Some spammers sign up to CC and then they leave their account hidden so they think it won't get noticed but with an obvious name like "SaxophoneRetailSingapore" it's hard not to know they are a spam bot. #ModLife

    Show comments (3)
  • Photo
      15 Sep

    Programming is something that I enjoy and want to make a career out of. But, I usually tend to start things and not finish them. Any advice on how I can finish what I start?

    Show comments (1)
  • Photo
      12 Sep

    Just joined. Really enjoy this sight. Excited to become a great programmer, and helper.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
- - - - -

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


    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"
  puts "You Lose! Suck It Chump "
puts "The computer chose: #{computer.upcase}"
    tracking = {
		 :wins => 0,
		 :loss => 0,
		 :ties => 0,
	    tracking[:wins] += 1
		  tracking[:loss] += 1
	    tracking[:ties] +=1

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

#2 gregwarner


    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,576 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.

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