硬件不可能?:“在不使用条件的情况下用 ruby​​ 创建石头剪刀布程序" [英] HW impossibility?: "Create a rock paper scissors program in ruby WITHOUT using conditionals"

查看:35
本文介绍了硬件不可能?:“在不使用条件的情况下用 ruby​​ 创建石头剪刀布程序"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在上一门介绍性的软件开发课程,我的作业是创建一个石头剪刀布程序,它接受两个参数(石头,纸)等,并返回获胜的 arg.

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.

现在如果我可以使用条件,我会快速解决这个问题,但是作业说我们需要知道的一切都在前三个中ruby 教科书的章节,这些章节不包括条件!如果没有它们,是否可以创建此程序?还是他只是希望我们足智多谋并使用条件?虽然这是一个非常简单的条件分配......我在想我可能在这里遗漏了一些东西.

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.

我正在考虑那个 chmod 数值系统,并认为通过该附加系统可能有一个解决方案......

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

推荐答案

这是一个只使用哈希的方法:

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

这篇关于硬件不可能?:“在不使用条件的情况下用 ruby​​ 创建石头剪刀布程序"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆