【发布时间】:2018-01-31 12:06:42
【问题描述】:
我正在尝试让我的 save_game() 方法工作,没有任何内容写入 JSON 文件。这是我在一般情况下使用 JSON 和序列化的第一个任务。我什至不确定我哪里出错了。
这些是我的序列化方法:
def to_json
JSON.generate({array: @array, filestuff: @filestuff, random_word: @random_word, cipher: @cipher, random_word2: @random_word2, counter: @counter})
end
def load
game_file = File.read("saved.json")
data = JSON.parse(game_file)
@cipher = data["cipher"]
@random_word2 = data["random_word2"]
@counter = data["counter"]
end
def save_game(string)
game_file = File.new("saved.json","w")
game_file.write(string)
game_file.close
end
这是我的程序,在第 92 行我尝试调用我的 save_game 方法。
require 'json'
load 'display.rb'
class Hangman
attr_accessor :name
@name = name
def initialize
puts "What is your name?"
@name = gets.chomp
puts "
################################################
HANGMAN
################################################
_________
|
| |
| O
| /|\\
| |
| / \\
|
-----------------
Welcome #{@name} to Hangman. The computer will generate
a 5-12 letter random word. You will try to guess
that word one letter at a time. Try to solve the
puzzle before time runs out!
"
end
end
class Gameplay
attr_accessor :array, :filestuff, :random_word, :cipher, :random_word2, :counter
def initialize
@array = []
@filestuff = File.foreach('5text.txt') do |x|
chomped = x.chomp
@array << chomped if (chomped.length >= 5 and chomped.length <= 12)
end
@random_word = @array.sample
@cipher = @random_word.gsub(/[a-z]/, '*').split(//)
@random_word2 = @random_word.split(//)
@counter = 5
def to_json
JSON.generate({array: @array, filestuff: @filestuff, random_word: @random_word, cipher: @cipher, random_word2: @random_word2, counter: @counter})
end
def load
game_file = File.read("saved.json")
data = JSON.parse(game_file)
@cipher = data["cipher"]
@random_word2 = data["random_word2"]
@counter = data["counter"]
end
def save_game(string)
game_file = File.new("saved.json","w")
game_file.write(string)
game_file.close
end
def choice(n)
@random_word2.each_with_index do |i,index|
if i == n
@cipher[index] = i
end
end
if n == @random_word2.join.to_s
puts "You win"
puts "would you like to start another game? Y/N"
new_game = gets.chomp
if new_game == "Y"
Hangman.new
else exit
end
end
if @random_word2.include?(n) == false
@counter -= 1
display
puts "#{@counter} guesses remaining."
puts "To save press 1"
save = gets.chomp
if save == "1"
#Will not save
save_game($b.to_json)
end
end
if @counter == 0
puts "would you like to start another game? Y/N"
new_game = gets.chomp
if new_game == "Y"
else exit
end
end
puts @cipher.join
end
@counter = 5
while @counter > 0
choice(gets.chomp)
end
end
end
Hangman.new
$b = Gameplay.new
【问题讨论】:
标签: json ruby serialization