2017-10-08 4 views
-1

私はチェスの2人プレイゲームをコマンドラインで演奏しています。私はすべてのタイプのチェスピースとボードクラスのクラスを持っています。オブジェクトを他のオブジェクトでデシリアライズする

class Board 
    attr_accessor :board, :choice 
    def initialize 
    @board = Array.new(8){Array.new(8," ")} 
    @choice = choice 
    end 
end 

私の他のクラスはこのようになります:ボードクラスは次のようになります

class Bishop 
    attr_accessor :x_position, :y_position, :piece, :color, :counter, :moves 
    def initialize(position,boolean) 
    @x_position = position[0] 
    @y_position = position[1] 
    @piece = boolean ? "♝" : "♗" 
    @color = boolean ? "white" : "black" 
    @counter = 0 
    @moves = [[+1,-1], 
    [+1,+1], 
    [-1,+1], 
    [-1,-1]] 
    end 

私はこのようなボードに自分の作品を追加しました:

@board[0][0] = Rook.new([0,0],false) 

は、これらは私の方法にありますデータをシリアル化および逆シリアル化する:

def to_json 
    JSON.generate({board: @board}) 
end 

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

def load_game 
    game_file = File.read("saved.json") 
    data = JSON.parse(game_file) 
    @board = data["board"] 
end 

は保存した後、saved.jsonファイルは次のようになります。私は、データをバックロードしようとすると、ボードを表示する方法は、このエラーをスロー

{"board":[[" ","#<Knight:0x00000000e4fc28>","#<Bishop:0x00000000e4fa20>","#<Queen:0x00000000e4f890>","#<King:0x00000000e4f610>","#<Bishop:0x00000000e4f3e0>","#<Knight:0x00000000e4f278>","#<Rook:0x00000000e4e1c0>"],[" "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "],["#<Rook:0x00000000e4fd90>","#<Knight:0x00000000e4ed78>","#<Bishop:0x00000000e4eb70>","#<Queen:0x00000000e4ea08>","#<King:0x00000000e4e7b0>","#<Bishop:0x00000000e4e580>","#<Knight:0x00000000e4e3f0>"," "]]} 

0 1 2 3 4 5 6 7 
+----+----+----+----+----+----+----+----+ 
0|   /home/jacob/Desktop/chess/board.rb:30:in `block (2 levels) in display': undefined method `piece' for "#<Knight:0x00000000e4fc28>":String (NoMethodError) 
    from /home/jacob/Desktop/chess/board.rb:27:in `each' 
    from /home/jacob/Desktop/chess/board.rb:27:in `each_with_index' 
    from /home/jacob/Desktop/chess/board.rb:27:in `block in display' 
    from /home/jacob/Desktop/chess/board.rb:20:in `each' 
    from /home/jacob/Desktop/chess/board.rb:20:in `each_with_index' 

それは私のように見えます問題は、オブジェクトが文字列として戻ってくることですか?

私の表示方法:

def display 
    axis = 0 
    print " 0 1 2 3 4 5 6 7" 
    @board.each_with_index do |row,index| 
     print "\n" 
     @draw = " +----+----+----+----+----+----+----+----+" 
     puts @draw 
     print axis 
     axis +=1 
     if index.even? 
     row.each_with_index do|column,i| 
      if i.odd? 
      if column != " " 
       print "|"+" #{column.piece} ".bruno 
      else print "|"+" #{column} ".bruno 
      end 
      else 
      if column != " " 
       print "|"+" #{column.piece} " 
      else print "|"+" #{column} " 
      end 
      end 
     end 
     else 
     row.each_with_index do|column,j| 
      if j.even? 
      if column != " " 
       print "|"+" #{column.piece} ".bruno 
      else print "|"+" #{column} ".bruno 
      end 
      else 
      if column != " " 
       print "|"+" #{column.piece} " 
      else print "|"+" #{column} " 
      end 
      end 
     end 
     end 
     print "|" 
    end 
    print "\n" 
    print @draw 
    end 
+0

どの部分がどのファイルに属していますか?我々はそれなしでバックトレースに従うことはできません。そして、エラーの原因となっている「ピース」と呼ばれる場所には何も表示されません。 – sawa

+0

ピースは、すべての種類のピースクラスのインスタンスです。 –

答えて

1

いいえ、問題は、あなたのオブジェクトを文字列としてをを保存しているということです。この場合、逆シリアル化は正常に機能します。あなたは、ゲームピースのjson表現になるべきものを明示的に指定する必要があります。このようなもの:

class Rook 
    def to_json(*) 
    { name: 'rook', position: 'A1', status: 'in_game' }.to_json 
    end 
end 

pieces = [Rook.new] 
pieces.to_json # => "[{\"name\":\"rook\",\"position\":\"A1\",\"status\":\"in_game\"}]" 
JSON.parse(pieces.to_json) # => [{"name"=>"rook", "position"=>"A1", "status"=>"in_game"}] 

デシリアライズ時には、逆の処理を行う必要があります。純粋なルビーハッシュから適切なゲームクラスを構築します。これはJSONファイルの解析から得られます。

実際にJSONを気にせずに、保存ファイル形式を作成したい場合は、Marshalがあなたの親友です。何も上書きする必要はありません。ゼロ摩擦。

pieces = [Rook.new] 
Marshal.dump(pieces) # => "\x04\b[\x06o:\tRook\x00" # write this to a file 
# restore it later 
Marshal.load("\x04\b[\x06o:\tRook\x00") # => [#<Rook:0x007fb50f825570>] 
+0

私はMarshalを使って動作させました。ありがとうございました。 –

関連する問題