Tic tac toe - unsure why I'm failing play_turn spec

Hi, I am failing the spec for play_turn, and the error is “no implicit conversion of symbol into integer”. When I tried debugging by stepping through the method, I do “step” and it skips all the way too the end of the method instead of stepping through, so I’m not sure what’s going on.

here’s my error:
Failure/Error: game.play_turn
TypeError:
no implicit conversion of Symbol into Integer
# ./lib/board.rb:12:in []' # ./lib/board.rb:12:in[]’
# ./lib/board.rb:25:in empty?' # ./lib/game.rb:41:inplay_turn’
# ./spec/game_spec.rb:37:in `block (3 levels) in <top (required)>’

here’s my play_turn code:

def play_turn
move = current_player.get_move

if board.empty?(move)
  board.place_mark(move, current_player.mark)
  switch_players!
else
  puts "That space is already taken. Try again."
end

current_player.display(board)

end

and here’s my board.empty? code, which may be the problem:

def
row, col = pos
grid[row][col]
end

def []=(pos, mark)
row, col = pos
grid[row][col] = mark
end

def empty?(pos)
self[pos] == nil
end

Any ideas what the problem is?

Can I see what method you’ve written on line 12 in your Board class? The error might be there according to the stack trace.

Line 12 is the line that says “grid[row][col]” within the method:
def
row, col = pos
grid[row][col]
end

My original post has a typo, it should say “” after “def” for that method.

weird, it keeps cutting off the empty bracket and pos in parentheses after def. It should be def followed by empty brackets, followed by pos in parentheses.

Hmm, your method on line 12 is correct. Can you also post your place_mark method?

def place_mark(pos, mark)
self[pos] = mark
end

Hi Amanda,

It’s going to turn out that the issue has more to do with a rigid spec than with your code, but this is a classic error, and one you will encounter many times throughout your coding career, so it’s worth it to take the time to look into. First let’s discuss what the error actually is.

TypeError:
no implicit conversion of Symbol into Integer

A TypeError means that Ruby was expecting one type of data, but got a different type which caused the error. In this case, Ruby was expecting an Integer but got a Symbol instead. So why did this cause an error? If we look at the first line of the stack trace, we can see that the error happened in a bracket method, specifically when your code was attempting to key into an array. This makes sense: when we key into an array, Ruby always expects an Integer and any other type of data (such as a Symbol) will throw an error. For example:

array = [“a”, “b”]
array[1] == “b”
array[:one] == TypeError

Great. Now lets follow the stack trace to find the root cause of the error. We know that the error was thrown on line 12 of your [ ] bracket method, and was passed down from board.empty? in #play_turn. We can see that the argument passed to #empty? is the return value of current_player.get_move. Take a look there and you’ll find your culprit.

I recommend specifically placing a debugger on line 40 in your play_turn method, right above board.empty, and checking the value of the ‘move’ variable. Any time you are debugging multiple methods at the same time, it’s often helpful to place debuggers in each of the methods so you can just continue through them and don’t have to worry about stepping over too much.

Hope this helps!

-Jason