Ruby Can';t在我的hangman项目中加载保存的游戏
我在一个刽子手项目中工作,以完成 游戏运行正常,但我完全迷失在加载已保存的游戏部分的过程中 如何恢复变量以继续游戏 为什么当我加载游戏并放置一个字母进行猜测时,“each.with.index”方法会给我一个错误 如何使我的代码加载到游戏中Ruby Can';t在我的hangman项目中加载保存的游戏,ruby,yaml,Ruby,Yaml,我在一个刽子手项目中工作,以完成 游戏运行正常,但我完全迷失在加载已保存的游戏部分的过程中 如何恢复变量以继续游戏 为什么当我加载游戏并放置一个字母进行猜测时,“each.with.index”方法会给我一个错误 如何使我的代码加载到游戏中 require "yaml" class Play attr_accessor :random_word def main_menu puts "------------------------------------------------
require "yaml"
class Play
attr_accessor :random_word
def main_menu
puts "------------------------------------------------------"
puts "------------------------------------------------------"
loop do
puts "Please select,: (N)ew game, (L)oad, (E)xit"
choice=gets.chomp.upcase
case choice
when "N"
new_game
break
when "L"
load
when "E"
puts "\nthanks for playing! see you soon!"
exit
else
puts "\nPlease select 'N' for new game, 'L' to load an existing game, or 'E' to exit"
end
end
end
def save_game
directory_name = "saved_games"
Dir.mkdir(directory_name) unless File.exists?(directory_name) #if "saved_games folrder doesnt exist create one"
puts "\nSelect a name to save your game"
save_name=gets.chomp
File.open("saved_games/#{save_name}.yaml","w").puts YAML.dump(self) #Save the information in another file
puts "\nGame saved!"
end
def load
display_saved_games
puts "\nWrite the name of the saved game you want to load"
load_game=gets.chomp
if File.exists?("saved_games/#{load_game}.yaml")
loaded_game=YAML::load(load_game)
game_loop
else
puts "\nNo saved game with that name found, please try again"
load
end
end
def display_saved_games #Display avaiable saved games
directory_name = "saved_games"
Dir.mkdir(directory_name) unless File.exists?(directory_name)
if Dir.glob('saved_games/*').empty?
puts "No saved games found!"
main_menu
else
puts "Found the following saved games"
puts Dir.glob('saved_games/*').join("\n")
end
end
def new_game #Generate a new word and set everything for a new game
@random_number=rand(61406) #Generate a random number
@random_word=File.readlines("5desk.txt")[@random_number].chomp.to_s.upcase #read the random number generated line
@correct_position=@random_word.split(//) #split the word in individual letters
@correct_guesses=Array.new #Create the list of the correct answers
@incorrect_guesses=Array.new #Create the list of the incorrect answers
@random_word.length.times {@correct_guesses<<"_ "} #It puts one underscore in guess array for each letter in word
puts "\nThe secret word is..\n\n"
@correct_guesses.each_with_index { |value, index| print @correct_guesses[index]} #draw underscores. one for each letter
puts "\n\n"
game_loop
end
def draw
puts "=============================================================================\n"
@correct_guesses.each_with_index { |value, index| print @correct_guesses[index]} #Current state of guessed correctly letters
puts "\n\nLetters not in secret word: "
@incorrect_guesses.each_with_index { |value, index| print @incorrect_guesses[index] + ", "} #letters guessed not in the word
end
def turn
puts "\nSelect a letter or write 'SAVE' to save your game and continue it later, or 'EXIT' to end the game" #Ask for letter or to save the game
try=gets.chomp.to_s.upcase #Reads the letter
if try=="SAVE"
save_game
elsif try=="EXIT"
puts "\n\n\nThanks for playing, see you soon!"
exit
else
check_correct(try) #Check if correct
end
end
def check_correct(try)
guessed_right=false
@correct_position.each_with_index do |value, index| #matches each letter with the guess attemp
if try==value
@correct_guesses[index]=try #if they are equal, deletes underscore and puts the correct letter
guessed_right=true
end
end
if guessed_right==false #if no letter was equal
if @incorrect_guesses.include?(try)==false #put the letter to the incorrect guesses array just if not included before
@incorrect_guesses<<try
else
guessed_right=true #Avoid the counter to go down if select a repeated letter
puts "\nYou already said that letter before!"
end
end
return guessed_right
end
def check_win
if @correct_guesses==@correct_position #it is a win if the guessed array is equal to the letters array
return true
puts "\n\n\nCongratulations!! you win!"
else
return false
end
end
def game_loop #Game logic
@turn_counter=0
loop do
while @turn_counter<10
if turn==false
@turn_counter+=1
puts "You have #{10-@turn_counter} turns left!"
end
draw
if check_win
break
end
end
puts "\n\n\n\nYou are out of turns, you lose!!!"
puts "\n the secret word was #{@random_word}"
break
end
end
end
start=Play.new.main_menu
需要“yaml”
课堂游戏
属性存取器:随机字
def主菜单
放入“--------------------------------------------------------------”
放入“--------------------------------------------------------------”
环道
放置“请选择::(N)新游戏,(L)加载,(E)退出”
choice=get.chomp.upcase
案例选择
当“N”时
新游戏
打破
当“L”
负载
当“E”
放入“\n感谢您的参与!再见!”
出口
其他的
放置“\N请为新游戏选择“N”,为“L”加载现有游戏,或为“E”退出”
结束
结束
结束
def保存_游戏
目录\u name=“已保存的\u游戏”
Dir.mkdir(directory_name),除非File.exists?(directory_name)#如果“saved_游戏文件夹不存在,请创建一个”
放置“\n选择一个名称以保存游戏”
save_name=gets.chomp
文件.open(“saved_games/#{save_name}.yaml”,“w”)。放置yaml.dump(self)#将信息保存在另一个文件中
放入“\n已保存名称!”
结束
def负载
显示保存的游戏
放入“\n写入要加载的已保存游戏的名称”
load\u game=get.chomp
如果文件.exists?(“saved_games/#{load_games}.yaml”)
加载的游戏=YAML::加载(加载游戏)
博弈循环
其他的
放置“\n未找到具有该名称的已保存游戏,请重试”
负载
结束
结束
def display_saved_games#显示可用的已保存游戏
目录\u name=“已保存的\u游戏”
Dir.mkdir(目录名),除非File.exists?(目录名)
如果Dir.glob('saved_games/*')。为空?
放置“未找到已保存的游戏!”
主菜单
其他的
放置“找到以下已保存的游戏”
放置目录glob('saved_games/*')。加入(“\n”)
结束
结束
def new#u game#生成新词并为新游戏设置所有内容
@随机数=兰德(61406)#生成一个随机数
@random_word=File.readlines(“5desk.txt”)[@random_number].chomp.to_.upcase#读取生成的随机数行
@correct_position=@random_word.split(//)#将单词拆分为单个字母
@correct_guesses=Array.new#创建正确答案的列表
@不正确的猜测=Array.new#创建不正确答案的列表
@随机字长乘以{@correct\u猜测您正在调用每个只为新游戏定义的@correct\u position
上带有索引的\u。加载现有游戏时,您需要定义@correct\u position
。请将代码添加到问题正文中,而不是因为可能过期的外部链接可能位于已关闭或已关闭的网站上d已被删除,或由未知的第三方控制。您可以编辑您的问题以添加与手头问题相关的任何代码,以帮助人们调查、重现和解决您的问题。请尝试将YAML::load更改为YAML::load_文件