Java實戰練習之撲克牌魔術

前言

用到瞭Scanner,for循環,if-else語句,集合,線程的Thread.sleep()方法

話不多說,直接上代碼

代碼

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class CardMagic {
    public static void main(String[] args) {

        //1.準備54張牌
        //新建一個集合存儲54張牌並打亂順序
        ArrayList<String> pokers = new ArrayList<>();
        //直接定義兩個數組並賦值,一個存四種花色,一個存13張牌的值(3,4,5,6,7,8,9,10,J,Q,K,A,2)
        String[] kind = {"♥","♠","♦","♣"};
        String[] number = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        //先存大王小王到pokers集合中
        pokers.add("大王");
        pokers.add("小王");
        //組裝兩個集合的數據,生成帶花色的52張牌(可以用普通for循環,也可以用增強for循環foreach)
        for (int i = 0; i < kind.length; i+  +) {
            for (int j = 0; j < 13; j++) {
                pokers.add(kind[i]+number[j]);
            }

        }
        System.out.println("這裡有54張牌:"+pokers);//順序打印
        //洗牌--打亂pokers集合裡的元素順序
        Collections.shuffle(pokers);//打亂順序
        //再新建一個集合,用來存儲隨機拿出的21張牌(偽隨機:其實是拿出前21張牌,但是pokers集合是隨機打亂順序的,所以每次運行程序,拿出的前21張牌也就不一樣且亂序瞭)
        ArrayList<String> list21 = new ArrayList<>();
        for (int i = 0; i < 21; i++) {
            list21.add(pokers.get(i));
        }


        //打印出這21張牌
        System.out.println("隨機取出21張撲克牌並打亂順序:");
        System.out.println(list21);
        System.out.println("請選擇任意一張撲克牌,並記住它。【輸入小寫字母'y'繼續,輸入其他任意字符結束遊戲!】");
        Scanner scanner1 = new Scanner(System.in);

        if (scanner1.nextLine().equals("y")){
            System.out.println("系統將這21張撲克牌均分為3疊,請等待...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else {
            System.exit(-1);
        }

        //定義3個集合,每個集合存儲7張牌
        ArrayList<String> card1 = new ArrayList<>();
        ArrayList<String> card2 = new ArrayList<>();
        ArrayList<String> card3 = new ArrayList<>();

        for (int i = 0; i < 7; i++) {
            card1.add(list21.get(i));
        }
        for (int i = 7; i < 14; i++) {
            card2.add(list21.get(i));
        }
        for (int i = 14; i < 21; i++) {
            card3.add(list21.get(i));
        }

        System.out.println("第1疊:"+card1);
        System.out.println("第2疊:"+card2);
        System.out.println("第3疊:"+card3);



        //判斷輸入
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Scanner scanner2 = new Scanner(System.in);
        System.out.println("請告訴我你記住的牌在第幾疊~如果在第1疊請輸入數字1;如果在第2疊請輸入數字2;如果在第3疊請輸入數字3");
        System.out.println("請輸入:");
        //新建3個數組,分3次重新存重新按指定規則排序的21張牌
        ArrayList<String> list21New1 = new ArrayList<>();
        int a = scanner2.nextInt();

        if (a==1){
            for (int i = 0; i < 7; i++) {
                list21New1.add(card2.get(i));

            }
            for (int i = 0; i < 7; i++) {
                list21New1.add(card1.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New1.add(card3.get(i));
            }
        }else if (a==2){
            for (int i = 0; i < 7; i++) {
                list21New1.add(card1.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New1.add(card2.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New1.add(card3.get(i));
            }
        }else {
            for (int i = 0; i < 7; i++) {
                list21New1.add(card1.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New1.add(card3.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New1.add(card2.get(i));
            }
        }

        card1.clear();
        card2.clear();
        card3.clear();
        for (int i = 0; i < 21; i++) {
            card1.add(list21New1.get(i));
            i++;
            card2.add(list21New1.get(i));
            i++;
            card3.add(list21New1.get(i));
        }
        System.out.println("第1疊:"+card1);
        System.out.println("第2疊:"+card2);
        System.out.println("第3疊:"+card3);

        //判斷輸入
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Scanner scanner3 = new Scanner(System.in);
        System.out.println("請告訴我你記住的牌在第幾疊~如果在第1疊請輸入數字1;如果在第2疊請輸入數字2;如果在第3疊請輸入數字3");
        System.out.println("請輸入:");
        int b = scanner3.nextInt();
        //新建3個數組,分3次重新存重新按指定規則排序的21張牌
        ArrayList<String> list21New2 = new ArrayList<>();
        if (b==1){
            for (int i = 0; i < 7; i++) {
                list21New2.add(card2.get(i));

            }
            for (int i = 0; i < 7; i++) {
                list21New2.add(card1.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New2.add(card3.get(i));
            }
        }else if (b==2){
            for (int i = 0; i < 7; i++) {
                list21New2.add(card1.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New2.add(card2.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New2.add(card3.get(i));
            }
        }else {
            for (int i = 0; i < 7; i++) {
                list21New2.add(card1.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New2.add(card3.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New2.add(card2.get(i));
            }
        }

        card1.clear();
        card2.clear();
        card3.clear();
        for (int i = 0; i < 21; i++) {
            card1.add(list21New2.get(i));
            i++;
            card2.add(list21New2.get(i));
            i++;
            card3.add(list21New2.get(i));
        }
        System.out.println("第1疊:"+card1);
        System.out.println("第2疊:"+card2);
        System.out.println("第3疊:"+card3);

        //判斷輸入
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Scanner scanner4 = new Scanner(System.in);
        System.out.println("請告訴我你記住的牌在第幾疊~如果在第1疊請輸入數字1;如果在第2疊請輸入數字2;如果在第3疊請輸入數字3");
        System.out.println("請輸入:");

        //新建3個數組,分3次重新存重新按指定規則排序的21張牌
        ArrayList<String> list21New3 = new ArrayList<>();
        int c = scanner2.nextInt();
        if (c==1){
            for (int i = 0; i < 7; i++) {
                list21New3.add(card2.get(i));

            }
            for (int i = 0; i < 7; i++) {
                list21New3.add(card1.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New3.add(card3.get(i));
            }
        }else if (c==2){
            for (int i = 0; i < 7; i++) {
                list21New3.add(card1.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New3.add(card2.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New3.add(card3.get(i));
            }
        }else {
            for (int i = 0; i < 7; i++) {
                list21New3.add(card1.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New3.add(card3.get(i));
            }
            for (int i = 0; i < 7; i++) {
                list21New3.add(card2.get(i));
            }
        }

        System.out.println("好瞭,稍等片刻!讓我猜猜你記住的是什麼牌......");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("真相永遠隻有一個,你選的牌是"+list21New3.get(10)+"!");
        System.out.println("什麼?你說我是運氣好?不!這是“魔法”!");
    }
}

運行結果

其中一次運行結果:開始我記住的就是梅花K!

魔術表演步驟

值得一提的是,將21張撲克牌分成3組的具體操作,應該是:

將21張撲克牌拿在手上,按順序,從左到右依次發牌,每發3次算一輪,一共發七輪,發完就會產生3疊牌,每疊7張!

到此這篇關於Java實戰練習之撲克牌魔術的文章就介紹到這瞭,更多相關java撲克牌魔術內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: