/**
*          Tim Teitelbaum (s ):
*          Sample Solution:
*          Solution for Step 1: Textual Output
*
*          CS100J Program 2 - Fall 2002
*
*          Draws a series of paths based on a list of commands provided by the user.
*		   Compute the number of edges for each paths, as well the number of paths.
*/
import java.io.*;
public class GraphicsPaths{
//public class P2Step1 {
      /**
      *  The main method.
      *  @param args Command line argument list
      */
      public static void main( String args[] )
      {
             // Initialize Text object in to read from standard input.
                TokenReader in = new TokenReader(System.in);

             int numOfPaths = 0;  // Current number of paths completed.
             int numOfEdges = 0;  // Number of edges in current path.
             int command;         // Current command, or 2, or -1.
	     	 int orientation = 0; // Current orientation: 0, 1, 2, or 3 signifying
			                      // right, down, left, and up, respectively.
			 int row, col;         // Current position of current path, or <-1, -1>.
			 int rowPrev, colPrev;// Previous position of current path.

			 // Range limitations on positions (optional).
             final int ROWMIN=1, COLMIN=1, ROWMAX=99, COLMAX=99;

			 // Input starting point, or <-1,-1>.
			    row = in.readInt(); col = in.readInt();

			 // Input first command, or 2, or -1.
                command = in.readInt();

             // Draw paths until the user enters -1 -1 -1.
                while ( row != -1  )
                {
                   if (command == -1 ) // Turn left 90 degrees
                         orientation = (orientation + 3) % 4;
                   else if ( command == +1 ) // Turn Right 90 degrees
                         orientation = (orientation + 1) % 4;
                   else if ( command == 0 ) // Step forward 1 unit w.r.t. current orientation.
						{
							// Record current point as previous point.
                               rowPrev = row; colPrev = col;

							// Increment row
							   if (orientation==0) row++;
							   else if (orientation ==2) row--;

							// Increment col
							   if (orientation==1) col++;
							   else if (orientation ==3) col--;

							// Print warning if position is out of bounds (optional).
            				   if (row<ROWMIN || row>ROWMAX || col<COLMIN || col>COLMAX)
			    			      System.out.println("The current path is out of the limit!");

							// Draw line from <rowPrev,colPrev> to <row,col>.
							   System.out.println("Drawing line from <" + rowPrev + ", " +
								   + colPrev + "> to <" + row + ", " + col + ">");

							numOfEdges++;
						}
                   else if (command == 2 ) // End of path
                   		{
                   			numOfPaths++;

                        	System.out.println("Path #" + numOfPaths + " has " +
								+ numOfEdges + " edges." );

						 	// Input starting point of next path, or <-1,-1>.
			 			 	   row = in.readInt(); col = in.readInt();

             			 	orientation = 0;
						 	numOfEdges = 0;
						 }
					else // Bad command (optional).
						System.out.println("Skipping bad command: " + command);

				  // Input next command, or 2, or -1.
               	     command = in.readInt();

			   } // end while

			System.out.println("Total number of paths: " + numOfPaths);
   		}
}

