import java.applet.*;
import java.awt.*;

public class Solitaire extends Applet implements Runnable{

   Image pic,mpic;
   int by = 20;
   int mdx,mdy;
   int mux,muy;
   int mx,my;
   int cx,cy,cd;
   int mmd[][]={{0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0}};
   int mmc[][]={{0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0}};
   int md1[][]={{2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,1,1,1,2,2,2,2},
                {2,2,2,2,1,1,1,2,2,2,2},
                {2,2,1,1,1,1,1,1,1,2,2},
                {2,2,1,1,1,0,1,1,1,2,2},
                {2,2,1,1,1,1,1,1,1,2,2},
                {2,2,2,2,1,1,1,2,2,2,2},
                {2,2,2,2,1,1,1,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2}};
   int md2[][]={{2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,1,1,1,2,2,2,2},
                {2,2,2,1,1,1,1,1,2,2,2},
                {2,2,1,1,1,1,1,1,1,2,2},
                {2,2,1,1,1,0,1,1,1,2,2},
                {2,2,1,1,1,1,1,1,1,2,2},
                {2,2,2,1,1,1,1,1,2,2,2},
                {2,2,2,2,1,1,1,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2}};
   int md3[][]={{2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,0,0,0,2,2,2,2},
                {2,2,2,2,0,1,0,2,2,2,2},
                {2,2,0,0,0,1,0,0,0,2,2},
                {2,2,0,1,1,1,1,1,0,2,2},
                {2,2,0,0,0,1,0,0,0,2,2},
                {2,2,2,2,0,1,0,2,2,2,2},
                {2,2,2,2,0,0,0,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2}};
   int md4[][]={{2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,0,0,1,2,2,2,2},
                {2,2,2,2,0,1,1,2,2,2,2},
                {2,2,0,0,1,1,1,0,0,2,2},
                {2,2,0,1,1,1,1,0,0,2,2},
                {2,2,0,0,1,1,1,0,0,2,2},
                {2,2,2,2,0,1,1,2,2,2,2},
                {2,2,2,2,0,0,1,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2}};
   int md5[][]={{2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,0,1,0,2,2,2,2},
                {2,2,2,2,1,1,1,2,2,2,2},
                {2,2,0,1,1,1,1,1,0,2,2},
                {2,2,1,1,1,0,1,1,1,2,2},
                {2,2,0,1,1,1,1,1,0,2,2},
                {2,2,2,2,1,1,1,2,2,2,2},
                {2,2,2,2,0,1,0,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2},
                {2,2,2,2,2,2,2,2,2,2,2}};
   Choice sel = new Choice();
   Button rep = new Button("  replay  ");
   Thread th = null;
   Image os;
   Graphics og;

   public void init(){
    
      setBackground(Color.white);
      os = createImage(320,335);
      og = os.getGraphics();
      setLayout(new BorderLayout());
      Panel pan = new Panel();
      pan.setLayout(new FlowLayout());

      sel.addItem("Plus");
      sel.addItem("Pyramid");
      sel.addItem("Diamond");
      sel.addItem("Solitaire1");
      sel.addItem("Solitaire2");

      pan.add(sel);
      pan.add(rep);
      add("South",pan);

      for(int i = 2;i < 9;i ++){
         for(int j = 2;j < 9;j ++){
            mmc[i][j] = md3[i][j];
            mmd[i][j] = mmc[i][j];
         }
      }
   }
 
   public void paint(Graphics g){
      og.setColor(Color.white); 
      og.fillRect(0,0,320,335);
      og.setColor(Color.black); 
      for(int k = 0;k < 8;k ++){
         og.drawLine(20,by+40*k,20+40*7,by+40*k);
         og.drawLine(20+40*k,by,20+40*k,by+40*7);
      }

      for(int i = 2;i < 9;i ++){
         for(int j = 2;j < 9;j ++){
            if(mmd[i][j] == 1){
               pic = getImage(getDocumentBase(),"Picture/peg.gif");
            }else if(mmd[i][j] == 2){
               pic = getImage(getDocumentBase(),"Picture/wall.gif");
            }else{
               pic = getImage(getDocumentBase(),"");
            }
            og.drawImage(pic,20+40*(i-2),by+40*(j-2),this);
            og.drawImage(mpic,mx,my,this);
         }
      }
      g.drawImage(os,0,0,this);
   }

   public boolean mouseDown(Event e,int x,int y){

      mdx = (x - 20) / 40 + 2;
      mdy = (y - by) / 40 + 2; 

      if(mmd[mdx][mdy] == 1){
         mpic = getImage(getDocumentBase(),"Picture/peg.gif");
         cx = mdx;
         cy = mdy;
         cd = 1;
         mmd[mdx][mdy] = 0;
      }else if(mmd[mdx][mdy] == 2){
         cd = 2;
         mpic = getImage(getDocumentBase(),"");
      }else{
         cd = 0;
         mpic = getImage(getDocumentBase(),"");
      }    
      mx = x - 20;
      my = y - 20;
      repaint();
      return true;
   }

   public boolean mouseDrag(Event e,int x,int y){
      mx = x - 20;
      my = y - 20;
      repaint();
      return true;  
   }

   public boolean mouseUp(Event e,int x,int y){

      mux = (x - 20) / 40 + 2;
      muy = (y - by) / 40 + 2; 

      if((mdx == mux)&&(mdy-2 == muy)&&(mmd[mdx][mdy-1]==1)&&(mmd[mdx][mdy-2]==0)){
         mmd[mdx][mdy-1]=0;
         mmd[mdx][mdy-2]=1;
         mx = (mux - 2) * 40 + 20;
         my = (muy - 2) * 40 + by;
      }else if((mdx == mux)&&(mdy+2 == muy)&&(mmd[mdx][mdy+1]==1)&&(mmd[mdx][mdy+2]==0)){
         mmd[mdx][mdy+1]=0;
         mmd[mdx][mdy+2]=1;
         mx = (mux - 2) * 40 + 20;
         my = (muy - 2) * 40 + by;
      }else if((mdx-2 == mux)&&(mdy == muy)&&(mmd[mdx-1][mdy]==1)&&(mmd[mdx-2][mdy]==0)){
         mmd[mdx-1][mdy]=0;
         mmd[mdx-2][mdy]=1;
         mx = (mux - 2) * 40 + 20;
         my = (muy - 2) * 40 + by;
      }else if((mdx+2 == mux)&&(mdy == muy)&&(mmd[mdx+1][mdy]==1)&&(mmd[mdx+2][mdy]==0)){
         mmd[mdx+1][mdy]=0;
         mmd[mdx+2][mdy]=1;
         mx = (mux - 2) * 40 + 20;
         my = (muy - 2) * 40 + by;
      }else{
         mmd[mdx][mdy] = cd;
      }
      mpic = getImage(getDocumentBase(),"");
      repaint();
      return true;
   }

   public boolean action(Event e,Object o){

      if(e.target == sel){
         if(o == "Plus"){
            for(int i = 2;i < 9;i ++){
               for(int j = 2;j < 9;j ++){
                  mmc[i][j] = md3[i][j];
                  mmd[i][j] = mmc[i][j];
               }
            }
         }else if(o == "Pyramid"){
            for(int i = 2;i < 9;i ++){
               for(int j = 2;j < 9;j ++){
                  mmc[i][j] = md4[i][j];
                  mmd[i][j] = mmc[i][j];
               }
            }
         }else if(o == "Diamond"){
            for(int i = 2;i < 9;i ++){
               for(int j = 2;j < 9;j ++){
                  mmc[i][j] = md5[i][j];
                  mmd[i][j] = mmc[i][j];
               }
            }
         }else if(o == "Solitaire1"){
            for(int i = 2;i < 9;i ++){
               for(int j = 2;j < 9;j ++){
                  mmc[i][j] = md1[i][j];
                  mmd[i][j] = mmc[i][j];
               }
            }
         }else if(o == "Solitaire2"){
            for(int i = 2;i < 9;i ++){
               for(int j = 2;j < 9;j ++){
                  mmc[i][j] = md2[i][j];
                  mmd[i][j] = mmc[i][j];
               }
            }
         }
         repaint();
         return true;
      }

      if(e.target == rep){
         for(int i = 2;i < 9;i ++){
            for(int j = 2;j < 9;j ++){
               mmd[i][j] = mmc[i][j];
            }
         }
         repaint();
         return true;
      } 
      return false;
   }
   public void start(){    
      if(th == null){
         th = new Thread(this);
         th.start();
      }
   }
   public void stop(){
      th = null;
   }
   public void run(){
      while(th != null){
         try{
            Thread.sleep(100);
         }
         catch (InterruptedException e){
         }
      }
   }
   public void update( Graphics g ){
      paint( g ) ;
   }
}