Hello, 因此,我正在撰写一场有关青少年的游戏,我有这一方法“作用”,我内部有两处。 当我使用“返回”的方法退出该方法时,该方法一直得到执行。
以下是我守则的一部分。 我用“第#########################################”来记述的两条线。
因此,当这种条件“面值3=0&序列化?”是真实的,它应当停止方案,退出,然而,它又执行了“引诱”的方法,而且这一方法甚至不会停留在一旁。 谁能帮助我? 非常感谢。
class Game
def play
if introduce == c ####################
unserialize
else
@white.pieces = @white.create_new_pieces
@black.pieces = @black.create_new_pieces
end
set_up
loop do
board.display
player = count.even? ? @white : @black
opponent = count.even? ? @black : @white
return if count % 3 == 0 && serialize? ###########################
loop do
move = get_move(player) #[piece, postion, destination]
next if move.nil?
piece = move[0]
position = move[1]
destination = move[2]
if piece.name == K
next if is_eaten_by(opponent.pieces, destination).length > 0
if piece.path_valid?(board, destination)
change(piece, position, destination, opponent)
piece.castle = false
else
next unless player.check_castle? && castle_succeed?(player, piece, destination)
end
else
player_king_coor = player.get_piece( K )[0].position
if piece.name == p
pawn_move = pawn_valid_move(piece, position, destination, opponent)
next unless pawn_move
change(piece, position, destination, opponent, pawn_move)
if is_eaten_by(opponent.pieces, player_king_coor).length > 0
undo(piece, position, destination, opponent)
next
end
pawn_promote(player, piece)
else
next unless piece.path_valid?(board, destination)
piece.castle = false if piece.name == R
change(piece, position, destination, opponent)
if is_eaten_by(opponent.pieces, player_king_coor).length > 0
undo(piece, position, destination, opponent)
next
end
end
end
break
end
modify_in_passing(player)
result = is_checkmated?(player, opponent)
result = tie?(player, opponent) unless result
if result != false
annouce_result(result)
board.display
break
end
@count += 1
end
end
def introduce
puts Welcome to the game!
unless File.zero?( ./lib/saved_game.yml )
loop do
puts "Press n if you want to play new game and c if you want to continue previous game"
answer = gets.chomp.strip
return answer if answer == n || answer == c
puts "Please enter a valid answer!"
end
end
end
end
game = Game.new
game.play
I tried to add a "puts" after the condition "if count % 3 == 0 && serialize?" and it was not printed. And I also tried to reproduce the problem by making a simple class and method with loop and everything works fine. I use Vscode.