Hi there! I have been able to pass all the specs for my tic tac toe method, but am having trouble getting it to all work together, even after looking at the answer key. Seems like there is something wrong with my computer_player method but would love some help going through it.
I’m only posting on here because I’m not sure how to private message someone.
Hey David, don’t worry about private messaging when you hit these kind of bugs. This is exactly what the forum is for!
In your screenshots, the most important thing to look at is our error message. We can see that on line 17 from computer_player.rb in our
get_move method, we’re trying to bracket index into
nil(in other words, there’s nothing at that reference point), which means that somewhere in your get_move code, we’re passing the wrong object. It looks to me like the only thing we’re indexing into is
@board. Can you use byebug or pry to find out what is stored at @board at that time?
Hi there, thank you for the timely response!!
This is what happened using byebug. @board seems to have values in it.
I randomly also tried @board.grid[row][col] == nil and got a similar error, but this time “undefined method grid for nil class”
Perhaps I’m not referencing the board class correctly?
I’m scratching my head on this one. Byebug clearly shows that you have a @board in memory, but the error message we get is that there is an
undefined method for nil: NilClass on line 17, in your each, in get move.
Can you run through the code step by step and see if the method gets called later on with nothing saved at board?
so after hours…
I found out where the error was coming from.
In my play_turn method of the game class,
I said loc = current_player.get_move.
When I put that directly into my place_mark method, it works. But if I set it equal to loc, and then try to put loc into my place_mark method, it does not work.
I think this mistake is occurring because I’m calling the get_move on current_player before current_player has been set. Wow, such an easy mistake to make!
This is the code that works. Notice that loc is created once the player has been set. We cannot call a get_move method on current player, if we haven’t set the current player.
What confuses me is that current player is set to the previous player that just moved. So shouldn’t loc be set to the previous players move instead of failing?
Did this error occur because there was no previous players move because it was the first move?
sorry for all the writing!
Would it make sense to avoid our if/else logic here and just call
get_move on current_player? Then we might be able to just avoid some of the bugs altogether.
I wrote it that way because thats how i was thinking about it.
But you’re right, it would have helped avoid the bugs and also looks a lot cleaner if I do it your way.