小王说来来斗地主,但是木有牌,怎么办-------模拟斗地主洗牌和发牌 对牌进行排序 并同时使用Map,List,Set等集合
程序员文章站
2024-03-15 09:17:41
...
直接上代码了。
package cn.ketang.lianxi03;
import java.util.ArrayList;
import java.util.Collections;
/**
* ArrayList实现
* 模拟斗地主洗牌和发牌
* 分析:
* A:创建一个牌盒
* B:装牌
* C:洗牌
* D:发牌
* E:看牌
* @author nice
*
*/
public class LandLordDemo {
public static void main(String[] args) {
ArrayList<String> ls = new ArrayList<>();
String[] colors = { "♠", "♥", "♦", "♣" };
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
for (String s1 : colors) {
for (String s2 : numbers) {
ls.add(s1.concat(s2));
}
}
// 把大小王装进去
ls.add("大王");
ls.add("小王");
// 洗牌
Collections.shuffle(ls);
// 发牌
ArrayList<String> wujulan = new ArrayList<>();
ArrayList<String> wufan = new ArrayList<>();
ArrayList<String> nicewu = new ArrayList<>();
ArrayList<String> dipai = new ArrayList<>();
int length = ls.size();
int number = 3;
for (int i = 0; i < length; i++) {
if (i >= length - 3) {
dipai.add(ls.get(i));
} else if (i % number == 0) {
wujulan.add(ls.get(i));
} else if (i % number == 1) {
wufan.add(ls.get(i));
} else if (i % number == 2) {
nicewu.add(ls.get(i));
}
}
// 看牌
LookPoker("吴居兰", wujulan);
LookPoker("吴凡", wufan);
LookPoker("nice吴", nicewu);
LookPoker("底牌", dipai);
}
static void LookPoker(String name, ArrayList<String> array) {
System.out.println(name + "的牌是:");
for (String s : array) {
System.out.print(s + " ");
}
System.out.println();
}
}
第二种实现:
package cn.ketang.lianxi04;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
/**
* HahMap实现
* 模拟斗地主洗牌和发牌
* 分析:
* A:创建一个牌盒
* B:装牌
* C:洗牌
* D:发牌
* E:看牌
* @author nice
*
*/
public class LandLordDemo2 {
public static void main(String[] args) {
// map存放索引和对应的牌
HashMap<Integer, String> hm = new HashMap<Integer, String>();
// 存放牌对应的索引
ArrayList<Integer> al = new ArrayList<Integer>();
String[] colors = { "♠", "♥", "♦", "♣" };
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
// 定义初始索引值为0
int index = 0;
for (String color : colors) {
for (String number : numbers) {
String poker = color.concat(number);
hm.put(index, poker);
al.add(index);
index++;
}
}
// 把大小王放进去
hm.put(index, "大王");
al.add(index);
index++;
hm.put(index, "小王");
al.add(index);
// 洗牌
Collections.shuffle(al);
// 发牌
TreeSet<Integer> wujulan = new TreeSet<Integer>();
TreeSet<Integer> wufan = new TreeSet<Integer>();
TreeSet<Integer> nicewu = new TreeSet<Integer>();
TreeSet<Integer> dipai = new TreeSet<Integer>();
//循环遍历al集合,按规则添加al中存储的HashMap的索引到TreeSet中
for (int i = 0; i < al.size(); i++) {
if (i >= (al.size() - 3)) {
dipai.add(al.get(i));
} else if (i % 3 == 0) {
wufan.add(al.get(i));
} else if (i % 3 == 1) {
nicewu.add(al.get(i));
} else if (i % 3 == 2) {
wujulan.add(al.get(i));
}
}
// 看牌
ShowPoker("吴居兰", wujulan, hm);
ShowPoker("吴凡", wufan, hm);
ShowPoker("nice吴", nicewu, hm);
ShowPoker("底牌", dipai, hm);
}
public static void ShowPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
System.out.println(name + "的牌是:");
for (Integer key : ts) {//遍历TreeSet中的值,将其作为key到HahMap中找到对应的value值
String value = hm.get(key);//将找到的value值付给变量value
System.out.print(value + " ");
}
System.out.println();
}
}
以上,谢谢观看。