24h購物| | PChome| 登入
2007-12-13 15:51:45| 人氣65| 回應0 | 上一篇 | 下一篇

notebookIV

推薦 0 收藏 0 轉貼0 訂閱站台

-------Craps.java-----

import java.util.Random;
public class Craps
{
private Random randomNumbers = new Random();
private enum Status { CONTINUE, WON, LOST };
private final static int SNAKE_EYES = 2;
private final static int TREY = 3;
private final static int SEVEN = 7;
private final static int YO_LEVEN = 11;
private final static int BOX_CARS = 12;
public void play()
{
int myPoint = 0;
Status gameStatus;
int sumOfDice = rollDice();
switch ( sumOfDice )
{
case SEVEN:
case YO_LEVEN:
gameStatus = Status.WON;
break;
case SNAKE_EYES:
case TREY:
case BOX_CARS:
gameStatus = Status.LOST;
break;
default:
gameStatus = Status.CONTINUE;
myPoint = sumOfDice;
System.out.printf( "Point is %dn", myPoint );
break;
} //end switch
while ( gameStatus == Status.CONTINUE )
{
sumOfDice = rollDice();
if ( sumOfDice == myPoint )
gameStatus = Status.WON;
else
if ( sumOf Dice == SEVEN )
gameStatus = Status.LOST;
}
if ( gameStatus == Status.WON )
System.out.println( "Player wins" );
else
System.out.println( "Player loses" );
}

public int rollDice()
{
int die1 = 1 + randomNumbers.nextInt( 6 );
int die2 = 1 + randomNumbers.nextInt( 6 );
int sum = die1 + die2;
System.out.printf( "Player rolled %d + %d = %dn",
die1, die2, sum );
return sum;
}
}


------CrapsTest.java------

public class CrapsTest
{
public static void main( String args[] )
{
Craps game = new Craps();
game.play();
}
}

台長: 娃娃♡°

您可能對以下文章有興趣

人氣(65) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 心情日記(隨筆、日記、心情手札) | 個人分類: 程式設計⊙ˇ⊙ |
此分類下一篇:notebookV
此分類上一篇:notebookIII

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文