|
001 packagepackage is used to name the directory or folder a class is in FA;
002
003 importimport means to make the classes and/or packages available in this program fang.*;
004 importimport means to make the classes and/or packages available in this program java.awt.*;
005 importimport means to make the classes and/or packages available in this program java.awt.geom.*;
006 importimport means to make the classes and/or packages available in this program wiki.Wiki;
007
008 /**
009 * The Monty Hall game.
010 * @ Frank Anderson
011 */
012 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 Assignment5_Monty extendsextends means to customize or extend the functionality of a class Game
013 {open braces start code blocks and must be matched with a close brace// Begin Main
014
015 privateprivate is used to restrict access to the current class only RectangleSprite door[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;
016 privateprivate is used to restrict access to the current class only OvalSprite dot[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;
017 privateprivate is used to restrict access to the current class only StringSprite label[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;
018 privateprivate is used to restrict access to the current class only StringSprite mes;
019 privateprivate is used to restrict access to the current class only StringSprite mes1;
020 privateprivate is used to restrict access to the current class only StringSprite instructions;
021 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer otherDoor ;
022 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer firstDoor ;
023 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer prize;
024 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer numClicks;
025
026
027 /**sets up the game*/
028 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup()
029 {open braces start code blocks and must be matched with a close brace//begin setup()
030
031 /*Initialize variables. */
032 door =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite[brackets are typically used to declare, initialize and index (indicate which element of) arrays4]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
033 dot =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite[brackets are typically used to declare, initialize and index (indicate which element of) arrays4]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
034 label =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite[brackets are typically used to declare, initialize and index (indicate which element of) arrays4]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
035 prize =this assignment operator makes the left side equal to the right side random.nextInt(3)+adds two numbers together or concatenates Strings together1; // prize is a random number between 1 and 3
036
037 numClicks =this assignment operator makes the left side equal to the right side 1;
038
039
040
041
042 /*Display instruction to start the game at the bottom area of the screen. */
043 instructions =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite("Click the 'START' button to begin the game.");
044 instructions.setSize(.8);
045 instructions.setLocation(.5,.9);
046 addSprite(instructions);
047
048 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 side 3; i++this is the increment operator, which increases the variable by 1)
049 {open braces start code blocks and must be matched with a close brace// begin making dots (prizes)
050 dot[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 =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(1,2);
051 dot[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.setColor(getColor("green"));
052 ifif executes the next statement only if the condition in parenthesis evaluates to true (i ==this is the comparison operator which evaluates to true if both sides are the same prize)
053 dot[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.setColor(getColor("yellow"));
054 dot[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.setSize(.1);
055 dot[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.setLocation(i*1.0/4, .4);
056 dot[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.setVisible(truetrue is the boolean value that is the opposite of false);
057 addSprite(dot[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);
058 }close braces end code blocks and must match an earlier open brace // end of displaying dots
059
060 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 side 3; i++this is the increment operator, which increases the variable by 1)
061 {open braces start code blocks and must be matched with a close brace// begin for display the doors
062 door[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=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(1,2);
063 door[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.setColor(getColor("red"));
064 door[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.setSize(.35);
065 door[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.setLocation(i*1.0/4, .5);
066 door[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.setVisible(truetrue is the boolean value that is the opposite of false);
067 addSprite(door[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);
068
069 label[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 =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite("door "+adds two numbers together or concatenates Strings togetheri);
070 label[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.setSize(.1);
071 label[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.setLocation(i*1.0/4, .45);
072 label[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.setVisible(truetrue is the boolean value that is the opposite of false);
073 addSprite(label[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);
074 }close braces end code blocks and must match an earlier open brace // end for dispaying doors
075
076
077
078 }close braces end code blocks and must match an earlier open brace// end setup
079
080 /* Display the wining message. */
081 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value winMes()
082 {open braces start code blocks and must be matched with a close brace
083 // begin winMes
084 mes =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite("YOU WIN !!");
085 mes.setLocation(.5,.5);
086 mes.setSize(.8);
087 mes.setVisible(truetrue is the boolean value that is the opposite of false);
088 addSprite(mes);
089 }close braces end code blocks and must match an earlier open brace
090 // end winMes
091
092 /*Display the losing message*/
093 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value loseMes()
094 {open braces start code blocks and must be matched with a close brace // begin loseMes
095 StringSprite mes =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite("You lose. Never gamble!!");
096 mes.setLocation(.5,.5);
097 mes.setSize(.8);
098 mes.setVisible(truetrue is the boolean value that is the opposite of false);
099 addSprite(mes);
100 }close braces end code blocks and must match an earlier open brace // end loseMes
101
102
103 /*Determine which door is being clicked*/
104 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer whichDoor()
105 // begin whichDoor()
106 {open braces start code blocks and must be matched with a close brace
107 intint is the type for whole numbers and it is short for integer dr;
108 dr=this assignment operator makes the left side equal to the right side0;
109 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 side3; i++this is the increment operator, which increases the variable by 1)
110 {open braces start code blocks and must be matched with a close brace// start for.
111 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 &&) door[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.intersects(getClick2D()))
112 {open braces start code blocks and must be matched with a close brace // open if
113 dr =this assignment operator makes the left side equal to the right side i;
114 }close braces end code blocks and must match an earlier open brace // end if
115 }close braces end code blocks and must match an earlier open brace //end for
116
117 returnreturn means to provide the result of the method and/or cease execution of the method immediately dr; // Returns the number of the door clicked.
118 }close braces end code blocks and must match an earlier open brace// ends whichDoor()
119
120 /**handle input and game events*/
121 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance()
122 {open braces start code blocks and must be matched with a close brace// open advanced
123 ifif executes the next statement only if the condition in parenthesis evaluates to true (numClicks ==this is the comparison operator which evaluates to true if both sides are the same 1)
124 {open braces start code blocks and must be matched with a close brace
125 /*turn off the START instruction. */
126 instructions.setVisible(falsefalse is a value for the boolean type and means not true);
127
128 /* Instructions forfor is a looping structure for repeatedly executing a block of code selecting a door.*/
129 mes1 =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite("Click on your first choice..");
130 mes1.setLocation(.5,.8);
131 mes1.setColor(getColor("blue"));
132 mes1.setSize(.7);
133 mes1.setVisible(truetrue is the boolean value that is the opposite of false);
134 addSprite(mes1);
135
136 /*Calls the method which checks forfor is a looping structure for repeatedly executing a block of code a mouse click and returns the door clicked. */
137 firstDoor =this assignment operator makes the left side equal to the right side whichDoor();
138 ifif executes the next statement only if the condition in parenthesis evaluates to true(firstDoor>=this evaluates to true if the left side is not less than the right side1 &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 &&) firstDoor<=this evaluates to true if the left side is not more than the right side3)
139 {open braces start code blocks and must be matched with a close brace //open if fc gte 1 and lte 3 and numClicks = 1
140 numClicks =this assignment operator makes the left side equal to the right side 2;
141 // begin using firstDoor
142 instructions.setVisible(falsefalse is a value for the boolean type and means not true); // remove 'click START' message.
143
144 /* ifif executes the next statement only if the condition in parenthesis evaluates to true firstDoor =this assignment operator makes the left side equal to the right side prize, display the winning message and open other two doors,
145 elseelse is what happens when the if condition is false open the other non wining doors as well as the winning door.*/
146 ifif executes the next statement only if the condition in parenthesis evaluates to true(firstDoor ==this is the comparison operator which evaluates to true if both sides are the same prize )
147 {open braces start code blocks and must be matched with a close brace
148 /* open ifif executes the next statement only if the condition in parenthesis evaluates to true fd =this assignment operator makes the left side equal to the right side prize; remove doors and lables; display win message */
149 winMes();
150 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 k =this assignment operator makes the left side equal to the right side 1; k <=this evaluates to true if the left side is not more than the right side 3; k++this is the increment operator, which increases the variable by 1) // open all doors
151 {open braces start code blocks and must be matched with a close brace
152 door[brackets are typically used to declare, initialize and index (indicate which element of) arraysk]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);
153 label[brackets are typically used to declare, initialize and index (indicate which element of) arraysk]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);
154 }close braces end code blocks and must match an earlier open brace
155 }close braces end code blocks and must match an earlier open brace // close if fd = prize
156 elseelse is what happens when the if condition is false
157 // open the other non-winning door if the first door is not a winner.
158 {open braces start code blocks and must be matched with a close brace//begin else
159 otherDoor =this assignment operator makes the left side equal to the right side 6/(firstDoor*prize);
160 door[brackets are typically used to declare, initialize and index (indicate which element of) arraysotherDoor]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);
161 mes1.setVisible(falsefalse is a value for the boolean type and means not true); // turn off 'choose any door' message
162 // display 'Click next door'
163 StringSprite door2 =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite("Choose a second door.");
164 door2.setSize(.9);
165 door2.setColor(getColor("white"));
166 door2.setLocation(.5,.8);
167 door2.setVisible(truetrue is the boolean value that is the opposite of false);
168 addSprite(door2);
169 }close braces end code blocks and must match an earlier open brace// end else
170
171 }close braces end code blocks and must match an earlier open brace// end fd gte 1 and lte 3
172
173 }close braces end code blocks and must match an earlier open brace// end numclicks 1
174
175 /* Check to see which door was clicked the second time.*/
176 elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true(numClicks ==this is the comparison operator which evaluates to true if both sides are the same2 &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 &&) firstDoor !=this is the not equals operator which evaluates to true if both sides are different prize)
177 {open braces start code blocks and must be matched with a close brace// open num clicks 2
178 /*Check to see which door was chosen the second time. */
179 otherDoor =this assignment operator makes the left side equal to the right side whichDoor();
180 ifif executes the next statement only if the condition in parenthesis evaluates to true (otherDoor >=this evaluates to true if the left side is not less than the right side1 &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 &&) otherDoor <=this evaluates to true if the left side is not more than the right side3)
181 {open braces start code blocks and must be matched with a close brace // checking value of otherDoor
182 /*Open both remaining doors */
183 door[brackets are typically used to declare, initialize and index (indicate which element of) arraysotherDoor]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); label[brackets are typically used to declare, initialize and index (indicate which element of) arraysotherDoor]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);
184 door[brackets are typically used to declare, initialize and index (indicate which element of) arraysprize]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); label[brackets are typically used to declare, initialize and index (indicate which element of) arraysprize]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);
185 /*Check to see ifif executes the next statement only if the condition in parenthesis evaluates to true otherDoor is the winning and display "You win".*/
186 ifif executes the next statement only if the condition in parenthesis evaluates to true(otherDoor ==this is the comparison operator which evaluates to true if both sides are the same prize) // prize = 2
187 {open braces start code blocks and must be matched with a close brace// begin if you win
188 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 k =this assignment operator makes the left side equal to the right side 1; k <=this evaluates to true if the left side is not more than the right side 3; k++this is the increment operator, which increases the variable by 1) // open all doors
189 {open braces start code blocks and must be matched with a close brace
190 door[brackets are typically used to declare, initialize and index (indicate which element of) arraysk]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);
191 label[brackets are typically used to declare, initialize and index (indicate which element of) arraysk]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);
192 }close braces end code blocks and must match an earlier open brace
193 winMes();
194 }close braces end code blocks and must match an earlier open brace// end if you win
195
196 elseelse is what happens when the if condition is false
197 /*Display "you lose" ifif executes the next statement only if the condition in parenthesis evaluates to true a non-winning door forfor is a looping structure for repeatedly executing a block of code second choice*/
198 {open braces start code blocks and must be matched with a close brace // open else for numClicks = 2
199 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 k =this assignment operator makes the left side equal to the right side 1; k <=this evaluates to true if the left side is not more than the right side 3; k++this is the increment operator, which increases the variable by 1) // open all doors
200 {open braces start code blocks and must be matched with a close brace
201 door[brackets are typically used to declare, initialize and index (indicate which element of) arraysk]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);
202 label[brackets are typically used to declare, initialize and index (indicate which element of) arraysk]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);
203 }close braces end code blocks and must match an earlier open brace
204
205 loseMes();
206 }close braces end code blocks and must match an earlier open brace // end else for numClicks = 2
207 }close braces end code blocks and must match an earlier open brace // end of checking value of otherDoor
208 }close braces end code blocks and must match an earlier open brace // numclicks = 2
209 }close braces end code blocks and must match an earlier open brace// end advanced()
210 }close braces end code blocks and must match an earlier open brace//end main
|