Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

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...

like image 768
boulder_ruby Avatar asked Jun 06 '12 22:06

boulder_ruby


People also ask

Is there an algorithm for Rock Paper Scissors?

This algorithm counts the previous moves of the opponent to determine if the opponent prefers playing one type of move over the others. Essentially, it keeps a “score” for each of rock, paper, and scissors. After each move, the respective score of the opponent's move is incremented.

How is probability used in Rock Paper Scissors?

Each choice of rock, paper or scissors should be normalized to 1 with equal probability. Theoretically, they would be equally likely, so Rock=0.33, Paper=0.33 and Scissors=0.33. The Law of Large Numbers: the more trials we perform, the closer we get to the expected probability.

How do you make a simple Rock Paper Scissors game in Java?

nextInt() function to take random input from computer (player 1) and prompt user (player 2) to choose an option rock, paper, or scissors and finishes the code by adding nested if-else statements to appropriately report “User won”, or “Computer won”, or "No Winner won ( Both choose same )."


1 Answers

Here's one only using hashes:

RULES = {
  :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)
  RULES[p1][p2]
end

puts play(:rock, :paper)        # :paper
puts play(:scissors, :rock)     # :rock
puts play(:scissors, :scissors) # :draw
like image 146
dereckrx Avatar answered Oct 08 '22 07:10

dereckrx