Java™ How to Program ( Deitel - Deitel) - Phần 11
Số trang: 50
Loại file: pdf
Dung lượng: 33.87 MB
Lượt xem: 16
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu java™ how to program ( deitel - deitel) - phần 11, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Java™ How to Program ( Deitel - Deitel) - Phần 11 [Page 250 (continued)]6.10. Case Study: A Game of Chance (IntroducingEnumerations)A popular game of chance is a dice game known as craps, which is played in casinos and backalleys throughout the world. The rules of the game are straightforward: You roll two dice. Each die has six faces, which contain one, two, three, four, five and six spots, respectively. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, you win. If the sum is 2, 3 or 12 on the first throw (called craps), you lose (i.e., the house wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, that sum becomes your point. To win, you must continue rolling the dice until you make your point (i.e., roll that same point value). You lose by rolling a 7 before making the point.The application in Fig. 6.9 and Fig. 6.10 simulates the game of craps, using methods to define thelogic of the game. In the main method of class CrapsTest (Fig. 6.10), line 8 creates an object of classCraps (Fig. 6.9) and line 9 calls its play method to start the game. The play method (Fig. 6.9, lines2165) calls the rollDice method (Fig. 6.9, lines 6881) as necessary to roll the two dice and computetheir sum. Four sample outputs in Fig. 6.10 show winning on the first roll, losing on the first roll,winning on a subsequent roll and losing on a subsequent roll, respectively.Figure 6.9. Craps class simulates the dice game craps. (This item is displayed on pages 251 - 252 in the print version) 1 // Fig. 6.9: Craps.java 2 // Craps class simulates the dice game craps. 3 import java.util.Random; 4 5 public class Craps 6 { 7 // create random number generator for use in method rollDice 8 private Random randomNumbers = new Random(); 9 10 // enumeration with constants that represent the game status 11 private enum Status { CONTINUE, WON, LOST }; 12 13 // constants that represent common rolls of the dice 14 private final static int SNAKE_EYES = 2; 15 private final static int TREY = 3; 16 private final static int SEVEN = 7; 17 private final static int YO_LEVEN = 11; 18 private final static int BOX_CARS = 12;1920 // plays one game of craps21 public void play()22 {23 int myPoint = 0; // point if no win or loss on first roll24 Status gameStatus; // can contain CONTINUE, WON or LOST2526 int sumOfDice = rollDice(); // first roll of the dice2728 // determine game status and point based on first roll29 switch ( sumOfDice )30 {31 case SEVEN: // win with 7 on first roll32 case YO_LEVEN: // win with 11 on first roll33 gameStatus = Status.WON;34 break;35 case SNAKE_EYES: // lose with 2 on first roll36 case TREY: // lose with 3 on first roll37 case BOX_CARS: // lose with 12 on first roll38 gameStatus = Status.LOST;39 break;40 default: // did not win or lose, so remember point41 gameStatus = Status.CONTINUE; // game is not over42 myPoint = sumOfDice; // remember the point43 System.out.printf( Point is %d , myPoint );44 break; // optional at end of switch45 } // end switch4647 // while game is not complete48 while ( gameStatus == Status.CONTINUE ) // not WON or LOST49 {50 sumOfDice = rollDice(); // roll dice again5152 // determine game status53 if ( sumOfDice == myPoint ) // win by making point54 gameStatus = Status.WON;55 else56 if ( sumOfDice == SEVEN ) // lose by rolling 7 before point57 gameStatus = Status.LOST;58 } // end while5960 // display won or lost message61 if ( gameStatus == Status.WON )62 System.out.println( Player wins );63 else64 System.out.println( Player loses );65 } // end method play6667 // roll dice, calculate sum and display results68 public int rollDice()69 {70 // pick random die values71 int die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll72 int die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll7374 int sum = die1 + die2; // sum of die values7576 // display results of this roll77 System.out.printf( Player rolled %d + %d = %d ,78 die1, die2, sum );7980 return sum; // return sum of dice81 } // end method rollDice82 } // end class CrapsFigure 6.10. Application to test class Craps. (This item is displayed on page 253 in the print version) 1 // Fig. 6.10: CrapsTest.java 2 // Application to test class Craps. 3 4 publi ...
Nội dung trích xuất từ tài liệu:
Java™ How to Program ( Deitel - Deitel) - Phần 11 [Page 250 (continued)]6.10. Case Study: A Game of Chance (IntroducingEnumerations)A popular game of chance is a dice game known as craps, which is played in casinos and backalleys throughout the world. The rules of the game are straightforward: You roll two dice. Each die has six faces, which contain one, two, three, four, five and six spots, respectively. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, you win. If the sum is 2, 3 or 12 on the first throw (called craps), you lose (i.e., the house wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, that sum becomes your point. To win, you must continue rolling the dice until you make your point (i.e., roll that same point value). You lose by rolling a 7 before making the point.The application in Fig. 6.9 and Fig. 6.10 simulates the game of craps, using methods to define thelogic of the game. In the main method of class CrapsTest (Fig. 6.10), line 8 creates an object of classCraps (Fig. 6.9) and line 9 calls its play method to start the game. The play method (Fig. 6.9, lines2165) calls the rollDice method (Fig. 6.9, lines 6881) as necessary to roll the two dice and computetheir sum. Four sample outputs in Fig. 6.10 show winning on the first roll, losing on the first roll,winning on a subsequent roll and losing on a subsequent roll, respectively.Figure 6.9. Craps class simulates the dice game craps. (This item is displayed on pages 251 - 252 in the print version) 1 // Fig. 6.9: Craps.java 2 // Craps class simulates the dice game craps. 3 import java.util.Random; 4 5 public class Craps 6 { 7 // create random number generator for use in method rollDice 8 private Random randomNumbers = new Random(); 9 10 // enumeration with constants that represent the game status 11 private enum Status { CONTINUE, WON, LOST }; 12 13 // constants that represent common rolls of the dice 14 private final static int SNAKE_EYES = 2; 15 private final static int TREY = 3; 16 private final static int SEVEN = 7; 17 private final static int YO_LEVEN = 11; 18 private final static int BOX_CARS = 12;1920 // plays one game of craps21 public void play()22 {23 int myPoint = 0; // point if no win or loss on first roll24 Status gameStatus; // can contain CONTINUE, WON or LOST2526 int sumOfDice = rollDice(); // first roll of the dice2728 // determine game status and point based on first roll29 switch ( sumOfDice )30 {31 case SEVEN: // win with 7 on first roll32 case YO_LEVEN: // win with 11 on first roll33 gameStatus = Status.WON;34 break;35 case SNAKE_EYES: // lose with 2 on first roll36 case TREY: // lose with 3 on first roll37 case BOX_CARS: // lose with 12 on first roll38 gameStatus = Status.LOST;39 break;40 default: // did not win or lose, so remember point41 gameStatus = Status.CONTINUE; // game is not over42 myPoint = sumOfDice; // remember the point43 System.out.printf( Point is %d , myPoint );44 break; // optional at end of switch45 } // end switch4647 // while game is not complete48 while ( gameStatus == Status.CONTINUE ) // not WON or LOST49 {50 sumOfDice = rollDice(); // roll dice again5152 // determine game status53 if ( sumOfDice == myPoint ) // win by making point54 gameStatus = Status.WON;55 else56 if ( sumOfDice == SEVEN ) // lose by rolling 7 before point57 gameStatus = Status.LOST;58 } // end while5960 // display won or lost message61 if ( gameStatus == Status.WON )62 System.out.println( Player wins );63 else64 System.out.println( Player loses );65 } // end method play6667 // roll dice, calculate sum and display results68 public int rollDice()69 {70 // pick random die values71 int die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll72 int die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll7374 int sum = die1 + die2; // sum of die values7576 // display results of this roll77 System.out.printf( Player rolled %d + %d = %d ,78 die1, die2, sum );7980 return sum; // return sum of dice81 } // end method rollDice82 } // end class CrapsFigure 6.10. Application to test class Craps. (This item is displayed on page 253 in the print version) 1 // Fig. 6.10: CrapsTest.java 2 // Application to test class Craps. 3 4 publi ...
Tìm kiếm theo từ khóa liên quan:
java cơ bản giáo trình java thủ thuật lập trình java nhập môn java mẹo lập trình javaTài liệu liên quan:
-
Bài giảng Nhập môn Java: Bài 12 – Võ Tấn Dũng
12 trang 60 0 0 -
Đề cương môn học Lập trình Java
28 trang 50 0 0 -
Giáo trình Java (Tập 2): Phần 2
429 trang 26 0 0 -
Java™ How to Program ( Deitel - Deitel) - Phần 1
50 trang 25 0 0 -
Giáo trình java cơ bản - Chương 5
45 trang 24 0 0 -
193 trang 24 0 0
-
Bài giảng Nhập môn Java: Bài 8 – Võ Tấn Dũng
50 trang 23 0 0 -
Java™ How to Program ( Deitel - Deitel) - Phần 17
50 trang 22 0 0 -
Giáo trình Java (Tập 1): Phần 2
157 trang 22 0 0 -
LTHDT - Bài 06. Một số kỹ thuật trong kế thừa
35 trang 21 0 0 -
Bài giảng Nhập môn Java: Bài 10 – Võ Tấn Dũng
46 trang 21 0 0 -
26 trang 21 0 0
-
Giáo trình java cơ bản - Chương 9
22 trang 21 0 0 -
Bài giảng Nhập môn Java: Bài 9 – Võ Tấn Dũng
47 trang 21 0 0 -
Java™ How to Program ( Deitel - Deitel) - Phần 6
50 trang 20 0 0 -
Kỹ thuật lập trình - Chapter 3
15 trang 20 0 0 -
Bài giảng Nhập môn Java: Bài 7 – Võ Tấn Dũng
35 trang 20 0 0 -
LTHDT- Bài 08. Ngoại lệ và xử lý ngoại lệ
55 trang 20 0 0 -
Giáo trình java cơ bản - Chương 10
12 trang 20 0 0 -
CÔNG NGHỆ JAVA ( Nguyễn Hữu Nghĩa ) - 3.5 Applet
40 trang 20 0 0