当前位置:   article > 正文

刘谦龙年春晚魔术模拟_刘谦魔术java代码实现

刘谦魔术java代码实现

守岁共此时

代码

直接贴代码了,异常处理有点问题,正常流程能跑通

package com.yuhan.snginx.util.chunwan;

import java.util.*;


/**
 * @author yuhan
 * @since 2024/02/10
 */
public class CWMS {

    static String[] num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

    static String[] style = {"♠️", "♥️", "♣️", "♦️"};

    static String[] wang = {"大王", "小王"};

    public static List<String> listPoker = new ArrayList<>(54);


    public static List<String> choose = new ArrayList<>(4);

    public static HashMap<Integer, String> map = new HashMap<>();

    static {
        map.put(1, "南方");
        map.put(2, "北方");
        map.put(3, "二家岗子");
    }


    public static List<String> getAllPoker() {
        for (String string : style) {
            for (String s : num) {
                listPoker.add(string + s);
            }
        }
        listPoker.add(Arrays.toString(wang));

        return listPoker;
    }

    public static void choosePoker() {
        String styleIn, numIn;

        for (int i = 0; i < 4; i++) {
            System.out.println("\n请选择花色");
            Scanner scanner = new Scanner(System.in);
            styleIn = scanner.nextLine();
            System.out.println("请选择数字");
            numIn = scanner.nextLine();
            System.out.println("是否确认选择 ok or any keys");
            String sc = scanner.nextLine();
            if (!Objects.equals(sc, "ok")) {
                return;
            }
            while (Objects.isNull(styleIn) || Objects.isNull(numIn)) {
                System.out.println("输入错误,请重新输入");
            }
            String chPoker = styleIn + numIn;
            while (!listPoker.contains(chPoker)) {
                System.out.println("花色非法或不存在的数字");
            }
            System.out.println("\n 本次选择" + chPoker);
            choose.add(chPoker);
        }
        System.out.println("你选择的扑克分别为:");
        choose.forEach(System.out::println);
    }

    public static void suffer() {
        System.out.println("\n 请打乱刚刚选择的牌,按ok键进行 !");
        Scanner scanner = new Scanner(System.in);
        if ("ok".equalsIgnoreCase(scanner.nextLine())) {
            System.out.println("\n洗牌前顺序");
            choose.forEach(s -> System.out.println(s + " "));
            Collections.shuffle(choose);
            System.out.println("\n洗牌后顺序");
            choose.forEach(s -> System.out.println(s + " "));
        }
    }

    public static List<String> push() {
        System.out.println("请从中间撕碎扑克,按 ok 撕碎 !");
        Scanner scanner = new Scanner(System.in);
        if (!"ok".equalsIgnoreCase(scanner.nextLine())) {
            System.out.println("不撕就别玩,结束了");
            return choose;
        }
        ArrayList<String> result = new ArrayList<>(choose);
        for (String s : choose) {
            String str = s + "副本";
            result.add(str);
        }
        result.forEach(System.out::println);
        return result;
    }

    public static void nameSuffer(List<String> push) {
        System.out.println("请输入你的姓名,名字有几个字,就将最上面的牌放到最下边几次");
        Scanner scanner = new Scanner(System.in);

        String name = scanner.nextLine();
        if (Objects.isNull(name)) {
            System.out.println("姓名为空");
            return;
        }
        for (int i = 0; i < name.length(); i++) {
            String first = push.get(0);
            push.remove(0);
            push.add(first);
        }
    }


    public static void magicTime(List<String> push) {

        String magic = "见证奇迹的时刻";

        for (int i = 0; i < magic.length(); i++) {
            String first = push.get(0);
            push.remove(0);
            push.add(first);
        }
    }

    public static void suffer_3(List<String> push, boolean region, int regionChoose) {
        Random random = new Random();

        if (region) {
            regionChoose = (regionChoose < 1 || regionChoose > 3) ? 3 : regionChoose;
        }

        // 生成3到5之间的随机数
        int randomNum = region ? random.nextInt(4 + (3 - regionChoose)) : random.nextInt(4);
        System.out.println(randomNum);
        ArrayList<String> third = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            third.add(push.get(i));
        }
//        third.forEach(System.out::println);
//        System.out.println("third");
        if (!region) {
            push.remove(0);
            push.remove(0);
            push.remove(0);
        } else {
            for (int i = 0; i < regionChoose; i++) {
                push.remove(0);
            }
        }

//        push.forEach(System.out::println);
//        System.out.println("removed");

        push.addAll(randomNum + 1, third);
    }

    public static String getFirst(List<String> suffered) {
        return suffered.get(0);
    }

    public static int region() {
        System.out.println("\n 请选择南北方人 ,如果你是南方人请输入 1;如果你是北方人请输入 2; 如果不能确认你是南北方人请输入 3");
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        while (Objects.isNull(map.get(i))) {
            System.out.println("输入错误,请重新输入 !");
            i = scanner.nextInt();
        }

        System.out.println("你已选择" + map.get(i));
        return i;
    }

    public static void drop(List<String> pushed, int i) {
        for (int i1 = 0; i1 < i; i1++) {
            pushed.remove(0);
        }
    }

    public static int chooseSex() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择 性别 男:1 女:2");
        int i = scanner.nextInt();
        System.out.println(i == 1 ? "男" : "女");
        return i;
    }

    /**
     * 好运留下来
     *
     * @param args
     */
    public static void luck(List<String> push) {
        String first = push.get(0);
        push.remove(0);
        System.out.println("好运留下来");
        push.add(first);
        System.out.println("烦恼丢出去");
        push.remove(0);
    }

    private static void anyWay(List<String> pushed) {
        while (pushed.size() > 1) {
            luck(pushed);
        }
    }


    public static void main(String[] args) {
        // 获取完整扑克
        getAllPoker().forEach(t -> System.out.print("   " + t));
        // 从中选择四张
        choosePoker();
        // 打乱
        suffer();
        // 撕碎
        List<String> pushed = push();
        // name
        nameSuffer(pushed);
        // suffer_3
        suffer_3(pushed, false, 0);

        // 记住取出的第一张牌
        String first = getFirst(pushed);

        // 南北方人选择
        // 南方北方切牌
        suffer_3(pushed, true, region());

        // 男女选择
        int sex = chooseSex();
        drop(pushed, sex);
        // 见证奇迹的时刻
        magicTime(pushed);

        // 多来几次
        anyWay(pushed);

        // 对比
        System.out.println("第一张牌" + first);
        System.out.println("丢完剩下的" + pushed.get(0));
    }


}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/151656?site
推荐阅读
相关标签
  

闽ICP备14008679号