2007-12-21 12:53:14娃娃♡°

NotebookVIII

//大家來玩21點!!!

。。。。PointTwentyOne.java 。。。。


import java.util.Scanner;

public class PointTwentyOne {

enum Status { //定義列舉型別,判斷用
WON, LOST, CONTINUE
};

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

Card bankerFirst = new Card(); //記錄莊家的第1張牌
Card bankerNext= new Card(); //記錄莊家的第下張牌
Card playerFirst = new Card(); //記錄玩家的第1張牌
Card playerNext= new Card(); //記錄玩家的第下張牌
double bankerPoint = 0; //記錄莊家點數
double playerPoint = 0; //記錄玩家點數
Status bankerState; //記錄莊家目前狀態
Status playerState; //記錄玩家目前狀態

double playerBucks = 0; //記錄玩家的全部賭金
double playerBet = 0; //記錄玩家這一次下注的賭金

DeckOfCards myCard = new DeckOfCards();
myCard.shuffle(); //洗牌

//收集基本資料=============================================
System.out.printf("請輸入你今天帶來的賭金: ");
playerBucks = input.nextDouble();
if(playerBucks<= 0){
System.out.println("賭金不夠喔,改天再來吧!n謝謝光臨!");
return;
}
do{ //開始玩遊戲===========================================
//重設狀態值
bankerState = Status.CONTINUE;
playerState = Status.CONTINUE;

//下注
System.out.printf("===============================================================n");
System.out.printf("請下注:");
playerBet = input.nextDouble();
while(playerBet> playerBucks){
System.out.println("下注金額超過你今天帶來的賭金喔!請重新下注。");
playerBet = input.nextDouble();
}
playerBucks -= playerBet;
System.out.printf("===============================================================n");

//發牌給莊家
bankerFirst = myCard.dealCard();
bankerNext = myCard.dealCard();
System.out.printf("莊家:%-20s%-20st目前點數:%.1fn", bankerNext, "", bankerNext.face());
bankerPoint = bankerFirst.face() + bankerNext.face();
if (bankerFirst.suitOfCard() == "Spades" & bankerNext.suitOfCard() == "Spades"){//判斷莊家是否拿到黑傑克:黑桃A與黑桃J
if ((bankerFirst.faceOfCard() == "Ace" & bankerNext.faceOfCard() == "Jack")
| (bankerNext.faceOfCard() == "Ace" & bankerFirst.faceOfCard() == "Jack")){
System.out.printf("喲,莊家拿到黑傑克了,莊家贏!");
bankerState= Status.WON;
}
}
System.out.printf("===============================================================n");

//發牌給玩家
playerFirst = myCard.dealCard();
playerNext = myCard.dealCard();
System.out.printf("玩家:%-20s%-20st", playerFirst, playerNext);
playerPoint = playerFirst.face() + playerNext.face();
System.out.printf("目前點數:%.1fn", playerPoint);
if (playerFirst.suitOfCard() == "Spades" & playerNext.suitOfCard() == "Spades"){//判斷玩家是否拿到黑傑克:黑桃A與黑桃J
if ((playerFirst.faceOfCard() == "Ace" & playerNext.faceOfCard() == "Jack")
| (playerNext.faceOfCard() == "Ace" & playerFirst.faceOfCard() == "Jack")) {
System.out.printf("哇,拿到黑傑克,恭喜你獎金加倍!"); //2賠3,即1.5倍
playerBucks += (2.5 * playerBet);
playerState= Status.WON;
}
}

//大家都沒有拿到黑傑克
if(playerState== Status.CONTINUE && bankerState== Status.CONTINUE){
//玩家拿牌
System.out.printf("你還要再加牌嗎?繼續按y/Y ");
while ( input.next().compareToIgnoreCase("y")==0 & playerState== Status.CONTINUE) {
playerNext = myCard.dealCard();
System.out.printf("玩家+ %-20s%-20st", playerNext, "");
playerPoint += playerNext.face();
System.out.printf("目前點數:%.1fn", playerPoint);

if (playerPoint <= 21) {
System.out.printf("你還要再加牌嗎?繼續按y/Y ");
//hit= input.next();
} else {
System.out.printf("哇,爆掉了啦!!!莊家贏!n");
playerState = Status.LOST;
bankerState = Status.WON;
break;
}
}

//莊家
System.out.printf("============================莊家掀牌============================n");
System.out.printf("莊家:%-20s%-20st", bankerNext, bankerFirst);
System.out.printf("目前點數:%.1fn", bankerPoint);
if(playerState== Status.CONTINUE & bankerState== Status.CONTINUE){
//莊家點數小於16則必須拿牌
while (bankerPoint < 16) {
bankerNext = myCard.dealCard();
System.out.printf("莊家+ %-20s%-20st", bankerNext, "");
bankerPoint += bankerNext.face();
System.out.printf("目前點數:%.1fn", bankerPoint);
}

//判斷最後的輸贏
if(bankerPoint >21)
{//莊家爆掉
System.out.printf("莊家爆掉了,耶!恭喜你贏得彩金 %.1f!n", playerBet);
}
else if(playerPoint > bankerPoint)
{//玩家的點數比較接近21點,玩家贏
playerBucks += 2*playerBet;
System.out.printf("你的點數比較大!恭喜你贏得彩金 %.1f!n", playerBet);
}
else if(bankerPoint> playerPoint)
{//莊家點數大於玩家點數,莊家贏
System.out.printf("莊家贏!真可惜,祝福你下次贏得彩金!n");
}
else //前面兩者都不是,平手
System.out.print("真巧,居然平手!");
}

}
//顯示玩家目前的狀態
System.out.printf("===============================================================n");
System.out.printf("玩家結餘 %.1fn", playerBucks);
System.out.printf("===============================================================n");
if(playerBucks<= 0){ //如果玩家的賭金已經不夠,則結束!
System.out.println("已經輸光光了喔,改天再來吧!n");
break;
}
else
System.out.printf("你還要再玩嗎?繼續按y/Y ");
}while(input.next().compareToIgnoreCase("y")== 0);
System.out.println("謝謝光臨!n");
}
}


。。。。The End。。。。

。。。。DeckOfcards.java。。。。


import java.util.Random;

public class DeckOfCards {

private Card deck[];
private int currentCard;
private final int NUMBER_OF_CARDS= 52;
private Random randomNumbers;

public DeckOfCards()
{
String faces[]= {"Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
String suits[]= {"Hearts", "Diamonds", "Clubs", "Spades"};

deck= new Card[NUMBER_OF_CARDS];
currentCard= 0;
randomNumbers= new Random();

for(int c= 0; c< deck.length; c++)
deck[c]= new Card(faces[c%13], suits[c/13], (c%13)+1);
}

public void shuffle()
{
currentCard= 0;
for(int f= 0; f< deck.length; f++)
{
int second = randomNumbers.nextInt(NUMBER_OF_CARDS);
Card temp= deck[f];
deck[f]= deck[second];
deck[second]= temp;
}
}

public Card dealCard()
{
if(currentCard< deck.length)
return deck[currentCard++];
else
return null;
}
}


。。。。The End。。。。

。。。。Card.java。。。。


public class Card {
private String face;
private String suit;
private double iface;

public Card()
{

}
public Card(String f, String s, int i)
{
face= f;
suit= s;
if(i>10)
iface= 0.5; //K, Q, J都算0.5點
else
iface= i; //Ace算1點
}

public String toString()
{
return face+ " of "+ suit;
}
public String faceOfCard()
{
return face;
}
public String suitOfCard()
{
return suit;
}

public double face()
{
return iface;
}
}

。。。。The End。。。。

上一篇:notebook VII

下一篇:NOTE 01