Charles/LightsOut

From ggc

Jump to: navigation, search

001 package Charles;
002 //start auto-imports
003 import java.util.*;
004 //end auto-imports
005 
006 import fang.*;
007 import java.awt.*;
008 import java.awt.geom.*;
009 
010 /**
011  * All about my game.
012  @author Charles
013  */
014 /**Used Frank Anderson's LightsOut to get an idea for the game structure*/
015 public class LightsOut extends Game
016 {
017   /**Creates the grid of boxes displayed as the playing board*/
018   private ArrayList<Sprite> allBoxes;
019   /**the image used to display the lights on the board*/
020   private OvalSprite light;
021   /**array list of the lights to be displayed*/
022   private Sprite[][]lights = new Sprite[7][7];
023   /**used to identify the horizantal position of an individual light*/
024   private int hor=0;
025   /**used to identify the vertical position of an individual light*/
026   private int vert=0;
027   /**sets up the game*/
028   public void setup()
029   {
030     makeBoxes();
031     makeLights();
032   }
033   /** Creates the boxes that hold the light*/
034   private void makeBoxes()
035   {
036     allBoxes=new ArrayList<Sprite>();
037     int boxesAcross=5;
038     int boxesDown=5;
039     for(int j=0; j<boxesDown; j++)
040     {
041       for(int i=1; i<=boxesAcross; i++)
042       {
043         RectangleSprite box=new RectangleSprite(1,1);
044         box.setSize(0.9/boxesAcross);
045         double x=0.5/boxesAcross+1.0/boxesAcross*(i-1);
046         double y=0.5/boxesAcross+(j+0.0)/boxesAcross;
047         box.setLocation(x,y);
048         box.setColor(getColor("gray"));
049         addSprite(box);
050       }
051     }
052   }
053   /**creates and adds the lights that are displayed on the grid already turned off*/
054   private void makeLights()
055   {
056     for(int i=0;i<=6;i++)
057     {
058       for (int = 0;j<=6;j++)
059       {
060         light =new OvalSprite(11);
061         lights[i][j]=light;
062         light.setSize(.18);
063         light.setLocation(-.1+ (i)*.2, -.1+(j)*.2);
064         light.setColor(getColor("red"));
065         light.setVisible(false);
066         addSprite(light);
067       }
068     }
069   }
070   /**returns the a value of i&j to determine which light was selected*/
071   private int getSelectedLight()
072   {
073     int loc=0;
074     for(int i=1; i<=5; i++)
075     {
076       for(int j=1; j<=5; j++)
077       {
078         if(getClick2D()!=null && getClick2D().intersects(lights[i][j]))
079         {
080           loc = 10*i+j;
081           return loc;
082         }
083       }
084     }
085     return 0;
086   }
087   /**turns on and off the selected light*/
088   private void lightSwitch(int hor, int vert)
089   {
090     if(lights[hor][vert].isVisible())
091     {
092       lights[hor][vert].setVisible(false);
093     }
094     else
095     {
096       lights[hor][vert].setVisible(true);
097     }
098   }
099   /**switches the lights to the left, right top and bottom of the selected light*/
100   private void switchLightsInGroups(int hor, int vert)
101   {
102     if(lights[hor+1][vert].isVisible())
103     {
104       lights[hor+1][vert].setVisible(false);
105     }
106     else
107     {
108       lights[hor+1][vert].setVisible(true);
109     }
110     if(lights[hor-1][vert].isVisible())
111     {
112       lights[hor-1][vert].setVisible(false);
113     }
114     else
115     {
116       lights[hor-1][vert].setVisible(true);
117     }
118     if(lights[hor][vert+1].isVisible())
119     {
120       lights[hor][vert+1].setVisible(false);
121     }
122     else
123     {
124       lights[hor][vert+1].setVisible(true);
125     }
126     if(lights[hor][vert-1].isVisible())
127     {
128       lights[hor][vert-1].setVisible(false);
129     }
130     else
131     {
132       lights[hor][vert-1].setVisible(true);
133     }
134   }
135   //generatePatterns();
136   {}
137   /**handle input and game events*/
138   public void advance()
139   {
140     /**gets the previously determined location of the selected light and convertes the values so that it appropriately matches the grid*/
141     int locationOfLight=getSelectedLight();
142     if(locationOfLight!=0)
143     {
144       int xOfLight=locationOfLight/10;
145       int yOfLight=locationOfLight%10;
146       lightSwitch(xOfLight, yOfLight);
147       switchLightsInGroups(xOfLight, yOfLight);
148     }
149   }
150 }


Download/View Charles/LightsOut.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter