/**
* 斗地主
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class Demo {
public static void main(String[] args) {
//定义一个集合
HashMap<Integer, String> map = new HashMap<>();
//定义牌的花色
String[] colors = {"♠", "♥", "♣", "♦"};
//定义牌的大小
String[] num = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
//定义count记录map键
int count = 1;
//利用循环把花色和数字组合起来
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < colors.length; j++) {
//组合花色和数字
String s = colors[j] + num[i];
//把键和键值存入到map中
map.put(count, s);
//每存入一个键值对,键加1
count++;
}
}
//单独添加,大王,小王(54张牌定义完毕)
map.put(53, "小S");
map.put(54, "大S");
//定义一个集合用来存54个数
ArrayList<Integer> cards = new ArrayList<>();
for (int i = 1; i < 55; i++) {
cards.add(i);
}
//打乱54个数
Collections.shuffle(cards);
//定义三个人和底牌(三张)
ArrayList<Integer> play1 = new ArrayList<>();
ArrayList<Integer> play2 = new ArrayList<>();
ArrayList<Integer> play3 = new ArrayList<>();
ArrayList<Integer> diPai = new ArrayList<>();
//遍历cards留三张底牌
for (int i = 0; i < cards.size() - 3; i++) {
if (i % 3 == 0) {
//得到玩家1得牌
play1.add(cards.get(i));
} else if (i % 3 == 1) {
//得到玩家2得牌
play2.add(cards.get(i));
} else {
//得到玩家3得牌
play3.add(cards.get(i));
}
}
//把三张底牌添加到底牌集合中
diPai.add(cards.get(53));
diPai.add(cards.get(52));
diPai.add(cards.get(51));
//排序玩家123和底牌
Collections.sort(play1);
Collections.sort(play2);
Collections.sort(play3);
Collections.sort(diPai);
//调用方法打印牌
lookPai(play1, map);
lookPai(play2, map);
lookPai(play3, map);
lookPai(diPai, map);
}
//定义一个方法 play1,2,3和底牌都可以调用lookPai打印出来
public static void lookPai(ArrayList<Integer> cards, HashMap<Integer, String> map) {
//增强for
for (Integer card : cards) {
String s = map.get(card);
System.out.print(s + " ");
}
System.out.println();
}
}
使用java写斗地主的部分代码
最新推荐文章于 2022-02-22 20:04:31 发布