Java-->简单的斗地主发牌流程

时间:2023-03-08 17:02:00
package com.dragon.java.hwddz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /*1. 写一个可以给斗地主三家随机发牌的程序。
a:牌可以随机发给三个玩家
b:在控制台打印每个玩家的牌。
c:对每个玩家手中的牌按照大写排序。
```
牌的花色:"♠", "♥", "♣", "♦"
*思路:创建一个容器存储所有的牌,再创建三个容器分别表示三个用户的牌,三个用户轮流从第一个容器中取牌,
*剩下三张为底牌。把大王和小王也算进去**/
public class Test {
public static void main(String args[]) {
Map<Integer, String> mapPoker = new HashMap<>();
List<Integer> listIndex = new ArrayList<>(); Poker.setPoker(mapPoker, listIndex); Poker.washPoker(listIndex);

     // 定义用户
Player[] players = new Player[3];
players[0] = new Player("躺之徳");
players[1] = new Player("小剪");
players[2] = new Player("呆立叫");
     
     // 用户容器
List<Integer> tzd = new ArrayList<>();
List<Integer> xj = new ArrayList<>();
List<Integer> dlj = new ArrayList<>();
List<Integer> dp = new ArrayList<>(); Player.getPoker(tzd, xj, dlj, dp, listIndex); // int index = Player.getMaxNumber(players);
// System.out.println(players[index].getName() + "抢到了地主!"); players[0].showPoker(tzd, mapPoker);
players[1].showPoker(xj, mapPoker);
players[2].showPoker(dlj, mapPoker);
// players[index].showDp(dp, mapPoker); System.out.println("\n" + "底牌:");
for (Integer integer : dp) {
System.out.print(mapPoker.get(integer) + " ");
}
}
}

--> Poker类用于存入54张扑克牌

package com.dragon.java.hwddz;

import java.util.Collections;
import java.util.List;
import java.util.Map; public class Poker {
private static String[] pokerColor = { "♠", "♥", "♣", "♦" };
private static String[] pokerNum = { "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K", "A", "2" };
  
   // 存入54张不同的牌,并给每张牌一个标号
public static void setPoker(Map<Integer, String> mapPoker,
List<Integer> listIndex) {
int index = 0;
for (String nums : pokerNum) {
for (String colors : pokerColor) {
String poker = colors.concat(nums);
mapPoker.put(index, poker);
listIndex.add(index);
index++;
}
}
mapPoker.put(index, "大☝王");
listIndex.add(index);
index++;
mapPoker.put(index, "小☟王");
listIndex.add(index);
}
   // 洗牌 --> 即洗标号
public static void washPoker(List<Integer> listIndex) {
Collections.shuffle(listIndex);
} }

--> Player类对玩家进行发牌和显示处理(写完我就后悔了,J,Q,K,A,2的排序太low了..package com.dragon.java.hwddz;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Random; public class Player {
private String name; Player() {
super();
} Player(String name) {
super();
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
   // 发牌流程
public static void getPoker(List<Integer> player1, List<Integer> player2,
List<Integer> player3, List<Integer> dp, List<Integer> list) {
for (int i = 0; i < list.size(); i++) {
if (i >= list.size() - 3) {
dp.add(list.get(i));
} else if (i % 3 == 1) {
player1.add(list.get(i));
} else if (i % 3 == 2) {
player2.add(list.get(i));
} else if (i % 3 == 0) {
player3.add(list.get(i));
}
}
}
   // 显示牌面
public void showPoker(List<Integer> list, Map<Integer, String> mapPoker) {
System.out.println("\n" + name + "的手牌是:");
List<String> listPoker = new ArrayList<>();
for (Integer key : list) {
listPoker.add(mapPoker.get(key));
}
Collections.sort(listPoker, new MyComparator());
for (String string : listPoker) {
System.out.print(string + " ");
}
} // public void showDp(List<Integer> dp, Map<Integer, String> mapPoker) {
// List<String> listPoker = new ArrayList<>();
// for (Integer key : dp) {
// listPoker.add(mapPoker.get(key));
// }
// Collections.sort(listPoker, new MyComparator());
// for (String string : listPoker) {
// System.out.print(string + " ");
// }
// } public int throwsElbows() {
Random random = new Random();
int num = random.nextInt(6) + 1;
System.out.println(name + "摇了" + num + "点");
return num;
} // 请忽略下面注释...
// public static int getMaxNumber(Player[] players) {
// List<Integer> numbers = new ArrayList<>();
// for (int i = 0; i < players.length; i++) {
// numbers.add(players[i].throwsElbows());
// }
// Collections.sort(numbers);
// for (int i = 0; i < numbers.size() - 1; i++) {
// if (numbers.get(numbers.size() - 1) == numbers.get(i)) {
// System.out.println(players[numbers.size() - 1].name + "和"
// + players[i].name + "摇的点数相同");
// System.out.println("他们打了一架! --> " + players[i].name + "赢了!");
// return i;
// }
// }
// return numbers.size() - 1;
// }   
// 牌面的比较排序 --> 看得就烦...
public static class MyComparator implements Comparator<String> { @Override
public int compare(String o1, String o2) {
String m1 = o1.substring(1, 2);
String m2 = o2.substring(1, 2);
if (m1.equals("1")) {
m1 = "a";
} else if (m1.equals("J")) {
m1 = "b";
} else if (m1.equals("Q")) {
m1 = "c";
} else if (m1.equals("K")) {
m1 = "d";
} else if (m1.equals("A")) {
m1 = "e";
} else if (m1.equals("2")) {
m1 = "f";
}
if (m2.equals("1")) {
m2 = "a";
} else if (m2.equals("J")) {
m2 = "b";
} else if (m2.equals("Q")) {
m2 = "c";
} else if (m2.equals("K")) {
m2 = "d";
} else if (m2.equals("A")) {
m2 = "e";
} else if (m2.equals("2")) {
m2 = "f";
}
return m1.compareTo(m2); }
}
}

--> 写完发现逻辑是不是有问题啊...