趣味编程游戏大集合
在编程的世界里,我们可以通过编写代码来实现各种各样有趣的游戏。下面将为大家介绍几款用 Ruby 语言编写的小游戏,包括它们的代码实现、运行方式以及工作原理。
1. 抽认卡游戏(Flash Cards)
抽认卡是许多临时抱佛脚的学生的救星,可用于记忆历史日期、词汇、外语等。这个脚本可以作为学习工具,它会随机抽取问题让你回答。
代码实现 :
unless ARGV[0]
puts "\n\nUsage is flashCards.rb <file>\n\n"
exit
end
flash = []
card = Struct.new(:question, :answer)
File.open(ARGV[0], "rb").each do |line|
if line =~ /(.*)\s{3,10}(.*)/
flash << card.new($1.strip, $2.strip)
end
end
flash.replace(flash.sort_by { rand })
until flash.empty?
drill = flash.pop
print "#{drill.question}? "
guess = $stdin.gets.chomp
if guess.downcase == drill.answer.downcase
puts "\n\nCorrect -- The answer is: #{drill.ans
超级会员免费看
订阅专栏 解锁全文
731

被折叠的 条评论
为什么被折叠?



