public class Doudizhu { public static void main(String[] args) { Scanner sc =
new Scanner(System.in); String[] huashe1 = {"♥", "♠", "♦", "♣"}; int[] daxiao1 =
{3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; String[] pai1 = {"3", "4", "5",
"6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"}; ArrayList<Pai> list = new
ArrayList<>(); for (String s : huashe1) { for (int i = 0; i < daxiao1.length; i
++) { list.add(new Pai(s, pai1[i], daxiao1[i])); } } list.add(new Pai("", "大王",
50)); list.add(new Pai("", "小王", 20)); Collections.shuffle((List<?>) list);
ArrayList<Pai> player1 = new ArrayList<>(); ArrayList<Pai> player2 = new
ArrayList<>(); ArrayList<Pai> player3 = new ArrayList<>(); ArrayList<Pai> under
= new ArrayList<>(); for (int i = 0; i < list.size(); i++) { if (i >= list.size(
) - 3) { under.add(list.get(i)); } else { if (i % 3 == 0) { player1.add(list.get
(i)); } else if (i % 3 == 1) { player2.add(list.get(i)); } else { player3.add(
list.get(i)); } } } System.out.println(under); int r = new Random().nextInt(51);
if (r % 3 == 0) { player1.addAll(under); } else if (r % 3 == 1) { player2.addAll
(under); } else { player3.addAll(under); } Collections.sort(player1);
Collections.sort(player2); Collections.sort(player3); System.out.println(player1
); System.out.println(player2); System.out.println(player3); ArrayList<Pai>
paikuplayer1= new ArrayList<>(); ArrayList<Pai> paikuplayer2 = new ArrayList<>()
; ArrayList<Pai> paikuplayer3 = new ArrayList<>(); ArrayList<Pai> paikuunderp1 =
new ArrayList<>(); ArrayList<Pai> paikuunderp2 = new ArrayList<>(); ArrayList<
Pai> paikuunderp3 = new ArrayList<>(); ArrayList<Pai> paikuunder = new ArrayList
<>(); String p1; String p2; String p3; if (player1.size() == 20) { p1 = "地主"; p2
= "玩家一"; p3 = "玩家二"; } else if (player2.size() == 20) { p1 = "玩家二"; p2 = "地主";
p3= "玩家一"; } else { p1 = "玩家一"; p2 = "玩家二"; p3 = "地主"; } if (player1.size() !=
20) { } else { System.out.println("=================="); System.out.println(p1 +
"的牌为" + player1); System.out.println("请" + p1 + "出牌"); System.out.println(
"请输入想要出的牌,输入-1停止出牌"); while (true) { if (paikuunderp2.size() == 0 &&
paikuunderp3.size() == 0) { paikuunderp1.clear(); } else { paikuunderp1.clear();
System.out.println("请选择,1.出牌 2.要不起 3.过"); if (sc.nextLine().equals("2")) {
System.out.println("要不起"); break; }else if(sc.nextLine().equals("3")){ System.
out.println("过"); break; } } while (true) { System.out.println("请输入要出的牌");
String a= sc.nextLine(); if (a.equals("-1")) { break; } else { if (!isTrue(
player1, a)) { System.out.println("您没有这张牌"); } else { Pai pai = throwcard(
player1, a); paikuplayer1.add(pai); player1.remove(pai); } } } System.out.
println("出的牌为" + paikuplayer1); if (chuPai(paikuplayer1)) { System.out.println(
"可以出牌"); if (daNi(paikuplayer1, paikuunderp2) && daNi(paikuplayer1, paikuunderp3
)) { paikuunderp1.addAll(paikuplayer1); paikuunder.addAll(paikuplayer1);
paikuplayer1.clear(); System.out.println(p1 + "出的牌为" + paikuunderp1);
paikuunderp3.clear(); break; } } System.out.println("不符合规则,请重新出牌"); player1.
addAll(paikuplayer1); Collections.sort(player1); System.out.println("当前牌库为" +
player1); paikuplayer1.clear(); } System.out.println("底库中牌为" + paikuunder); } if
(player2.size() != 20 && paikuunderp1.size() == 0) { } else { System.out.println
("=================="); System.out.println(p2 + "的牌为" + player2); System.out.
println("请" + p2 + "出牌"); System.out.println("请输入想要出的牌,输入-1停止出牌"); while (true)
{ if (paikuunderp1.size() == 0 && paikuunderp3.size() == 0) { paikuunderp2.clear
(); } else { paikuunderp2.clear(); System.out.println("请出入选择,1.出牌 2.要不起 3.过");
if (sc.nextLine().equals("2")) { System.out.println("要不起"); break; }else if(sc.
nextLine().equals("3")){ System.out.println("过"); break; } } while (true) {
System.out.println("请输入要出的牌"); String a = sc.nextLine(); if (a.equals("-1")) {
break; } else { if (!isTrue(player2, a)) { System.out.println("您没有这张牌"); } else
{ Pai pai = throwcard(player2, a); paikuplayer2.add(pai); player2.remove(pai); }
} } System.out.println("出的牌为" + paikuplayer2); if (chuPai(paikuplayer2)) {
System.out.println("可以出牌"); if (daNi(paikuplayer2, paikuunderp1) && daNi(
paikuplayer2, paikuunderp3)) { paikuunderp2.addAll(paikuplayer2); paikuunder.
addAll(paikuplayer2); paikuplayer2.clear(); System.out.println(p2 + "出的牌为" +
paikuunderp2); paikuunderp1.clear(); break; } } System.out.println("不符合规则,请重新出牌"
); player2.addAll(paikuplayer2); Collections.sort(player2); System.out.println(
"当前牌库为" + player2); paikuplayer2.clear(); } System.out.println("底库中牌为" +
paikuunder); } System.out.println("=================="); System.out.println(p3 +
"的牌为" + player3); System.out.println("请" + p3 + "出牌"); System.out.println(
"请输入想要出的牌,输入-1停止出牌"); while (true) { if (paikuunderp1.size() == 0 &&
paikuunderp2.size() == 0) { paikuunderp3.clear(); } else { paikuunderp3.clear();
System.out.println("请出入选择,1.出牌 2.要不起 3.过"); if (sc.nextLine().equals("2")) {
System.out.println("要不起"); break; }else if(sc.nextLine().equals("3")){ System.
out.println("过"); break; } } while (true) { System.out.println("请输入要出的牌");
String a= sc.nextLine(); if (a.equals("-1")) { break; } else { if (!isTrue(
player3, a)) { System.out.println("您没有这张牌"); } else { Pai pai = throwcard(
player3, a); paikuplayer3.add(pai); player3.remove(pai); } } } System.out.
println("出的牌为" + paikuplayer3); if (chuPai(paikuplayer3)) { System.out.println(
"可以出牌"); if (daNi(paikuplayer3, paikuunderp2) && daNi(paikuplayer3, paikuunderp1
)) { paikuunderp3.addAll(paikuplayer3); paikuunder.addAll(paikuplayer3);
paikuplayer3.clear(); System.out.println(p3 + "出的牌为" + paikuunderp3);
paikuunderp2.clear(); break; } } System.out.println("不符合规则,请重新出牌"); player3.
addAll(paikuplayer3); Collections.sort(player3); System.out.println("当前牌库为" +
player3); paikuplayer3.clear(); } while (true) { System.out.println(
"=================="); System.out.println(p1 + "的牌为" + player1); System.out.
println("请" + p1 + "出牌"); System.out.println("请输入想要出的牌,输入-1停止出牌"); while (true)
{ if (paikuunderp2.size() == 0 && paikuunderp3.size() == 0) { paikuunderp1.clear
(); } else { paikuunderp1.clear(); System.out.println("请出入选择,1.出牌 2.要不起 3.过");
if (sc.nextLine().equals("2")) { System.out.println("要不起"); break; }else if(sc.
nextLine().equals("3")){ System.out.println("过"); break; } } while (true) {
System.out.println("请输入要出的牌"); String a = sc.nextLine(); if (a.equals("-1")) {
break; } else { if (!isTrue(player1, a)) { System.out.println("您没有这张牌"); } else
{ Pai pai = throwcard(player1, a); paikuplayer1.add(pai); player1.remove(pai); }
} } System.out.println("出的牌为" + paikuplayer1); if (chuPai(paikuplayer1)) {
System.out.println("可以出牌"); if (daNi(paikuplayer1, paikuunderp2) && daNi(
paikuplayer1, paikuunderp3)) { paikuunderp1.addAll(paikuplayer1); paikuunder.
addAll(paikuplayer1); paikuplayer1.clear(); System.out.println(p1 + "出的牌为" +
paikuunderp1); paikuunderp3.clear(); break; } else { System.out.println(
"你的牌太小,管不上"); break; } } System.out.println("不符合规则,请重新出牌"); player1.addAll(
paikuplayer1); Collections.sort(player1); System.out.println("当前牌库为" + player1);
paikuplayer1.clear(); } System.out.println("底库中牌为" + paikuunder); if (player1.
size() == 0 || player2.size() == 0 || player3.size() == 0) { System.out.println(
"游戏结束"); break; } System.out.println("=================="); System.out.println(
p2+ "的牌为" + player2); System.out.println("请" + p2 + "出牌"); System.out.println(
"请输入想要出的牌,输入-1停止输出"); while (true) { if (paikuunderp1.size() == 0 &&
paikuunderp3.size() == 0) { paikuunderp2.clear(); } else { paikuunderp2.clear();
System.out.println("请出入选择,1.出牌 2.要不起 3.过"); if (sc.nextLine().equals("2")) {
System.out.println("要不起"); break; }else if(sc.nextLine().equals("3")){ System.
out.println("过"); break; } } while (true) { System.out.println("请输入要出的牌");
String a= sc.nextLine(); if (a.equals("-1")) { break; } else { if (!isTrue(
player2, a)) { System.out.println("您没有这张牌"); } else { Pai pai = throwcard(
player2, a); paikuplayer2.add(pai); player2.remove(pai); } } } System.out.
println("出的牌为" + paikuplayer2); if (chuPai(paikuplayer2)) { System.out.println(
"可以出牌"); if (daNi(paikuplayer2, paikuunderp1) && daNi(paikuplayer2, paikuunderp3
)) { paikuunderp2.addAll(paikuplayer2); paikuunder.addAll(paikuplayer2);
paikuplayer2.clear(); System.out.println(p2 + "出的牌为" + paikuunderp2);
paikuunderp1.clear(); break; } } System.out.println("不符合规则,请重新出牌"); player2.
addAll(paikuplayer2); Collections.sort(player2); System.out.println("当前牌库为" +
player2); paikuplayer2.clear(); } System.out.println("底库中牌为" + paikuunder); if (
player1.size() == 0 || player2.size() == 0 || player3.size() == 0) { System.out.
println("游戏结束"); break; } System.out.println("=================="); System.out.
println(p3 + "的牌为" + player3); System.out.println("请" + p3 + "出牌"); System.out.
println("请输入想要出的牌,输入-1停止出牌"); while (true) { if (paikuunderp1.size() == 0 &&
paikuunderp2.size() == 0) { paikuunderp3.clear(); } else { paikuunderp3.clear();
System.out.println("请出入选择,1.出牌 2.要不起 3.过"); if (sc.nextLine().equals("2")) {
System.out.println("要不起"); break; }else if(sc.nextLine().equals("3")){ System.
out.println("过"); break; } } while (true) { System.out.println("请输入要出的牌");
String a= sc.nextLine(); if (a.equals("-1")) { break; } else { if (!isTrue(
player3, a)) { System.out.println("您没有这张牌"); } else { Pai pai = throwcard(
player3, a); paikuplayer3.add(pai); player3.remove(pai); } } } System.out.
println("出的牌为" + paikuplayer3); if (chuPai(paikuplayer3)) { System.out.println(
"可以出牌"); if (daNi(paikuplayer3, paikuunderp2) && daNi(paikuplayer3, paikuunderp1
)) { paikuunderp3.addAll(paikuplayer3); paikuunder.addAll(paikuplayer3);
paikuplayer3.clear(); System.out.println(p3 + "出的牌为" + paikuunderp3);
paikuunderp2.clear(); break; } } System.out.println("不符合规则,请重新出牌"); player3.
addAll(paikuplayer3); Collections.sort(player3); System.out.println("当前牌库为" +
player3); paikuplayer3.clear(); } System.out.println("当前的底牌池" + paikuunder); if
(player1.size() == 0 || player2.size() == 0 || player3.size() == 0) { System.out
.println("游戏结束"); break; } } } public static int sameNum(ArrayList<Pai> list) {
int max = 0; for (int i = 0; i < list.size(); i++) { int num = 0; for (int j = 0
; j < list.size(); j++) { if (list.get(i).getId() == list.get(j).getId()) { num
++; } } max = max >= num ? max : num; } return max; } public static boolean
chuPai(ArrayList<Pai> list) { switch (list.size()) { case 1: return true; case 2
: if (list.get(0).getId() == list.get(1).getId()) { return true; } else if (list
.get(0).getId() + list.get(1).getId() == 70) { return true; } else { return
false; } case 3: if (sameNum(list)==3) { return true; } else return false; case
4: if (sameNum(list) == 4 || sameNum(list) == 3) { return true; } else return
false; case 5: if (sameNum(list) == 1 && (max(list) - min(list) == list.size() -
1)) { return true; } else if (sameNum(list) == 3) { for (int i = 0; i < list.
size(); i++) { if (theSameNum(list, list.get(i).getNum()) < 2) { return false; }
} return true; } case 6: if (sameNum(list) == 1 && (max(list) - min(list) ==
list.size() - 1)) { return true; } else if (sameNum(list) == 4) { for (int i = 0
; i < list.size(); i++) { if (theSameNum(list, list.get(i).getNum()) == 2) {
return false; } } return true; } else if (sameNum(list) == 2 && (max(list) - min
(list)) * 2 + 2 == list.size() && max(list) != 15) { return true; } else return
false; case 8: if (sameNum(list) == 1 && (max(list) - min(list) == list.size() -
1)) { return true; } else if (sameNum(list) == 4) { for (int i = 0; i < list.
size(); i++) { if (theSameNum(list, list.get(i).getNum()) == 2) { return true; }
} return false; } else if (sameNum(list)==2 && (max(list) - min(list)) * 2 + 2
== list.size() && max(list) != 15) { return true; } else if (sameNum(list) == 3
&& noSameNum(list) == 4 && (whichNumSame(list, 3).get(1) - whichNumSame(list, 3)
.get(0) == 1) && max(list) != 15) { for (int i = 0; i < list.size(); i++) { if (
theSameNum(list, list.get(i).getNum()) == 2) { return false; } } return true; }
else if (sameNum(list) == 3 && noSameNum(list) == 3 && (whichNumSame(list, 3).
get(1) - whichNumSame(list, 3).get(0) == 1) && max(list) != 15) { return true; }
else return false; default: if (sameNum(list) == 1 && (max(list) - min(list) ==
list.size() - 1) && max(list) < 15) { return true; } else if (list.size() % 2 ==
0&&sameNum(list)==2 && (max(list) - min(list)) * 2 + 2 == list.size() && max(
list) != 15) { return true; } else if (list.size() == 10 && sameNum(list) == 3
&& noSameNum(list) == 4 && (whichNumSame(list, 3).get(1) - whichNumSame(list, 3)
.get(0) == 1) && max(list) != 15) { for (int i = 0; i < list.size(); i++) { if (
theSameNum(list, list.get(i).getNum()) == 1) { return false; } } return true; }
else if (list.size() == 12 && sameNum(list) == 3 && noSameNum(list) == 6 && (
whichNumSame(list, 3).get(2) - whichNumSame(list, 3).get(0) == 2) && max(list)
!= 15) { for (int i = 0; i < list.size(); i++) { if (theSameNum(list, list.get(i
).getNum()) == 2) { return false; } } return true; } else if (list.size() == 15
&& sameNum(list) == 3 && noSameNum(list) == 6 && (whichNumSame(list, 3).get(2) -
whichNumSame(list, 3).get(0) == 2) && max(list) != 15) { for (int i = 0; i <
list.size(); i++) { if (theSameNum(list, list.get(i).getNum()) == 1) { return
false; } } return true; } else if (list.size() == 16 && sameNum(list) == 3 &&
noSameNum(list) == 8 && (whichNumSame(list, 3).get(2) - whichNumSame(list, 3).
get(0) == 2) && max(list) != 15) { for (int i = 0; i < list.size(); i++) { if (
theSameNum(list, list.get(i).getNum()) == 1) { return false; } } return true; }
return false; } } public static int max(ArrayList<Pai> list) { int max = 0; for
(int i = 0; i < list.size(); i++) { max = max >= list.get(i).getId() ? max :
list.get(i).getId(); } return max; } public static int min(ArrayList<Pai> list)
{ int min = list.get(0).getId(); for (int i = 0; i < list.size(); i++) { min =
min<= list.get(i).getId() ? min : list.get(i).getId(); } return min; } public
static int theSameNum(ArrayList<Pai> list, String a) { int num = 0; for (int i =
0; i < list.size(); i++) { if (list.get(i).getNum().equals(a)) { num++; } }
return num; } public static Pai throwcard(ArrayList<Pai> list, String a) { for (
int i = 0; i < list.size(); i++) { if (list.get(i).getNum().equals(a)) { return
list.get(i); } } System.out.println("您手中没有" + a ); return null; } public static
boolean daNi(ArrayList<Pai> list, ArrayList<Pai> list1) { if ((list.size() >= 2
&& list.get(0).getId() + list.get(1).getId() == 70) || list1.size() == 0) return
true; if (list1.size() == 2 && list1.get(0).getId() + list1.get(1).getId() == 70
) return false; if (list.size() == list1.size()) { switch (list1.size()) { case
1: return max(list) > max(list1); case 2: return max(list) > max(list1); case 3:
return max(list) > max(list1); case 4: if (sameNum(list) == 4 && sameNum(list1)
== 4 && max(list) > max(list1)) { return true; } else if (sameNum(list) == 4) {
return true; } else if (sameNum(list) == 3 && sameNum(list1) == 3) { return (
list.get(0).getId() == list.get(1).getId() ? list.get(0).getId() : list.get(2).
getId()) > (list1.get(0).getId() == list1.get(1).getId() ? list1.get(0).getId()
: list1.get(2).getId()); } return false; case 5: if (sameNum(list) == sameNum(
list1) && sameNum(list) == 1) { return max(list) > max(list1); } else { return (
theSameNum(list, idtoNum(list, max(list))) == 3 ? max(list) : min(list)) > (
theSameNum(list1, idtoNum(list1, max(list1))) == 3 ? max(list1) : min(list1)); }
case 6: if (sameNum(list) == sameNum(list1) && sameNum(list) == 1) { return max(
list) > max(list1); } else if (sameNum(list) == sameNum(list1) && sameNum(list)
== 2) { return max(list) > max(list1); } else { return (theSameNum(list, idtoNum
(list, max(list))) == 4 ? max(list) : min(list)) > (theSameNum(list1, idtoNum(
list1, max(list1))) == 4 ? max(list1) : min(list1)); } default: if (sameNum(list
) == sameNum(list1) && sameNum(list) == 1) { return max(list) > max(list1); }
else if (sameNum(list) == sameNum(list1) && sameNum(list) == 2) { return max(
list) > max(list1); } else if (sameNum(list) == sameNum(list1) && sameNum(list)
== 3) { return whichNumSame(list, 3).get(0) > whichNumSame(list1, 3).get(1); }
else if (list.size() == 10 && sameNum(list) == 3) { return whichNumSame(list, 3)
.get(0) > whichNumSame(list1, 3).get(1); } else if (list.size() == 12 && sameNum
(list) == 3) { return whichNumSame(list, 3).get(0) > whichNumSame(list1, 3).get(
2); } else if (list.size() == 15 && sameNum(list) == 3) { return whichNumSame(
list, 3).get(0) > whichNumSame(list1, 3).get(3); } else if (list.size() == 16 &&
sameNum(list) == 3) { return whichNumSame(list, 3).get(0) > whichNumSame(list1,
3).get(3); } else return false; } } else { if (list.size() == 4 && sameNum(list)
== 4) { return true; } else if (list1.size() == 4 && sameNum(list1) == 4) {
return false; } else return false; } } public static boolean isTrue(ArrayList<
Pai> list, String a) { for (int i = 0; i < list.size(); i++) { if (list.get(i).
getNum().equals(a)) { return true; } } return false; } public static String
idtoNum(ArrayList<Pai> list, int id) { for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId() == id) { return list.get(i).getNum(); } } return ""; }
public static int noSameNum(ArrayList<Pai> list) { HashSet<Integer> set = new
HashSet<>(); for (int i = 0; i < list.size(); i++) { set.add(list.get(i).getId()
); } return set.size(); } public static ArrayList<Integer> whichNumSame(
ArrayList<Pai> list, int a) { ArrayList<Integer> li = new ArrayList<>(); for (
int i = 0; i < list.size(); i++) { if (theSameNum(list, list.get(i).getNum()) ==
a&& !li.contains(list.get(i).getId())) { li.add(list.get(i).getId()); } }
return li; }
实体类
public class Pai implements Comparable<Pai>{ private String name ; private
String num; private int id ; public Pai(String name, String num, int id) { this.
name= name; this.num = num; this.id = id; } public Pai() { } public String
getName() { return name; } public void setName(String name) { this.name = name;
} public String getNum() { return num; } public void setNum(String num) { this.
num= num; } public int getId() { return id; } public void setId(int id) { this.
id= id; } @Override public String toString() { return name+num; } @Override
public int compareTo(Pai o) { return this.getId()-o.getId(); } }

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信