|
001 packagepackage is used to name the directory or folder a class is in FA;
002 //start auto-imports
003 importimport means to make the classes and/or packages available in this program java.lang.*;
004 //end auto-imports
005
006 importimport means to make the classes and/or packages available in this program fang.*;
007 importimport means to make the classes and/or packages available in this program java.awt.*;
008 importimport means to make the classes and/or packages available in this program 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 * @authorthis is the Javadoc tag for documenting who created the source code 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 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects Assignment8 extendsextends means to customize or extend the functionality of a class Game
025 {open braces start code blocks and must be matched with a close brace
026 /**light is the object to be made invisible. */
027 privateprivate is used to restrict access to the current class only OvalSprite light;
028 /** box is the background element forfor is a looping structure for repeatedly executing a block of code each light. */
029 privateprivate is used to restrict access to the current class only RectangleSprite box;
030 /** row represents a horizonal row in a 2-Dimensional array. */
031 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer row =this assignment operator makes the left side equal to the right side 0;
032 /** column represents the number of vertical columns in a 2-Dimensional array. */
033 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer column =this assignment operator makes the left side equal to the right side 0;
034 /** lights is a 2-dimensional array[brackets are typically used to declare, initialize and index (indicate which element of) arrays7]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrays7]brackets are typically used to declare, initialize and index (indicate which element of) arrays of light which are to be put out whilewhile is a looping structure for executing code repeatedly playing the game. */
035 privateprivate is used to restrict access to the current class only Sprite[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays lights =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Sprite[brackets are typically used to declare, initialize and index (indicate which element of) arrays7]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrays7]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
036 /** tenXRowPlusColumn is an integer equal in value to 10 times the row position of a light plus the column position. */
037 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer tenXRowPlusColumn =this assignment operator makes the left side equal to the right side 0;
038 /** lightRow is the horizontal row of a clicked light. */
039 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer lightRow =this assignment operator makes the left side equal to the right side 0;
040 /** lightColumn is the vertical column of a clicked light. */
041 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer lightColumn =this assignment operator makes the left side equal to the right side 0;
042
043
044
045
046 /**sets up the game*/
047 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup()
048 {open braces start code blocks and must be matched with a close brace
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 ifif executes the next statement only if the condition in parenthesis evaluates to true program works.*/
054 addRandomLights();
055 }close braces end code blocks and must match an earlier open brace
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 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeDisplay()
060 {open braces start code blocks and must be matched with a close brace
061 forfor is a looping structure for repeatedly executing a block of code(intint is the type for whole numbers and it is short for integer i=this assignment operator makes the left side equal to the right side1;i<=this evaluates to true if the left side is not more than the right side6;i++this is the increment operator, which increases the variable by 1)
062 {open braces start code blocks and must be matched with a close brace
063 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer j =this assignment operator makes the left side equal to the right side 1;j<=this evaluates to true if the left side is not more than the right side6;j++this is the increment operator, which increases the variable by 1)
064 {open braces start code blocks and must be matched with a close brace
065 box=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(1, 1);
066 box.setSize(.19);
067 box.setLocation(-.1+adds two numbers together or concatenates Strings together (i-1)*.2, -.1+adds two numbers together or concatenates Strings together(j-1)*.2);
068 box.setColor(getColor("blue"));
069 box.setVisible(truetrue is the boolean value that is the opposite of false);
070 addSprite(box);
071 }close braces end code blocks and must match an earlier open brace
072 }close braces end code blocks and must match an earlier open brace
073 }close braces end code blocks and must match an earlier open brace
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 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value createLightsTurnedOff()
077 {open braces start code blocks and must be matched with a close brace
078 forfor is a looping structure for repeatedly executing a block of code(intint is the type for whole numbers and it is short for integer i=this assignment operator makes the left side equal to the right side0;i<=this evaluates to true if the left side is not more than the right side6;i++this is the increment operator, which increases the variable by 1)
079 {open braces start code blocks and must be matched with a close brace
080 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer j =this assignment operator makes the left side equal to the right side 0;j<=this evaluates to true if the left side is not more than the right side6;j++this is the increment operator, which increases the variable by 1)
081 {open braces start code blocks and must be matched with a close brace
082 light =this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor OvalSprite(1, 1);
083 lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysi]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arraysj]brackets are typically used to declare, initialize and index (indicate which element of) arrays=this assignment operator makes the left side equal to the right sidelight;
084 light.setSize(.18);
085 light.setLocation(-.1+adds two numbers together or concatenates Strings together (i)*.2, -.1+adds two numbers together or concatenates Strings together(j)*.2);
086 light.setColor(getColor("red"));
087 light.setVisible(falsefalse is a value for the boolean type and means not true);
088 addSprite(light);
089 }close braces end code blocks and must match an earlier open brace
090 }close braces end code blocks and must match an earlier open brace
091 }close braces end code blocks and must match an earlier open brace
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 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value addRandomLights()
096 {open braces start code blocks and must be matched with a close brace
097 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 1;i<=this evaluates to true if the left side is not more than the right side5;i++this is the increment operator, which increases the variable by 1)
098 {open braces start code blocks and must be matched with a close brace
099 row =this assignment operator makes the left side equal to the right side 1+adds two numbers together or concatenates Strings together(intint is the type for whole numbers and it is short for integer)(4*Math.random());
100 column=this assignment operator makes the left side equal to the right side1+adds two numbers together or concatenates Strings together(intint is the type for whole numbers and it is short for integer)(4*Math.random());
101 changeLight(row,column);
102 changeLightsInCross(row,column);
103 }close braces end code blocks and must match an earlier open brace
104 }close braces end code blocks and must match an earlier open brace
105
106 /** Determine which of the lights have been clicked. The position is
107 * returned in the integer position =this assignment operator makes the left side equal to the right side 10*row +adds two numbers together or concatenates Strings together column. */
108 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer getLightClicked()
109 {open braces start code blocks and must be matched with a close brace
110 intint is the type for whole numbers and it is short for integer position =this assignment operator makes the left side equal to the right side 0;
111 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i=this assignment operator makes the left side equal to the right side1;i<=this evaluates to true if the left side is not more than the right side5;i++this is the increment operator, which increases the variable by 1)
112 {open braces start code blocks and must be matched with a close brace
113 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer j =this assignment operator makes the left side equal to the right side 1; j <=this evaluates to true if the left side is not more than the right side5; j++this is the increment operator, which increases the variable by 1)
114 {open braces start code blocks and must be matched with a close brace
115 ifif executes the next statement only if the condition in parenthesis evaluates to true(getClick2D() !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) getClick2D().intersects(lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysi]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arraysj]brackets are typically used to declare, initialize and index (indicate which element of) arrays))
116 {open braces start code blocks and must be matched with a close brace
117 position =this assignment operator makes the left side equal to the right side 10*i +adds two numbers together or concatenates Strings together j;
118 /** @returnnull position is used to determine the location of the light clicked.*/
119 returnreturn means to provide the result of the method and/or cease execution of the method immediately position;
120 }close braces end code blocks and must match an earlier open brace
121 }close braces end code blocks and must match an earlier open brace
122 }close braces end code blocks and must match an earlier open brace
123 /** @returnnull 0 is returned ifif executes the next statement only if the condition in parenthesis evaluates to true no click of the mouse is detected. */
124 returnreturn means to provide the result of the method and/or cease execution of the method immediately 0;
125 }close braces end code blocks and must match an earlier open brace
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 /** @paramthis is the Javadoc tag for documenting the purpose of parameters row designates the row of the clicked position. */
129 /** @paramthis is the Javadoc tag for documenting the purpose of parameters column designates the column of the clicked position. */
130 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value changeLight(intint is the type for whole numbers and it is short for integer row, intint is the type for whole numbers and it is short for integer column)
131 {open braces start code blocks and must be matched with a close brace
132 ifif executes the next statement only if the condition in parenthesis evaluates to true(lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn]brackets are typically used to declare, initialize and index (indicate which element of) arrays.isVisible())
133 lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setVisible(falsefalse is a value for the boolean type and means not true);
134 elseelse is what happens when the if condition is false
135 lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setVisible(truetrue is the boolean value that is the opposite of false);
136 }close braces end code blocks and must match an earlier open brace
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 /** @paramthis is the Javadoc tag for documenting the purpose of parameters row designates the row of the clicked position. */
140 /** @paramthis is the Javadoc tag for documenting the purpose of parameters column designates the column of the clicked position. */
141 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value changeLightsInCross(intint is the type for whole numbers and it is short for integer row, intint is the type for whole numbers and it is short for integer column)
142 {open braces start code blocks and must be matched with a close brace
143 ifif executes the next statement only if the condition in parenthesis evaluates to true(lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow+adds two numbers together or concatenates Strings together1]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn]brackets are typically used to declare, initialize and index (indicate which element of) arrays.isVisible())
144 lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow+adds two numbers together or concatenates Strings together1]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setVisible(falsefalse is a value for the boolean type and means not true);
145 elseelse is what happens when the if condition is false
146 lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow+adds two numbers together or concatenates Strings together1]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setVisible(truetrue is the boolean value that is the opposite of false);
147
148 ifif executes the next statement only if the condition in parenthesis evaluates to true(lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow-1]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn]brackets are typically used to declare, initialize and index (indicate which element of) arrays.isVisible())
149 lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow-1]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setVisible(falsefalse is a value for the boolean type and means not true);
150 elseelse is what happens when the if condition is false
151 lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow-1]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setVisible(truetrue is the boolean value that is the opposite of false);
152
153 ifif executes the next statement only if the condition in parenthesis evaluates to true(lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn+adds two numbers together or concatenates Strings together1]brackets are typically used to declare, initialize and index (indicate which element of) arrays.isVisible())
154 lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn+adds two numbers together or concatenates Strings together1]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setVisible(falsefalse is a value for the boolean type and means not true);
155 elseelse is what happens when the if condition is false
156 lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn+adds two numbers together or concatenates Strings together1]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setVisible(truetrue is the boolean value that is the opposite of false);
157
158 ifif executes the next statement only if the condition in parenthesis evaluates to true(lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn-1]brackets are typically used to declare, initialize and index (indicate which element of) arrays.isVisible())
159 lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn-1]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setVisible(falsefalse is a value for the boolean type and means not true);
160 elseelse is what happens when the if condition is false
161 lights[brackets are typically used to declare, initialize and index (indicate which element of) arraysrow]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrayscolumn-1]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setVisible(truetrue is the boolean value that is the opposite of false);
162 }close braces end code blocks and must match an earlier open brace
163
164 /**handle input and game events*/
165 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance()
166 {open braces start code blocks and must be matched with a close brace
167 /** Get the row and columm number of the light clicked. */
168 tenXRowPlusColumn=this assignment operator makes the left side equal to the right sidegetLightClicked();
169 ifif executes the next statement only if the condition in parenthesis evaluates to true(tenXRowPlusColumn !=this is the not equals operator which evaluates to true if both sides are different0)
170 {open braces start code blocks and must be matched with a close brace
171 /** Determines the row number of the light clicked by truncating. */
172 lightRow =this assignment operator makes the left side equal to the right sidetenXRowPlusColumn/10;
173 /** Determines by column number of the light clicked by the remainder. */
174 lightColumn=this assignment operator makes the left side equal to the right sidetenXRowPlusColumn%this divides the left side by the right side and evaluates to the remainder10;
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 }close braces end code blocks and must match an earlier open brace
180 }close braces end code blocks and must match an earlier open brace
181 }close braces end code blocks and must match an earlier open brace
|