斗地主案例 Console version
package pers.landlord_fighting.thj;
/*
按照斗地主的规则,完成洗牌发牌的动作。
要求完成以下功能:
准备牌:组装54张扑克牌
洗牌:54张牌顺序打乱
发牌:三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
看牌:查看三人各自手中的牌(按照牌的大小排序)、底牌
规则:手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3
*/
点击查看代码
import java.util.*;
public class LandlordFighting {
public static void main(String[] args) {
HashMap<Integer, String> pokers = new HashMap<>();
//1.准备牌:组装54张扑克牌
Ready_card(pokers);
//洗牌:54张牌顺序打乱 打乱序号 再根据序号拿的值就乱了
final List<Integer> listIntegers = shuffleCard(pokers);
//发牌:三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
final Result result = getResult();
Deal_cards(listIntegers, result.player1(), result.player2(), result.player3(), result.bottom());
//看牌
Watch_cards(result.player1(), pokers, result.player2(), result.player3(), result.bottom());
}
private static Result getResult() {
TreeSet<Integer> player1 = new TreeSet<>();//玩家1的手牌
TreeSet<Integer> player2 = new TreeSet<>();//玩家2的手牌
TreeSet<Integer> player3 = new TreeSet<>();//玩家3的手牌
TreeSet<Integer> bottom = new TreeSet<>();//底牌
Result result = new Result(player1, player2, player3, bottom);
return result;
}
private record Result(TreeSet<Integer> player1, TreeSet<Integer> player2, TreeSet<Integer> player3, TreeSet<Integer> bottom) {
}
private static void Watch_cards(TreeSet<Integer> player1, HashMap<Integer, String> pokers, TreeSet<Integer> player2, TreeSet<Integer> player3, TreeSet<Integer> bottom) {
//看牌:查看三人各自手中的牌(按照牌的大小排序)、底牌
//player1的手牌
System.out.print("player1 :");
for(Integer integer: player1){
System.out.print(pokers.get(integer)+"\t");
}
System.out.println("\n");
//player2的手牌
System.out.print("player2 :");
for(Integer integer: player2){
System.out.print(pokers.get(integer)+"\t");
}
System.out.println("\n");
//player3的手牌
System.out.print("player3 :");
for(Integer integer: player3){
System.out.print(pokers.get(integer)+"\t");
}
System.out.println("\n");
//底牌
System.out.print("底牌:");
for (Integer integer : bottom){
System.out.print(pokers.get(integer)+"\t");
}
}
private static void Deal_cards(List<Integer> listIntegers, TreeSet<Integer> player1, TreeSet<Integer> player2, TreeSet<Integer> player3, TreeSet<Integer> bottom) {
//循环拿到每一张牌 留3张底牌
for (int i = 0; i < listIntegers.size() - 3; i++) {
Integer integer = listIntegers.get(i);
if (i % 3 == 0) {
//player1
player1.add(integer);
} else if (i % 3 == 1) {
//player2
player2.add(integer);
} else {
//player3
player3.add(integer);
}
}
//给底牌赋值
bottom.add(listIntegers.get(51));
bottom.add(listIntegers.get(52));
bottom.add(listIntegers.get(53));
}
private static List<Integer> shuffleCard(HashMap<Integer, String> pokers) {
//洗牌:54张牌顺序打乱 打乱序号 再根据序号拿的值就乱了
//获取每一张牌的编号
final Set<Integer> integers = pokers.keySet();//拿到键
List<Integer> listIntegers = new ArrayList<>(integers);//将键转为列表
Collections.shuffle(listIntegers);//打乱顺序
return listIntegers;
}
private static void Ready_card(HashMap<Integer, String> pokers) {
//4种花色
String[] colors = {"♥", "♦", "♣", "♠"};//红桃 方片 梅花 黑桃
//每种花色有13张牌
String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
pokers.put(0, "大王");
pokers.put(1, "小王");
//通过循环拿到13张牌
for (String number : numbers) {
//通过循环拿到4种花色
for (String color : colors) {
//eg:2 ♥2
pokers.put(pokers.size(), color + number);
//System.out.print(color + number + "\t");
}
}
//System.out.println(pokers);
}
}
运行效果图
点击查看代码
"D:\DevTools\Program Files\Java Development Kit\Java 17\jdk-17\bin\java.exe"
player1 :小王 ♥3 ♦3 ♥4 ♣4 ♣6 ♦7 ♥8 ♠8 ♦9 ♣9 ♥10 ♦J ♠Q ♣K ♠K ♥A
player2 :♦4 ♠4 ♥5 ♣5 ♥6 ♦6 ♣7 ♥9 ♦10 ♥J ♣J ♥Q ♥K ♦K ♠A ♣2 ♠2
player3 :大王 ♠3 ♦5 ♠5 ♥7 ♠7 ♦8 ♣8 ♠9 ♣10 ♠J ♦Q ♣Q ♦A ♣A ♥2 ♦2
底牌:♣3 ♠6 ♠10
Process finished with exit code 0