пытаясь посчитать, сколько раз что-то выпало на кости.код рубина - PullRequest
0 голосов
/ 29 сентября 2011

Это мой код для игры в кости, которая показывает направление. На экране отображается север, юг, восток или запад. Я пытаюсь найти способ подсчитать, сколько раз каждый из них появляется каждый раз, когда я кидаю кости.

У кого-нибудь есть идеи?

class Dice

  #def initialize()
  #end 


  def roll 
    @dice = Array['north','south','east','west'] # makes dice with four sides (directions)
    @dice_index = 0 + rand(4)                    # gets the random index of the array
    puts @dice[@dice_index]                      # prints random direction like a dice
  end

  def stats
    puts @dice_index
    north_count =0;
    south_count =0;
    east_count=0;
    west_count=0;
  end
end


game_dice = Dice.new
game_dice.roll
game_dice.stats

1 Ответ

1 голос
/ 29 сентября 2011

Ваш класс должен выглядеть примерно так:

class Dice
  SIDES = [:north, :south, :east, :west]
  def initialize
    @rolls = Hash.new(0)
    @num_of_sides = SIDES.count
  end
  def roll
    roll = SIDES[rand(@num_of_sides)]
    @rolls[roll] += 1
    roll
  end
  def stats
    puts @rolls.inspect
  end
end
...