Java基础(二十一)十点半游戏

十点半游戏

十点半是一种流行的纸牌游戏,可以说是一种变体的二十一点游戏。游戏的规则是,每个玩家根据所拿到的牌点数的总和来决定是否继续要牌。目标是尽量接近但不超过十点半的点数,超过十点半即为爆牌。如果两名玩家都未爆牌,则点数更接近十点半的人获胜。这个游戏非常简单且容易上手,适合多人一起娱乐。
代码实现

java 复制代码
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Poker {
    private static final List<String> suits = Arrays.asList("♠", "♥", "♦", "♣");  // 花色
    private static final List<String> ranks = Arrays.asList("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "小王", "大王");  // 点数

    private List<String> deck;
    private Boolean isComplete;

    public Poker() {
        this(true);
    }

    public Poker(boolean isComplete) {
        deck = new ArrayList<>();  // 扑克牌
        // 向扑克牌列表中储存元素
        for (String suit : suits) {
            for (String rank : ranks.subList(0, 13)) {
                String card = suit + rank;
                deck.add(card);
            }
        }
        if (isComplete) {
            deck.add("大王");
            deck.add("小王");
        }
    }

    public List<String> getDeck() {
        return deck;
    }
}
java 复制代码
import java.util.*;

/**
 * @author BXB
 */
public class Game {
    public static void main(String[] args) {
        List<String> poker = new Poker(false).getDeck();

        shuffle(poker);
        gameing(poker);
    }
    
    // 进行游戏
    public static void gameing(List<String> poker){
        ArrayList<String> player = new ArrayList<>();
        ArrayList<String> bot = new ArrayList<>();
        boolean isTermination = true;
        Scanner input = new Scanner(System.in);
        // 玩家发牌
        do {
            player.add(poker.get(0));  // 向玩家发牌
            System.out.println(player);
            poker.remove(0);  // 去除已经发出去的牌
            if (countPoints(player) > 10.5) {
                break;
            }
            System.out.println("还要继续取牌吗?(Y or N)");
            if ("N".equals(input.next())) {
                isTermination = false;
            }
        } while (isTermination);

        if (isWin(player)) {
            System.out.println("你赢了,恭喜恭喜");
            System.out.println(bot);
        } else if (countPoints2(player) <= 10.5) {
            // 机器人取牌
            while (countPoints2(bot) <= countPoints2(player) && countPoints2(bot) != 10.5) {
                bot.add(poker.get(0));
                poker.remove(0);
            }
            // 判断机器人是否赢了
            if (isLost(bot)) {
                System.out.println("机器人输了\n" + bot);
            } else if (isWin(bot) || isWin(bot, player)) {
                System.out.println("机器人赢了\n" + bot);
            } else {
                System.out.println("你赢了,恭喜恭喜\n" + bot);
            }
        } else {
            System.out.println("你输了");
        }
    }

    // 洗牌
    public static void shuffle(List<String> list) {
        for (int i = 0; i < 3; i++) {
            // System.currentTimeMillis() 来设置随机种子。每一次运行程序时都会使用不同的随机种子,从而产生更随机的结果。
            Collections.shuffle(list, new Random(System.currentTimeMillis()));
        }
    }

    // 计算点数和2
    public static double countPoints2(List<String> list) {
        List<String> ranks = Arrays.asList("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K");  // 点数
        double count = 0;
        // 匿名函数中局部变量 count 必须声明为 final 或者实际上是 final 的(即该变量值不可更改)。
        for (String str : list) {
            str = str.replaceAll("[♠♥♦♣]", "");  // 去除花色
            switch (str) {
                case "A" -> count += 1;
                case "J","Q","K" -> count += 0.5;
                default -> count += Double.parseDouble(str);
            }
        }
        return count;
    }

    // 判断输赢
    public static boolean isLost(List<String> list) {
        if (countPoints2(list) > 10.5) {
            return true;
        }
        return false;
    }
    
    public static boolean isWin(List<String> list) {
        if (countPoints2(list) > 10.5) {
            return false;
        } else if (list.size() == 5) {
            return true;
        }
        return false;
    }
    
    public static boolean isWin(List<String> list, List<String> botList) {
        return countPoints2(list) > countPoints2(botList);
    }
}
相关推荐
FakeOccupational9 分钟前
【碎碎念】宝可梦 Mesh GO : 基于MESH网络的口袋妖怪 宝可梦GO游戏自组网系统
网络·游戏
sky_ph10 分钟前
JAVA-GC浅析(二)G1(Garbage First)回收器
java·后端
IDRSolutions_CN32 分钟前
PDF 转 HTML5 —— HTML5 填充图形不支持 Even-Odd 奇偶规则?(第二部分)
java·经验分享·pdf·软件工程·团队开发
hello早上好35 分钟前
Spring不同类型的ApplicationContext的创建方式
java·后端·架构
虾球xz1 小时前
CppCon 2015 学习:CLANG/C2 for Windows
开发语言·c++·windows·学习
码上库利南2 小时前
Windows开机自动启动中间件
windows
HelloWord~2 小时前
SpringSecurity+vue通用权限系统2
java·vue.js
让我上个超影吧2 小时前
黑马点评【基于redis实现共享session登录】
java·redis
BillKu2 小时前
Java + Spring Boot + Mybatis 插入数据后,获取自增 id 的方法
java·tomcat·mybatis
全栈凯哥2 小时前
Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解
java·算法·leetcode·链表