Battling to pass Battleship spec


Battleship… I blasted through all other specs, but I cannot get #attack to pass for the life of me.
Please, SOS!

The error:

   1) BattleshipGame#attack marks the board at the specified position
 Failure/Error: game.attack([1, 1])
   no implicit conversion from nil to integer
 # ./lib/board.rb:14:in `[]'
 # ./lib/board.rb:14:in `[]'
 # ./lib/board.rb:19:in `[]='
 # ./lib/battleship.rb:15:in `attack'
 # ./spec/battleship_spec.rb:24:in `block (3 levels) in <top (required)>'

Using the syntactic sugar from the Board class, my #attack method is:

def attack(pos)
  board[pos] = :x

It should be that easy, right?!! :tired_face:
The amount of time wasted on a single spec is a bit discouraging.

The most annoying part is that when I test it out in pry, the method works and it places the x on the board!!!

Ok. I got it working, but I’m still confused.
I needed to use .grid when creating the instance in BattleshipGame class.

def initialize(player, board=nil)
  @player = player
  @board =    <<<<<<< why do I need .grid???? 

For tic-tac-toe I didn’t use .grid… I used @board = and that was fine.

Why am I needing to use .grid here?

Did you create a []= method in the board class? It should look like the code below.

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

I did indeed create the []= method in my board class. If you look at the screenshot I posted, the method was working in pry but wouldn’t pass the spec.

I’m not sure where I went wrong… I think I had my Board#initialize set up incorrectly. I was so confused I ended up basically rewriting/starting from scratch.

All working now (and no longer need .grid), but definitely wasted a lot of time on an issue that is still very unclear.

Thanks for your reply!