21点小游戏

本文介绍了一个使用Java实现的简易21点纸牌游戏。该程序包括游戏逻辑、牌组管理、发牌和比较点数等功能。通过循环让玩家决定是否继续要牌,并根据最终点数判断胜负。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. import java.util.Scanner;
  2. /*游戏的逻辑类*/
  3. public class BlackJack {
  4.   private Deck mydeck;
  5.   public BlackJack(){
  6.       
  7.   }
  8.   
  9.   /*开始游戏*/
  10.   public void play(){
  11.       Scanner myscanner = new Scanner(System.in);                        //获得输入
  12.       String  choice;                                                    //玩家的选择
  13.       mydeck = new Deck();                                               //牌桌
  14.       out:
  15.       /*设定为无线循环*/
  16.       while(true){
  17.           System.out.println("do you want more cards? Y or N");           //问玩家是否还要牌
  18.           choice = myscanner.nextLine();                                  //读取选择
  19.           if(choice.equalsIgnoreCase("n")){                               //如果玩家选择不要牌
  20.            
  21.           /* 这里要判断一下电脑的牌是不是小于15
  22.               如果小于15的话,电脑应该继续叫牌的*/
  23.               while(mydeck.cHands.getpoint()<15){                         
  24.                   mydeck.nextround(false);
  25.               }
  26.               
  27.               /*把电脑的牌设定为全部打印,不然会输的不甘心的*/
  28.               mydeck.cHands.setmine(true);
  29.               
  30.               /*判断结果*/
  31.               switch(mydeck.checkcards()){
  32.                  case 0:
  33.                     System.out.println("you lose");
  34.                     break;
  35.                  case 1:
  36.                     System.out.println("you win");
  37.                     break;
  38.                  case 2:
  39.                     System.out.println("equal");
  40.                     break;
  41.               }
  42.               System.out.println(mydeck);
  43.               break out;
  44.               
  45.           }else if(choice.equalsIgnoreCase("y")){                //玩家继续要牌
  46.               
  47.               /*发一轮牌*/
  48.               mydeck.nextround(true);
  49.               
  50.               /*这里要判断是否大于21,爆掉。或者正好是21,赢了*/
  51.               switch(mydeck.checkcards()){
  52.                   case 3:
  53.                       mydeck.cHands.setmine(true);
  54.                       System.out.println(mydeck);
  55.                       System.out.println("you both has over 21");
  56.                       break out;
  57.                   case 4:
  58.                       mydeck.cHands.setmine(true);
  59.                       System.out.println(mydeck);
  60.                       System.out.println("you win");
  61.                       break out;
  62.                   case 5:
  63.                       mydeck.cHands.setmine(true);
  64.                       System.out.println(mydeck);
  65.                       System.out.println("you lose");
  66.                       break out;
  67.               }
  68.               System.out.println(mydeck);
  69.           }else{
  70.              System.out.println(mydeck);
  71.              System.out.println("the input is not vaild"); 
  72.           }
  73.       }
  74.   }
  75. }
  1. import java.util.Collections;
  2. import java.util.Stack;
  3. public class Deck {
  4.     
  5.     Hands pHands;                                  //the player's hand
  6.     Hands cHands;                                  //the computer's hand
  7.     Stack<Cards> allCards;                         //it store all kinds of Cards
  8.     
  9.     /*构造函数*/
  10.     public Deck(){
  11.         this.initialgame();
  12.     }
  13.     
  14.    /* 初始化牌*/
  15.     private void initialcards(){
  16.         for(Rank rank:Rank.values()){
  17.             for(Suit suit:Suit.values()){
  18.                 allCards.push(new Cards(rank,suit));
  19.             }
  20.         }
  21.         Collections.shuffle(allCards);
  22.     }
  23.     
  24.     /*初始化游戏*/
  25.     public void initialgame(){
  26.         pHands = new Hands(true);                                 //确认pHands表示玩家
  27.         cHands = new Hands(false);                                //确认cHands表示电脑
  28.         allCards = new Stack<Cards>();
  29.         
  30.         this.initialcards();
  31.         
  32.         /*发玩家的牌*/
  33.         pHands.addCard(allCards.pop());
  34.         pHands.addCard(allCards.pop());
  35.         
  36.         /*发电脑的牌*/
  37.         cHands.addCard(allCards.pop());
  38.         cHands.addCard(allCards.pop());
  39.         
  40.         /*答应嗲一次,大家手中的牌*/
  41.         System.out.println(this);
  42.     }
  43.     
  44.    /* 打印现在双方的的牌*/
  45.     @Override
  46.     public String toString() {
  47.        String returnstring;
  48.        returnstring ="the cards in coputer's: /n";
  49.        returnstring+=cHands.toString();
  50.        returnstring+="the cards in your hand: /n";
  51.        returnstring+=pHands.toString();
  52.        return returnstring;
  53.     }
  54.     
  55.     /*发牌,这里电脑的策略为小于15点,就需要发牌,如果变量onemore为true,则表示玩家要牌*/
  56.     public void nextround(boolean onemore){        //if the player want one more card,then the onemore is true,otherwise,it's false
  57.         if(cHands.getpoint()< 15){                 //when the computer's points is less than 11,it will ask another one 
  58.             cHands.addCard(allCards.pop());
  59.         }
  60.         if(onemore){
  61.             pHands.addCard(allCards.pop());
  62.         }
  63.     }
  64.     
  65.     /*比较大小*/
  66.     public int checkcards(){
  67.         int whowin = -1;
  68.         if(pHands.getpoint()==21&&cHands.getpoint()==21){
  69.             whowin = 2;                                      //when whowin = 2 ,equal
  70.         }else if(cHands.getpoint()==21){
  71.             whowin = 0;                                      //when whowin = 0, computers win
  72.         }else if(pHands.getpoint()==21){
  73.             whowin = 1;                                      //when whowin = 1, players win
  74.         }else if(pHands.getpoint()>21&&cHands.getpoint()>21){// whe whowin = 3,both has over 21;
  75.             whowin = 3;
  76.         }else if(cHands.getpoint()>21){                      // when whowin = 4,computer has over 21;
  77.             whowin = 4;
  78.         }else if(pHands.getpoint()>21){
  79.             whowin = 5;
  80.         }else{
  81.             if(pHands.getpoint()==cHands.getpoint()){
  82.                 whowin = 2;
  83.             }
  84.             else if(pHands.compare(cHands)){
  85.                 whowin = 1;
  86.             }else{
  87.                 whowin = 0;
  88.             }
  89.         }
  90.         return whowin;
  91.     }
  92. }
  1. import java.util.Stack;
  2. public class Hands {
  3.     private boolean mine;                             //to check the hands is player's of not
  4.     private Stack<Cards> myCards;                     //the Stack store cards;
  5.     
  6.     public Hands(boolean mine){                       //if the cards is player's,the the mine should be true,otherwise should be false;
  7.         this.mine = mine;
  8.         myCards = new Stack<Cards>();
  9.     }
  10.     
  11.     /*add a card*/
  12.     public void addCard(Cards card){
  13.         myCards.add(card);
  14.     }
  15.     
  16.     //设置这张牌是否是自己
  17.     public void setmine(boolean mine){
  18.         this.mine = mine;
  19.     }
  20.     
  21.     //获得牌的点数
  22.     public int getpoint(){
  23.         int mypoint=0;
  24.         for(Cards card:myCards){
  25.             mypoint +=card.getRank();
  26.         }
  27.         return mypoint;
  28.     }
  29.     
  30.     //输出手中的牌
  31.     public String toString() {
  32.         // TODO Auto-generated method stub
  33.         String returnstring = "";
  34.         int  size = myCards.size()-1;
  35.         if(myCards.empty()){                         //the hands is empty  
  36.             return "the hands is empty";
  37.         }
  38.         else if(mine){                               //the Hand is players
  39.            for(Cards card:myCards){
  40.                returnstring +=card.toString()+"/n";
  41.            }
  42.            returnstring += "you points are "
  43.                  + String.valueOf(this.getpoint())
  44.                  + "/n";
  45.         }else{                                       //the Hand is not players                                         
  46.            for(int i=0;i<size;i++){
  47.               returnstring  += myCards.get(i).toString()+"/n";
  48.            }
  49.         }
  50.         return returnstring;
  51.     }
  52.     
  53.     public boolean compare(Hands newone){          //return true if it's bigger than the input one ,otherwise return false
  54.         return(this.getpoint()>newone.getpoint());
  55.     }
  56. }
  1. /*此类代表一张牌*/
  2. public class Cards {
  3.    private Rank rank;                            //大小
  4.    private Suit suit;                            //花色
  5.    
  6.   /* 构造函数*/
  7.    public Cards(Rank rank,Suit suit){
  8.        this.rank = rank;
  9.        this.suit = suit;
  10.    }
  11.    
  12.     public int getRank() {
  13.         return rank.value();
  14.     }
  15.     
  16.     public Suit getSuit() {
  17.         return suit;
  18.     }
  19.     
  20.     @Override
  21.     public String toString() {
  22.         return rank.toString() + " of " + suit.toString();
  23.     }
  24. }
  1. /*花色类*/
  2. public enum Suit {
  3.     Clubs, 
  4.     Diamonds, 
  5.     Hearts, 
  6.     Spades
  7. }
  1. /*牌大小类*/
  2. public enum Rank {
  3.     Ace(1), deuce(2), three(3), four(4), five(5), six(6), seven(7),
  4.     eight(8), nine(9), ten(10), Jack(10), Queen(10), King(10);
  5.     Rank(int value) {
  6.       this.value = value;
  7.     }
  8.     int value() {
  9.       return value;
  10.     }
  11.     private final int value;                   //用于比较牌的大小          
  12. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值