FA/Assignment8

From ggc

Jump to: navigation, search

001 package FA;
002 //start auto-imports
003 import java.lang.*;
004 //end auto-imports
005 
006 import fang.*;
007 import java.awt.*;
008 import java.awt.geom.*;
009 
010 /**
011  * Assignment 8: Lights Out.
012  * Lights Out is a board game where the player tries to put out an array of lights
013  * displayed in the game field. This is done by clicking on a block. Once a block
014  * is clicked, the light in that position changes status from on to off, or vice versa.
015  * The lights directly above and below the chosen light as well as the lights
016  * directly to the right and left of the chosen light change status.
017  *
018  @author Frank Anderson
019  * I used Derrick Dixon's idea of creating the game board by making non visible rows 
020  * above the top  and below the bottom of the visible playing field as well as
021  * to the left and right of the columns marking the edges of the playing field.
022  * Derrick also helped with some of the debugging of the game. 
023  */
024 public class Assignment8 extends Game
025 {
026   /**light is the object to be made invisible. */
027   private OvalSprite light;
028   /** box is the background element for each light.  */
029   private RectangleSprite box;
030   /** row represents a horizonal row in a 2-Dimensional array. */
031   private int row = 0;
032   /** column represents the number of vertical columns in a 2-Dimensional array. */
033   private int column = 0;
034   /** lights is a 2-dimensional array[7][7] of light which are to be put out while playing the game. */
035   private Sprite[][] lights  = new Sprite[7][7];
036   /** tenXRowPlusColumn is an integer equal in value to 10 times the row position of a light plus the column position. */
037   private int tenXRowPlusColumn = 0;
038   /** lightRow is the horizontal row of a clicked light. */
039   private int lightRow = 0;
040   /** lightColumn is the vertical column of a clicked light.  */
041   private int lightColumn = 0;
042 
043 
044 
045 
046   /**sets up the game*/
047   public void setup()
048   {
049     /** Make the background of blue boxes.  */
050     makeDisplay();
051     /** Place the light (turned off) on top of the blue boxes.   */
052     createLightsTurnedOff();
053     /** Add random light to check if program works.*/
054     addRandomLights();
055   }
056 
057   /**Place 25 blue squares on the screen on to which light will be placed.
058     * The squares will be arranged in a 5 x 5 matrix occuping the entire screen.*/
059   private void makeDisplay()
060   {
061     for(int i=1;i<=6;i++)
062     {
063       for (int = 1;j<=6;j++)
064       {
065         box=new RectangleSprite(11);
066         box.setSize(.19);
067         box.setLocation(-.1+ (i-1)*.2, -.1+(j-1)*.2);
068         box.setColor(getColor("blue"));
069         box.setVisible(true);
070         addSprite(box);
071       }
072     }
073   }
074   /** Creates 49 circles in a 7x7 matrix with columns 0 and 6 off the screen.
075     * Rows 0 and 6 are above and below the playing field.   `*/
076   private void createLightsTurnedOff()
077   {
078     for(int i=0;i<=6;i++)
079     {
080       for (int = 0;j<=6;j++)
081       {
082         light =new OvalSprite(11);
083         lights[i][j]=light;
084         light.setSize(.18);
085         light.setLocation(-.1+ (i)*.2, -.1+(j)*.2);
086         light.setColor(getColor("red"));
087         light.setVisible(false);
088         addSprite(light);
089       }
090     }
091   }
092   /**Add a pattern of lights by making 4 random cliks in the game field.
093    * each click changes the status of that light as well as those adjacent
094    * to the light clicked. Those on the diagonal are not changed. */
095   private void addRandomLights()
096   {
097     for (int = 1;i<=5;i++)
098     {
099       row = 1+(int)(4*Math.random());
100       column=1+(int)(4*Math.random());
101       changeLight(row,column);
102       changeLightsInCross(row,column);
103     }
104   }
105 
106   /** Determine which of the lights have been clicked. The position is
107     * returned in the integer   position  = 10*row + column.    */
108   private int getLightClicked()
109   {
110     int position = 0;
111     for (int i=1;i<=5;i++)
112     {
113       for (int = 1; j <=5; j++)
114       {
115         if(getClick2D() != null && getClick2D().intersects(lights[i][j]))
116         {
117           position = 10*i + j;
118           /** @return  position is used to determine the location of the light clicked.*/
119           return  position;
120         }
121       }
122     }
123     /** @return 0 is returned if no click of the mouse is detected. */
124     return 0;
125   }
126   /** Changes the status of a light from on to off and vice versa given the row
127     * and column numbers of the light stored in the array lights.   */
128   /** @param row designates the row of the clicked position. */
129   /** @param column designates the column of the clicked position.  */
130   private void changeLight(int row, int column)
131   {
132     if(lights[row][column].isVisible())
133       lights[row][column].setVisible(false);
134     else
135       lights[row][column].setVisible(true);
136   }
137   /** Changes the adjacent lighte above, below, to the left of, and to the right
138     * of the light designated as the center of the cross by  (row,column).   */
139   /** @param row designates the row of the clicked position. */
140   /** @param column designates the column of the clicked position.  */
141   private void changeLightsInCross(int row, int column)
142   {
143     if(lights[row+1][column].isVisible())
144       lights[row+1][column].setVisible(false);
145     else
146       lights[row+1][column].setVisible(true);
147 
148     if(lights[row-1][column].isVisible())
149       lights[row-1][column].setVisible(false);
150     else
151       lights[row-1][column].setVisible(true);
152 
153     if(lights[row][column+1].isVisible())
154       lights[row][column+1].setVisible(false);
155     else
156       lights[row][column+1].setVisible(true);
157 
158     if(lights[row][column-1].isVisible())
159       lights[row][column-1].setVisible(false);
160     else
161       lights[row][column-1].setVisible(true);
162   }
163 
164   /**handle input and game events*/
165   public void advance()
166   {
167     /** Get the row and columm number of the light clicked. */
168     tenXRowPlusColumn=getLightClicked();
169     if(tenXRowPlusColumn !=0)
170     {
171       /** Determines the row number of the light clicked by truncating. */
172       lightRow =tenXRowPlusColumn/10;
173       /** Determines by column number of the light clicked by the remainder. */
174       lightColumn=tenXRowPlusColumn%10;
175       /** Change the status of the light that was clicked.  */
176       changeLight(lightRow, lightColumn);
177       /** Change the status of the lights in the cross about the clicked light. */
178       changeLightsInCross(lightRow, lightColumn);
179     }
180   }
181 }


Download/View FA/Assignment8.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