【JavaProject】洗牌斗牛系统

JavaProject & 洗牌斗牛系统

  • 结合ArrayList类的使用,小小实践一下吧

1. 基础模板

  • 重点1: 做扑克牌所需要用到的原料

            String str1 = "♥♠♣♦";
            String str2 = " 1 2 3 4 5 6 7 8 910 J Q K";
    
  • 重点2:扑克牌类

    • 属性
      • 花色
    • getter 和 setter方法
    • toString方法
    • 构造方法
    public class Poker {
        char quality;
        String value;
    
        @Override
        public String toString() {
            return " [" +
                    quality +
                    value +
                    "] ";
        }
    
        public Poker(char quality, String value) {
            this.quality = quality;
            this.value = value;
        }
    
        public char getQuality() {
            return quality;
        }
    
        public void setQuality(char quality) {
            this.quality = quality;
        }
    
        public String getValue() {
            return value;
        }
    
        public void setValue(String value) {
            this.value = value;
        }
    }
    
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        //这个循环是按一次回车就会走一次
        while(scanner.hasNextLine()) {
            String str = scanner.nextLine();
            
            List<Poker> pokerList = new ArrayList<>();
            
            String str1 = "♥♠♣♦";
            String str2 = " 1 2 3 4 5 6 7 8 910 J Q K";
            
            //买牌--------------

            //取牌--------------

            //洗牌--------------

            //发牌--------------
            
            //斗牛--------------
            bullfighting(lists);
        }
    }

2. 买牌(整齐的牌)

  • 说是买牌,我们还是要用刚才的两个“原料”做牌,但是还有大小鬼呢

  •         String str1 = "♥♠♣♦";
            String str2 = " 1 2 3 4 5 6 7 8 910 J Q K";
    
    public static void buy(List<Poker> pokerList, String str1, String str2) {
        for(int i = 0; i < 4; i++) {
            for(int j = 1; j <= 13; j++) {
                char ch = '\u0000';
                // 初始化的unicode值
                String str = "";
                //空字符串
                //花色提取
                ch = str1.charAt(i);
                
                //两个两个提取【因为10不能被一个字符表示】
                str += "" + str2.charAt(2 * j - 2) + str2.charAt(2 * j - 1);
                Poker poker = new Poker(ch, str);
                pokerList.add(poker);
            }
        }
        
        //大小鬼放在最后
        pokerList.add(new Poker('小', "鬼"));
        pokerList.add(new Poker('大', "鬼"));
    }

3. 取牌(就是看牌)

  • 理论上System.out.println(pokerList)就足够了,但是打印的不好看
    • 可以自己写个show方法,当然用匿名内部类重写toString方法也可以
    • 这里我增加了没六个换一行
public static void show(List<Poker> pokerList) {
    for (int i = 1; i <= pokerList.size(); i++) {
        System.out.print(pokerList.get(i - 1));
        if(i % 6 == 0) {
            System.out.println();
        }
    }
    System.out.println();
}

4. 洗牌(乱序)

  • 基础思想,在顺序表的最后一个往前开始,与自己或者前面的任何一个(同一概率)交换,交换后便能确定这个位置的牌是随机的。
  • Random random = new Random();随机方法!
public static void swap(List<Poker> pokerList, int index1, int index2) {
    Poker tmp = pokerList.get(index1);
    pokerList.set(index1, pokerList.get(index2));
    pokerList.set(index2, tmp);
}
public static void disorder(List<Poker> pokerList) {
    for (int i = pokerList.size() - 1; i >= 0 ; i--) {
        //随机数的选取
        Random random = new Random();
        int index = random.nextInt(i + 1);//左闭右开的!
        
        swap(pokerList, i, index);
    }
}
	show(pokerList);
	disorder(pokerList);
	show(pokerList);

在这里插入图片描述

5. 发牌(随机发牌)【默认三个人】

  • List<Poker> pokerList, List<List<Poker>> lists,二位顺序表标准写法!不是T[ ]这么简单
  • 用之前的乱序牌组,随机度就够了
  • size == 一人几张
public static void handOutFive(List<Poker> pokerList, List<List<Poker>> lists, int size) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < size; j++) {
            lists.get(i).add(pokerList.remove(0));
            if(0 == pokerList.size()) {
                System.out.println("发完了!");
                return;
            }
        }
    }
}
List<List<Poker>> lists = new ArrayList<>();
List<Poker> list1 = new ArrayList<>();
List<Poker> list2 = new ArrayList<>();
List<Poker> list3 = new ArrayList<>();
lists.add(list1);
lists.add(list2);
lists.add(list3);
//发牌
handOutFive(pokerList, lists, 5);
for(List<Poker> list : lists) {
    show(list);
}

在这里插入图片描述

6. 斗牛

  • 用刚才发来的三组牌,判断牛数

  • 我这里是暴力检测,目前我没有其他方法

  • 我们需要解析一下这个二位顺序表, 因为它并非整数

    • 全部和能被10整除,就为牛牛
    • 通过多重循环检测是否出现三张和为10倍数,剩余两种和模10就为牛数
    • 如果无结果,就为无牛
    • 鬼代表10
public static void bullfighting(List<List<Poker>> lists) {
    String[][] strings = new String[3][5];
    for (int j = 0; j < 3; j++) {
        int sum = 0;
        System.out.print("第" + (j + 1) + "个人:");
        show(lists.get(j));
        
		// 区域1
        
        // 区域2
        
    }
}

6.1 区域1:

  • 解析每一组牌的值的总和
    for(int i = 0; i < 5; i++) {
        if(lists.get(j).get(i).getValue().equals("鬼")) {
            sum += 10;
            strings[j][i] = "10";
            continue;
        }
        char ch = lists.get(j).get(i).getValue().charAt(1);
        if(ch >= '1' && ch <= '9') {
            sum += ch - '0';
            strings[j][i] = "" + ch;
        }else {
            sum += 10;
            strings[j][i] = "10";
        }
    }

6.2 区域2:

  • 牛数判断
	if(sum % 10 == 0) {
            System.out.println("牛牛!");
 	}else {
            int count = 0;
            for (int a = 0; a < 5; a++) {
                for (int b = 0; b < 5; b++) {
                    for (int c = 0; c < 5; c++) {
                        if(a != b && b != c && a != c) {
                            int s = Integer.parseInt(strings[j][a]) + 				    							   Integer.parseInt(strings[j][b]) + 
                                Integer.parseInt(strings[j][c]);
                            if(s % 10 == 0) {
                            //一次就够了,不用多次输出,并且后面可能还有牌,所以不能直接return
                                if(count++ == 0) {
                                    //count可以证明有牛
                                    System.out.println("牛" + sum % 10 + "!");
                                }
                            }
                        }
                    }
                }
            }
            if(count == 0) {
                System.out.println("无牛!");
            }
		}

测试

  • 只打印斗牛结果
   public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()) {
        
            String str = scanner.nextLine();
            List<Poker> pokerList = new ArrayList<>();
            String str1 = "♥♠♣♦";
            String str2 = " 1 2 3 4 5 6 7 8 910 J Q K";
            //买牌--------------
            buy(pokerList, str1, str2);
            //洗牌--------------
            disorder(pokerList);
            //发牌--------------
            List<List<Poker>> lists = new ArrayList<>();
            List<Poker> list1 = new ArrayList<>();
            List<Poker> list2 = new ArrayList<>();
            List<Poker> list3 = new ArrayList<>();
            lists.add(list1);
            lists.add(list2);
            lists.add(list3);
            handOutFive(pokerList, lists, 5);
            //斗牛
            bullfighting(lists);
        }
    }
}

在这里插入图片描述

  • 测试结果正常

文章到此结束!谢谢观看

可以叫我 小马,我可能写的不好或者有错误,但是一起加油鸭🦆

这是我的代码仓库!(在马拉圈的23.1里)代码仓库 洗牌具体代码位置

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

s:103

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值