// Mukkai Krishnamoorthy
// Subh Game 

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

// This is the main Applet class 


public class Subh1 extends Applet {


  final int MAXSPOTS = 5;
  final int MAXLINES = 10;
  Point starts[] = new Point[MAXLINES];
  Point ends[] = new Point[MAXLINES];
  Point anchor; // start of current line
  Point currentpoint; // end of current line
  Point mystarts[] = new Point[MAXLINES];
  Point myends[] = new Point[MAXLINES];
  int xspots[] = new int[5];
  int yspots[] = new int[5];

   private Choice B,C,D,E;
  private int choices=1; // Computer 2nd
  private Color co;
  public int initialize=0;

  private int randomS=1,optimalS=0;

  private int xValue = -10;
  private int yValue = -10;
  private int x1=-10, y1=-10;
  private int drag=0, remember=0;
  private int xtemp1,ytemp1,xtemp,ytemp;
  
  //
  // Arrow
  //
  protected final int arrowSize = 5;
  protected int _dir = 1;

  //
  //  For Game Variables
  //

  public int AdjMat[][],inDeg[],outDeg[];
  
  private int turn=choices-1;
  int totalEdgesSelected=0;
  boolean overOver=false;
  private int From, To;
  private int goesFirst = 0; // 0 is the first player 1 is the second player
  private boolean isHam=false;
  Label labelResults;
  public TextField compResp;

  // Add the GUI Components to the applet

  public void init()
  {
    int i,j;
    co =new Color(255,0,0); // red;
    //    setBackground(Color.white);

    // Game Variable Initialization

    AdjMat = new int[5][5];
    inDeg = new int[5];
    outDeg = new int[5];
    for ( i=0;i<5;i++)
      for( j=0; j < 5; j++)
	AdjMat[i][j] = 0;
    for(i=0;i<5;i++)
      {
      AdjMat[i][i]= 2;
      inDeg[i] = 0;
      outDeg[i]= 0;
      }
    addMouseMotionListener(new MotionHandler(this));
    addMouseListener(new DetailHandler(this));
    
    //labelResp = new Label("Computer Response");
    compResp = new TextField("",40);
    compResp.setEditable(false);
    
    // add(labelResp); 
    add(compResp);
    B = new Choice();
    B.add("Computer Second");
    B.add("Computer First");
    C = new Choice();
    C.add("Red");
    C.add("blue");
    C.add("green");
    D = new Choice();
    D.add("random");
    D.add("optimal");
    E = new Choice();
    E.add("Restart");
    add(C); add(D);add(B);add(E);
    B.addItemListener(new BHandler(this));
    C.addItemListener(new CHandler(this));
    D.addItemListener(new DHandler(this));
    E.addItemListener(new EHandler(this));
    
    xspots[0] = 150; yspots[0]= 150;
    xspots[1] = 250; yspots[1]= 150;
    xspots[2] = 150; yspots[2]= 250;
    xspots[3] = 250; yspots[3]= 300;
    xspots[4] = 350; yspots[4]= 250;
    

    }

  // reinitializing
  public void init1()
  {
    int i,j;
    co =new Color(255,0,0); // red;
    setBackground(Color.pink);

    // Game Variable Initialization

    AdjMat = new int[5][5];
    inDeg = new int[5];
    outDeg = new int[5];
    for ( i=0;i<5;i++)
      for( j=0; j < 5; j++)
	AdjMat[i][j] = 0;
    for(i=0;i<5;i++)
      {
      AdjMat[i][i]= 2;
      inDeg[i] = 0;
      outDeg[i]= 0;
      }
    //        initialize=1;
	turn =0;
	if (randomS==1){ randomS=1;optimalS=0;}
	else {optimalS=1; randomS=0;}
	goesFirst = 0;
	drag =0;
	isHam = false;
	choices = 1;
	overOver =false;
	compResp.setText("New Game");
	totalEdgesSelected=0;
	repaint();
	//setBackground(Color.white);

}

  // Does the drawing

  public void paint(Graphics g)
  {
    int i,j;
    String s = new String();
    g.setColor(co);
    if ((initialize%2) == 1)
      {
	setBackground(Color.pink);
	g.setColor(Color.pink);
	g.fillOval(200,200,150,150);
	g.fillRect(100,100,400,400);
      }
    else
      {
	setBackground(Color.yellow);
	g.setColor(Color.yellow);
	g.fillOval(200,200,150,150);
	g.fillRect(100,100,400,400);
      }
    if ((drag == 1 ) && (remember%5 == 0) )
      { 
	if (initialize%2 ==1) g.setColor(Color.pink);
	else g.setColor(Color.yellow);
      g.fillRect(125,125,300,300);
	}
    g.setColor(co);
    for (i=0;i<5;i++)
      {
	g.setColor(Color.red);
	g.fillOval(xspots[i], yspots[i], 15, 15); 
	
	g.drawString(s.valueOf(i+1),xspots[i],yspots[i]-20);
      }
    g.setColor(co);
    if (drag==1) {
      if (remember%5==0)
	{ 
	  if (initialize%2==1) g.setColor(Color.pink);
	  else g.setColor(Color.yellow);
	  g.drawLine(xspots[From]+8,yspots[From]+8,xtemp1,ytemp1);
	  g.setColor(Color.red);
	  g.drawLine(xspots[From]+8,yspots[From]+8,xtemp,ytemp);
	}
    }

    for (i=0; i < 5; i++)
      for (j=0; j < 5; j++)
	{
	  if ((AdjMat[i][j] == 1) || (AdjMat[i][j] == 3))
	    {
	      { 
		g.drawLine(xspots[i]+8,yspots[i]+8,xspots[j]+8,yspots[j]+8);
		g.setColor(Color.black);
		//		g.drawLine(xspots[i]+(xspots[j]-xspots[i])/2,yspots[i]+
		//	(yspots[j]-yspots[i])/2,xspots[j],yspots[j]);
		g.fillPolygon( getArrow(i,j) );
		g.setColor(co);
	      }
	    }
	}
    if (overOver) check_ham();
    if (isHam) 
      {
	for (i=0; i < 5; i++)
	  for (j=0; j < 5; j++)
	    {
	      if (AdjMat[i][j] == 3)
		{
		  { g.setColor(Color.cyan);
		    g.drawLine(xspots[i],yspots[i],xspots[j],yspots[j]);
		    g.setColor(Color.black);
		    g.fillPolygon( getArrow(i,j) );
		    g.setColor(Color.cyan);
		  }
		}
	    }
      }
  }
  public void check_ham()
  {
    int i1,i2,i3,i4,i5;
    for (i1=0;(i1<5)&&(!isHam);i1++)
      for (i2=0;(i2<5)&&(!isHam);i2++)
	if (i1 !=i2)
	  for (i3=0;(i3<5)&&(!isHam);i3++)
	    if ((i1 != i3) && (i2 != i3))
	      for (i4=0;(i4<5)&&(!isHam);i4++)
		if ((i1 != i4) && (i2 != i4) && (i3 != i4))
		  for (i5=0;(i5<5)&&(!isHam);i5++)
		    if ((i1 != i5) && (i2 != i5) && (i3 != i5)&& (i4 != i5))
		      {
			if ((AdjMat[i1][i2]==1)&&(AdjMat[i2][i3]==1) 
			    && (AdjMat[i3][i4]==1) && (AdjMat[i4][i5]==1)
			    && (AdjMat[i5][i1]==1))
			  {
			    AdjMat[i1][i2]=3;
			    AdjMat[i2][i3]=3;
			    AdjMat[i3][i4]=3;
			    AdjMat[i4][i5]=3;
			    AdjMat[i5][i1]=3;
			    isHam=true;
			  }
		      }
  }
								     

  protected Polygon getArrow(int i, int j) 
  {	
    
    int dX = xspots[j] - xspots[i];
    int dY = yspots[j] - yspots[i]; 
    double len = Math.sqrt( dX*dX + dY*dY );
    double ndx = _dir*arrowSize*dX/len;
    double ndy = _dir*arrowSize*dY/len;
    double cx = (xspots[j]+8 + xspots[i]+8)/2;
    double cy = (yspots[j]+8+ yspots[i]+8)/2;
    Polygon tmp = new Polygon();
    tmp.addPoint( (int)(cx - ndy), (int)(cy + ndx) );
    tmp.addPoint( (int)(cx + ndx), (int)(cy + ndy) );
    tmp.addPoint( (int)(cx + ndy), (int)(cy - ndx) );
    tmp.addPoint( (int)(cx - ndy), (int)(cy + ndx) );
    return tmp;
  }

 public void update(Graphics g)
  {
    paint(g);
  }



  // Setter functions for class variables

  public void setC(int x,int y)
  {
     Point d = new Point(x, y);
     Point rd = error_checking(d);


      if (rd != null)
	{
         anchor = new Point(rd.x, rd.y);
	 xValue = x;
	 yValue = y;
	 From  = error_checking1(d);
	 // System.out.println(" " + xValue + " " + yValue);
	}
      else {
             anchor = null;
	     System.out.println("Please click on the existed points");
	   }



  }

  public void setCH(int x)
  {
    choices = x; 
    goesFirst = x -1;
    System.out.println(" choices = " + choices);
    if (choices == 2) { 
      turn =1 ;goStrat();
    }
  }

  public void setCO(int x)
  {
    if (x ==1) co = Color.red;
    if (x==2) co = Color.blue;
    if (x==3) co = Color.green;
    System.out.println("Color = " + co);
  }
  public void setSelect(int x)
  {
    if (x ==1) { randomS=1; optimalS=0;}
    if (x==2) { randomS=0; optimalS=1;}
    System.out.println( " Random " + randomS);
    System.out.println(" Optimal " + optimalS);
  }

  
  public void setM(int x, int y) 
  {
     Point d = new Point(x, y);
     Point rd = error_checking(d);
	 drag = 0;
      if (rd != null)
	{
         anchor = new Point(rd.x, rd.y);
	 To = error_checking1(d);

	 if (AdjMat[From][To]==0)
	   {
	     x1 = x;
	     y1 = y;
	     System.out.println("Your Choices are" + (From +1)+ ","+(To+1));
	     AdjMat[From][To] = 1;
	     AdjMat[To][From] = -1;
	     inDeg[To] +=1;
	     outDeg[From] +=1;
	     totalEdgesSelected++;
	     //	 System.out.println(" " + x1 + " " + y1);
	     repaint();
	     displayResult();
	   }
	 else 
	   { 
	     To = -1;
	     System.out.println(" Edge is Already There");
	   }
	}
      else
	{
	  anchor = null; To =- 1; 
	  System.out.println("Please click on the existed points");
	}
  }

public void setM1(int x,int y)
   { 
     drag=1;
	 xtemp  = x;
	 ytemp = y;
	 repaint(); 

	 if (remember%5 == 0) {
	   xtemp1 = xtemp;
	   ytemp1 = ytemp;
	 }
	 // System.out.println(" " + xValue + " " + yValue);
	 remember++;


  }

  public Point error_checking(Point p) {
       int rx, ry;
       Point R;

       for (int i = 0; i < MAXSPOTS; i++) {
          rx = Math.abs(p.x - xspots[i]);
          ry = Math.abs(p.y - yspots[i]);  
          if ( (rx < 15) && (ry < 15) )  {
	     R = new Point(xspots[i], yspots[i]);
  	     return R;
          }
       }

       return null;
    }

  public int error_checking1(Point p) {
       int rx, ry;
       Point R;
       int ans=-1;

       for (int i = 0; i < MAXSPOTS; i++) {
          rx = Math.abs(p.x - xspots[i]);
          ry = Math.abs(p.y - yspots[i]);  
          if ( (rx < 15) && (ry < 15) )  {
	    ans = i;
  	     return ans;
          }
       }

       return -1;
    }



public void displayResult()
  {
        
	 if (!gameover())
	   {
	     turn++;
	     if (turn==1) goStrat();
	     else 
	       {
		 turn=0;
		 compResp.setText("Your Turn");
	       }
	   }
	 else overOver = true;
  }

public boolean gameover()
  {
    int i=0,j=0;
    boolean over=true,over1=true;;
    
    while (over&&over1)
      {
	if ((inDeg[i] == 0) || (outDeg[i++] == 0))
	  over = false;
	if ((i==5)&&over) over1 = false;
      }
    if (!over1 && (totalEdgesSelected==10))
      {
	compResp.setText(" FirstPlayer Wins and Second Player loses!");
	overOver = true;
	repaint();
	return true;
      }
    if (!over1 && (totalEdgesSelected < 10))
      {
	return false;
      }
   
    if (totalEdgesSelected==10)
      {
	    compResp.setText("Second Player wins and First Player loses");
	    overOver = true;
	    return true;
      }
    over = true; over1= true;
    i=0;
    while (over&& over1)
      {
	if ((inDeg[i] ==4) || (outDeg[i++] ==4))
	  over = false;
	if (over&&(i==5))
	  over1=false;
      }
    
    if (!over)
      {
	compResp.setText("Second Player wins and First Player Loses");
	overOver = true;
	return true;
      }
     
    return false;
  }


public void randomStrat()
  {
    int i,j;
    int myFrom=20, myTo=20;
    boolean over;
     i = 0;

     over = true;
     if (!gameover()) 
       {
	 while ( (i < 5) && over) 
	   {
	     j = 0;
	     while ( ( j < 5) && over )
	       {
		 if ((AdjMat[i][j] ==0) && (AdjMat[j][i]==0))
		   {
		     myFrom = i;
		     myTo= j;
		     AdjMat[i][j]=1;
		     AdjMat[j][i] = -1;
		     inDeg[j] +=1;
		     outDeg[i] +=1;
		     totalEdgesSelected++;
		     System.out.println("My Choice " + (myFrom +1) + " ," + (myTo +1));
		     over = false;
		   }
		 j++;
	       }
	     i++;
	   }
	 compResp.setText("Your Turn");
	 System.out.println("My Choice " + (myFrom +1) + " ," + (myTo +1));
	 repaint();
	 displayResult();
       }
  }


  public void goStrat()
  { 
    System.out.println("I am Here");
    compResp.setText("My Turn");
    if (randomS ==1) randomStrat();
    else
      {
	if ((optimalS == 1)&& (goesFirst==1)) optimalStrat1();
	else 
	  if ((optimalS == 1)&& (goesFirst==0)) optimalStrat2();
	else 
	  System.out.println(" I am confused"+goesFirst+" Optimal Strategy "+
			     optimalS + " Random Strategy " +randomS);
      }
  }

  public void optimalStrat1()
  {
    int i,j,candFrom=-1,candTo=-1;
    boolean selected=false;
    int maxinDeg, maxoutDeg;
    boolean notover = true;
    check_ham();
    if (isHam) { compResp.setText("First Player Wins");return;}
    // Case 1 a
    for(i=0;i<5;i++)
      {
	if ((inDeg[i] == 3) && (outDeg[i]==0))
	  { 
	    candFrom = i;
	    for(j=0;j<5;j++)
	      {
		if (AdjMat[candFrom][j] ==0)
		  {
		    candTo = j;
		    AdjMat[candFrom][candTo] = 1;
		    AdjMat[candTo][candFrom] = -1;
		    inDeg[candTo] +=1;
		    outDeg[candFrom] +=1;
		    totalEdgesSelected++;
		    selected = true;
		  }
	      }
	  }
      }
    // Case 1 b
    if (!selected)
      {
	for(i=0;i<5;i++)
	  {
	    if ((outDeg[i] == 3) && (inDeg[i]==0))
	      { 
		candTo = i;
		for(j=0;j<5;j++)
		  {
		    if (AdjMat[j][candTo] ==0)
		      {
			candFrom = j;
			AdjMat[candFrom][candTo] = 1;
			AdjMat[candTo][candFrom] = -1;
			inDeg[candTo] +=1;
			outDeg[candFrom] +=1;
			totalEdgesSelected++;
			selected = true;
		      }
		  }
	      }
	  }
      }
    // case 2
    if (!selected)
      {
	for(i=0;i<5;i++)
	  { 
	    if ((inDeg[i] == 0) && (outDeg[i] > 0))
	      candTo =i;
	    for(j=0;j<5;j++)
	      {
		if (!selected)
		  {
		    if ((outDeg[j] ==0 ) && (inDeg[j] > 0))
		      candFrom = j;
		    //System.out.println("MSK *** From "+candFrom+" **To "+ candTo);
		    
		    if ((candFrom >= 0) && (candTo >= 0) && (AdjMat[candFrom][candTo] == 0))
		      {
			AdjMat[candFrom][candTo] = 1;
			AdjMat[candTo][candFrom] = -1;
			inDeg[candTo] +=1;
			outDeg[candFrom] +=1;
			totalEdgesSelected++;
			selected = true;
		      }
		  }
	      }
	  }
      }
    // Case 3
    if (!selected)
      {
	maxoutDeg = -1;
	maxinDeg = -1;
	for(i=0;i< 5; i++)
	  {
	    if (outDeg[i] > maxoutDeg)
	      {
		maxoutDeg = outDeg[i];
		candTo = i;
	      }
	    if (inDeg[i] > maxinDeg)
	      {
		maxinDeg = inDeg[i];
		candFrom = i;
	      }
	  }
	   
	if (maxoutDeg > maxinDeg)
	     {
	       for(i=0;(i<5)&&notover;i++)
		 {
		   if ((AdjMat[i][candTo] == 0) && (outDeg[i] < 1))
		     {
		       candFrom = i;
		       AdjMat[candFrom][candTo] = 1;
		       AdjMat[candTo][candFrom] = -1;
		       inDeg[candTo] +=1;
		       outDeg[candFrom] +=1;
		       totalEdgesSelected++;
		       selected = true;
		       notover = false;
		     }
		 }
	     }
	else
	  {
	    for(i=0;(i<5)&&notover;i++)
	      {
		if ((AdjMat[candFrom][i] == 0) && (inDeg[i] < 1))
		     {
		       candTo = i;
		       AdjMat[candFrom][candTo] = 1;
		       AdjMat[candTo][candFrom] = -1;
		       inDeg[candTo] +=1;
		       outDeg[candFrom] +=1;
		       totalEdgesSelected++;
		       selected = true;
		       notover = false;
		     }
		 }
	     }
      }

    // Lost Case
    if(!selected)
      {
	notover = true;
	for(i=0; (i< 5) && notover; i++)
	  { 
	    if (inDeg[i] == 0)
	      {
		candTo = i;
		for(j=0; (j< 5) && notover; j++)
		  {
		    if (AdjMat[j][candTo] == 0)
		      {
			candFrom = j;
			AdjMat[candFrom][candTo] = 1;
			AdjMat[candTo][candFrom] = -1;
			inDeg[candTo] +=1;
			outDeg[candFrom] +=1;
			totalEdgesSelected++;
			selected = true;
			notover = false;
		      }
		  }
	      }
	    if (!selected)
	      {
		if (outDeg[i] == 0)
		  {
		    candFrom = i;
		    for(j=0; (j< 5) && notover; j++)
		      {
			if (AdjMat[candFrom][j] == 0)
			  {
			    candTo = j;
			    AdjMat[candFrom][candTo] = 1;
			    AdjMat[candTo][candFrom] = -1;
			    inDeg[candTo] +=1;
			    outDeg[candFrom] +=1;
			    totalEdgesSelected++;
			    selected = true;
			    notover = false;
			  }
		      }
		  }
	      }
	  }
      }
    if(!selected)
      {
	
	if (gameover())
	  {
	    notover = true;
	    for(i=0;(i<5)&&notover;i++)
	      {
		for(j=0;(j<5)&&notover;j++)
		  {
		    if ((AdjMat[i][j] == 0) && (AdjMat[j][i] == 0))
		      {
			candTo = j;
			AdjMat[candFrom][candTo] = 1;
			AdjMat[candTo][candFrom] = -1;
			inDeg[candTo] +=1;
			outDeg[candFrom] +=1;
			totalEdgesSelected++;
			selected = true;
			notover = false;
		      }
		  }
	      }
	  }
	else 
	  {
	      for(i=0;i<5;i++)
		for(j=0;j<5;j++)
		  {
		    if ((AdjMat[i][j] ==0) &&(AdjMat[j][i]==0))
		      {
			AdjMat[i][j] = 1;
			AdjMat[j][i] = -1;
			inDeg[j] +=1;
			outDeg[i] +=1;
			totalEdgesSelected++;
			selected = true;
		      }
		  }
	  }
      }
    if (!selected)
      {
	if (!gameover())
	      for(i=0;i<5;i++)
		for(j=0;j<5;j++)
		  {
		    if ((AdjMat[i][j] ==0) &&(AdjMat[j][i]==0))
		      {
			AdjMat[i][j] = 1;
			AdjMat[j][i] = -1;
			inDeg[j] +=1;
			outDeg[i] +=1;
			totalEdgesSelected++;
			selected = true;
		      }
		  }
      }

    if (selected) 
      {
	showStatus("Your Turn");
	compResp.setText("Your Turn");
	System.out.println("My Choice " + (candFrom +1) + " ," + (candTo +1));
	repaint();
	displayResult();
      }

  }


	
  public void optimalStrat2()
  {
    int i,j,candFrom,candTo;
    boolean selected=false;
    int maxinDeg, maxoutDeg;
    boolean notover = true;
   
    candFrom=-1;candTo=-1;
    // Case 1 a
    for(i=0;i<5;i++)
      {
	if ((inDeg[i] == 3) && (outDeg[i]==0))
	  { 
	    candTo = i;
	    for(j=0;j<5;j++)
	      {
		if (AdjMat[j][candTo] ==0)
		  {
		    candFrom = j;
		    AdjMat[candFrom][candTo] = 1;
		    AdjMat[candTo][candFrom] = -1;
		    inDeg[candTo] +=1;
		    outDeg[candFrom] +=1;
		    totalEdgesSelected++;
		    selected = true;
		  }
	      }
	  }
      }
  
    // Case 1 b
    if (!selected)
      {
	//	candFrom=-1;candTo=-1;
	for(i=0;i<5;i++)
	  {
	    if ((outDeg[i] == 3) && (inDeg[i]==0))
	      { 
		candFrom = i;
		for(j=0;j<5;j++)
		  {
		    if (AdjMat[candFrom][j] ==0)
		      {
			candTo = j;
			AdjMat[candFrom][candTo] = 1;
			AdjMat[candTo][candFrom] = -1;
			inDeg[candTo] +=1;
			outDeg[candFrom] +=1;
			totalEdgesSelected++;
			selected = true;
		      }
		  }
	      }
	  }
      }
    // case 2
    if (!selected)
      {
	candFrom=-1;candTo=-1;
	for(i=0;i<5;i++)
	  { 
	    if ((inDeg[i] == 0) && (outDeg[i] > 0))
	      candFrom =i;
	    if ((outDeg[i] ==0 ) && (inDeg[i] > 0))
	      candTo = i;
	  }
	if ((candFrom >= 0) && (candTo >= 0) && (AdjMat[candFrom][candTo] == 0)
	    && (AdjMat[candTo][candFrom] == 0))
	  {
	    AdjMat[candFrom][candTo] = 1;
	    AdjMat[candTo][candFrom] = -1;
	    inDeg[candTo] +=1;
	    outDeg[candFrom] +=1;
	    totalEdgesSelected++;
	    selected = true;
	  }
	else 
	  {
	    candFrom = -1;
	    candTo = -1;
	  }
      }
    // Case 3
    if (!selected)
      {
	candFrom=-1;candTo=-1;
	maxoutDeg = -1;
	maxinDeg = -1;
	for(i=0;i< 5; i++)
	  {
	    if (outDeg[i] > maxoutDeg)
	      {
		maxoutDeg = outDeg[i];
		candFrom = i;
	      }
	    if (inDeg[i] > maxinDeg)
	      {
		maxinDeg = inDeg[i];
		candTo = i;
	      }
	  }
         
	if (maxoutDeg > maxinDeg)
	     {
	       for(i=0;(i<5)&&notover;i++)
		 {
		   if ((AdjMat[candFrom][i] == 0) && (inDeg[i] >= 1))
		     {
		       candTo = i;
		       AdjMat[candFrom][candTo] = 1;
		       AdjMat[candTo][candFrom] = -1;
		       inDeg[candTo] +=1;
		       outDeg[candFrom] +=1;
		       totalEdgesSelected++;
		       selected = true;
		       notover = false;
		     }
		 }
	     }
      	else
	  {
	    for(i=0;(i<5)&&notover;i++)
	      {
		if ((AdjMat[i][candTo] == 0) && (inDeg[i] >= 1))
		     {
		       candFrom = i;
		       AdjMat[candFrom][candTo] = 1;
		       AdjMat[candTo][candFrom] = -1;
		       inDeg[candTo] +=1;
		       outDeg[candFrom] +=1;
		       totalEdgesSelected++;
		       selected = true;
		       notover = false;
		     }
		 }
	     }
      }

    // Lost Case
    if(!selected)
      {
	candFrom=-1;candTo=-1;
	notover = true;
	for(i=0; (i< 5) && notover; i++)
	  { 
	    if (inDeg[i] == 0)
	      {
		candFrom = i;
		for(j=0; (j< 5) && notover; j++)
		  {
		    if (AdjMat[candFrom][j] == 0)
		      {
			candTo = j;
			AdjMat[candFrom][candTo] = 1;
			AdjMat[candTo][candFrom] = -1;
			inDeg[candTo] +=1;
			outDeg[candFrom] +=1;
			totalEdgesSelected++;
			selected = true;
			notover = false;
		      }
		  }
	      }
	    if (!selected)
	      {
		candFrom=-1;candTo=-1;
		if (outDeg[i] == 0)
		  {
		    candTo = i;
		    for(j=0; (j< 5) && notover; j++)
		      {
			if (AdjMat[j][candTo] == 0)
			  {
			    candFrom = j;
			    AdjMat[candFrom][candTo] = 1;
			    AdjMat[candTo][candFrom] = -1;
			    inDeg[candTo] +=1;
			    outDeg[candFrom] +=1;
			    totalEdgesSelected++;
			    selected = true;
			    notover = false;
			  }
		      }
		  }
	      }
	  }
      }
    if(!selected)
      {
	candFrom=-1;candTo=-1;
	if (gameover())
	  {
	    notover = true;
	    for(i=0;(i<5)&&notover;i++)
	      {
		for(j=0;(j<5)&&notover;j++)
		  {
		    if ((AdjMat[i][j] == 0) && (AdjMat[j][i] == 0))
		      {
			candTo = j; candFrom=i;
			AdjMat[candFrom][candTo] = 1;
			AdjMat[candTo][candFrom] = -1;
			inDeg[candTo] +=1;
			outDeg[candFrom] +=1;
			totalEdgesSelected++;
			selected = true;
			notover = false;
		      }
		  }
	      }
	  }
	else 
	  {
	    if (!gameover())
	      for(i=0;i<5;i++)
		for(j=0;j<5;j++)
		  {
		    if ((AdjMat[i][j] ==0) &&(AdjMat[j][i]==0))
		      {
			AdjMat[i][j] = 1;
			AdjMat[j][i] = -1;
			inDeg[j] +=1;
			outDeg[i] +=1;
			totalEdgesSelected++;
			selected = true;
		      }
		  }
	  }
      }
    if (!selected)
      {
	if (!gameover())
	      for(i=0;i<5;i++)
		for(j=0;j<5;j++)
		  {
		    if ((AdjMat[i][j] ==0) &&(AdjMat[j][i]==0))
		      {
			AdjMat[i][j] = 1;
			AdjMat[j][i] = -1;
			inDeg[j] +=1;
			outDeg[i] +=1;
			totalEdgesSelected++;
			selected = true;
		      }
		  }
      }

    if (selected) 
      {
	showStatus("Your Turn");
	compResp.setText("Your Turn");
	System.out.println("My Choice " + (candFrom +1) + " ," + (candTo +1));
	repaint();
	displayResult();
      }

  }
}
      





// Action Handler for Clicking theMouse

class DetailHandler extends MouseAdapter {
  
  private Subh1 details;

  public DetailHandler(Subh1 m) { details = m;}
  
  public void mousePressed(MouseEvent e) {
    details.setC(e.getX(),e.getY());
  }
  public void mouseReleased(MouseEvent e) {
    details.setM(e.getX(),e.getY());
  }
}




class Point {
  protected int x, y;

  public Point(int a, int b) { setPoint(a, b); }

  public void setPoint(int a, int b)
  {
    x = a;
    y = b;
  }

  public boolean Equal(Point p)
  {
    if ((x == p.x) && (y == p.y))
      return true;
    
    else 
      return false;
  }
} 
// Action Handler for the Shape Choice

class BHandler implements ItemListener {
  private Subh1 x;
  
  public BHandler(Subh1 c) { x =c;}

  public void itemStateChanged (ItemEvent e)
  {
    int i;
    Choice ch;
    ch = (Choice) e.getItemSelectable();
  
    i = ch.getSelectedIndex();
    x.setCH(i+1);

  }
}

// Action Handler for the color choice

class CHandler implements ItemListener {
  private Subh1 x;
  
  public CHandler(Subh1 c) { x =c;}

  public void itemStateChanged (ItemEvent e)
  {
    int i;
    Choice ch;
    ch = (Choice) e.getItemSelectable();
  
    i = ch.getSelectedIndex();
    x.setCO(i+1);

  }
}

class DHandler implements ItemListener {
  private Subh1 x;
  
  public DHandler(Subh1 c) { x =c;}

  public void itemStateChanged (ItemEvent e)
  {
    int i;
    Choice ch;
    ch = (Choice) e.getItemSelectable();
  
    i = ch.getSelectedIndex();
    x.setSelect(i+1);

  }
}


class EHandler implements ItemListener {
  private Subh1 x;
  
  public EHandler(Subh1 c) { x =c;}

  public void itemStateChanged (ItemEvent e)
  {
    int i;
    Choice ch;
    ch = (Choice) e.getItemSelectable();
  
    i = ch.getSelectedIndex();
    x.initialize +=1;
    x.init1();
    x.repaint();
    
  }
}

class MotionHandler extends MouseMotionAdapter {
  
  private Subh1 dragger;
  
  public MotionHandler(Subh1 d) { dragger = d;}
  
  public void mouseDragged(MouseEvent e) {
    
      dragger.setM1(e.getX(),e.getY());
    
  }
}
