尝试使用JSON序列化我的Ruby Hangman游戏中的保存方法

时间:2017-08-23 04:18:21

标签: json ruby serialization

我试图让我的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

1 个答案:

答案 0 :(得分:1)

您需要close该文件,以确保您的输出实际写入磁盘(&#34;刷新&#34;)。您可以手动拨打close

def save_game(string)
  game_file = File.new("saved.json","w") 
  game_file.write(string)
  game_file.close
end

或者,你可以使用File.open,当块结束时,它会占用一个块并close文件:

File.open("saved.json", "w") do |game_file|
  game_file.write(string)
end

因为写入磁盘是一个缓慢的操作,Ruby(以及我现在能想到的所有语言)将暂停实际写入文件,直到它在缓冲区中累积了一定量的文本。一旦达到此限制,它将刷新缓冲区并将其中的所有内容写入磁盘。为了确保在尝试编写文件时实际写入所有文本,您需要在文件上调用close,并且作为关闭它的一部分,Ruby将刷新其缓冲区中剩余的内容。

还有其他方法可以确保您的内容已刷新,但当您刚刚开始了解这些内容时,只需确保始终close个文件就足够了完成阅读或写作。