Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

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
  • 1586 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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download