ruby - TicTacToe invalid move method issue -
below take tictactoe game. far works, it's not perfect. i'm having issue 1 of methods - game#invalid_move_check? after game asked "where to:"once choose new destination , game change symbol if new turn .in fact not , suppose keep same player symbol until next turn.
p.s code need refactoring. i'm in learning phase.
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 name of first player: " @player_1 =gets.chomp @names << @player_1 print "please enter name of second player: " @player_2 = gets.chomp @names << @player_2 puts "\n" puts"welcome #{@player_1.upcase} , #{@player_2.upcase}" puts"------------------------------------------------" puts"\n" puts "randomizing who'll start..." puts"\n" # assign player calling player_assigment function determine start first player_assigment puts"\n" end def player_assigment # merge names array , symbol array # zip method , return nested array player , symbol. @choice = @names.zip(@symbol) # iterate on choice nested array , # print out each player , assigned symbol @choice.each |player, symbol| puts "#{player.upcase} 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 |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 :" @move = gets.chomp.to_i mapping(@move,@marker) end end def make_move(marker) # after each turn make_move method called place move on board puts "where :" @move = gets.chomp.to_i mapping(@move,@marker) print_board end # display board in matrix format def print_board @board.each_slice(1) { |a| p } puts"\n" end def instructions puts "instructions :enter first move entering number 1-9" puts "corresponding grid on bottom , press enter" puts"\n" puts "0 | 1 | 2 ", "----------", "3 | 4 | 5 ", "----------", "6 | 7 | 8 " print"\n" end def self.start(symbol) # start new game new_game =game.new(symbol) # create players new_game.create_players new_game.instructions new_game.print_board # checking wining combo matching patter if none while new_game.check_win?(@move_first, @move_second) new_game.first_turn # player switch turn new_game.next_turn end end loop puts"------------------------------------------------" puts" welcome tictactoe ".upcase puts"------------------------------------------------" print"\n" game.start(@symbol) end end
this should trick:
#will return true or false 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 :" @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 cases @board[0][0] = marker #to have assignment of board #only if move valid end ... end end
the reason of bug assignment happens if move invalid.
this band-aid solution current problem, refactoring ,there things can done optimize code , make better :) still have fix first 'ending'. refactoring own code practice. wish joyful ruby journey !
Comments
Post a Comment