天天看點

用Java寫的一個簡單的撲克牌魔術

一個用Java寫了的簡單的撲克牌魔術

一個用Java代碼寫的簡單的撲克牌魔術,就是常見的在心裡記一張牌,然後問你兩次範圍就知道答案,好像原理有點像二進制,小時候大人用這個魔術逗小孩開心,10幾年了,原理我也說不清楚,反正代碼可以實作,能講清楚邏輯道理的可以教教我哈,謝謝Thanks♪(・ω・)ノ大佬。代碼可以優化很多。。。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeSet;

public class 有趣的撲克牌遊戲 {

	static Map<String, Integer> map = new HashMap<String, Integer>();
	static String temp[][] = new String[3][7];

	public static void Rand() {
		String arr[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "大王", "小王" };
		String colour[] = { "紅桃", "黑桃", "梅花", "方塊" };
		while (map.size() != 21) {
			int x1 = (int) (Math.random() * 15);
			if (x1 == 13 || x1 == 14) {
				map.put(arr[x1], 1);
			} else {
				int x2 = (int) (Math.random() * 4);
				String s = "" + colour[x2] + arr[x1];
				map.put(s, 1);
			}
		}
		System.out.println("随機生成的21張牌為:");
		int count = 0;
		for (String index : map.keySet()) {
			System.out.print(index + "  ");

			temp[0 + count / 7][count % 7] = index;
			count++;
		}
		f();
	}

	public static void f() {
		int count = 0;
		for (String index : map.keySet()) {
			count++;
			if (count == 1) {
				System.out.println();
				System.out.println();

				System.out.println("區間1:你選的1" + "-7張:");

			}
			if (count == 8) {
				System.out.println();
				System.out.println();

				System.out.println("區間2:你選的8" + "-14張:");

			}
			if (count == 15) {
				System.out.println();
				System.out.println();

				System.out.println("區間3:你選的15" + "-21張:");

			}
			System.out.print(index + "  ");

		}
		System.out.println();
		System.out.println("請問你喜歡的牌是在區間____?");
		Scanner input = new Scanner(System.in);
		System.out.print("區間:");
		int n = input.nextInt();
		System.out.println("...................我是分割線......................");

		System.out.println("我把牌重新洗了一下.......");

		for (int i = 0; i <= 6; i++) { // 模拟洗牌
			String exchange = temp[n - 1][i];
			temp[n - 1][i] = temp[1][i];
			temp[1][i] = exchange;
		}
		String clone[] = new String[21];
		for (int i = 0; i < temp.length; i++) {
			for (int j = 0; j < 7; j++) {
				clone[i * 7 + j] = temp[i][j];
			}

		}
		System.out.println("然後重新分區間");
		System.out.println();
		System.out.println("區間1:");
		for (int i = 0; i <= 6; i++) {
			System.out.print(clone[20 - i * 3] + " ");
			temp[0][i] = clone[20 - i * 3];

		}
		System.out.println();
		System.out.println();
		System.out.println("區間2:");
		for (int i = 0; i <= 6; i++) {
			System.out.print(clone[19 - i * 3] + " ");
			temp[1][i] = clone[19 - i * 3];
		}
		System.out.println();
		System.out.println();
		System.out.println("區間3:");
		for (int i = 0; i <= 6; i++) {
			System.out.print(clone[18 - i * 3] + " ");
			temp[2][i] = clone[18 - i * 3];

		}

		System.out.println("請問你剛剛喜歡的牌又在區間____?");
		System.out.print("區間:");
		n = input.nextInt();

		System.out.println("...................我是分割線......................");

		System.out.println("我最後把牌重新洗了一下.......");

		for (int i = 0; i <= 6; i++) { // 模拟洗牌
			String exchange = temp[n - 1][i];
			temp[n - 1][i] = temp[1][i];
			temp[1][i] = exchange;
		}
		for (int i = 0; i < temp.length; i++) {
			for (int j = 0; j < 7; j++) {
				clone[i * 7 + j] = temp[i][j];
			}

		}
		System.out.println("然後重新分區間");
		System.out.println();
		System.out.println("區間1:");
		TreeSet<String> set = new TreeSet<>(); 
		for (int i = 0; i <= 6; i++) {
			// System.out.print(clone[20-i*3]+" ");
			set.add(clone[20 - i * 3]);
			temp[0][i] = clone[20 - i * 3];
		}
		for (String key : set) {
			System.out.print(key + " ");

		}
		set.clear();
		System.out.println();
		System.out.println();
		System.out.println("區間2:");
		for (int i = 0; i <= 6; i++) {
			// System.out.print(clone[19-i*3]+" ");
			set.add(clone[19 - i * 3]);

			temp[1][i] = clone[19 - i * 3];
		}
		for (String key : set) {
			System.out.print(key + " ");

		}
		set.clear();
		System.out.println();
		System.out.println();

		System.out.println("區間3:");
		for (int i = 0; i <= 6; i++) {
			// System.out.print(clone[18-i*3]+" ");
			set.add(clone[18 - i * 3]);
			temp[2][i] = clone[18 - i * 3];
		}
		for (String key : set) {
			System.out.print(key + " ");

		}
		set.clear();
		System.out.println("最後問你一次剛剛喜歡的牌又在區間____?");
		n = input.nextInt();
		System.out.println("你喜歡的牌極有可能是:");
		System.out.println(temp[n - 1][3]);
	}

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("請從一副撲克牌中抽21張撲克牌");
		System.out.println("輸入任意字元開始系統随機選取");
		String n = input.nextLine();
		if (true) {
			Rand(); //生成随機21張
		}

	}
}