home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / APPLETS / SOLITAIR.JAV < prev    next >
Encoding:
Text File  |  1997-02-27  |  4.5 KB  |  212 lines

  1. //  A simple solitaire game
  2. //
  3. //  Karl Hornell, February 28, 1996
  4.  
  5. import java.awt.*;
  6.  
  7. public class solitaire extends java.applet.Applet
  8. {
  9.     int i,j,k,deadBalls;
  10.     int pointX,pointY,pickX=-1,pickY=-1,dragX=-1,dragY=-1;
  11.     int map[];
  12.     boolean drawBoard;
  13.  
  14.     public void init()
  15.     {
  16.         map = new int[81];
  17.         fillMap();
  18.         resize(280,190);
  19.         repaint();
  20.     }
  21.  
  22.     public void fillMap()
  23.     {
  24.         for (i=0;i<81;i++)
  25.             map[i]=1;
  26.         for (i=0;i<3;i++)
  27.             for (j=0;j<3;j++)
  28.             {
  29.                 map[i*9+j]=-1;
  30.                 map[i*9+j+6]=-1;
  31.                 map[i*9+j+54]=-1;
  32.                 map[i*9+j+60]=-1;
  33.             }
  34.         map[40]=0;
  35.         drawBoard=true;
  36.         deadBalls=0;
  37.     }
  38.  
  39.     public void fixBox(Graphics g,int x,int y,int w,int h,Color c)
  40.     {
  41.         g.setColor(c);
  42.         g.fillRect(x,y,w,h);
  43.     }
  44.  
  45.     public void fixDisc(Graphics g,int x,int y,int r,Color c)
  46.     {
  47.         g.setColor(c);
  48.         g.fillOval(x,y,r,r);
  49.     }
  50.  
  51.     public void fixCircle(Graphics g,int x,int y,int r,Color c)
  52.     {
  53.         g.setColor(c);
  54.         g.drawOval(x,y,r,r);
  55.     }
  56.  
  57.     public void boardBall(Graphics g,int x,int y)
  58.     {
  59.         g.setColor(Color.blue);
  60.         g.fillRect(x,y,16,16);
  61.         fixDisc(g,x+1,y+1,15,Color.black);
  62.         fixDisc(g,x,y,14,Color.cyan);
  63.         fixDisc(g,x+6,y+6,7,Color.blue);
  64.         fixDisc(g,x+1,y+1,8,Color.white);
  65.         fixDisc(g,x+9,y+9,3,Color.white);
  66.     }
  67.  
  68.     public void deadBall(Graphics g,int x,int y)
  69.     {
  70.         fixDisc(g,x+3,y+3,14,Color.gray);
  71.         fixDisc(g,x,y,14,Color.cyan);
  72.         fixDisc(g,x+6,y+6,7,Color.gray);
  73.         fixDisc(g,x+1,y+1,8,Color.white);
  74.         fixDisc(g,x+9,y+9,3,Color.white);
  75.     }
  76.  
  77.     public void boardHole(Graphics g,int x,int y)
  78.     {
  79.         g.setColor(Color.blue);
  80.         g.fillRect(x,y,16,16);
  81.         fixDisc(g,x+1,y+1,12,Color.black);
  82.         fixDisc(g,x+4,y+4,10,Color.blue);
  83.         g.setColor(Color.cyan);
  84.         g.drawArc(x,y,14,14,-135,180);
  85.     }
  86.  
  87.     public boolean mouseDown(java.awt.Event evt, int x, int y)
  88.     {
  89.         pointX=-100;
  90.         pointY=-100;
  91.         pickX=-1;
  92.         pickY=-1;
  93.         i=(y-18)/17;
  94.         j=(x-18)/17;
  95.         if ((i>-1)&&(i<9)&&(j>-1)&&(j<9))
  96.         {
  97.             if (map[i*9+j]==1)
  98.             {
  99.                 pickX=j;
  100.                 pickY=i;
  101.                 pointY=y;
  102.                 pointX=x;
  103.             }
  104.         }
  105.         else if ((x>206)&&(x<266)&&(y>166)&&(y<190))
  106.         {
  107.             fillMap();
  108.             repaint();
  109.         }
  110.         return false;
  111.     }
  112.  
  113.     public boolean mouseDrag(java.awt.Event evt, int x, int y)
  114.     {
  115.         dragX=-1;
  116.         if ((x>(pointX-8))&&(x<pointX+8))
  117.         {
  118.             if ((y<pointY-14)&&(pickY>1))
  119.                 if ((map[pickX+9*pickY-18]==0)&&(map[pickX+9*pickY-9]==1))
  120.                 {
  121.                     dragX=pickX;
  122.                     dragY=pickY-2;
  123.                 }
  124.             if ((y>pointY+14)&&(pickY<7))
  125.                 if ((map[pickX+9*pickY+18]==0)&&(map[pickX+9*pickY+9]==1))
  126.                 {
  127.                     dragX=pickX;
  128.                     dragY=pickY+2;
  129.                 }
  130.         }
  131.         else if ((y>(pointY-8))&&(y<pointY+8))
  132.         {
  133.             if ((x<pointX-14)&&(pickX>1))
  134.                 if ((map[pickX+9*pickY-2]==0)&&(map[pickX+9*pickY-1]==1))
  135.                 {
  136.                     dragX=pickX-2;
  137.                     dragY=pickY;
  138.                 }
  139.             if ((x>pointX+14)&&(pickX<7))
  140.                 if ((map[pickX+9*pickY+2]==0)&&(map[pickX+9*pickY+1]==1))
  141.                 {
  142.                     dragX=pickX+2;
  143.                     dragY=pickY;
  144.                 }
  145.         }
  146.         return false;
  147.     }
  148.  
  149.     public boolean mouseUp(java.awt.Event evt, int x, int y)
  150.     {
  151.         if (dragX>-1)
  152.         {
  153.             drawBoard=false;
  154.             repaint();
  155.         }
  156.         return false;
  157.     }
  158.  
  159.     public void update(Graphics g)
  160.     {
  161.         paint(g);
  162.     }
  163.  
  164.     public void paint(Graphics g)
  165.     {
  166.         if (drawBoard)
  167.         {
  168.             g.setColor(Color.lightGray);
  169.             g.fillRect(0,0,size().width,size().height);
  170.             fixBox(g,207,166,60,24,Color.black);
  171.             fixBox(g,208,167,58,22,Color.blue);
  172.             g.setColor(Color.cyan);
  173.             g.drawString("Restart",215,183);
  174.             fixDisc(g,2,2,187,Color.gray);
  175.             fixDisc(g,0,0,185,Color.blue);
  176.             fixDisc(g,0,0,183,Color.cyan);
  177.             fixDisc(g,2,2,183,Color.black);
  178.             fixDisc(g,2,2,181,Color.blue);
  179.             fixCircle(g,8,8,171,Color.cyan);
  180.             fixCircle(g,7,7,171,Color.black);
  181.  
  182.             for (i=0;i<9;i++)
  183.                 for (j=0;j<9;j++)
  184.                     if (map[i*9+j]>-1)
  185.                         if (map[i*9+j]==0)
  186.                             boardHole(g,18+j*17,18+i*17);
  187.                         else
  188.                             boardBall(g,18+j*17,18+i*17);
  189.             for (i=0;i<deadBalls;i++)
  190.             {
  191.                 k=i/5;
  192.                 j=i-k*5;
  193.                 deadBall(g,193+j*17,5+k*17);
  194.             }
  195.         }
  196.         else
  197.         {
  198.             map[pickX+9*pickY]=0;
  199.             map[(pickX+dragX)/2+9*(pickY+dragY)/2]=0;
  200.             map[dragX+9*dragY]=1;
  201.             boardHole(g,18+pickX*17,18+pickY*17);
  202.             boardHole(g,18+((pickX+dragX)/2)*17,18+((pickY+dragY)/2)*17);
  203.             boardBall(g,18+dragX*17,18+dragY*17);
  204.             i=deadBalls/5;
  205.             j=deadBalls-i*5;
  206.             deadBall(g,193+j*17,5+i*17);
  207.             deadBalls++;
  208.             drawBoard=true;
  209.         }
  210.     }    
  211. }
  212.