2017-10-08 73 views
-1

我正在命令行中玩雙棋棋局遊戲。我爲每一種類型的棋子和一個板類都有一堂課。董事會類看起來是這樣的:使用其他對象反序列化它的對象

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

哪個部分屬於哪個文件?沒有它,我們不能跟蹤回溯。而且你所顯示的那個地方沒有任何地方叫'piece',這是導致錯誤的原因。 – 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文件中獲得純ruby哈希來構建正確的遊戲類。

或者如果你實際上不關心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

我把它運用到了元帥身上。謝謝。 –