/*David Price, 7-23-01, Unit 3, MstrMind.java*/
   import java.awt.*;
   import java.awt.event.*;
   import java.util.Random;

   public class MstrMind extends java.applet.Applet implements ActionListener
   {
      int lastPeg, lastCol;
      BorderLayout b;
      GridLayout gCenter, gEast, gSouth;
      Panel north, south, east, west, center;
      Button[] nextPegs;
      Button[] colors;
      Button[] ansPegs;
      Button[] solution;
      Label blank = new Label(""), solWords = new Label("SOLUTION");
      Random r = new Random();
      Color [] cA;
      Button bButton;
      Color[] colAns;
      Font f = new Font("Arial", Font.BOLD, 14);
      Button done;
      boolean justDone=false;
      Color[] colAns2 = new Color[4];
      Color[] line = new Color[4];
   
   
      public void init()
      {
      
         cA = new Color[6];
         cA[0] = Color.black;
         cA[1] = Color.white;
         cA[2] = Color.red;
         cA[3] = Color.green;
         cA[4] = Color.yellow;
         cA[5] = Color.blue;
      
      
         colAns = new Color[4];
         for(int j=0; j<4; j++)
         {
            int yoyo = r.nextInt(6);
            colAns[j] = cA[yoyo];/*(int)Math.floor(Math.random()*6)];*/
            System.out.println(j + " " + yoyo);
         }
      
      
         bButton = new Button("");
         bButton.setVisible(false);
         bButton.setEnabled(false);
      
         b = new BorderLayout();
         setLayout(b);
      
         north = new Panel();
         south = new Panel();
         east = new Panel();
         west = new Panel();
         center = new Panel();
         colors = new Button [6];
      
      
         for(int j=0; j<6; j++)
         {
            colors[j] = new Button("         ");
            colors[j].setVisible(true);
            colors[j].setEnabled(false);
            colors[j].addActionListener(this);
            colors[j].setBackground(cA[j]);
            north.add(colors[j]);
         }
         done = new Button("Done");
         done.setVisible(true);
         done.setEnabled(false);
         done.addActionListener(this);
         north.add(done);
      
      
      
         gSouth = new GridLayout(1, 5, 7, 7);
         south.setLayout(gSouth);
         south.setFont(f);
         south.add(solWords);
         solution = new Button [4];
         for(int j=0; j<4; j++)
         {
            solution [j] = new Button();
            solution [j].setVisible(false);
            solution [j].setEnabled(false);
            solution [j].setBackground(colAns[j]);
            south.add(solution[j]);
         }
      
      
         gEast = new GridLayout(20, 2, 10, 7);
         east.setLayout(gEast);
         ansPegs = new Button[40];
         for(int j=0; j<40; j++)
         {
            ansPegs[j] = new Button();
            ansPegs[j].setVisible(false);
            ansPegs[j].setEnabled(false);
            east.add(ansPegs[j]);
         }
      
         Button bWest = new Button();
         bWest.setVisible(false);
         bWest.setEnabled(false);
         west.add(bWest);      
      
         gCenter = new GridLayout(10, 4, 7, 7);
         center.setLayout(gCenter);
      
         nextPegs = new Button [40];
      
         for(int j=0; j<4; j++)
         {
            nextPegs[j] = new Button();
            nextPegs[j].setLabel("?");
            nextPegs[j].setBackground(Color.lightGray);
            nextPegs[j].addActionListener(this);
            center.add(nextPegs[j]);
         }
      
         for(int j=4; j<40; j++)
         {
            nextPegs[j] = new Button();
            nextPegs[j].setLabel("?");
            nextPegs[j].setBackground(Color.lightGray);
            nextPegs[j].addActionListener(this);
            nextPegs[j].setVisible(false);
            nextPegs[j].setEnabled(false);
            center.add(nextPegs[j]);
         }
      
      
         add("North", north);
         add("South", south);
         add("East", east);
         add("West", west);
         add("Center", center);
      
      
         //showStatus("Choose a peg");
      
      
      
      
      }
   
      public void actionPerformed(ActionEvent e)
      {
         Object src = e.getSource();
         int cNum = 5, pNum=39, numPer=0, numCor=0;
         while(cNum >= 0 && colors[cNum] != src)
            cNum--;
         while(pNum >= 0 && nextPegs[pNum] != src)
            pNum--;
      
         if(pNum >-1)  //if a peg was pressed
         {
            //showStatus("Choose a color");
            lastPeg = pNum;
            for(int j=0; j<6; j++)
            {
               colors[j].setEnabled(true);
               colors[j].setVisible(true);
            }
            for(int j=0; j<40; j++)
               nextPegs[j].setEnabled(false);
            justDone=false;
         }
         
         else if(cNum>-1 && justDone==false)  //if a color was pressed
         {
            nextPegs[lastPeg].setBackground(cA[cNum]);
            nextPegs[lastPeg].setLabel("");
            for(int j=lastPeg/4*4; j<lastPeg/4*4+4; j++)
               //if(j != lastPeg)
               nextPegs[j].setEnabled(true);
            if((nextPegs[lastPeg/4*4].getBackground()!=Color.lightGray) && (nextPegs[lastPeg/4*4+1].getBackground()!=Color.lightGray) && (nextPegs[lastPeg/4*4+2].getBackground()!=Color.lightGray) && (nextPegs[lastPeg/4*4+3].getBackground()!=Color.lightGray))
               done.setEnabled(true);
         	//justDone=false;
         }
         
         else if(src == done)
         {
            justDone=true;
            done.setEnabled(false);
         
            for(int j=0; j<4; j++)
               colAns2[j] = colAns[j];
         
            for(int j=0; j<4; j++)
               line[j] = nextPegs[lastPeg/4*4+j].getBackground();
         
            for(int j=0; j<4; j++)
               if(line[j] == colAns2[j])
               {
                  numPer++;
                  line[j] = Color.gray;
                  colAns2[j] = Color.pink;
               }
         
            for(int j=0; j<4; j++)
               for(int i=0; i<4; i++)
                  if(line[j] == colAns2[i])
                  {
                     numCor++;
                     line[j] = Color.gray;
                     colAns2[i] = Color.pink;
                  }
         
            for(int j=lastPeg/4*4; j<lastPeg/4*4+numPer; j++)
               ansPegs[j].setBackground(Color.black);
         
            for(int j=lastPeg/4*4+numPer; j<lastPeg/4*4+numPer+numCor; j++)
               ansPegs[j].setBackground(Color.white);
         
            for(int j=lastPeg/4*4; j<lastPeg/4*4+4; j++)
               ansPegs[j].setVisible(true);
         
            if(numPer==4)
            {
               for(int j=0; j<4; j++)
                  solution[j].setVisible(true);
               for(int j=0; j<40; j++)
               {
                  nextPegs[j].setEnabled(false);
                  nextPegs[j].removeActionListener(this);
                  nextPegs[j].setVisible(true);
                  nextPegs[j].setLabel("YOU WIN!");
               }
            }            
            else if(lastPeg/4*4<36)
            {
               for(int j=lastPeg/4*4; j<lastPeg/4*4+4; j++)
               {
                  nextPegs[j].setEnabled(false);
                  nextPegs[j].removeActionListener(this);
               }
               for(int j=lastPeg/4*4+4; j<lastPeg/4*4+8; j++)
               {
                  nextPegs[j].setEnabled(true);
                  nextPegs[j].setVisible(true);
               }
               done.setEnabled(false);
            }
            else
            {
               for(int j=0; j<4; j++)
                  solution[j].setVisible(true);
               for(int j=0; j<40; j++)
               {
                  nextPegs[j].setEnabled(false);
                  nextPegs[j].removeActionListener(this);
                  nextPegs[j].setVisible(true);
                  nextPegs[j].setLabel("YOU LOSE!");
               }
            }     
         }
      }
   
   }