Rohrer/Lights Out Assignment 8

From ggc

Jump to: navigation, search

001 package Rohrer;
002 //start auto-imports
003 import java.util.*;
004 //end auto-imports
005 
006 import java.lang.*;
007 import fang.*;
008 import java.awt.*;
009 import java.awt.geom.*;
010 
011 /**
012  * Assignment 8: Lights Out.
013  @author Kevin Rohrer
014  * with permission from Frank Anderson I used his base code and made my improvments
015  */
016 public class Lights_Out_Assignment_8 extends Game
017 {
018   /** shapes used to create and then add to the grid and lights arrays*/
019   private OvalSprite light;
020   private RectangleSprite box;
021   /**screen outline enabling the display of text around the border*/
022   public OutlineSprite screenOutline;
023   private int row = 0;
024   private int column = 0;
025   /** the size of the grid both in the horizontal and vertical*/
026   private int gridSize = 7;
027   /** the arrays used to create the correct amount of boxes and lights*/
028   private Sprite[][] lights  = new Sprite[gridSize][gridSize];
029   private Sprite[][] grid  = new Sprite[gridSize][gridSize];
030   /** tenXRowPlusColumn is an integer equal in value to 10 times the row position of a light plus the column position. */
031   private int tenXRowPlusColumn = 0;
032   /** lightRow is the horizontal row of a clicked light. */
033   private int lightRow = 0;
034   /** lightColumn is the vertical column of a clicked light.  */
035   private int lightColumn = 0;
036   /** the arraylist that shows how to finish the puzzle when activated*/
037   ArrayList<Integer> hints = new ArrayList<Integer>();
038   /** creates the clicks remaining text and helpscreen notification text*/
039   private StringSprite clicksRemaining, help;
040   /**ammount of clicks until auto loose*/
041   int clicks = 20;
042 
043   /**sets up the game*/
044   public void setup()
045   {
046     makeGridBoxes();
047     createLights();
048     addRandomLights();
049     addScreenOutline();
050     helpScreen();
051     textDisplay();
052   }
053 
054   /**handle input and game events*/
055   public void advance()
056   {
057     changeWhenClicked();
058     addHints();
059   }
060 
061   /** gets which light has been clicked and returns the value */
062   private int getLightClicked()
063   {
064     int position = 0;
065     for (int i=1;i<=5;i++)
066     {
067       for (int = 1; j <=5; j++)
068       {
069         if(getClick2D().intersects(lights[i][j]))
070         {
071           position = 10*i + j;
072           clicks--;
073           clicksRemaining.setText("Clicks Remaining: " + clicks);
074           hints.add(position);
075           grid[i][j].setColor(getColor("green"));
076 
077 
078           /** return  position is used to determine the location of the light clicked.*/
079           return position;
080         }
081       }
082     }
083     /** @return 0 is returned if no click of the mouse is detected. */
084     return(0);
085   }
086 
087 
088 
089 
090 
091 
092 
093 
094 
095 
096 
097 
098 
099 
100 
101 
102 
103   /** when the (h) key is pressed all the preveously clicked gris spaces are highlighted*/
104   public void addHints()
105   {
106     if(getKeyPressed()=='h')
107     {
108       for(int hint: hints)
109       {
110         int hintRow = hint/10;
111         int hintColumn = hint%10;
112         grid[hintRow][hintColumn].setColor(getColor("purple"));
113       }
114     }
115   }
116   /**Makes random clicks to create the puzzle but still have it solvable */
117   private void addRandomLights()
118   {
119     for (int = 1;i<=5;i++)
120     {
121       row = 1+(int)(4*Math.random());
122       column=1+(int)(4*Math.random());
123       changeLight(row,column);
124       changeLightsInCross(row,column);
125       hints.add(row*10+column);
126     }
127   }
128   /** changes the light that is clicked when the user clickes a light*/
129   public void changeWhenClicked()
130   {
131     if(getClick2D() != null && clicks > 0)
132     {
133       /** Get the row and columm number of the light clicked */
134       tenXRowPlusColumn=getLightClicked();
135       if(tenXRowPlusColumn != 0)
136       {
137         /** Determines the row number of the light clicked by int devision */
138         lightRow = tenXRowPlusColumn/10;
139         /** Determines by column number of the light clicked by the remainder */
140         lightColumn = tenXRowPlusColumn%10;
141         /** Change the status of the light that was clicked */
142         changeLight(lightRow, lightColumn);
143         /** Change the status of the lights in the cross about the clicked light */
144         changeLightsInCross(lightRow, lightColumn);
145       }
146     }
147   }
148   /**makes the squares in the backround to create the basic grid */
149   private void makeGridBoxes()
150   {
151     for(int i=0;i<gridSize;i++)
152     {
153       for (int j=0;j<gridSize;j++)
154       {
155         box=new RectangleSprite(11);
156         grid[i][j] = box;
157         box.setSize(.9/gridSize);
158         double = 0.5/gridSize+1.0/gridSize*i;
159         double = 0.5/gridSize+1.0/gridSize*j;
160         box.setLocation(x, y);
161         box.setColor(getColor("green"));
162         box.setVisible(true);
163         addSprite(box);
164       }
165     }
166   }
167   /** Creates 49 circles in a 7x7 matrix with columns 0 and 6 off the screen.
168     * Rows 0 and 6 are above and below the playing field */
169   private void createLights()
170   {
171     for(int i=0;i<gridSize;i++)
172     {
173       for (int j=0;j<gridSize;j++)
174       {
175         light =new OvalSprite(11);
176         lights[i][j] = light;
177         light.setSize(.85/gridSize);
178         double = 0.5/gridSize+1.0/gridSize*i;
179         double = 0.5/gridSize+1.0/gridSize*j;
180         light.setLocation(x, y);
181         light.setColor(getColor("orange red"));
182         light.setVisible(false);
183         addSprite(light);
184       }
185     }
186   }
187   /** creates a outline sprite to hide the outer grid bricks and corosponding lights*/
188   public void addScreenOutline()
189   {
190     PolygonSprite screen = new PolygonSprite(.0,.0,1,.0,1,1,.0,1);
191     screenOutline = new OutlineSprite(screen);
192     screenOutline.setSize(1.1);
193     screenOutline.setLineThickness(0.205);
194     screenOutline.setLocation(0.50.5);
195     screenOutline.setColor(getColor("black"));
196     addSprite(screenOutline);
197   }
198   /** changes the status of the clicked light depending on preveous status */
199   private void changeLight(int row, int column)
200   {
201     if(lights[row][column].isVisible())
202     {
203       lights[row][column].setVisible(false);
204     }
205     else
206     {
207       lights[row][column].setVisible(true);
208     }
209   }
210   /** changes the light above and below and to ither side of the clicked light */
211   private void changeLightsInCross(int row, int column)
212   {
213     if(lights[row+1][column].isVisible())
214       lights[row+1][column].setVisible(false);
215     else
216       lights[row+1][column].setVisible(true);
217 
218     if(lights[row-1][column].isVisible())
219       lights[row-1][column].setVisible(false);
220     else
221       lights[row-1][column].setVisible(true);
222 
223     if(lights[row][column+1].isVisible())
224       lights[row][column+1].setVisible(false);
225     else
226       lights[row][column+1].setVisible(true);
227 
228     if(lights[row][column-1].isVisible())
229       lights[row][column-1].setVisible(false);
230     else
231       lights[row][column-1].setVisible(true);
232   }
233   /** creates the text displaying the remaining clicks and the text informing of help screen*/
234   public void textDisplay()
235   {
236     clicksRemaining = new StringSprite("Clicks Remaining: " + clicks);
237     clicksRemaining.leftJustify();
238     clicksRemaining.setWidth(.7);
239     clicksRemaining.setLocation(.01.055);
240     clicksRemaining.setColor(getColor("White"));
241     addSprite(clicksRemaining);
242 
243     help = new StringSprite("click help for further instructions");
244     help.leftJustify();
245     help.setWidth(.5);
246     help.setLocation(.01.975);
247     help.setColor(getColor("white"));
248     addSprite(help);
249   }
250   /** creates the text to go into the help screen*/
251   public void helpScreen()
252   {
253     String helpText=
254         "To solve the puzzle you must turn all the lights off<br>"+
255         "you have a total of 20 clicks to finish the puzzle or you lose<br>"+
256         "Press the (H) key to highlight the presed lights in an attempt to help solve the puzzle<br>"+
257         "Enjoy.";
258     setHelpText(helpText);
259   }
260 }


Download/View Rohrer/Lights_Out_Assignment_8.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