Write an application that runs 1,000,000 games of craps (Fig. 6.8) and answers the following questions:
a) How many games are won on the first roll, second roll, …,twentieth roll and after the twentieth roll?
b) How many games are lost on the first roll, second roll, …,twentieth roll and after the twentieth roll?
c) What are the chances of winning at craps? [Note: You should discover that craps is one of the fairest casino games. What do you suppose this means?]
d) What is the average length of a game of craps?
e) Do the chances of winning improve with the length of the game?
// Fig. 6.8: Craps.java
// Craps class simulates the dice game craps.
importjava.security.SecureRandom;publicclassCraps{// create secure random number generator for use in method rollDice
privatestaticfinalSecureRandomrandomNumbers=newSecureRandom();// enum type with constants that represent the game status
privateenumStatus{CONTINUE,WON,LOST};// constants that represent common rolls of the dice
privatestaticfinalintSNAKE_EYES=2;privatestaticfinalintTREY=3;privatestaticfinalintSEVEN=7;privatestaticfinalintYO_LEVEN=11;privatestaticfinalintBOX_CARS=12;// plays one game of craps
publicstaticvoidmain(String[]args){intmyPoint=0;// point if no win or loss on first roll
StatusgameStatus;// can contain CONTINUE, WON or LOST
intsumOfDice=rollDice();// first roll of the dice
// determine game status and point based on first roll
switch(sumOfDice){caseSEVEN:// win with 7 on first roll
caseYO_LEVEN:// win with 11 on first roll
gameStatus=Status.WON;break;caseSNAKE_EYES:// lose with 2 on first roll
caseTREY:// lose with 3 on first roll
caseBOX_CARS:// lose with 12 on first roll
gameStatus=Status.LOST;break;default:// did not win or lose, so remember point
gameStatus=Status.CONTINUE;// game is not over
myPoint=sumOfDice;// remember the point
System.out.printf("Point is %d%n",myPoint);break;}// while game is not complete
while(gameStatus==Status.CONTINUE){// not WON or LOST
sumOfDice=rollDice();// roll dice again
// determine game status
if(sumOfDice==myPoint){// win by making point
gameStatus=Status.WON;}else{if(sumOfDice==SEVEN){// lose by rolling 7 before point
gameStatus=Status.LOST;}}}// display won or lost message
if(gameStatus==Status.WON){System.out.println("Player wins");}else{System.out.println("Player loses");}}// roll dice, calculate sum and display results
publicstaticintrollDice(){// pick random die values
intdie1=1+randomNumbers.nextInt(6);// first die roll
intdie2=1+randomNumbers.nextInt(6);// second die roll
intsum=die1+die2;// sum of die values
// display results of this roll
System.out.printf("Player rolled %d + %d = %d%n",die1,die2,sum);returnsum;}}/**************************************************************************
* (C) Copyright 1992-2018 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
把這個程式拿去編譯執行得到的結果如下:
7.18 題幹
Write an application that runs 1,000,000 games of craps (Fig. 6.8) and answers the following questions
// Fig. 7.11: DeckOfCardsTest.java
// Card shuffling and dealing.
publicclassDeckOfCardsTest{// execute application
publicstaticvoidmain(String[]args){DeckOfCardsmyDeckOfCards=newDeckOfCards();myDeckOfCards.shuffle();// place Cards in random order
// print all 52 Cards in the order in which they are dealt
for(inti=1;i<=52;i++){// deal and display a Card
System.out.printf("%-19s",myDeckOfCards.dealCard());if(i%4==0){// output a newline after every fourth card
System.out.println();}}}}/**************************************************************************
* (C) Copyright 1992-2018 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
// Fig. 7.10: DeckOfCards.java
// DeckOfCards class represents a deck of playing cards.
importjava.security.SecureRandom;publicclassDeckOfCards{// random number generator
privatestaticfinalSecureRandomrandomNumbers=newSecureRandom();privatestaticfinalintNUMBER_OF_CARDS=52;// constant # of Cards
privateCard[]deck=newCard[NUMBER_OF_CARDS];// Card references
privateintcurrentCard=0;// index of next Card to be dealt (0-51)
// constructor fills deck of Cards
publicDeckOfCards(){String[]faces={"Ace","Deuce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};String[]suits={"Hearts","Diamonds","Clubs","Spades"};// populate deck with Card objects
for(intcount=0;count<deck.length;count++){deck[count]=newCard(faces[count%13],suits[count/13]);}}// shuffle deck of Cards with one-pass algorithm
publicvoidshuffle(){// next call to method dealCard should start at deck[0] again
currentCard=0;// for each Card, pick another random Card (0-51) and swap them
for(intfirst=0;first<deck.length;first++){// select a random number between 0 and 51
intsecond=randomNumbers.nextInt(NUMBER_OF_CARDS);// swap current Card with randomly selected Card
Cardtemp=deck[first];deck[first]=deck[second];deck[second]=temp;}}// deal one Card
publicCarddealCard(){// determine whether Cards remain to be dealt
if(currentCard<deck.length){returndeck[currentCard++];// return current Card in array
}else{returnnull;// return null to indicate that all Cards were dealt
}}}/**************************************************************************
* (C) Copyright 1992-2018 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
// Fig. 7.9: Card.java
// Card class represents a playing card.
publicclassCard{privatefinalStringface;// face of card ("Ace", "Deuce", ...)
privatefinalStringsuit;// suit of card ("Hearts", "Diamonds", ...)
// two-argument constructor initializes card's face and suit
publicCard(StringcardFace,StringcardSuit){this.face=cardFace;// initialize face of card
this.suit=cardSuit;// initialize suit of card
}// return String representation of Card
publicStringtoString(){returnface+" of "+suit;}}/**************************************************************************
* (C) Copyright 1992-2018 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
把這三個 .java 檔放到同一個專案裡面後,執行的結果如下:
7.30 題幹
Modify Fig.7.11 to deal a five-card poker hand. Then modify class DeckOfCards of Fig.7.10 to include methods that determine whether a hand contains: … 略
[Hint: Add methods getFace and getSuit to class Card of Fig. 7.9.]
secondValueCount 需剛好是(5 - firstValueCount),才符合 Full House 的定義(2 配 3)。
7.30 執行結果截圖
題目 7.31 (Card Shuffling and Dealing)
Use the methods developed in Exercise 7.30 to write an application that deals two five-card poker hands, evaluates each hand and determines which is better.