/**
Maze.java

Starter code for HW6
*/

import java.util.*;

public class Maze {
    public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;

    public Maze(int myRows, int myCols) {
	rows = myRows;
	columns = myCols;
	vWalls = new boolean[rows+1][columns];
	hWalls = new boolean[rows][columns+1];
	path = new boolean[rows][columns];
    }

    /**
       Main test function
    */
    public static void main(String [] args) {

	int rows = 30, cols = 20;

	Maze test = new Maze(rows,cols);

	System.out.println("The maze is \n"+test);
	

	// try removing random walls and output the resulting maze
	Random rand = new Random();
	for (int i=0; i<100; i++) {
	    test.removeWall(rand.nextInt(rows),rand.nextInt(cols),
			    rand.nextInt(4));
	    System.out.println("The maze is \n"+test);
	}

	for (int i=0; i<rows; i++)
	    test.drawPath(i,3);
	System.out.println("The maze is \n"+test);
	
    }

    /**
      Converts maze into an ASCII drawing representation
    */
    public String toString() {
	StringBuilder sb = new StringBuilder();
	
	for (int i=-1; i<rows; i++) {
	    for (int j=0; j<columns; j++) {
		
		if (i>=0 && !hWalls[i][j])
		    sb.append("|");
		else
		    sb.append(" ");
		
		if (!vWalls[i+1][j])
		    if (i>=0&& path[i][j])
			sb.append("_o");
		    else
			sb.append("__");		
		else
		    if (i>=0&& path[i][j])
			sb.append(" o");
		    else
			sb.append("  ");
	    }
	    if (i>=0 && !hWalls[i][columns])
		sb.append("|\n");
	    else
		sb.append(" \n");
	}
	return sb.toString();
    }

    /**
       wallCheck returns whether a wall exists in a certain direction
       from the i j square of the maze
     */
    public boolean wallCheck(int i, int j, int direction) {
       	boolean ret;
	switch(direction) {
	case UP:
	    ret = vWalls[i][j];
	    break;
	case DOWN:
	    ret = vWalls[i+1][j];
	    break;
	case LEFT:
	    ret = hWalls[i][j];
	    break;
	case RIGHT:
	    ret = hWalls[i][j+1];
	    break;
	default:
	    return false;
	}
	return ret;
    }

    /**
       removeWall takes an ij square and a direction and removes the
       wall at that location
    */
    public void removeWall(int i, int j, int direction) {
	switch(direction) {
	case UP:
	    vWalls[i][j] = true;
	    break;
	case DOWN:
	    vWalls[i+1][j] = true;
	    break;
	case LEFT:
	    hWalls[i][j] = true;
	    break;
	case RIGHT:
	    hWalls[i][j+1] = true;
	    break;
	default:
	    return;
	}	
    }

    /**
       Accessor for size
    */
    public int rows() {
	return rows;
    }

    /**
       Accessor for size
    */
    public int columns() {
	return columns;
    }

    /**
       drawPath draws an x in the square indicated
    */
    public void drawPath(int i, int j) {
	path[i][j] = true;
    }

    private boolean[][] path;
    private boolean[][] vWalls;
    private boolean[][] hWalls;
    private int rows, columns; 
    
}
