나는 두 사람의 체스 게임을 명령 행에서 수행하고 있습니다. 나는 모든 종류의 체스 조각과 보드 클래스를위한 클래스를 가지고 있습니다. 보드 클래스는 다음과 같습니다 다른 객체로 객체를 비 직렬화하기
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
어떤 파일이 어떤 파일에 속합니까? 우리는 그것없이 백 트레이스를 따라갈 수 없습니다. 그리고 오류의 원인 인'조각 '이라고 부르는 것을 보여 주신 곳은 없습니다. – sawa
조각은 조각 클래스의 모든 유형에있는 인스턴스입니다. –