boulder_ruby boulder_ruby - 1 year ago 71
Ruby Question

HW impossibility?: "Create a rock paper scissors program in ruby WITHOUT using conditionals"

I'm in an introductory software development class, and my homework is to
create a rock paper scissors program that takes two arguments (rock,
paper), etc, and returns the arg that wins.

Now I would make quick work of this problem if I could use conditionals,
but the assignment says everything we need to know is in the first three
chapters of the ruby textbook, and these chapters DO NOT include
conditionals! Would it be possible to create this program without them?
Or is he just expecting us to be resourceful and use the conditionals?
It's a very easy assignment with conditionals though...I'm thinking that
I might be missing something here.

EDIT: I'm thinking of that chmod numerical system and think a solution may be possible through that additive system...

Answer Source

Here's one only using hashes:

  :rock     => {:rock => :draw, :paper => :paper, :scissors => :rock},
  :paper    => {:rock => :paper, :paper => :draw, :scissors => :scissors},
  :scissors => {:rock => :rock, :paper => :scissors, :scissors => :draw}

def play(p1, p2)

puts play(:rock, :paper)        # :paper
puts play(:scissors, :rock)     # :rock
puts play(:scissors, :scissors) # :draw
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download