import java.awt.*; // needed for Graphics, Color
import java.util.Random;
/**
 * The FlippistCabsDemo class is the Flippist Cabdriver simulation main program.
 *
 * Navigate the grid of streets of New York City by making random turns.
 * There are two cabs, yours and that of a partner.
 *
 * Let the life philosophy of Flipism guide your decisions, be a Flippist.
 * Choose your life's direction with the toss of a coin: heads or tails.
 * 
 * The first version is to have your cab and the other cab turn in opposite
 * directions, determined by random chance.
 *
 * Will you meet up? How soon?
 *
 * <p>
 * Uses 
 * {@link LineVector}
 * {@link DrawingPanel}
 *
 * @author Fred Kral
 * @author AddYourNameHere
 * @version 1.1
 *
 */

   public class FlippistCabsDemo1 {

   /* Fields */
   public static final int BOARD_WIDTH = 550;
   public static final int BOARD_LENGTH = 550;
   public static final int XSTART = BOARD_WIDTH/2;
   public static final int YSTART = BOARD_LENGTH/2;
   public static final int BLOCK_WIDTH = 15;
   public static final int BLOCK_LENGTH = 15;
   public static final int SLEEPTURN = 25;//ms
   
   public static void main (String[] args){
   
      boolean continueDrive = true;
   
      DrawingPanel panel = new DrawingPanel(BOARD_WIDTH, BOARD_LENGTH); // canvas
      Graphics g = panel.getGraphics();  // paintbrushes
      Color bkgColor = Color.WHITE;
   
      System.out.println("Greetings Flippist Cabdrivers");
      
      while (continueDrive) {
      
         /* Clear Board */
         g.setColor(Color.WHITE);
         g.fillRect(0, 0, BOARD_WIDTH, BOARD_LENGTH);
      
         /* Cab colors */
         Color cab1TrailColor = new Color(255, 0, 0);
         Color cab2TrailColor = new Color(0, 255, 0);
      
         /* Initial positions */
         LineVector vCab1 = new LineVector(XSTART, YSTART, XSTART, YSTART-BLOCK_LENGTH);//start South
         LineVector vCab2 = new LineVector(XSTART, YSTART, XSTART, YSTART-BLOCK_LENGTH);//start South
      
         /* Paint initial positions */
         g.setColor(cab1TrailColor); 
         g.drawLine( vCab1.x(), vCab1.y(), vCab1.x2(), vCab1.y2() );
         
         g.setColor(cab2TrailColor); 
         g.drawLine( vCab2.x(), vCab2.y(), vCab2.x2(), vCab2.y2() );
      
         /* Random number generator, better than Math.random */
         Random rand = new Random();
      
         /* Stop when cabs are together, meet again */
         boolean cabsTogether = false; // start does not count
         
         /* statistics */
         int nTurns = 0;
      
         /* Principal loop for moving the cabs across the board/grid/panel/canvas */
         while (  vCab1.onGrid(BOARD_WIDTH, BOARD_LENGTH) &&
                  vCab2.onGrid(BOARD_WIDTH, BOARD_LENGTH) && 
                 (! cabsTogether )  ){
         
            
            /* Turning determination: heads or tails */
            int coinFlip1 = rand.nextInt(2); // 0 or 1
         
            boolean rightTurn1 = coinFlip1 > 0;
            boolean rightTurn2 = ! rightTurn1;  // Turn In Opposite Direction
         
            /* CAB 1 */
            /* Wait, Turn, Paint trail, new cab position */
            
            panel.sleep(SLEEPTURN);
            
            vCab1.turn( rightTurn1, BLOCK_WIDTH, BLOCK_LENGTH ); // Turn a random direction
            
            g.setColor(cab1TrailColor); 
            g.drawLine( vCab1.x(), vCab1.y(), vCab1.x2(), vCab1.y2() );
         
            /* CAB 2 */
            /* Wait, Turn, Paint trail, new cab position */
            
            panel.sleep(SLEEPTURN);
            
            vCab2.turn( rightTurn2, BLOCK_WIDTH, BLOCK_LENGTH ); 
            
            g.setColor(cab2TrailColor); 
            g.drawLine( vCab2.x(), vCab2.y(), vCab2.x2(), vCab2.y2() );
            
            /* Are the cabs together? */
            cabsTogether = vCab1.equalsFront(vCab2);
            
            /* statistics */
            nTurns++;
         
         }// end while on board
         
      
        /* statistics */    
         System.out.println(" nTurns = " + nTurns + " Together " + cabsTogether);         
            
         /* Click to continue */     
         System.out.println("Click anywhere to Continue [Upper left corner to Quit]"); 
         while (! panel.mousePressed() ) { } // wait for mouse press             
         while (panel.mousePressed() ) { } // wait for mouse release
         // condition for continuing the while loop (x and y positions of mouse)         
         continueDrive = panel.getMouseX() > BOARD_WIDTH/10 || panel.getMouseY() > BOARD_LENGTH/10;
      
      } // end while continue
      
      System.out.println("quitting");
      panel.close();
      System.exit(0);
   
   }// END method main
   
}// END class