English 中文(简体)
尽管我没有把我的方法放在一旁,但废墟中仍然 ex灭。
原标题:ruby keep excute my method eventhough I did not put it in a loop
  • 时间:2023-06-14 02:54:57
  •  标签:
  • ruby
  • repeat
  • 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.

问题回答

暂无回答




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签