/**
 * Shows a "Mosaic" square in which a red square wanders around...
 */
public class Wanderer {
	
	public static void main(String[] args) {

		int rows, columns;  // Number of rows and columns in the mosaic;
		rows = 30;
		columns = 40;
		Mosaic.open(rows,columns,15,15);  // Create the mosaic window.
		
		int currentRow, currentCol;  // The current position of the red square.
		
		currentRow = rows/2;   // Put the red square at the center of the mosaic.
		currentCol = columns/2;
		Mosaic.setColor( currentRow, currentCol, 255, 0, 0);
		
		int steps; // How many steps has the red square taken?
		steps = 0;
		
		while (Mosaic.isOpen()) {  // Move red square to a neighboring position.
			
			int nextRow, nextCol;  // Next position of the red square
			
			switch ( (int)(4 * Math.random()) ) {
			case 0: // Move up.
				nextRow = currentRow - 1;
				nextCol = currentCol;
				if (nextRow < 0)  // Square is off top of screen; move it to bottom.
					nextRow = rows - 1;
				break;
			case 1: // Move down.
				nextRow = currentRow + 1;
				nextCol = currentCol;
				if (nextRow >= rows)  // Square is off bottom of screen; move it to top.
					nextRow = 0;
				break;
			case 2: // Move left.
				nextRow = currentRow;
				nextCol = currentCol - 1;
				if (nextCol < 0)  // Square is off left edge of screen; move it to right edge.
					nextCol = columns - 1;
				break;
			default:  // Move right.
				nextRow = currentRow;
				nextCol = currentCol + 1;
				if (nextCol >= columns)   // Square is off right edge of screen; move it to left edge.
					nextCol = 0;
			}
			
			Mosaic.setColor(currentRow, currentCol, 0, 0, 0);  // Make current position black.
			Mosaic.setColor(nextRow, nextCol, 255, 0, 0); // Make next position red.
			
			currentRow = nextRow; // Keep track of the current position, which has just changed.
			currentCol = nextCol;
			
			steps++;
			Mosaic.setTitle(steps + " steps taken");
			
			Mosaic.delay(50);  // Limits steps to about 20 per second.
		}
	}

}
