//
class Board
{
	int	xSize = 10;
	int	ySize = 10;
	int routeLayer1[][] = new int[xSize][ySize];
	int routeLayer2[][] = new int[xSize][ySize];
	int routePins1[][] = new int[xSize][ySize];
	int routePins2[][] = new int[xSize][ySize];

	Board()
	{
		int	i;
		int	j;

		System.out.print("Construct Board\n");

		for ( i=0; i<10; i++ )
			for( j=0; j<10; j++ )
			{
				routeLayer1[i][j] = 0;
			}
		// Read list of pins and mark on routeLayer1
		routeLayer1[0][1] = 5;
		routeLayer1[0][3] = 5;
		routeLayer1[0][5] = 5;
		routeLayer1[0][7] = 5;

		routeLayer1[5][7] = 5;
		routeLayer1[9][7] = 5;
		routeLayer1[9][9] = 5;
		routeLayer1[9][1] = 5;
		
		for ( i=0; i<10; i++ )
			for( j=0; j<10; j++ )
			{
				routeLayer2[i][j] = 0;
			}

		for ( i=0; i<10; i++ )
			for( j=0; j<10; j++ )
			{
				routePins1[i][j] = 0;
			}
		// Read list of pins and mark on routePins1
		routePins1[0][1] = 1;
		routePins1[0][3] = 2;
		routePins1[0][5] = 3;

		routePins1[5][7] = 1;
		routePins1[9][7] = 2;
		routePins1[9][9] = 3;


		for ( i=0; i<10; i++ )
			for( j=0; j<10; j++ )
			{
				routePins2[i][j] = 0;
			}
			

	}

	int Read()
	{
		System.out.print("Read Board from file.\n");
		System.out.print("Not implemented yet.\n");
		return(1);
	}

	int Write()
	{
		int	i;
		int	j;
		System.out.print("Write Board to file\n");
		System.out.print("Not implemented yet.\n");
		System.out.print("Display board w/ text.\n");

		System.out.print("routeLayer1:\n");
		for ( j=0; j<10; j++ )
		{
			for( i=0; i<10; i++ )
			{
				System.out.print(routeLayer1[i][j]);
			}
			System.out.println();
		}
		//System.out.print("routePins1:\n");
		//for ( j=0; j<10; j++ )
		//{
		//	for( i=0; i<10; i++ )
		//	{
		//		System.out.print(routePins1[i][j]);
		//	}
		//	System.out.println();
		//}

		return(1);
	}

	int Route( int iStart, int jStart, int iStop, int jStop )
	{
		//int	iStart = 0;
		//int	jStart = 1;
		int	iCurrent = iStart;
		int	jCurrent = jStart;
		//int	iStop = 5;
		//int	jStop = 7;
		double	angleToStop;

		
		System.out.print("Route Board\n");

		// Clear stop point to make it routable.
		// Will reset to pin code (5) when done routing this net.
		System.out.print("Set routeLayer1[iStop][jStop] = 0 for routing.\n");
		routeLayer1[iStop][jStop] = 0;

		while( ( ( iStop - iCurrent ) != 0 ) || ( ( jStop - jCurrent ) != 0 ) )
		{

		    angleToStop = Math.atan2( ( jStop - jCurrent ), ( iStop - iCurrent ) );

		    // If the target to route to is to the east
		    if ( ( angleToStop <= ( Math.PI / 4 ) ) && ( angleToStop > -( Math.PI / 4 ) ) )
		    {
			System.out.print("Target is to the east:" + ( ( angleToStop / ( Math.PI * 2 ) ) * 360 ) + "\n");
			if ( routeLayer1[(iCurrent + 1 )][jCurrent] == 0 ) 
			{
				System.out.print("East is clear -- moving east\n");
				routeLayer1[iCurrent][jCurrent] = 1;
				iCurrent++;
			}
			else if ( routeLayer1[iCurrent][( jCurrent + 1 )] == 0 )
			{
				System.out.print("East is not clear -- look south\n");
				System.out.print("South is clear -- moving south\n");
				routeLayer1[iCurrent][jCurrent] = 2;
				jCurrent++;
			}
			else if ( routeLayer1[iCurrent][( jCurrent - 1 )] == 0 )
			{
				System.out.print("East is not clear -- look south\n");
				System.out.print("South is not clear -- look north\n");
				System.out.print("North is clear -- moving north\n");
				routeLayer1[iCurrent][jCurrent] = 4;
				jCurrent--;
			}
			else if ( routeLayer1[( iCurrent - 1 )][jCurrent] == 0 )
			{
				System.out.print("East is not clear -- look south\n");
				System.out.print("South is not clear -- look north\n");
				System.out.print("North is not clear -- look west\n");
				System.out.print("West is clear -- moving west\n");
				routeLayer1[iCurrent][jCurrent] = 3;
				iCurrent--;
			}
			else 
			{
				System.out.print("East is not clear -- look south\n");
				System.out.print("South is not clear -- look north\n");
				System.out.print("North is not clear -- look west\n");
				System.out.print("West is not clear -- moving back\n");
				// Set this location to tried. (= 6?)
				routeLayer1[iCurrent][jCurrent] = 6;
				// Find which way is back
				if ( routeLayer1[( iCurrent + 1 )][jCurrent] == 1 ) iCurrent++;
				else if ( routeLayer1[iCurrent][( jCurrent + 1 )] == 2 ) jCurrent++;
				else if ( routeLayer1[( iCurrent - 1 )][jCurrent] == 3 ) iCurrent--;
				else if ( routeLayer1[iCurrent][( jCurrent - 1 )] == 4 ) jCurrent--;
				else
				{
					System.out.print("No way back, route failed\n");
					iCurrent = iStop;
					jCurrent = jStop;
				}
		
				// If here was the starting point move to Stop point, this route attempt failed.
				// Move there (set iCurrent and jCurrent.)
			}
		    }
		

		    // If the target to route to is to the north
		    else if ( ( angleToStop <= -( Math.PI / 4 ) ) && ( angleToStop > -( 3 * Math.PI / 4 ) ) )
		    {
			System.out.print("Target is to the north:" + ( ( angleToStop / ( Math.PI * 2 ) ) * 360 ) + "\n");
			if ( routeLayer1[iCurrent][( jCurrent - 1 )] == 0 ) 
			{
				System.out.print("North is clear -- moving north\n");
				routeLayer1[iCurrent][jCurrent] = 4;
				jCurrent--;
			}
			else if ( routeLayer1[( iCurrent + 1 )][jCurrent] == 0 )
			{
				System.out.print("North is not clear -- look east\n");
				System.out.print("East is clear -- moving east\n");
				routeLayer1[iCurrent][jCurrent] = 1;
				iCurrent++;
			}
			else if ( routeLayer1[( iCurrent - 1 )][jCurrent] == 0 )
			{
				System.out.print("North is not clear -- look east\n");
				System.out.print("East is not clear -- look west\n");
				System.out.print("West is clear -- moving west\n");
				routeLayer1[iCurrent][jCurrent] = 3;
				iCurrent--;
			}
			else if ( routeLayer1[( iCurrent - 1 )][jCurrent] == 0 )
			{
				System.out.print("North is not clear -- look east\n");
				System.out.print("East is not clear -- look west\n");
				System.out.print("West is not clear -- look south\n");
				System.out.print("South is clear -- moving south\n");
				routeLayer1[iCurrent][jCurrent] = 2;
				jCurrent++;
			}
			else 
			{
			
				System.out.print("No direction is clear -- moving back\n");
				// Set this location to tried. (= 6?)
				routeLayer1[iCurrent][jCurrent] = 6;
				// Find which way is back
				if ( routeLayer1[( iCurrent + 1 )][jCurrent] == 1 ) iCurrent++;
				else if ( routeLayer1[iCurrent][( jCurrent + 1 )] == 2 ) jCurrent++;
				else if ( routeLayer1[( iCurrent - 1 )][jCurrent] == 3 ) iCurrent--;
				else if ( routeLayer1[iCurrent][( jCurrent - 1 )] == 4 ) jCurrent--;
				else
				{
					System.out.print("No way back, route failed\n");
					iCurrent = iStop;
					jCurrent = jStop;
				}


			}
		    }


		    // If the target to route to is to the south
		    else if ( ( angleToStop <= ( 3 * Math.PI / 4 ) ) && ( angleToStop > ( Math.PI / 4 ) ) )
		    {
			System.out.print("Target is to the south:" + ( ( angleToStop / ( Math.PI * 2 ) ) * 360 ) + "\n");
			if ( routeLayer1[iCurrent][( jCurrent + 1 )] == 0 )
			{
				System.out.print("South is clear -- moving south\n");
				routeLayer1[iCurrent][jCurrent] = 2;
				jCurrent++;
			}
			else if ( routeLayer1[( iCurrent - 1 )][jCurrent] == 0 )
			{
				System.out.print("South not clear, West is clear -- moving west\n");
				routeLayer1[iCurrent][jCurrent] = 3;
				iCurrent--;
			}
			else if ( routeLayer1[(iCurrent + 1 )][jCurrent] == 0 ) 
			{
				System.out.print("South, west not clear, East is clear -- moving east\n");
				routeLayer1[iCurrent][jCurrent] = 1;
				iCurrent++;
			}
			else if ( routeLayer1[iCurrent][( jCurrent - 1 )] == 0 )
			{
				System.out.print("South, west, east not clear, North is clear -- moving north\n");
				routeLayer1[iCurrent][jCurrent] = 4;
				jCurrent--;
			}
			else 
			{
				System.out.print("No direction is clear -- moving back\n");
				// Set this location to tried. (= 6?)
				routeLayer1[iCurrent][jCurrent] = 6;
				// Find which way is back
				if ( routeLayer1[( iCurrent + 1 )][jCurrent] == 1 ) iCurrent++;
				else if ( routeLayer1[iCurrent][( jCurrent + 1 )] == 2 ) jCurrent++;
				else if ( routeLayer1[( iCurrent - 1 )][jCurrent] == 3 ) iCurrent--;
				else if ( routeLayer1[iCurrent][( jCurrent - 1 )] == 4 ) jCurrent--;
				else
				{
					System.out.print("No way back, route failed\n");
					iCurrent = iStop;
					jCurrent = jStop;
				}

			}
		    }



		    // If the target to route to is to the west
		    else if ( ( angleToStop <= -( 3 * Math.PI / 4 ) ) && ( angleToStop > ( 3 * Math.PI / 4 ) ) )
		    {
			System.out.print("Target is to the west:" + ( ( angleToStop / ( Math.PI * 2 ) ) * 360 ) + "\n");
			if ( routeLayer1[( iCurrent - 1 )][jCurrent] == 0 )
			{
				System.out.print("West is clear -- moving west\n");
				routeLayer1[iCurrent][jCurrent] = 3;
				iCurrent--;
			}
			else if ( routeLayer1[iCurrent][( jCurrent - 1 )] == 0 )
			{
				System.out.print("West not clear, North is clear -- moving north\n");
				routeLayer1[iCurrent][jCurrent] = 4;
				jCurrent--;
			}
			else if ( routeLayer1[iCurrent][( jCurrent + 1 )] == 0 )
			{
				System.out.print("West, north not clear, South is clear -- moving south\n");
				routeLayer1[iCurrent][jCurrent] = 2;
				jCurrent++;
			}
			else if ( routeLayer1[(iCurrent + 1 )][jCurrent] == 0 ) 
			{
				System.out.print("West, north, south not clear East is clear -- moving east\n");
				routeLayer1[iCurrent][jCurrent] = 1;
				iCurrent++;
			}
			else 
			{
				System.out.print("No direction is clear -- moving back\n");
				// Set this location to tried. (= 6?)
				routeLayer1[iCurrent][jCurrent] = 6;
				// Find which way is back
				if ( routeLayer1[( iCurrent + 1 )][jCurrent] == 1 ) iCurrent++;
				else if ( routeLayer1[iCurrent][( jCurrent + 1 )] == 2 ) jCurrent++;
				else if ( routeLayer1[( iCurrent - 1 )][jCurrent] == 3 ) iCurrent--;
				else if ( routeLayer1[iCurrent][( jCurrent - 1 )] == 4 ) jCurrent--;
				else
				{
					System.out.print("No way back, route failed\n");
					iCurrent = iStop;
					jCurrent = jStop;
				}


			}
		    }

		    else
		    {
			System.out.print("ERROR: Direction to target not found.\n");
		    }
		}
		
		// Change stop point back to code for pin (or wire end).
		System.out.print("Set routeLayer1[iStop][jStop] = 5(pin/end wire).\n");
		routeLayer1[iStop][jStop] = 5;
		
		return(1);
	}
}

class Router
{
	public static void main( String args[] )
	{
		Board myBoard = new Board();

		myBoard.Read();
		myBoard.Route( 0, 1, 5, 7 );
		myBoard.Route( 0, 3, 9, 7 );
		myBoard.Route( 0, 5, 9, 9 );
		myBoard.Route( 0, 7, 9, 1 );
		myBoard.Write();
	}
}
