扎金花java程序个人编写

//以下是java程序
package org.mbox.zajinhua;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 此扎金花暂时只能2人玩
*
* */
public class ZaJinHua {
// 一副扑克中的13张牌的数字
static String shu[] = new String[] { "A", "K", "Q", "J", "10", "9", "8",
"7", "6", "5", "4", "3", "2" };
// 一副扑克牌中的4种花色
static String se[] = new String[] { "红桃", "黑桃", "方块", "梅花" };
// 花色和数字的组合,用来判断随机牌的时候,判断前边所发的牌是否存在,且为最后所发放的牌数
static List seshulist = new ArrayList();
// 分别为玩家1和玩家2的牌
static List pone = new ArrayList();
static List ptwo = new ArrayList();
// 扑克牌级别集合
static String bz = "豹子";
static String ths = "同花顺";
static String th = "同花";
static String sz = "顺子";
static String dz = "对子";
static String sp = "散牌";
static String[] secarray = new String[] { bz, ths, th, sz, dz, sp };

// 随机发扑克牌
public static List getListRandomseshu() {
for (int i = 0; i < 6; i++) {
getRandomseshu();
}
return seshulist;
}

// 对随机发扑克牌的处理
public static String getRandomseshu() {
String randomse = se[(int) (Math.floor(Math.random() * 10 / 3))];
String randomshu = shu[(int) (Math.floor(Math.random() * 100 / 8))];
String randomseshu = randomse + randomshu;
if (seshulist.contains(randomseshu)) {
getRandomseshu();
} else {
seshulist.add(randomseshu);
}
return randomseshu;
}

// 分别分配给两个玩家
public static void pukerPk() {
for (int i = 0; i < seshulist.size(); i++) {
if (i < (seshulist.size()) / 2) {
pone.add(seshulist.get(i));
} else {
ptwo.add(seshulist.get(i));
}
}
}

// 根据长度决定取最后的几位,数字
public static String getShu(String str) {
String newstr = null;
if (4 == str.length()) {
newstr = str.substring(2, 4);
} else if (3 == str.length()) {
newstr = str.substring(2, 3);
if ("J".equals(newstr)) {
newstr = "11";
} else if ("Q".equals(newstr)) {
newstr = "12";
} else if ("K".equals(newstr)) {
newstr = "13";
} else if ("A".equals(newstr)) {
newstr = "14";
}
}
return newstr;
}

// 把字符转为数字
public static int getIntShu(String str) {
return Integer.parseInt(str);
}

// 三张牌的总数之和
public static int getAllShu(List list) {
int j = 0;
for (int i = 0; i < list.size(); i++) {
j = j + getIntShu(getShu(list.get(i)));
}
return j;
}

// 判断两玩家的牌牌面数字对比是否相同
public static boolean oneShuvstwoShu(List shuone,
List shutwo) {
boolean b = false;
List onelist = new ArrayList();
List

ng> twolist = new ArrayList();
for (int i = 0; i < shuone.size(); i++) {
onelist.add(getShu(shuone.get(i)));
}
for (int i = 0; i < shutwo.size(); i++) {
twolist.add(getShu(shutwo.get(i)));
}
if (onelist.containsAll(twolist)) {
b = true;
} else {
b = false;
}
return b;

}

// 取得扑克牌A的花色
public static String getAse(List list) {
String strAse = null;
for (int i = 0; i < list.size(); i++) {
if ("14".equals(getShu(list.get(i)))) {
strAse = getSe(list.get(i));
break;
}
}
return strAse;
}

// 获取扑克牌花色
public static String getSe(String str) {
String newstr = null;
newstr = str.substring(0, 2);
return newstr;
}

// 判断三张是否为相邻的扑克牌
public static boolean isUpon(int a, int b, int c) {
boolean boo = false;
// 第一种情况
int a1 = Math.abs(a - b);
int b1 = Math.abs(b - c);
int c1 = Math.abs(a - c);
// 第二中情况
int a2 = Math.abs(b - a);
int b2 = Math.abs(a - c);
int c2 = Math.abs(b - c);
// 第三种情况
int a3 = Math.abs(b - c);
int b3 = Math.abs(c - a);
int c3 = Math.abs(b - a);

if ((a1 == 1 && b1 == 1 && c1 == 2) || (a2 == 1 && b2 == 1 && c2 == 2)
|| (a3 == 1 && b3 == 1 && c3 == 2)) {
boo = true;
return boo;
}
return boo;
}

// 判断扑克牌级别
public static Map> bijiaoPuker(List plist) {
Map> map = new HashMap>();
String a = getShu(plist.get(0));
String b = getShu(plist.get(1));
String c = getShu(plist.get(2));
int ai = Integer.parseInt(a);
int bi = Integer.parseInt(b);
int ci = Integer.parseInt(c);
if (a.equals(b) && a.equals(c) && b.equals(c)) {
map.put(bz, plist);
} else if (isUpon(ai, bi, ci)
&& getSe(plist.get(0)).equals(getSe(plist.get(1)))
&& getSe(plist.get(1)).equals(getSe(plist.get(2)))) {
map.put(ths, plist);
} else if (getSe(plist.get(0)).equals(getSe(plist.get(1)))
&& getSe(plist.get(1)).equals(getSe(plist.get(2)))
&& !isUpon(ai, bi, ci)) {
map.put(th, plist);
} else if (isUpon(ai, bi, ci)
&& (!getSe(plist.get(1)).equals(getSe(plist.get(0)))
|| !getSe(plist.get(0)).equals(getSe(plist.get(2))) || !getSe(
plist.get(1)).equals(getSe(plist.get(2))))) {
map.put(sz, plist);
} else if (ai == bi || ai == ci || bi == ci
&& (ai != bi || ai != ci || bi != ci)) {
map.put(dz, plist);
} else if (ai != bi && bi != ci && ci != ai) {
map.put(sp, plist);
}
return map;
}

// 如果玩家1和玩家2的牌面数字比较相同,则进行最大值花色的判断
public static int pkMaxSe(String str) {
int j = 0;
for (int i = 0; i < se.length; i++) {
if (se[i] == str) {
j = i;
break;
}
}
return j;

}

// 对扑克牌中的数字排序
public static int[] ascList(List list) {
Str

ing[] str = new String[list.size()];
String[] strarr = list.toArray(str);
int[] intarr = new int[strarr.length];
for (int i = 0; i < strarr.length; i++) {
intarr[i] = getIntShu(getShu(strarr[i]));
}
int temp;
for (int i = 0; i < intarr.length; i++) {
for (int j = i + 1; j <= intarr.length - 1; j++) {
if (intarr[i] < intarr[j]) {
temp = intarr[i];
intarr[i] = intarr[j];
intarr[j] = temp;
}
}
}

return intarr;
}

// 获取对子中相同的数
public static int getSameShu(List list) {
int temp = 0;
for (int i = 0; i < list.size() - 1; i++) {
if (getShu(list.get(i)).equals(getShu(list.get(i + 1)))) {
temp = getIntShu(getShu(list.get(i)));
break;
}
}
if (temp == 0) {
if (getShu(list.get(0)).equals(getShu(list.get(2)))) {
temp = getIntShu(getShu(list.get(0)));
}
}
return temp;
}

// 获取对子中对子相同,第三张牌不同的牌
public static String getNotSameSeShu(List list) {
String str = null;
for (int i = 0; i < list.size(); i++) {
if (getSameShu(list) != getIntShu(getShu(list.get(i)))) {
str = list.get(i);
break;
}
}
return str;

}

// 获取牌中最大数的花色
public static String getMaxShuSe(List list) {
String temp = getShu(list.get(0));
for (int i = 0; i < list.size() - 1; i++) {
if (getIntShu(temp) < getIntShu(getShu(list.get(i + 1)))) {
temp = getShu(list.get(i + 1));
} else {
temp = temp;
}
}
return temp;

}

// 获取最大牌中的最大数
public static int getMaxShu(List list) {
int j = getIntShu(list.get(0));
for (int i = 0; i < list.size() - 1; i++) {
if (j < getIntShu(list.get(i + 1))) {
j = getIntShu(list.get(i + 1));
}
}
return j;
}

// 根据扑克级别返回不同的int值来进行判断扑克级别大小
public static int pkSec(Map> map) {
int i = 0;
String[] str = new String[map.keySet().size()];
String[] strarr = map.keySet().toArray(str);
for (int j = 0; j < strarr.length; j++) {
for (int sec = 0; sec < secarray.length; sec++) {
if (strarr[j].equals(secarray[sec])) {
i = sec;
break;
}

}
}
return i;

}

// 根据玩家1和玩家2的扑克级别以及数字和花色来判断大小
public static String panduanDaxiao(Map> ponemap,
Map> ptwomap) {
// 根据级别判断
if (pkSec(ponemap) > pkSec(ptwomap)) {
System.out.println("玩家2胜出");
} else if (pkSec(ponemap) < pkSec(ptwomap)) {
System.out.println("玩家1胜出");
// 级别为豹子的情况下判断
} else if (pkSec(ponemap) == pkSec(ptwomap) && pkSec(ponemap) == 0) {
if (Integer.parseInt(getShu(pone.get(0))) > Integer
.parseInt(getShu(ptwo.get(0)))) {
System.out.println("玩家1胜出");
} else {
System.out.println("

玩家2胜出");
}
// 级别为同花顺的情况下判断
} else if (pkSec(ponemap) == pkSec(ptwomap) && pkSec(ponemap) == 1) {
if (getAllShu(pone) == 19 && getAllShu(ptwo) != 19) {
System.out.println("玩家2胜出");
} else if (getAllShu(pone) != 19 && getAllShu(ptwo) == 19) {
System.out.println("玩家1胜出");
} else if (getAllShu(pone) == getAllShu(ptwo)
&& getAllShu(pone) == 19) {
if (pkMaxSe(getAse(pone)) > pkMaxSe(getAse(ptwo))) {
System.out.println("玩家2胜出");
} else if (pkMaxSe(getAse(pone)) < pkMaxSe(getAse(ptwo))) {
System.out.println("玩家1胜出");
}
} else if (getAllShu(pone) > getAllShu(ptwo)
&& getAllShu(pone) != 19 && getAllShu(ptwo) != 19) {
System.out.println("玩家1胜出");
} else if (getAllShu(pone) < getAllShu(ptwo)
&& getAllShu(pone) != 19 && getAllShu(ptwo) != 19) {
System.out.println("玩家2胜出");
} else if (getAllShu(pone) == getAllShu(ptwo)
&& getAllShu(pone) != 19) {
if (pkMaxSe(getMaxShuSe(pone)) > pkMaxSe(getMaxShuSe(ptwo))) {
System.out.println("玩家2获胜");
} else if (pkMaxSe(getMaxShuSe(pone)) < pkMaxSe(getMaxShuSe(ptwo))) {
System.out.println("玩家1获胜");
}
}
// 级别同花的情况进行判断
} else if (pkSec(ponemap) == pkSec(ptwomap) && pkSec(ponemap) == 2) {
if (oneShuvstwoShu(pone, ptwo)) {
if (pkMaxSe(getMaxShuSe(pone)) > pkMaxSe(getMaxShuSe(ptwo))) {
System.out.println("玩家2获胜");
} else if (pkMaxSe(getMaxShuSe(pone)) < pkMaxSe(getMaxShuSe(ptwo))) {
System.out.println("玩家1获胜");
}
} else {
if (ascList(pone)[0] > ascList(ptwo)[0]) {
System.out.println("玩家1胜出");
} else if (ascList(pone)[0] == ascList(ptwo)[0]) {
if (ascList(pone)[1] > ascList(ptwo)[1]) {
System.out.println("玩家1胜出");
} else if (ascList(pone)[1] == ascList(ptwo)[1]) {
if (ascList(pone)[2] > ascList(ptwo)[2]) {
System.out.println("玩家1胜出");
} else if (ascList(pone)[2] < ascList(ptwo)[2]) {
System.out.println("玩家2胜出");
}
} else if (ascList(pone)[1] < ascList(ptwo)[1]) {
System.out.println("玩家2胜出");
}
} else if (ascList(pone)[0] < ascList(ptwo)[0]) {
System.out.println("玩家2胜出");
}
}
// 级别是顺子的情况进行判断
} else if (pkSec(ponemap) == pkSec(ptwomap) && pkSec(ponemap) == 3) {
if (getAllShu(pone) == 19 && getAllShu(ptwo) != 19) {
System.out.println("玩家2胜出");
} else if (getAllShu(pone) != 19 && getAllShu(ptwo) == 19) {
System.out.println("玩家1胜出");
} else if (getAllShu(pone) == getAllShu(ptwo)
&& getAllShu(pone) == 19) {
if (pkMaxSe(getAse(pone)) > pkMaxSe(getAse(ptwo))) {
System.out.println("玩家2胜出");
} else if (pkMaxSe(getAse(pone)) < pkMaxSe(getAse(ptwo))) {
System.out.println("玩

家1胜出");
}
} else if (getAllShu(pone) > getAllShu(ptwo)
&& getAllShu(pone) != 19 && getAllShu(ptwo) != 19) {
System.out.println("玩家1胜出");
} else if (getAllShu(pone) < getAllShu(ptwo)
&& getAllShu(pone) != 19 && getAllShu(ptwo) != 19) {
System.out.println("玩家2胜出");
} else if (getAllShu(pone) == getAllShu(ptwo)
&& getAllShu(pone) != 19) {
if (pkMaxSe(getMaxShuSe(pone)) > pkMaxSe(getMaxShuSe(ptwo))) {
System.out.println("玩家2获胜");
} else if (pkMaxSe(getMaxShuSe(pone)) < pkMaxSe(getMaxShuSe(ptwo))) {
System.out.println("玩家1获胜");
}
}
// 级别为对子的情况下进行判断
} else if (pkSec(ponemap) == pkSec(ptwomap) && pkSec(ponemap) == 4) {
if (getSameShu(pone) > getSameShu(ptwo)) {
System.out.println("玩家1获胜");
} else if (getSameShu(pone) < getSameShu(ptwo)) {
System.out.println("玩家2获胜");
} else if (getSameShu(pone) == getSameShu(ptwo)) {
if (getIntShu(getShu(getNotSameSeShu(pone))) > getIntShu(getShu(getNotSameSeShu(ptwo)))) {
System.out.println("玩家1胜出");
} else if (getIntShu(getShu(getNotSameSeShu(pone))) < getIntShu(getShu(getNotSameSeShu(ptwo)))) {
System.out.println("玩家2胜出");
} else if (getIntShu(getShu(getNotSameSeShu(pone))) == getIntShu(getShu(getNotSameSeShu(ptwo)))) {
if (pkMaxSe(getSe(getNotSameSeShu(pone))) > pkMaxSe(getSe(getNotSameSeShu(ptwo)))) {
System.out.println("玩家2胜出");
} else {
System.out.println("玩家1胜出");
}
}
}
// 级别为散牌的进行判断
} else if (pkSec(ponemap) == pkSec(ptwomap) && pkSec(ponemap) == 5) {
if (oneShuvstwoShu(pone, ptwo)) {
if (pkMaxSe(getMaxShuSe(pone)) > pkMaxSe(getMaxShuSe(ptwo))) {
System.out.println("玩家2获胜");
} else if (pkMaxSe(getMaxShuSe(pone)) < pkMaxSe(getMaxShuSe(ptwo))) {
System.out.println("玩家1获胜");
}
} else {
if (ascList(pone)[0] > ascList(ptwo)[0]) {
System.out.println("玩家1胜出");
} else if (ascList(pone)[0] == ascList(ptwo)[0]) {
if (ascList(pone)[1] > ascList(ptwo)[1]) {
System.out.println("玩家1胜出");
} else if (ascList(pone)[1] == ascList(ptwo)[1]) {
if (ascList(pone)[2] > ascList(ptwo)[2]) {
System.out.println("玩家1胜出");
} else if (ascList(pone)[2] < ascList(ptwo)[2]) {
System.out.println("玩家2胜出");
}
} else if (ascList(pone)[1] < ascList(ptwo)[1]) {
System.out.println("玩家2胜出");
}
} else if (ascList(pone)[0] < ascList(ptwo)[0]) {
System.out.println("玩家2胜出");
}
}
}
return null;

}

public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println("第"+(i+1)+"次发牌");
getListRandomseshu();
pukerPk();
System.out.println("玩家1牌为:" + bijiaoPuker(pone));
System.

out.println("玩家2牌为:" + bijiaoPuker(ptwo));
Map> map1 = bijiaoPuker(pone);
Map> map2 = bijiaoPuker(ptwo);
panduanDaxiao(map1, map2);
seshulist.clear();
pone.clear();
ptwo.clear();
System.out.println("-------------------------------------");
}

}

}

相关主题
相关文档
最新文档