Tic Tac Toe - Computer Player Board

Hi! I’m having trouble understanding how we need to initialize the board in the Computer player and why we need display to use it.

  attr_accessor :board

def initialize(name)
@board = board
@name = name
@mark = :0

def display(board)

This is what I current have on my code, which is what I understood from the lectures. I’m getting this error:

Failure/Error: expect(comp.board).to eq(board)

   expected: #<Board:0x00007f83643ade98 @grid=[[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]>
        got: nil
   (compared using ==)

I’ve tried setting the display method to self, but that doesn’t correct the right answer, and if I set it to Board.new, I get a new instance of the board, not the one that was created in the game class. What am I doing wrong?


You’re really close!

In your display method, you declare the instance variable @board but you don’t set its value to anything. Instance variables default to nil, which is why your test is getting nil. You have to set the value of @board in your display method.

Also, since you will be setting @board in the display method, you don’t need to set it in initialize :slight_smile:

Hope this helps!


1 Like

Thanks! This worked! Could you explain why? I don’t get why we couldn’t just initialize the board variable in the initialize method

Hi mafeg95,

Like what Jason said, you should be declaring your board instance variable in the display method because that is what the specs want. Also, if you look at your initialize method. You set @board = board. However, board is not defined there, but rather in your display method. So, you have access to the current board object in your display method not in your initialize method.

There is nothing restricting you from having the board variable in the initialize method. You just have to add the board as an argument like so def initialize(name, board). However, the specs want the board initialized differently so :man_shrugging:


1 Like