Skip to content

Instantly share code, notes, and snippets.

@jyfeather
Last active December 18, 2015 07:39
Show Gist options
  • Save jyfeather/5748392 to your computer and use it in GitHub Desktop.
Save jyfeather/5748392 to your computer and use it in GitHub Desktop.
又一个猜单词的Ruby小游戏
#!/usr/bin/ruby -w
#
# 脚本名: WordGuess.rb
# 版本号: 1.0
# 作者: Yan
# 日期: June 2013
#
# 描述: 一个猜单词的游戏,尽快猜中Ta
#
##### 自定义类 #####
# 定义代表控制台的类
class Screen
def cls
puts "\n" * 35 # 屏幕滚动至25行
puts "\a" # 响铃,提醒
end
def pause
STDIN.gets # 用户输入字符串回车后结束
end
end
# 定义Ruby猜谜游戏的类
class Game
def display_greeting
Console_Screen.cls
print "\t\t欢迎猜词游戏\n\n\n\n\n\n\n\n\n\n\n按回车继续."
Console_Screen.pause
end
def display_instructions # 显示游戏规则
Console_Screen.cls
puts "规则:\n\n"
puts "每轮游戏开始的时候,游戏会选择一个字符长度5到10之间的单词。"
puts "每次猜词之前, 你需要指定5个辅音和1个元音,若这6个字母在单词中出现,他们将会被显示。"
puts "这样,能够有助于你猜这个单词"
puts "祝你好运\n\n\n\n"
print "按回车继续."
Console_Screen.pause
end
def select_word # 随机产生一个待猜的单词
words = ['w i n d o w', 's t a t i o n', 'h a m b u r g e r', 'e x p r e s s i o n',
'w a l l e t', 'c a m e r a', 'a i r p l a n e', 'c a n d l e', 'c o m p u t e r',
'p i c t u r e', 'f r a m e', 's h e l f', 'b o w l i n g', 'p o l i t e',
's t a t e m e n t', 'n a v i g a t i o n',
'n e g a t i v e', 'm e t h o d', 'f i s h i n g', 'c o m p e n s a t e', 'h a p p y']
randNo = rand(21)
words[randNo]
end
def get_consonant
list = Array.new # 声明一个用来存储辅音字母的数组
puts "猜词前,需要指定5个辅音字母。\n\n"
print "按回车继续。"
Console_Screen.pause
5.times do # 循环五次
Console_Screen.cls
print "\n输入一个辅音,并回车。"
input = STDIN.gets
input.chop!
if input !~ /[bcdfghjklmnpqrstvwxyz]/i
Console_Screen.cls
print "错误:" + input + "不是辅音。" + "按回车继续。"
Console_Screen.pause
redo
end
if input.length > 1
Console_Screen.cls
print "错误: 你输入超过1个字母。按回车继续。"
Console_Screen.pause
redo
end
if list.include?(input.downcase) == true
Console_Screen.cls
print "错误:已经猜过了。按回车继续。"
Console_Screen.pause
redo
else
list.push(input.downcase)
end
end
return list # 返回辅音数组
end
def get_vowel
puts "猜词前,你需要指定一个元音。\n\n"
1.times do
Console_Screen.cls
print "\n输入一个元音。按回车继续。"
input = STDIN.gets
input.chop!
if input !~ /[aeiou]/i
Console_Screen.cls
print "错误:" + input + "不是一个元音。" + "按回车继续。"
Console_Screen.pause
redo
end
if input.length > 1
Console_Screen.cls
print "错误: 你一次只能输入一个元音字母。按回车继续。"
Console_Screen.pause
redo
end
input = input.downcase
return input # 返回元音字母
end
end
def prompt_for_guess(shortWord, word, consonants, vowel)
Console_Screen.cls
consonants.push(vowel)
wordArray = word.split(" ") # 把单词拆成字母数组
i = 0
wordArray.each do |letter|
match = false
consonants.each do |character|
if character == letter then
match = true
break
end
end
if match == false
wordArray[i] = "_"
end
i = i + 1
end
word = wordArray.join(" ")
3.times do |i| # 允许用户猜三次,i初始值为0
Console_Screen.cls
puts "正在想......\n\n\n"
print "这是你的线索: " + word + "\n\n\n"
print "这个单词是?"
reply = STDIN.gets
reply.chop!
reply = reply.downcase
if reply == shortWord
Console_Screen.cls
print "恭喜你,猜中啦!按回车继续。"
Console_Screen.pause
break
else
Console_Screen.cls
if i == 1
print "注意了,还有最后一次机会。按回车继续。"
elsif i == 2
puts "对不起,你输了。"
print "正确答案是 " + shortWord + " 按回车继续。"
else
print "别灰心,还有机会。按回车继续。"
end
Console_Screen.pause
end
end
end
def play_game
word = select_word
Console_Screen.cls
consonant = get_consonant
Console_Screen.cls
vowel = get_vowel
shortWord = word.gsub(" ", "")
prompt_for_guess(shortWord, word, consonant, vowel)
Console_Screen.cls
end
# 关于这个游戏
def display_credits
Console_Screen.cls
puts "\t\t感谢玩这个游戏.\n\n\n"
puts "\n\t\t由jyfeather开发.\n"
puts "\t\t网址:http://www.yanjin.info\n\n\n"
end
end
##### 实例化脚本对象 #####
Console_Screen = Screen.new # 实例化Screen对象
WordGuess = Game.new # 实例化Game对象
WordGuess.display_greeting # 执行Game的display_greeting方法
answer = "" # 全局变量,用以控制循环
# 无限循环,直到玩家输入'y'或'n'为止
loop do
Console_Screen.cls
print "准备好开始玩游戏了吗?(y/n):"
answer = STDIN.gets
answer.chop! # 去掉最后的回车符
break if answer =~ /^[y|n]$/i
end
# 分析用户的输入
if answer =~ /n/i
Console_Screen.cls
puts "换个时间再来玩哟!\n\n"
else
WordGuess.display_instructions # 显示游戏玩法
loop do
WordGuess.play_game
print "按Q退出,或按其他键继续:"
playAgain = STDIN.gets
playAgain.chop!
break if playAgain =~ /q/i
end
WordGuess.display_credits # 显示游戏信息
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment