2017-10-08 4 views
-1

나는 두 사람의 체스 게임을 명령 행에서 수행하고 있습니다. 나는 모든 종류의 체스 조각과 보드 클래스를위한 클래스를 가지고 있습니다. 보드 클래스는 다음과 같습니다 다른 객체로 객체를 비 직렬화하기

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

내 다른 클래스

는 다음과 같이 :

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

이가 내 방법이 있습니다 :

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":[[" ","#<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>"," "]]} 

내가 데이터를 다시로드하려고, 보드를 표시하는 방법이 오류가 발생합니다 : 23,414,저장 한 후 saved.json 파일은 다음과 같습니다

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

아니, 문제는 객체가 이 문자열로을 저장 있다는 것입니다. deserialization 잘 작동합니다,이 경우. 게임 조각의 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

육군 원수를 사용하여 일하게했습니다. 고맙습니다. –