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. //
  4.  
  5. //  Karl Hornell, February 28, 1996
  6.  
  7.  
  8.  
  9. import java.awt.*;
  10.  
  11.  
  12.  
  13. public class solitaire extends java.applet.Applet
  14.  
  15. {
  16.  
  17.     int i,j,k,deadBalls;
  18.  
  19.     int pointX,pointY,pickX=-1,pickY=-1,dragX=-1,dragY=-1;
  20.  
  21.     int map[];
  22.  
  23.     boolean drawBoard;
  24.  
  25.  
  26.  
  27.     public void init()
  28.  
  29.     {
  30.  
  31.         map = new int[81];
  32.  
  33.         fillMap();
  34.  
  35.         resize(280,190);
  36.  
  37.         repaint();
  38.  
  39.     }
  40.  
  41.  
  42.  
  43.     public void fillMap()
  44.  
  45.     {
  46.  
  47.         for (i=0;i<81;i++)
  48.  
  49.             map[i]=1;
  50.  
  51.         for (i=0;i<3;i++)
  52.  
  53.             for (j=0;j<3;j++)
  54.  
  55.             {
  56.  
  57.                 map[i*9+j]=-1;
  58.  
  59.                 map[i*9+j+6]=-1;
  60.  
  61.                 map[i*9+j+54]=-1;
  62.  
  63.                 map[i*9+j+60]=-1;
  64.  
  65.             }
  66.  
  67.         map[40]=0;
  68.  
  69.         drawBoard=true;
  70.  
  71.         deadBalls=0;
  72.  
  73.     }
  74.  
  75.  
  76.  
  77.     public void fixBox(Graphics g,int x,int y,int w,int h,Color c)
  78.  
  79.     {
  80.  
  81.         g.setColor(c);
  82.  
  83.         g.fillRect(x,y,w,h);
  84.  
  85.     }
  86.  
  87.  
  88.  
  89.     public void fixDisc(Graphics g,int x,int y,int r,Color c)
  90.  
  91.     {
  92.  
  93.         g.setColor(c);
  94.  
  95.         g.fillOval(x,y,r,r);
  96.  
  97.     }
  98.  
  99.  
  100.  
  101.     public void fixCircle(Graphics g,int x,int y,int r,Color c)
  102.  
  103.     {
  104.  
  105.         g.setColor(c);
  106.  
  107.         g.drawOval(x,y,r,r);
  108.  
  109.     }
  110.  
  111.  
  112.  
  113.     public void boardBall(Graphics g,int x,int y)
  114.  
  115.     {
  116.  
  117.         g.setColor(Color.blue);
  118.  
  119.         g.fillRect(x,y,16,16);
  120.  
  121.         fixDisc(g,x+1,y+1,15,Color.black);
  122.  
  123.         fixDisc(g,x,y,14,Color.cyan);
  124.  
  125.         fixDisc(g,x+6,y+6,7,Color.blue);
  126.  
  127.         fixDisc(g,x+1,y+1,8,Color.white);
  128.  
  129.         fixDisc(g,x+9,y+9,3,Color.white);
  130.  
  131.     }
  132.  
  133.  
  134.  
  135.     public void deadBall(Graphics g,int x,int y)
  136.  
  137.     {
  138.  
  139.         fixDisc(g,x+3,y+3,14,Color.gray);
  140.  
  141.         fixDisc(g,x,y,14,Color.cyan);
  142.  
  143.         fixDisc(g,x+6,y+6,7,Color.gray);
  144.  
  145.         fixDisc(g,x+1,y+1,8,Color.white);
  146.  
  147.         fixDisc(g,x+9,y+9,3,Color.white);
  148.  
  149.     }
  150.  
  151.  
  152.  
  153.     public void boardHole(Graphics g,int x,int y)
  154.  
  155.     {
  156.  
  157.         g.setColor(Color.blue);
  158.  
  159.         g.fillRect(x,y,16,16);
  160.  
  161.         fixDisc(g,x+1,y+1,12,Color.black);
  162.  
  163.         fixDisc(g,x+4,y+4,10,Color.blue);
  164.  
  165.         g.setColor(Color.cyan);
  166.  
  167.         g.drawArc(x,y,14,14,-135,180);
  168.  
  169.     }
  170.  
  171.  
  172.  
  173.     public boolean mouseDown(java.awt.Event evt, int x, int y)
  174.  
  175.     {
  176.  
  177.         pointX=-100;
  178.  
  179.         pointY=-100;
  180.  
  181.         pickX=-1;
  182.  
  183.         pickY=-1;
  184.  
  185.         i=(y-18)/17;
  186.  
  187.         j=(x-18)/17;
  188.  
  189.         if ((i>-1)&&(i<9)&&(j>-1)&&(j<9))
  190.  
  191.         {
  192.  
  193.             if (map[i*9+j]==1)
  194.  
  195.             {
  196.  
  197.                 pickX=j;
  198.  
  199.                 pickY=i;
  200.  
  201.                 pointY=y;
  202.  
  203.                 pointX=x;
  204.  
  205.             }
  206.  
  207.         }
  208.  
  209.         else if ((x>206)&&(x<266)&&(y>166)&&(y<190))
  210.  
  211.         {
  212.  
  213.             fillMap();
  214.  
  215.             repaint();
  216.  
  217.         }
  218.  
  219.         return false;
  220.  
  221.     }
  222.  
  223.  
  224.  
  225.     public boolean mouseDrag(java.awt.Event evt, int x, int y)
  226.  
  227.     {
  228.  
  229.         dragX=-1;
  230.  
  231.         if ((x>(pointX-8))&&(x<pointX+8))
  232.  
  233.         {
  234.  
  235.             if ((y<pointY-14)&&(pickY>1))
  236.  
  237.                 if ((map[pickX+9*pickY-18]==0)&&(map[pickX+9*pickY-9]==1))
  238.  
  239.                 {
  240.  
  241.                     dragX=pickX;
  242.  
  243.                     dragY=pickY-2;
  244.  
  245.                 }
  246.  
  247.             if ((y>pointY+14)&&(pickY<7))
  248.  
  249.                 if ((map[pickX+9*pickY+18]==0)&&(map[pickX+9*pickY+9]==1))
  250.  
  251.                 {
  252.  
  253.                     dragX=pickX;
  254.  
  255.                     dragY=pickY+2;
  256.  
  257.                 }
  258.  
  259.         }
  260.  
  261.         else if ((y>(pointY-8))&&(y<pointY+8))
  262.  
  263.         {
  264.  
  265.             if ((x<pointX-14)&&(pickX>1))
  266.  
  267.                 if ((map[pickX+9*pickY-2]==0)&&(map[pickX+9*pickY-1]==1))
  268.  
  269.                 {
  270.  
  271.                     dragX=pickX-2;
  272.  
  273.                     dragY=pickY;
  274.  
  275.                 }
  276.  
  277.             if ((x>pointX+14)&&(pickX<7))
  278.  
  279.                 if ((map[pickX+9*pickY+2]==0)&&(map[pickX+9*pickY+1]==1))
  280.  
  281.                 {
  282.  
  283.                     dragX=pickX+2;
  284.  
  285.                     dragY=pickY;
  286.  
  287.                 }
  288.  
  289.         }
  290.  
  291.         return false;
  292.  
  293.     }
  294.  
  295.  
  296.  
  297.     public boolean mouseUp(java.awt.Event evt, int x, int y)
  298.  
  299.     {
  300.  
  301.         if (dragX>-1)
  302.  
  303.         {
  304.  
  305.             drawBoard=false;
  306.  
  307.             repaint();
  308.  
  309.         }
  310.  
  311.         return false;
  312.  
  313.     }
  314.  
  315.  
  316.  
  317.     public void update(Graphics g)
  318.  
  319.     {
  320.  
  321.         paint(g);
  322.  
  323.     }
  324.  
  325.  
  326.  
  327.     public void paint(Graphics g)
  328.  
  329.     {
  330.  
  331.         if (drawBoard)
  332.  
  333.         {
  334.  
  335.             g.setColor(Color.lightGray);
  336.  
  337.             g.fillRect(0,0,size().width,size().height);
  338.  
  339.             fixBox(g,207,166,60,24,Color.black);
  340.  
  341.             fixBox(g,208,167,58,22,Color.blue);
  342.  
  343.             g.setColor(Color.cyan);
  344.  
  345.             g.drawString("Restart",215,183);
  346.  
  347.             fixDisc(g,2,2,187,Color.gray);
  348.  
  349.             fixDisc(g,0,0,185,Color.blue);
  350.  
  351.             fixDisc(g,0,0,183,Color.cyan);
  352.  
  353.             fixDisc(g,2,2,183,Color.black);
  354.  
  355.             fixDisc(g,2,2,181,Color.blue);
  356.  
  357.             fixCircle(g,8,8,171,Color.cyan);
  358.  
  359.             fixCircle(g,7,7,171,Color.black);
  360.  
  361.  
  362.  
  363.             for (i=0;i<9;i++)
  364.  
  365.                 for (j=0;j<9;j++)
  366.  
  367.                     if (map[i*9+j]>-1)
  368.  
  369.                         if (map[i*9+j]==0)
  370.  
  371.                             boardHole(g,18+j*17,18+i*17);
  372.  
  373.                         else
  374.  
  375.                             boardBall(g,18+j*17,18+i*17);
  376.  
  377.             for (i=0;i<deadBalls;i++)
  378.  
  379.             {
  380.  
  381.                 k=i/5;
  382.  
  383.                 j=i-k*5;
  384.  
  385.                 deadBall(g,193+j*17,5+k*17);
  386.  
  387.             }
  388.  
  389.         }
  390.  
  391.         else
  392.  
  393.         {
  394.  
  395.             map[pickX+9*pickY]=0;
  396.  
  397.             map[(pickX+dragX)/2+9*(pickY+dragY)/2]=0;
  398.  
  399.             map[dragX+9*dragY]=1;
  400.  
  401.             boardHole(g,18+pickX*17,18+pickY*17);
  402.  
  403.             boardHole(g,18+((pickX+dragX)/2)*17,18+((pickY+dragY)/2)*17);
  404.  
  405.             boardBall(g,18+dragX*17,18+dragY*17);
  406.  
  407.             i=deadBalls/5;
  408.  
  409.             j=deadBalls-i*5;
  410.  
  411.             deadBall(g,193+j*17,5+i*17);
  412.  
  413.             deadBalls++;
  414.  
  415.             drawBoard=true;
  416.  
  417.         }
  418.  
  419.     }    
  420.  
  421. }
  422.  
  423.