• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

摸鱼一小时实现的斗牛GameJava版,希望各位友友远离赌博~

武飞扬头像
讲码德的桃子同学
帮助1

前言

今天下午上班的时候脑海里无意间闪过了家里长辈们逢年过节时在桌前的牌局娱乐行为,有麻将、扑克等。小时候不以为然,长大后才慢慢意识到这种娱乐行为给家庭带来的危害【指输赢比较大的牌局】。我花了1个小时的时间模拟了“斗牛”扑克牌游戏的实现,尽管是非常简陋的程序,但我任有操控后台的手段使玩家的胜率为0,更何况是复杂的人心。

学新通

斗牛规则

常规的斗牛规则相信大家了解一二,由于本人水平和时间的关系,我并没有将细节全部实现。程序设计方面也有所欠缺,望大家海涵。
学新通学新通


庄家与玩家各发5张牌,2 - 10 为牌面值,A 代表 1,J、Q、K代表10,计算5张牌总和

3张牌总和需满足 sum % 10 == 0

sum <= 10 :五小牛 (输赢翻5倍)

sum % 10 == 0 :牛牛 (输赢翻4倍)

sum % 10 == 9 :牛9 (输赢翻3倍)

sum % 10 == 8 :牛8 (输赢翻2倍)

1 <= sum % 10 < 8 :(输赢翻1倍)

每局固定押注10000

斗牛v0.1

程序运行效果如下

学新通

  • 输入1

学新通

  • 输入2

 学新通

下面来看看实现程序的三个类

  •  玩家类
  1.  
    @Data
  2.  
    public class Person {
  3.  
    // 昵称
  4.  
    private String name;
  5.  
    // 账户余额
  6.  
    private int money;
  7.  
     
  8.  
    public Person(String name, int money) {
  9.  
    this.name = name;
  10.  
    this.money = money;
  11.  
    }
  12.  
     
  13.  
    }
  • 卡片类
  1.  
    public class Card {
  2.  
     
  3.  
    /* 构造cards所需条件 */
  4.  
    private static final String[] color = {"♣", "♦", "♠", "♥"};
  5.  
    private static final String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
  6.  
    // 所有的cards
  7.  
    private static final List<String> cards = new ArrayList<>(52);
  8.  
    // 维护所有的翻倍条件
  9.  
    private static final Map<String, Integer> ruleMap = new HashMap<>();
  10.  
     
  11.  
    static {
  12.  
    // 初始化所有卡片
  13.  
    for (int i = 0; i < numbers.length; i ) {
  14.  
    for (int j = 0; j < color.length; j ) {
  15.  
    cards.add(color[j] numbers[i]);
  16.  
    }
  17.  
    }
  18.  
    // 初始化所有翻倍条件
  19.  
    ruleMap.put("五小牛", 5);
  20.  
    ruleMap.put("牛牛", 4);
  21.  
    ruleMap.put("牛9", 3);
  22.  
    ruleMap.put("牛8", 2);
  23.  
    ruleMap.put("牛7", 1);
  24.  
    ruleMap.put("牛6", 0);
  25.  
    ruleMap.put("牛5", -1);
  26.  
    ruleMap.put("牛4", -2);
  27.  
    ruleMap.put("牛3", -3);
  28.  
    ruleMap.put("牛2", -4);
  29.  
    ruleMap.put("牛1", -5);
  30.  
    ruleMap.put("无牛", -6);
  31.  
    }
  32.  
     
  33.  
    /**
  34.  
    * 得到两组 5 张卡片的数组
  35.  
    * @param cards 卡片集合
  36.  
    * @return
  37.  
    */
  38.  
    public String[][] getFiveCard(List<String> cards) {
  39.  
    // 洗牌
  40.  
    Collections.shuffle(cards);
  41.  
    String[][] res = new String[2][5];
  42.  
    int index = 0;
  43.  
    for (int i = 0; i < res.length; i ) {
  44.  
    for (int j = 0; j < res[0].length; j ) {
  45.  
    res[i][j] = cards.get(index);
  46.  
    index ;
  47.  
    }
  48.  
    }
  49.  
     
  50.  
    return res;
  51.  
    }
  52.  
     
  53.  
    /**
  54.  
    * 获取牌面对应的【牛】
  55.  
    * @param nums 5张卡片的数组
  56.  
    * @return
  57.  
    */
  58.  
    public String getSkill(String[] nums) {
  59.  
    // ["♠5", "♣10"]
  60.  
    int sum = 0;
  61.  
    List<Integer> integerList = new ArrayList<>();
  62.  
    for (String num : nums) {
  63.  
    String indexNum = num.substring(1);
  64.  
    // JQKA对应数字转换
  65.  
    String number = convertJQKA(indexNum);
  66.  
    // 求和
  67.  
    sum = Integer.parseInt(number);
  68.  
    integerList.add(Integer.parseInt(number));
  69.  
    }
  70.  
     
  71.  
    String nForSum = computeNForSum(sum);
  72.  
    boolean hasN = hasN(integerList);
  73.  
    // 若有牛则返回
  74.  
    if (hasN || nForSum.equals("五小牛")) {
  75.  
    return nForSum;
  76.  
    }
  77.  
     
  78.  
    return "无牛";
  79.  
    }
  80.  
     
  81.  
    /**
  82.  
    * 判断三张牌是否满足有牛需求 sum % 10 == 0
  83.  
    * @param numbers
  84.  
    * @return
  85.  
    */
  86.  
    private static boolean hasN(List<Integer> numbers) {
  87.  
    int size = numbers.size();
  88.  
    for (int i = 0; i < size; i ) {
  89.  
    for (int j = i 1; j < size; j ) {
  90.  
    for (int k = j 1; k < size; k ) {
  91.  
    int sum = numbers.get(i) numbers.get(j) numbers.get(k);
  92.  
    if (sum % 10 == 0) {
  93.  
    return true;
  94.  
    }
  95.  
    }
  96.  
    }
  97.  
    }
  98.  
     
  99.  
    return false;
  100.  
    }
  101.  
     
  102.  
    /**
  103.  
    * JQKA对应数字转换
  104.  
    * @param num
  105.  
    * @return
  106.  
    */
  107.  
    private String convertJQKA(String num) {
  108.  
    String res = num;
  109.  
    if (num.equals("J") || num.equals("Q") || num.equals("K")) {
  110.  
    res = "10";
  111.  
    } else if (num.equals("A")) {
  112.  
    res = "1";
  113.  
    }
  114.  
    return res;
  115.  
    }
  116.  
     
  117.  
    /**
  118.  
    * 计算牌面【牛】
  119.  
    * @param sum
  120.  
    * @return
  121.  
    */
  122.  
    private String computeNForSum(int sum) {
  123.  
    String res = "";
  124.  
    if (sum <= 10) {
  125.  
    res = "五小牛";
  126.  
    } else if (sum % 10 == 0) {
  127.  
    res = "牛牛";
  128.  
    } else {
  129.  
    res = "牛" (sum % 10);
  130.  
    }
  131.  
     
  132.  
    return res;
  133.  
    }
  134.  
     
  135.  
     
  136.  
    public static List<String> getCards() {
  137.  
    return cards;
  138.  
    }
  139.  
     
  140.  
    /**
  141.  
    * 比较boss与person间的牌面
  142.  
    * @param skillBoss boss的牌面
  143.  
    * @param boss
  144.  
    * @param skillPerson person的牌面
  145.  
    * @param person
  146.  
    */
  147.  
    public static void compare(String skillBoss, Person boss, String skillPerson, Person person) {
  148.  
    // 获取牌面对应倍数
  149.  
    Integer intBoss = ruleMap.get(skillBoss);
  150.  
    Integer intPerson = ruleMap.get(skillPerson);
  151.  
    if (intBoss >= intPerson) {
  152.  
    // 如果倍数小于等于1 则置为1方便计算输赢
  153.  
    if (intBoss <= 1) {
  154.  
    intBoss = 1;
  155.  
    }
  156.  
    System.out.println("本局 " boss.getName() " 获胜~");
  157.  
    System.out.println(person.getName() "在本局输掉了 " 10000 * intBoss "¥");
  158.  
    person.setMoney(person.getMoney() - 10000 * intBoss);
  159.  
    System.out.println(person.getName() "当前账户余额: " person.getMoney() "¥");
  160.  
    boss.setMoney(boss.getMoney() 10000 * intBoss);
  161.  
    System.out.println(boss.getName() "当前账户余额: " boss.getMoney() "¥");
  162.  
    System.out.println("赌博违法,十赌九输!");
  163.  
    } else {
  164.  
    if (intPerson <= 1) {
  165.  
    intPerson = 1;
  166.  
    }
  167.  
    System.out.println("本局 " person.getName() " 获胜~");
  168.  
    System.out.println(person.getName() "在本局赢了 " 10000 * intPerson "¥");
  169.  
    person.setMoney(person.getMoney() 10000 * intPerson);
  170.  
    System.out.println(person.getName() "当前账户余额: " person.getMoney() "¥");
  171.  
    boss.setMoney(boss.getMoney() - 10000 * intPerson);
  172.  
    System.out.println(boss.getName() "当前账户余额: " boss.getMoney() "¥");
  173.  
    System.out.println("赌博违法,十赌九输!");
  174.  
    }
  175.  
    }
  176.  
     
  177.  
    }
学新通
  •  斗牛APP
  1.  
    public class DN_App {
  2.  
    public static void main(String[] args) {
  3.  
    Person boss = new Person("黄老板", 20000000);
  4.  
    Person lly = new Person("卢姥爷", 200000);
  5.  
    System.out.println("经典语录【doge】");
  6.  
    System.out.println("你们可能不知道只用20万赢到578万是什么概念,我们一般只会用两个字来形容这种人:赌怪。\n"
  7.  
    "我经常说一句话,当年陈刀仔他能用20块赢到3700万,我卢本伟用20万赢到500万,不是问题。");
  8.  
    System.out.println();
  9.  
    Scanner scanner = new Scanner(System.in);
  10.  
    while (true) {
  11.  
    System.out.println("===========欢迎来到斗牛game v0.1 ===========");
  12.  
    System.out.println("=========== 请输入你的选择 ===========");
  13.  
    System.out.println("1:开始游戏");
  14.  
    System.out.println("2:退出游戏");
  15.  
    int choose = scanner.nextInt();
  16.  
    if (choose == 1) {
  17.  
    Card card = new Card();
  18.  
    String[][] fiveCard = card.getFiveCard(Card.getCards());
  19.  
    System.out.println(boss.getName() "的牌如下: ");
  20.  
    System.out.println(Arrays.toString(fiveCard[0]));
  21.  
    String skillBoss = card.getSkill(fiveCard[0]);
  22.  
    System.out.println("\t" skillBoss);
  23.  
    System.out.println(lly.getName() "的牌如下: ");
  24.  
    System.out.println(Arrays.toString(fiveCard[1]));
  25.  
    String skillLLY = card.getSkill(fiveCard[1]);
  26.  
    System.out.println("\t" skillLLY);
  27.  
    Card.compare(skillBoss, boss, skillLLY, lly);
  28.  
    if (lly.getMoney() <= 0) {
  29.  
    System.out.println(lly.getName() "已经输光了所有钱...game over!");
  30.  
    break;
  31.  
    }
  32.  
    } else if (choose == 2) {
  33.  
    System.out.println("珍爱生命,远离赌博~");
  34.  
    System.out.println("小赌怡情,大赌伤财,强赌灰飞烟灭~");
  35.  
    break;
  36.  
    }
  37.  
    }
  38.  
    }
  39.  
    }
学新通

结语

开发上述程序纯属个人兴趣,目的也是希望各位远离赌博,拥抱阳光~

不知不觉又到周五了,希望大家过一个愉快的周末~
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgejcif
系列文章
更多 icon
同类精品
更多 icon
继续加载