ruby - Proc causing a random TypeError -
i'm refactoring code , proc
causing error randomly , don't know why or how debug it... ideas?
new code proc
defense_moves, offense_moves = [], [] determine_move = ->move,side,i { side << move.count(move[i]) } defense.size.times { |i| determine_move.(defense, defense_moves, i) } offense.size.times { |i| determine_move.(offense, offense_moves, i) } dm = defense[defense_moves.index(defense_moves.max)].nil? ? [0] : defense[defense_moves.index(defense_moves.max)] om = offense[offense_moves.index(offense_moves.max)].nil? ? [0] : offense[offense_moves.index(offense_moves.max)]
original code:
d = 0 defense_moves = [] loop defense_moves << defense.count(defense[d]) break if defense.count(defense[d]).zero? d += 1 end o = 0 offense_moves = [] loop offense_moves << offense.count(offense[o]) break if offense.count(offense[o]).zero? o += 1 end dm = defense[defense_moves.index(defense_moves.max)].nil? ? [0] : defense[defense_moves.index(defense_moves.max)] om = offense[offense_moves.index(offense_moves.max)].nil? ? [0] : offense[offense_moves.index(offense_moves.max)]
typeerror
ttt2.rb:95:in `[]': no implicit conversion nil integer (typeerror) ttt2.rb:95:in `computer_make_move' ttt2.rb:133:in `draw_board' ttt2.rb:24:in `place' ttt2.rb:209:in `block in start_new_game' ttt2.rb:188:in `loop' ttt2.rb:188:in `start_new_game' ttt2.rb:199:in `block in start_new_game' ttt2.rb:188:in `loop' ttt2.rb:188:in `start_new_game' ttt2.rb:199:in `block in start_new_game' ttt2.rb:188:in `loop' ttt2.rb:188:in `start_new_game' ttt2.rb:199:in `block in start_new_game' ttt2.rb:188:in `loop' ttt2.rb:188:in `start_new_game' ttt2.rb:199:in `block in start_new_game' ttt2.rb:188:in `loop' ttt2.rb:188:in `start_new_game' ttt2.rb:234:in `<main>'
so, before code that:
defense_moves, offense_moves = [], [] determine_move = -> move, side, { side << move.count(move[i]) } (defense.size + 1).times { |i| determine_move.(defense, defense_moves, i) } (offense.size + 1).times { |i| determine_move.(offense, offense_moves, i) } dm = defense[defense_moves.index(defense_moves.max)].nil? ? [0] : defense[defense_moves.index(defense_moves.max)] om = offense[offense_moves.index(offense_moves.max)].nil? ? [0] : offense[offense_moves.index(offense_moves.max)]
(defense/offence.size + 1)
, cause break after adding array.
i'm not sure proper or not, guess works same loops.
Comments
Post a Comment