21点java

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

/**
 * @author suny
 * @version 1.0
 */
public class Main {
    public static void main(String[] args) {
        new Player().strategy();

    }

}

/**
 * 花色的常量
 */
interface Decor {
    int Spade = 1;
    int Heart = 2;
    int Club = 4;
    int Diamond = 3;
}

/**
 * 牌的种类
 */

class Card implements Decor {
    int decor;//花色
    String value;//值

    public Card(String decor, String value) {
        this.decor = getDecor(decor);
        this.value = value;
    }


    @Override
    public String toString() {
        return decor_(decor) + value;
    }

    /**
     * 将花色转换为字符串,用于输出
     * @param decor:花色
     * @return:花色的字符串
     */
    public String decor_(int decor) {
        switch (decor) {
            case 1:
                return "Spade";
            case 2:
                return "Heart";
            case 4:
                return "Club";
            case 3:
                return "Diamond";
            default:
                return "error";
        }
    }

    /**
     * 将牌的值转换为数字,根据A的值不同,返回不同的值
     * A的值为11时,返回A的值为11,其他值为10
     * A的值为1时,返回A的值为1,其他值为按照排列顺序的值
     * @param value
     * @param A
     * @return
     */

    public int value_(String value, int A) {
        switch (value) {
            case "A":
                return A;
            case "K":
                return A == 11 ? 10 : 13;
            case "Q":
                return A == 11 ? 10 : 12;
            case "J":
                return A == 11 ? 10 : 11;
            default:
                return Integer.parseInt(value);
        }
    }

    //将牌的值转换为数字
    public int getValue() {
        return value_(value, 11);
    }

    public int getValue_print() {
        return value_(value, 1);
    }

    //将花色转换为数字
    public int getDecor(String decor) {
        switch (decor) {
            case "Spade":
                return Decor.Spade;
            case "Heart":
                return Decor.Heart;
            case "Club":
                return Decor.Club;
            case "Diamond":
                return Decor.Diamond;
            default:
                return 0;
        }

    }

}


class Hand {

    ArrayList<Card> allCard = new ArrayList<>();//记录所有的牌
    //有多少A牌
    int A = 0;


    public void add(boolean before2) {
        String str = Tool.str();
        Card ca;
        String[] split = str.split(" ");
        ca = new Card(split[0], split[1]);
        allCard.add(ca);
        //如果是A牌,则A的数量加1
        if (ca.getValue() == 11) {
            A++;
            if (!before2) {
                System.out.println(ca.decor_(ca.decor) + " 1 11");
                return;
            }
        }
        if (!before2) {
            System.out.println(ca.decor_(ca.decor) + " " + ca.getValue());
        }
    }

    //返回当前的总值,指定A的规则:如果总值大于21,则A的值为1,否则为11
    public int getTotal() {
        int total = 0;
        //A==11,如果总和大于21,则A的值为1,否则为11
        for (Card card : allCard) {
            total += card.getValue();
        }
        for (int i = 0; i < A; i++) {
            if (total > 21) {
                total -= 10;
            } else {
                break;
            }
        }
        return total;
    }

    //点数之和小于17点则继续要下一张牌,直到大于等于17点为止
    public Boolean isAdd() {
        return getTotal() < 17;
    }

    //总点数超过21点
    public Boolean isLose() {
        return getTotal() > 21;
    }

    //等于21点
    public Boolean isBlackjack() {
        return getTotal() == 21 && allCard.size() == 2;
    }

}

class Player {
    Hand myCards = new Hand();

    public void strategy() {
        //前两张牌不输出
        myCards.add(true);
        myCards.add(true);

        while (true) {
            if (myCards.isLose()) {
                System.out.println("Stand");
                print();
                System.out.println("Bust");
                break;
            } else if (myCards.isBlackjack()) {
                System.out.println("Stand");
                print();
                System.out.println("Blackjack");
                break;
            } else if (myCards.isAdd()) {
                System.out.println("Hit");
                myCards.add(false);
            } else if (!myCards.isAdd()) {
                System.out.println("Stand");
                print();
                System.out.println(myCards.getTotal());
                break;
            }
        }

    }

    //输出当前的牌:牌与牌之间用一个空格分隔。
    // 牌输出的顺序为先看牌面,牌面小的在前(牌面由小到大的顺序为A,2,3....J,Q,K),
    // 当牌面相同时看花色,输出顺序从前到后为Spade, Heart, Diamond, Club。
    public void print() {
        myCards.allCard.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getValue_print() == o2.getValue_print()) {
                    return o1.decor - o2.decor;
                } else {
                    return o1.getValue_print() - o2.getValue_print();
                }
            }
        });
        for (Card card : myCards.allCard) {
            System.out.print(card + " ");
        }
        System.out.println();
    }


}

class Tool {
    private static Scanner scanner = new Scanner(System.in);

    public static String str() {
        return scanner.nextLine();
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的21游戏的Java代码示例: ``` import java.util.Scanner; public class TwentyOneGame { public static void main(String[] args) { Scanner input = new Scanner(System.in); int playerTotal = 0; int dealerTotal = 0; boolean gameOver = false; while(!gameOver){ System.out.println("Dealer shows: " + dealerTotal); System.out.println("Player total: " + playerTotal); System.out.println("Enter 'hit' or 'stay': "); String choice = input.nextLine(); if(choice.equalsIgnoreCase("hit")){ int card = drawCard(); playerTotal += card; System.out.println("You drew a " + card); if(playerTotal > 21){ System.out.println("Bust! You lose!"); gameOver = true; } } else if (choice.equalsIgnoreCase("stay")){ System.out.println("Dealer's turn."); while(dealerTotal < 17){ int card = drawCard(); dealerTotal += card; System.out.println("Dealer drew a " + card); } if(dealerTotal > 21){ System.out.println("Dealer busts! You win!"); } else if(dealerTotal > playerTotal){ System.out.println("Dealer wins!"); } else if(dealerTotal < playerTotal){ System.out.println("You win!"); } else { System.out.println("It's a tie!"); } gameOver = true; } else { System.out.println("Invalid input!"); } } input.close(); } public static int drawCard(){ //generates a random number between 1 and 10 (inclusive) return (int)(Math.random() * 10) + 1; } } ``` 这个程序使用了一个while循环来控制游戏的进行。每次循环,程序会提示玩家选择“hit”或“stay”,然后根据玩家的选择来继续游戏。如果玩家选择“hit”,程序会随机生成一张牌并将其加到玩家的牌总数中。如果玩家总分数超过21,那么游戏结束,玩家输了。如果玩家选择“stay”,那么程序将进入到庄家的回合,庄家会一直抽牌(直到手牌总数大于等于17),然后比较庄家和玩家的牌总数,决定谁赢了。 这只是一个简单的示例,可以根据需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值