一个用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张
}
}
}