import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class 炸金花 {
public static void main(String[] args) {
while(true){
炸金花();
System.out.println("\n\n\t>>>>>>>>>>再来一局~<<<<<<<<<<<");}
}
public static void 炸金花() {
HashMap<Integer, String> puke = new HashMap<>();
ArrayList<Integer> xuhao = new ArrayList<>();
String[] num = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
"Q", "K", "A" };
String[] color = { "梅花", "红桃", "方片", "红桃", };
int count = 0;
for (int i = 0; i < num.length; i++) {// 设置扑克
for (int j = 0; j < color.length; j++) {
puke.put(count, color[j].concat(num[i]));
xuhao.add(count++);
}
}
Collections.shuffle(xuhao); // 洗牌
ArrayList<Integer> me = new ArrayList<>(); // 个人容器
ArrayList<Integer> comp = new ArrayList<>();
for (int i = 0; i < 6; i++) { // 分牌
if (i % 2 == 0)
me.add(xuhao.get(i));
else
comp.add(xuhao.get(i));
}
System.out.println("你的牌:");
for (Integer r : me) {
System.out.println("\t" + puke.get(r));
}
Scanner sc = new Scanner(System.in);
int flag;
zhajinhua zjh = new zhajinhua();
int mel = zjh.level(me, puke); // 我的牌等级
int compl = zjh.level(comp, puke); // 电脑的等级
if (mel < compl) flag=1;
else if (mel > compl) flag=2;
else {
int a = zjh.check(mel, me, comp, puke);
if (a == 1) flag=1;
else if (a == -1) flag=2;
else flag=0;
}
String[] jieguo={"平局","你赢了","你输了"};
hehe: while (true) {
System.out.println("1 下一注 2 比比看 3 亮牌 4 下一局 5 退出");
String ax = sc.nextLine();
try {
int ac = Integer.parseInt(ax);
switch (ac) {
case 1:
System.out.println("\t\t 我跟! : 对方");
break;
case 2:
System.out.println(jieguo[flag]);
System.out.println("看对方的牌吗? 1 看 2 不看");
while (true) {
try {
String ag=sc.nextLine();
int ap = Integer.parseInt(ag);
switch (ap) {
case 1:
System.out.println("对方的牌:");
for (Integer r : comp) {
System.out.println("\t"+puke.get(r));
}
System.out.println("\t\tGameOver!");
break hehe;
case 2:
System.out.println("\t\tGameOver!");
break hehe;
}
} catch (Exception e) {
System.out.println("输入错误,重新输入:");
}
}
case 3:
System.out.println("对方的牌:");
for (Integer r : comp) {
System.out.println("\t"+puke.get(r));
}
System.out.println(jieguo[flag]);
System.out.println(">>>>>>>>>>>>>>>>GameOver!<<<<<<<<<<<<<<<<<<<");
break hehe;
case 4:
System.out.println(">>>>>>>>>>>>>>>>GameOver!<<<<<<<<<<<<<<<<<<<!");
return;
case 5:
System.exit(0);
}// switch
} catch (Exception e) {
System.out.println("输入错误,再输一次:");
}
}
}
}
class zhajinhua {
public int level(ArrayList<Integer> a, HashMap<Integer, String> b) { // 传入序号列表,和映射表
int[] p = { a.get(0), a.get(1), a.get(2) }; // 数组存放序号表
Arrays.sort(p); // 排序
int level; // 建立等级
String[] s = new String[3]; // 存放三张牌的花色
for (int i = 0; i < 3; i++) { // 获取花色
s[i] = b.get(p[i]).substring(0, 2);
}
if (p[0] / 4 == p[1] / 4 && p[1] / 4 == p[2] / 4)
level = 1; // 三个相同
else if ((p[2] / 4 - p[1] / 4 == 1 && p[1] / 4 - p[0] / 4 == 1)
&& (s[0].equals(s[1]) && s[0].equals(s[1])))
level = 2; // 拖拉机 同花
else if (p[2] / 4 - p[1] / 4 == 1 && p[1] / 4 - p[0] / 4 == 1)
level = 3; // 拖拉机
else if (s[0].equals(s[1]) && s[0].equals(s[2]))
level = 4; // 同花色 <不会有对子>
else if (p[0] / 4 == p[1] / 4 || p[0] / 4 == p[2] / 4
|| p[1] / 4 == p[2] / 4)
level = 5;// 对子
else
level = 6; // 普通
return level;
}
public int check(int i, ArrayList<Integer> a, ArrayList<Integer> b,
HashMap<Integer, String> c) {
int[] a1 = { a.get(0) / 4, a.get(1) / 4, a.get(2) / 4 }; // 数组存放序号表
Arrays.sort(a1);
int[] a2 = { b.get(0) / 4, b.get(1) / 4, b.get(2) / 4 }; // 数组存放序号表
Arrays.sort(a2);
if (i == 1) { // 炸
return a1[0] > a2[0] ? 1 : a1[0] == a2[0] ? 0 : -1;
} else if (i == 2||i==3) { // 同花色+拖拉机 //拖拉机
return a1[2] > a2[2] ? 1 : a1[2] == a2[2] ? 0 : -1;
}
else if (i == 4||i==6) { // 同花色 // 普通
return a1[2] > a2[2] ? 1 : a1[2] < a2[2] ? -1 :a1[1] > a2[1]?1:a1[1] < a2[1]?-1: a1[0] > a2[0]?1:a1[0] < a2[0]?-1:0 ;
}
else { //对子
int[] x=getSame(a1);
int[] y=getSame(a2);
if(x[0]>y[0])return 1;
else if(x[0]<y[0])return -1;
else{
if(x[1]>y[1]) return 1;
else return -1;
}
}
}
public int[] getSame(int[] a){
if(a[0]==a[1]){
int[] b={a[0],a[1]};
return b;
}
else{
int[] c={a[2],a[0]};
return c;
}
}
}
|
|