利用所学的几个集合完成的小案例,只能实现到发完牌…
package cn.itcast.demo03;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import javax.sql.PooledConnection;
/*
* 斗地主分为四个步骤
* 1.准备阶段
* 2.洗牌阶段
* 3.发牌阶段
* 4.查看每个玩家的手牌及底牌
*/
public class DoDiZhu {
public static void main(String[] args) {
// 创建map集合,存储牌
HashMap<Integer, String> pooker = new HashMap<Integer, String>();
// 创建一个List集合,用来存储编号,方便后面的操作,发牌和看牌
ArrayList<Integer> pookerNum = new ArrayList<Integer>();
// 先加入花色
ArrayList<String> color = new ArrayList<String>();
color.add("♥");
color.add("♠");
color.add("♦");
color.add("♣");
// 添加排种
ArrayList<String> number = new ArrayList<String>();
// 利用工具类的方法一次性给出牌种
Collections.addAll(number, "2","A", "K", "Q", "J", "10", "9", "8", "7",
"6", "5", "4", "3");
// 定义编号,除开大小王
pooker.put(0, "大王");
pooker.put(1, "小王");
pookerNum.add(0);
pookerNum.add(1);
int index = 2;
for (String numbers : number) {
for (String colors : color) {
pooker.put(index, colors + numbers);
pookerNum.add(index);
index++;
}
}
// 洗牌
Collections.shuffle(pookerNum);
// 发牌,定义三个玩家和底牌
ArrayList<Integer> player1 = new ArrayList<Integer>();
ArrayList<Integer> player2 = new ArrayList<Integer>();
ArrayList<Integer> player3 = new ArrayList<Integer>();
ArrayList<Integer> diPai = new ArrayList<Integer>();
for (int i = 0; i < pookerNum.size();i++) {
//将最后三张赋值给底牌
if(i>50){
diPai.add(pookerNum.get(i));
}else if(i%3 == 0){
//将pookerNum中的元素取出一次赋值给玩家
player1.add(pookerNum.get(i));
}else if(i%3 == 1){
player2.add(pookerNum.get(i+1));
}else if(i%3 == 2){
player3.add(pookerNum.get(i+2));
}
}
//对玩家的手牌进行排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
Collections.sort(diPai);
//调用方法遍历
printArrayList(pooker,player1);
printArrayList(pooker,player2);
printArrayList(pooker,player3);
printArrayList(pooker,diPai);
}
/*
* 自定义一个方法。用来遍历三个玩家的手牌及底牌
*/
public static void printArrayList(HashMap<Integer, String> pooker,ArrayList<Integer> player) {
//遍历ArrayList集合,获取元素,作为键,在map中寻找值
for (Integer key : player) {
String value = pooker.get(key);
System.out.print(value+" ");
}
System.out.println();
}
}