Problem Solving: Navigation with Arrays

Allow movement some places, and prevent it in others

In the problem solving topic around mapping objects on a Ms. Pacman grid, we learned how to convert two dimensional array information into coordinates for accurately placing dot images in the right places.

The core concept-using a data structure to make decisions-is also useful in solving one of the classic problems in video gaming: How do you allow movement in some places, but restrict in in others, for example, moving a person through a maze?

People have tried all sorts of other strategies to make this happen. Beginners often want to make no-go areas a certain color on the screen, and then read the screen. But this is slow (reading pixels off the screen is goes through many layers of the operating system) and ends up restricting realistic colors.

Imagine you have the following situation:

Which way, Blinky?

A data structure to match

var mazeArray = [	[0,1,0,0],	// use as: mazeArray[row][col]
			[0,1,0,0],
			[0,1,1,1],
			[0,0,0,0]	];
			

And a way to visualize coordinates

col-1 col col+1 col+2
row-2 col-1
row-2
col
row-2
col+1
row-2
col+2
row-2
row-1 col-1
row-1
col
row-1
col+1
row-1
col+2
row-1
row col-1
row
col+1
row
col+2
row
row+1 col-1
row+1
col
row+1
col+1
row+1
col+2
row+1
1. What is mazeArray[0][1]? click for answer...
2. What is the current position of Blinky in mazeArray? click for answer...
3. What are the mazeArray coordinates of the cell above Blinky? click for answer...
4. Looking at the coordinate chart, how can you express MOVING UP as rows and columns? click for answer...

From our class conversation, it is obvious that if we take walls into consideration, our problem has to be broken down into two parts:

  1. Check ahead into mazeArray to see if it is OK to move into there
  2. If it is OK, then trigger the output function that animates in that direction

Blinky is in a maze. Blinky can clearly go up or right, but cannot go down or left. It is obvious if you can just look at it. But how would you create a function to determine whether or not you can move into the new any square?

	function itCanMoveThatWay (row, column, direction, mazeArray) {	// PROCESS: check if you can move that direction...
		// direction can be: "up", "down", "right", or "left"
		var result =false;	// this function will return false by default
		
		// So what do you think goes in here?
		
		
		
		
		
		return result; 	// return if it can move that way as either true || false
	}