ruby - TicTacToe无效移动方法问题

ruby - TicTacToe无效移动方法问题,第1张

以下是我对TicTacToe游戏的看法。到目前为止它有效,但它并不完美。我遇到了其中一种方法的问题 - Game#invalid_move_check?在游戏问你“在哪里:”一旦选择新的目的地后,游戏就会改变符号,就好像它是一个新的转弯。实际上不是,假设在下一回合之前保持相同的玩家符号。

P.S代码可能需要一些重构。我正处于学习阶段。

class  Game

def initialize(symbol) 
    @board  = Array.new(3){Array.new(3)}
    # [0,1,2]
    # [3,4,5]
    # [6,7,8]
    @symbol = ["X", "O"]
end

WINNING_COMBO = [
  # Horizontal wins:
  [0, 1, 2], [3, 4, 5], [6, 7, 8],
  # Vertical wins:
  [0, 3, 6], [1, 4, 7], [2, 5, 8],
  # Diagonal wins:
  [0, 4, 8], [2, 4, 6]
]

def create_players
    # create both players 
    @names = []

    print "Please enter the name of the first player: "
    @player_1 =gets.chomp
    @names << @player_1

    print "Please enter the name of the second player: "
    @player_2 = gets.chomp
    @names << @player_2 

    puts "\n"
    puts"welcome #{@player_1.upcase} and #{@player_2.upcase}"
    puts"------------------------------------------------"
    puts"\n"

    puts "Randomizing who'll start..."
    puts"\n"
    # assign player by calling the player_assigment function that will determine who will start first
    player_assigment
    puts"\n"

end

def player_assigment
    # merge the names array and symbol array 
    # with the zip method and return a nested array with player and symbol.  
    @choice = @names.zip(@symbol)
    # iterate over the choice nested array and 
    # print out each player and their assigned symbol
    @choice.each do |player, symbol|
       puts "#{player.upcase} will use #{symbol}"
    end

end

def current
    @current = @names.first
    @current
end

def switch_turn
    @current = @names.last
    @current
end

def first_turn
    current
    puts "#{@current.upcase} turn"
    @marker = @symbol.first
    make_move(@marker)
end

def next_turn
    switch_turn
    puts "#{@current.upcase} turn"
    @marker = @symbol.last
    make_move(@marker)
end

def check_win?(first_arr, second_arr)
    WINNING_COMBO.select do |item|
        if 
            item == first_arr 
            puts"#{@player_1} won!!"
        elsif 
            item == second_arr
            puts "#{@player_2} won!!"
        end
    end

end

def mapping(move, marker)
     case move
        when 0
          arr_index = 0
          index = 0
          invalid_move_check?(arr_index,index)
          @board[0][0] = marker
        when 1
          arr_index = 0
          index = 1
          invalid_move_check?(arr_index,index)
          @board[0][1] = marker
        when 2
          arr_index = 0
          index = 2
          invalid_move_check?(arr_index,index)
          @board[0][2] = marker
        when 3
          arr_index = 1
          index = 0
          invalid_move_check?(arr_index,index)
          @board[1][0] = marker
        when 4
          arr_index = 1
          index = 1
          invalid_move_check?(arr_index,index)
          @board[1][1] = marker
        when 5
          arr_index = 1
          index = 2
          invalid_move_check?(arr_index,index)
          @board[1][2] = marker
        when 6
          arr_index = 2
          index = 0
          invalid_move_check?(arr_index,index)
          @board[2][0] = marker
        when 7
          arr_index = 2
          index = 1
          invalid_move_check?(arr_index,index)
          @board[2][1] = marker
        when 8
          arr_index = 2
          index = 2
          invalid_move_check?(arr_index,index)
          @board[2][2] = marker
    end
end

def invalid
    puts"move invalid"
end

def invalid_move_check?(arr_index, index)
    array = @board
    if array[arr_index][index]  == "X" || 
        array[arr_index][index]  == "O"
        invalid
        puts "Where to :"
        @move = gets.chomp.to_i
        mapping(@move,@marker)
    end
end

def make_move(marker)
    # after each turn the make_move method will called to place move on the board 
    puts "Where to :"
    @move = gets.chomp.to_i
    mapping(@move,@marker)
    print_board
end

# display board in a matrix format 
def print_board
    @board.each_slice(1) { |a| p a }
    puts"\n"
end

def instructions
    puts "Instructions :Enter your first move by entering  a number 1-9" 
    puts "corresponding to the grid on the bottom and press enter"
    puts"\n"
    puts "0 | 1 | 2 ",             
         "----------",              
         "3 | 4 | 5 ",             
         "----------",            
         "6 | 7 | 8 " 
    print"\n"

end

def self.start(symbol)
    # start a new game 
    new_game =Game.new(symbol)
    # create players
    new_game.create_players
    new_game.instructions
    new_game.print_board
    # Checking wining combo for matching patter if none 
    while new_game.check_win?(@move_first, @move_second) do
    new_game.first_turn
    # the player switch turn 
    new_game.next_turn
    end 
end

loop do
    puts"------------------------------------------------"
    puts"              Welcome to tictactoe              ".upcase
    puts"------------------------------------------------"
    print"\n"
     Game.start(@symbol)

end

end

最佳答案:

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

#will return true or false to check validity of move
def invalid_move_check?(arr_index, index)
    array = @board
    if array[arr_index][index]  == "X" || 
        array[arr_index][index]  == "O"
        invalid
        puts "Where to :"
        @move = gets.chomp.to_i
        mapping(@move,@marker)
        return true            
    end
return false
end

def mapping(move, marker)
     case move
        ...
        when 0
          arr_index = 0
          index = 0
          unless invalid_move_check?(arr_index,index) #change all cases
            @board[0][0] = marker #to have the assignment of board
                                  #only if the move is valid
          end
          ...
     end
end 

您的错误的原因是,即使移动无效,也会发生分配。

这只是解决当前问题的创可贴解决方案,至于重构,可以采取一些措施来优化代码并使其更好:)但是你仍然需要先修复你的结果&#39;结束& #39 ;.重构自己的代码将是一个非常好的做法。祝你红宝石快乐 !

答案 1 :(得分:0)

在抓取前面的代码并重新思考代码之后,我已经提出了这个解决方案,并不优雅,因为我希望它是有效的.i在if / else块代码中移动符号赋值,因此当移动无效下一步移动将使用当前播放器的符号

 def mapping(move, symbol)
         case move
            when 0
                if @board[0][0]=="X" || @board[0][0] == "O"
                   invalid
                   make_move
                else
                   @board[0][0] = symbol
                   track_move(@move)
                   print_board
                end
               ...
           end
  end
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复