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("", " king ",
50)); list.add(new Pai("", " Xiao Wang ", 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 = " landlord "; p2
= " Player one "; p3 = " Player 2 "; } else if (player2.size() == 20) { p1 = " Player 2 "; p2 = " landlord ";
p3= " Player one "; } else { p1 = " Player one "; p2 = " Player 2 "; p3 = " landlord "; } if (player1.size() !=
20) { } else { System.out.println("=================="); System.out.println(p1 +
" My card is " + player1); System.out.println(" please " + p1 + " play "); System.out.println(
" Please enter the card you want to play , input -1 Stop playing "); while (true) { if (paikuunderp2.size() == 0 &&
paikuunderp3.size() == 0) { paikuunderp1.clear(); } else { paikuunderp1.clear();
System.out.println(" Please select ,1. play 2. I can't afford it 3. too "); if (sc.nextLine().equals("2")) {
System.out.println(" I can't afford it "); break; }else if(sc.nextLine().equals("3")){ System.
out.println(" too "); break; } } while (true) { System.out.println(" Please enter the card to play ");
String a= sc.nextLine(); if (a.equals("-1")) { break; } else { if (!isTrue(
player1, a)) { System.out.println(" You don't have this card "); } else { Pai pai = throwcard(
player1, a); paikuplayer1.add(pai); player1.remove(pai); } } } System.out.
println(" What's the card " + paikuplayer1); if (chuPai(paikuplayer1)) { System.out.println(
" You can play "); if (daNi(paikuplayer1, paikuunderp2) && daNi(paikuplayer1, paikuunderp3
)) { paikuunderp1.addAll(paikuplayer1); paikuunder.addAll(paikuplayer1);
paikuplayer1.clear(); System.out.println(p1 + " What's the card " + paikuunderp1);
paikuunderp3.clear(); break; } } System.out.println(" Not in accordance with the rules , Please play again "); player1.
addAll(paikuplayer1); Collections.sort(player1); System.out.println(" The current library is " +
player1); paikuplayer1.clear(); } System.out.println(" In the bottom Library " + paikuunder); } if
(player2.size() != 20 && paikuunderp1.size() == 0) { } else { System.out.println
("=================="); System.out.println(p2 + " My card is " + player2); System.out.
println(" please " + p2 + " play "); System.out.println(" Please enter the card you want to play , input -1 Stop playing "); while (true)
{ if (paikuunderp1.size() == 0 && paikuunderp3.size() == 0) { paikuunderp2.clear
(); } else { paikuunderp2.clear(); System.out.println(" Please go in and out ,1. play 2. I can't afford it 3. too ");
if (sc.nextLine().equals("2")) { System.out.println(" I can't afford it "); break; }else if(sc.
nextLine().equals("3")){ System.out.println(" too "); break; } } while (true) {
System.out.println(" Please enter the card to play "); String a = sc.nextLine(); if (a.equals("-1")) {
break; } else { if (!isTrue(player2, a)) { System.out.println(" You don't have this card "); } else
{ Pai pai = throwcard(player2, a); paikuplayer2.add(pai); player2.remove(pai); }
} } System.out.println(" What's the card " + paikuplayer2); if (chuPai(paikuplayer2)) {
System.out.println(" You can play "); if (daNi(paikuplayer2, paikuunderp1) && daNi(
paikuplayer2, paikuunderp3)) { paikuunderp2.addAll(paikuplayer2); paikuunder.
addAll(paikuplayer2); paikuplayer2.clear(); System.out.println(p2 + " What's the card " +
paikuunderp2); paikuunderp1.clear(); break; } } System.out.println(" Not in accordance with the rules , Please play again "
); player2.addAll(paikuplayer2); Collections.sort(player2); System.out.println(
" The current library is " + player2); paikuplayer2.clear(); } System.out.println(" In the bottom Library " +
paikuunder); } System.out.println("=================="); System.out.println(p3 +
" My card is " + player3); System.out.println(" please " + p3 + " play "); System.out.println(
" Please enter the card you want to play , input -1 Stop playing "); while (true) { if (paikuunderp1.size() == 0 &&
paikuunderp2.size() == 0) { paikuunderp3.clear(); } else { paikuunderp3.clear();
System.out.println(" Please go in and out ,1. play 2. I can't afford it 3. too "); if (sc.nextLine().equals("2")) {
System.out.println(" I can't afford it "); break; }else if(sc.nextLine().equals("3")){ System.
out.println(" too "); break; } } while (true) { System.out.println(" Please enter the card to play ");
String a= sc.nextLine(); if (a.equals("-1")) { break; } else { if (!isTrue(
player3, a)) { System.out.println(" You don't have this card "); } else { Pai pai = throwcard(
player3, a); paikuplayer3.add(pai); player3.remove(pai); } } } System.out.
println(" What's the card " + paikuplayer3); if (chuPai(paikuplayer3)) { System.out.println(
" You can play "); if (daNi(paikuplayer3, paikuunderp2) && daNi(paikuplayer3, paikuunderp1
)) { paikuunderp3.addAll(paikuplayer3); paikuunder.addAll(paikuplayer3);
paikuplayer3.clear(); System.out.println(p3 + " What's the card " + paikuunderp3);
paikuunderp2.clear(); break; } } System.out.println(" Not in accordance with the rules , Please play again "); player3.
addAll(paikuplayer3); Collections.sort(player3); System.out.println(" The current library is " +
player3); paikuplayer3.clear(); } while (true) { System.out.println(
"=================="); System.out.println(p1 + " My card is " + player1); System.out.
println(" please " + p1 + " play "); System.out.println(" Please enter the card you want to play , input -1 Stop playing "); while (true)
{ if (paikuunderp2.size() == 0 && paikuunderp3.size() == 0) { paikuunderp1.clear
(); } else { paikuunderp1.clear(); System.out.println(" Please go in and out ,1. play 2. I can't afford it 3. too ");
if (sc.nextLine().equals("2")) { System.out.println(" I can't afford it "); break; }else if(sc.
nextLine().equals("3")){ System.out.println(" too "); break; } } while (true) {
System.out.println(" Please enter the card to play "); String a = sc.nextLine(); if (a.equals("-1")) {
break; } else { if (!isTrue(player1, a)) { System.out.println(" You don't have this card "); } else
{ Pai pai = throwcard(player1, a); paikuplayer1.add(pai); player1.remove(pai); }
} } System.out.println(" What's the card " + paikuplayer1); if (chuPai(paikuplayer1)) {
System.out.println(" You can play "); if (daNi(paikuplayer1, paikuunderp2) && daNi(
paikuplayer1, paikuunderp3)) { paikuunderp1.addAll(paikuplayer1); paikuunder.
addAll(paikuplayer1); paikuplayer1.clear(); System.out.println(p1 + " What's the card " +
paikuunderp1); paikuunderp3.clear(); break; } else { System.out.println(
" Your card is too small , I don't care "); break; } } System.out.println(" Not in accordance with the rules , Please play again "); player1.addAll(
paikuplayer1); Collections.sort(player1); System.out.println(" The current library is " + player1);
paikuplayer1.clear(); } System.out.println(" In the bottom Library " + paikuunder); if (player1.
size() == 0 || player2.size() == 0 || player3.size() == 0) { System.out.println(
" game over "); break; } System.out.println("=================="); System.out.println(
p2+ " My card is " + player2); System.out.println(" please " + p2 + " play "); System.out.println(
" Please enter the card you want to play , input -1 Stop output "); while (true) { if (paikuunderp1.size() == 0 &&
paikuunderp3.size() == 0) { paikuunderp2.clear(); } else { paikuunderp2.clear();
System.out.println(" Please go in and out ,1. play 2. I can't afford it 3. too "); if (sc.nextLine().equals("2")) {
System.out.println(" I can't afford it "); break; }else if(sc.nextLine().equals("3")){ System.
out.println(" too "); break; } } while (true) { System.out.println(" Please enter the card to play ");
String a= sc.nextLine(); if (a.equals("-1")) { break; } else { if (!isTrue(
player2, a)) { System.out.println(" You don't have this card "); } else { Pai pai = throwcard(
player2, a); paikuplayer2.add(pai); player2.remove(pai); } } } System.out.
println(" What's the card " + paikuplayer2); if (chuPai(paikuplayer2)) { System.out.println(
" You can play "); if (daNi(paikuplayer2, paikuunderp1) && daNi(paikuplayer2, paikuunderp3
)) { paikuunderp2.addAll(paikuplayer2); paikuunder.addAll(paikuplayer2);
paikuplayer2.clear(); System.out.println(p2 + " What's the card " + paikuunderp2);
paikuunderp1.clear(); break; } } System.out.println(" Not in accordance with the rules , Please play again "); player2.
addAll(paikuplayer2); Collections.sort(player2); System.out.println(" The current library is " +
player2); paikuplayer2.clear(); } System.out.println(" In the bottom Library " + paikuunder); if (
player1.size() == 0 || player2.size() == 0 || player3.size() == 0) { System.out.
println(" game over "); break; } System.out.println("=================="); System.out.
println(p3 + " My card is " + player3); System.out.println(" please " + p3 + " play "); System.out.
println(" Please enter the card you want to play , input -1 Stop playing "); while (true) { if (paikuunderp1.size() == 0 &&
paikuunderp2.size() == 0) { paikuunderp3.clear(); } else { paikuunderp3.clear();
System.out.println(" Please go in and out ,1. play 2. I can't afford it 3. too "); if (sc.nextLine().equals("2")) {
System.out.println(" I can't afford it "); break; }else if(sc.nextLine().equals("3")){ System.
out.println(" too "); break; } } while (true) { System.out.println(" Please enter the card to play ");
String a= sc.nextLine(); if (a.equals("-1")) { break; } else { if (!isTrue(
player3, a)) { System.out.println(" You don't have this card "); } else { Pai pai = throwcard(
player3, a); paikuplayer3.add(pai); player3.remove(pai); } } } System.out.
println(" What's the card " + paikuplayer3); if (chuPai(paikuplayer3)) { System.out.println(
" You can play "); if (daNi(paikuplayer3, paikuunderp2) && daNi(paikuplayer3, paikuunderp1
)) { paikuunderp3.addAll(paikuplayer3); paikuunder.addAll(paikuplayer3);
paikuplayer3.clear(); System.out.println(p3 + " What's the card " + paikuunderp3);
paikuunderp2.clear(); break; } } System.out.println(" Not in accordance with the rules , Please play again "); player3.
addAll(paikuplayer3); Collections.sort(player3); System.out.println(" The current library is " +
player3); paikuplayer3.clear(); } System.out.println(" Current pool of cards " + paikuunder); if
(player1.size() == 0 || player2.size() == 0 || player3.size() == 0) { System.out
.println(" game over "); 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(" You don't have it " + 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; }
Entity class
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(); } }
Technology