1 Tutorial 15 Craps Game Application Introducing Random

  • Slides: 45
Download presentation
1 Tutorial 15 – Craps Game Application Introducing Random Number Generation and the JPanel

1 Tutorial 15 – Craps Game Application Introducing Random Number Generation and the JPanel Outline 15. 1 15. 2 15. 3 15. 4 15. 5 Test-Driving the Craps Game Application Random Number Generation Using Constants in the Craps Game Application Using Random Numbers in the Craps Game Application Wrap-Up © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

2 Objective • In this tutorial, you will learn to: – Use simulation techniques

2 Objective • In this tutorial, you will learn to: – Use simulation techniques that employ random number generation. – Use methods of class Random. – Generate random numbers. – Use constants to enhance code readability. – Use a JPanel and a Titled. Border to add a border around components. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

15. 1 Test-Driving the Craps Game Application © Copyright 1992 -2004 by Deitel &

15. 1 Test-Driving the Craps Game Application © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 1 Initial appearance of

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 1 Initial appearance of Craps Game application. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 2 Player winning on

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 2 Player winning on first roll by rolling 7. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 3 Player losing on

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 3 Player losing on first roll by rolling 3. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 4 Player’s first roll

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 4 Player’s first roll setting the point that the player must match to win. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 5 Player winning the

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 5 Player winning the game by matching the point before rolling a 7. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 6 Player losing by

15. 1 Test-Driving the Craps Game Application (Cont. ) Figure 15. 6 Player losing by rolling a 7 before matching the point. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9

10 15. 2 Random Number Generation • Class Random random. Generator = new Random();

10 15. 2 Random Number Generation • Class Random random. Generator = new Random(); int random. Number = random. Generator. next. Int(); • Reference variable – Refers to an object – Contains the location in the computer’s memory of an object • Keyword new – Creates an object and assigns it a location in memory • Method next. Int – Generates a random int from all possible int values (positive and negative) © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

11 15. 2 Random Number Generation (Cont. ) • Method next. Double – Generates

11 15. 2 Random Number Generation (Cont. ) • Method next. Double – Generates a random double – Returns a positive double value between 0. 0 and 1. 0 (not including 1. 0) © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

15. 3 Using Constants in the Craps Game Application When the player clicks the

15. 3 Using Constants in the Craps Game Application When the player clicks the Play JButton Roll the two dice using random numbers Calculate the sum of the two dice Display images of the rolled dice Switch based on the sum of the two dice: Case where the sum of the two dice is 7 or 11 Display the winning message Case where the sum of the two dice is 2, 3 or 12 Display the losing message Case where none of the preceding cases are true Set the value of the point to the sum of the dice and display the value Disable the Play JButton and enable the Roll JButton © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12

15. 3 Using Constants in the Craps Game Application (Cont. ) When the player

15. 3 Using Constants in the Craps Game Application (Cont. ) When the player clicks the Roll JButton Roll the two dice using random numbers Calculate the sum of the two dice Display images of the rolled dice If the player rolls the same value as the point Display the winning message Clear the value of the point Enable the Play JButton and disable the Roll JButton If the player rolls a 7 Display the losing message Clear the value of the point Enable the Play JButton and disable the Roll JButton © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13

15. 3 Using Constants in the Craps Game Application (Cont. ) © Copyright 1992

15. 3 Using Constants in the Craps Game Application (Cont. ) © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 9 Importing

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 9 Importing class Random of the java. util package. Importing the java. util. Random class • Packages – Predefined classes are grouped into categories of related classes – Package java. util • Provides random number processing capabilities with class Random • Keyword import – Imports a class – Allows your application to access that class © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 10 Declaring

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 10 Declaring constants in the Craps Game application. Declaring constants © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 11 Declaring

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 11 Declaring constants in the Craps Game application. Declaring constants © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 12 Adding

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 12 Adding instance variables to the Craps Game application. Declaring variable for point value Using new to create a Random object © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18

15. 3 Using Constants in the Craps Game Application (Cont. ) • Component JPanel

15. 3 Using Constants in the Craps Game Application (Cont. ) • Component JPanel – Container that allows grouping of related components – The content pane covered in Tutorial 2 is a JPanel © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19

15. 3 Using Constants in the Craps Game Application (Cont. ) • layout property

15. 3 Using Constants in the Craps Game Application (Cont. ) • layout property – Controls how components are arranged on a JPanel – Setting the value to null allows absolute positioning • Component locations are specified exactly by coordinates – Other layouts involve relative positioning • Components are placed in relation to other components on a container © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 13 Customizing

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 13 Customizing point. Dice. JPanel. Customizing the JPanel © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 14 Adding

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 14 Adding components to the JPanel. Adding a JLabel to the JPanel © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 15 Setting

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 15 Setting the border of point. Dice. JPanel. Creating a Titled. Border Setting the border property • border property – Titled. Border places a line and a title around a GUI component. – Any GUI component attached to the component with the border will appear inside the border – Default borders © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 16 Running

15. 3 Using Constants in the Craps Game Application (Cont. ) Figure 15. 16 Running Craps Game application. Titled. Border is displayed with Point as the title © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) • Keyword

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) • Keyword null – Clears a reference’s value • The Titled. Border’s title property – Controls the text that is displayed in the border • The JPanel’s repaint method – Redraws the JPanel © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15.

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15. 17 Clearing images and rolling the dice. Removing images from JLabels Setting the title of the border and updating the JPanel “Rolling” the dice © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15.

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15. 18 switch statement in play. JButton. Action. Performed. Winning on the first roll Losing on the first roll © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15.

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15. 19 default case in play. JButton. Action. Performed. Player must match the point Displaying the die images Displaying the point and updating the application Allowing the player to roll again © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15.

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15. 20 Rolling the dice in roll. JButton. Action. Performed. “Rolling” the dice © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15.

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15. 21 Determining the outcome of a roll. Displaying winning message Displaying losing message © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15.

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15. 22 Declaring the roll. Dice method. Getting two random numbers Displaying the die images Returning the sum of the dice © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15.

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15. 23 Declaring the display. Die method. Creating a new Image. Icon Displaying a die image © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15.

15. 4 Using Random Numbers in the Craps Game Application (Cont. ) Figure 15. 24 Running the completed Craps Game application. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Tutorial 15: Craps. Game. java // This application plays a simple craps game import java. awt. *; import java. awt. event. *; import java. util. Random; import javax. swing. *; import javax. swing. border. *; public class Craps. Game extends JFrame { // JPanel and Titled. Border to contain dice private JPanel point. Dice. JPanel; private Titled. Border point. Dice. Titled. Border; Outline Importing the class java. util. Random Craps. Game. java (1 of 12) // JLabels to display the die images in point. Dice. JPanel private JLabel point. Die 1 JLabel; private JLabel point. Die 2 JLabel; // JLabels to display the die images from the rolls of the dice private JLabel die 1 JLabel; private JLabel die 2 JLabel; // JButtons to allow user to interact with game private JButton play. JButton; private JButton roll. JButton; 2003 Prentice Hall, Inc. All rights reserved. 34

26 27 28 29 30 31 32 33 34 35 // JLabel and JText.

26 27 28 29 30 31 32 33 34 35 // JLabel and JText. Field show results of game private JLabel result. JLabel; private JText. Field result. JText. Field; // constants representing winning dice rolls private final int LUCKY_SEVEN = 7; private final int YO_LEVEN = 11; // constants representing losing dice rolls 36 37 38 39 40 41 42 43 // file name and directory constants private final String FILE_PREFIX = "Images/die"; private final String FILE_SUFFIX = ". png"; 44 45 46 47 // instance variables private int my. Point = 0; private Random random. Object = new Random(); 48 Outline private final int int SNAKE_EYES = 2; TREY = 3; BOX_CARS = 12; CRAPS = 7; Declaring constants for dice rolls Craps. Game. java (2 of 12) Declaring the constants for the file name Declaring instance variable my. Point Creating a Random object using the new keyword 2003 Prentice Hall, Inc. All rights reserved. 35

49 50 51 52 53 54 55 56 57 58 59 60 61 62

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 // no-argument constructor public Craps. Game() { create. User. Interface(); } Outline // create and position GUI components; register event handlers private void create. User. Interface() { // get content pane for attaching GUI components Container content. Pane = get. Content. Pane(); // enable explicit positioning of GUI components content. Pane. set. Layout( null ); // set up point. Dice. Titled. Border for use with point. Dice. JPanel point. Dice. Titled. Border = new Titled. Border( "Point" ); // set up point. Dice. JPanel = new JPanel(); point. Dice. JPanel. set. Bounds( 16, 200, 116 ); point. Dice. JPanel. set. Layout( null ); point. Dice. JPanel. set. Border( point. Dice. Titled. Border ); content. Pane. add( point. Dice. JPanel ); Craps. Game. java (3 of 12) Creating a Titled. Border object Adding a border to the JPanel 2003 Prentice Hall, Inc. All rights reserved. 36

74 75 76 77 78 79 80 81 82 83 84 85 86 87

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 // set up point. Die 1 JLabel = new JLabel(); point. Die 1 JLabel. set. Bounds( 24, 34, 64, 56 ); point. Dice. JPanel. add( point. Die 1 JLabel ); // set up point. Die 2 JLabel = new JLabel(); point. Die 2 JLabel. set. Bounds( 120, 34, 64, 56 ); point. Dice. JPanel. add( point. Die 2 JLabel ); // set up die 1 JLabel = new JLabel(); die 1 JLabel. set. Bounds( 40, 150, 64 ); content. Pane. add( die 1 JLabel ); // set up die 2 JLabel = new JLabel(); die 2 JLabel. set. Bounds( 136, 150, 64, 56 ); content. Pane. add( die 2 JLabel ); Outline Craps. Game. java (4 of 12) Adding a JLabel to a JPanel using the add method 2003 Prentice Hall, Inc. All rights reserved. 37

94 95 96 97 98 99 100 101 102 103 104 105 106 107

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 // set up play. JButton = new JButton(); play. JButton. set. Bounds( 232, 16, 88, 23 ); play. JButton. set. Text( "Play" ); content. Pane. add( play. JButton ); play. JButton. add. Action. Listener( new Action. Listener() // anonymous inner class { // event handler called when play. JButton is pressed public void action. Performed ( Action. Event event ) { play. JButton. Action. Performed( event ); } Outline Craps. Game. java (5 of 12) } // end anonymous inner class ); // end call to add. Action. Listener // set up roll. JButton = new JButton(); roll. JButton. set. Bounds( 232, 56, 88, 23 ); roll. JButton. set. Text( "Roll" ); roll. JButton. set. Enabled( false ); content. Pane. add( roll. JButton ); 2003 Prentice Hall, Inc. All rights reserved. 38

119 120 121 122 123 124 125 126 127 128 129 130 131 132

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 roll. JButton. add. Action. Listener( Outline new Action. Listener() // anonymous inner class { // event handler called when roll. JButton is pressed public void action. Performed ( Action. Event event ) { roll. JButton. Action. Performed( event ); } } // end anonymous inner class ); // end call to add. Action. Listener Craps. Game. java (6 of 12) // set up result. JLabel = new JLabel(); result. JLabel. set. Bounds( 232, 90, 48, 16 ); result. JLabel. set. Text( "Result: " ); content. Pane. add( result. JLabel ); 2003 Prentice Hall, Inc. All rights reserved. 39

139 140 141 142 143 144 145 146 147 148 149 150 151 152

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 // set up result. JText. Field = new JText. Field(); result. JText. Field. set. Bounds( 232, 106, 88, 24 ); result. JText. Field. set. Horizontal. Alignment( JText. Field. CENTER ); result. JText. Field. set. Editable( false ); content. Pane. add( result. JText. Field ); // set properties of application’s window set. Title( "Craps Game" ); // set title bar string set. Size( 350, 250 ); // set window size set. Visible( true ); // display window Outline Craps. Game. java (7 of 12) } // end method create. User. Interface // start new game of craps private void play. JButton. Action. Performed( Action. Event event ) { // clear point icons point. Die 1 JLabel. set. Icon( null ); point. Die 2 JLabel. set. Icon( null ); // reset title of border point. Dice. Titled. Border. set. Title( "Point" ); point. Dice. JPanel. repaint(); Removing images from JLabels Setting the title of the border and updating the JPanel 2003 Prentice Hall, Inc. All rights reserved. 40

164 165 166 167 168 169 170 171 172 173 174 175 176 177

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 int sum. Of. Dice = roll. Dice(); // roll dice // check results of the first dice roll switch ( sum. Of. Dice ) { // win on first roll case LUCKY_SEVEN: case YO_LEVEN: result. JText. Field. set. Text( "You win!!!" ); break; // lose on first roll case SNAKE_EYES: case TREY: case BOX_CARS: result. JText. Field. set. Text( "Sorry, you lose. " ); break; Outline Winning on the first roll Craps. Game. java (8 of 12)on the Losing first roll // remember point in instance variable default: // set the point and output result my. Point = sum. Of. Dice; result. JText. Field. set. Text( "Roll again!" ); Player must match the point 2003 Prentice Hall, Inc. All rights reserved. 41

189 190 191 192 193 194 195 196 197 198 199 200 201 202

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 // show the dice images point. Die 1 JLabel. set. Icon( die 1 JLabel. get. Icon() ); point. Die 2 JLabel. set. Icon( die 2 JLabel. get. Icon() ); // update the border title point. Dice. Titled. Border. set. Title( "Point is " + sum. Of. Dice ); point. Dice. JPanel. repaint(); // change the state of the JButtons play. JButton. set. Enabled( false ); roll. JButton. set. Enabled( true ); } // end switch statement Outline Displaying die images Displaying point and updating the JPanel Craps. Game. java Allowing the player (9 ofagain 12) to roll } // end method play. JButton. Action. Performed // continue the game private void roll. JButton. Action. Performed( Action. Event event ) { int sum. Of. Dice = roll. Dice(); // roll dice 2003 Prentice Hall, Inc. All rights reserved. 42

211 212 213 214 215 216 217 218 219 220 221 222 223 224

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 // determine outcome of roll, player matches point if ( sum. Of. Dice == my. Point ) { result. JText. Field. set. Text( "You win!!!" ); roll. JButton. set. Enabled( false ); play. JButton. set. Enabled( true ); } // determine outcome of roll, player loses else if ( sum. Of. Dice == CRAPS ) { result. JText. Field. set. Text( "Sorry, you lose" ); roll. JButton. set. Enabled( false ); play. JButton. set. Enabled( true ); } Outline Craps. Game. java (10 of 12) } // end method roll. JButton. Action. Performed // generate random die rolls private int roll. Dice() { // generate random die values int die 1 = 1 + random. Object. next. Int( 6 ); int die 2 = 1 + random. Object. next. Int( 6 ); Generating random numbers 234 2003 Prentice Hall, Inc. All rights reserved. 43

235 236 237 238 239 240 241 242 243 244 245 246 247 248

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 // display the dice images display. Die( die 1 JLabel, die 1 ); display. Die( die 2 JLabel, die 2 ); Outline 44 return die 1 + die 2; // return sum of dice values } // end method roll. Dice // displays the die image private void display. Die( JLabel pic. Die. JLabel, int face ) { Image. Icon image = new Image. Icon( FILE_PREFIX + face + FILE_SUFFIX ); // display die images in pic. Die. JLabel. set. Icon( image ); Craps. Game. java Displaying the image (11 of 12) in the JLabel } // end method display. Die 2003 Prentice Hall, Inc. All rights reserved.

254 // main method 255 public static void main( String args[] ) 256 {

254 // main method 255 public static void main( String args[] ) 256 { 257 Craps. Game application = new Craps. Game(); 258 application. set. Default. Close. Operation( JFrame. EXIT_ON_CLOSE ); 259 260 } // end method main 261 262 } // end class Craps. Game Outline Craps. Game. java (12 of 12) 2003 Prentice Hall, Inc. All rights reserved. 45