I'm trying to figure out how to select an array value if it is adjacent to a filled array value and is empty.
So let's say the grid the array references is laid out like so.
a1,a2,a3,
b1,b2,b3,
c1,c2,c3
If a1 holds an X the grid will now look like so.
X,a2,a3,
b1,b2,b3,
c1,c2,c3
I want the method to be able to find the adjacent array values, in this case, a2, b1 and b2.
But in this next case where O holds the center value.
X,a2,a3,
b1,O,b3,
c1,c2,c3
...the method would select a2, b1
Can anyone provide a solution or a tip?
This is a simple tic-tac-toe grid
Here is a look at the class where the grid lives.
class Board
attr_reader :grid
def initialize(cell_value = " ")
@grid = {
a1: cell_value, a2: cell_value, a3: cell_value,
b1: cell_value, b2: cell_value, b3: cell_value,
c1: cell_value, c2: cell_value, c3: cell_value
}
end
def printgrid
board = "\n"
board << "a #{@grid[:a1]}|#{@grid[:a2]}|#{@grid[:a3]} \n"
board << "----------\n"
board << "b #{@grid[:b1]}|#{@grid[:b2]}|#{@grid[:b3]} \n"
board << "----------\n"
board << "c #{@grid[:c1]}|#{@grid[:c2]}|#{@grid[:c3]} \n"
board << "----------\n"
board << " 1 2 3\n"
end
def space_taken?(cell_location)
cell_value = cell_location
@grid[cell_value] != " "
end
end