4 Replies - 1982 Views - Last Post: 30 January 2013 - 11:19 PM Rate Topic: -----

#1 frost-tien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 07-June 12

How to update a array to track wins, loss, ties

Posted 30 January 2013 - 10:53 AM

i know how to make an array but i cant figure out how to update it to track the wins and losses of a game
track_array = ["wins", "losses", "ties"]

thats my array i cant think of the code to keep track all i can think of is
if win 
"win" + 1 
elsif loss 
"loss" + 1
elsif tied
"ties" + 1
end 

or should i make 3 arrays like "win_array", "loss_array", "tie_array" and go that route
and thanks for any help that anyone givs

This post has been edited by frost-tien: 30 January 2013 - 10:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to update a array to track wins, loss, ties

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,205
  • Joined: 12-May 09

Re: How to update a array to track wins, loss, ties

Posted 30 January 2013 - 10:57 AM

I think you need to step back on this.

Why are you adding 1 to "win"? That means adding an integer to a string. You have an array of strings - you aren't going to be able to track it that way.

IF you want to track this in an array, I'd keep track of which position represents which count:
win_index = 0
loss_index = 1
tie_index = 2
track_array = [0, 0, 0]
if win
  track_array[win_index] += 1
...
end



You could also do it without the array:
wins = 0
losses = 0
ties = 0
if win
  wins += 1
elsif loss
  losses += 1
...
end


However, this problem is well-suited to a hash:
tracking = {
  :wins => 0,
  :losses => 0,
  :ties => 0}

if win
  tracking[:wins] += 1
....
end
puts tracking[:wins]


Was This Post Helpful? 4
  • +
  • -

#3 frost-tien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 07-June 12

Re: How to update a array to track wins, loss, ties

Posted 30 January 2013 - 11:05 AM

oh ok i never thought of using a hash for this
it does make better sense on doing it, the array was only cause
i thought it would be the simplest way to keep track of wins/losses
and thank you for your help
Was This Post Helpful? 0
  • +
  • -

#4 frost-tien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 07-June 12

Re: How to update a array to track wins, loss, ties

Posted 30 January 2013 - 11:35 AM

now i just need to figure out how to weave it into my code
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
      end
      if
          tracking[:loss] += 1
      end
      if
        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



i dont know what to put in the if statements for the wins loss
so yeah im still alittle lost on this

This post has been edited by frost-tien: 30 January 2013 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#5 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,514
  • Joined: 28-November 09

Re: How to update a array to track wins, loss, ties

Posted 30 January 2013 - 11:19 PM

You might also want to think on why an Array wouldn't work in that situation. An array is a collection of items indexed by a number. A hash is a collection of keys and data where the key can be anything.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1