-1

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
9
  • What is the structure of the grid? Is it 2D? How is it related to an array? What is a "center value"? Commented Oct 22, 2012 at 9:14
  • how on earth does someone get 11000 points on a 2year account? Commented Oct 22, 2012 at 9:24
  • While Stefan's method below will work, your most fundamental problem here is that you are using a 1D data structure to represent a 2D situation. Make grid a 2D structure and the whole problem becomes a lot easier to understand. Commented Oct 22, 2012 at 9:29
  • I'd also suggest doing this on paper or something first - getting too caught up in the code may well be what led you to have this problem. Commented Oct 22, 2012 at 9:30
  • @BenParsons I'm a noob at ruby. I've been building this game for months. I didn't even know to ask the question "should this be a 2d structure" Commented Oct 22, 2012 at 9:31

1 Answer 1

1

You could implement a method to determine if a cell is next to an X:

def next_to_x?(cell_location)
  case cell_location
  when :a1
    @grid[:a2] == 'X' or @grid[:b1] == 'X' or @grid[:b2] == 'X'
  when :a2
    @grid[:a1] == 'X' or @grid[:a3] == 'X' or @grid[:b1] == 'X' or @grid[:b2] == 'X' or @grid[:b3] == 'X'
  # ...
  end
end

Then, to find all cells that are both empty and next to an X:

[:a1, :a2, :a3, :b1, :b2, :b3, :c1, :c2, :c3].find_all { |cell_location|
  !space_taken(cell_location) and next_to_x?(cell_location)
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you I think I'll try this answer to my original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.