三人斗地主
思路
1.用list集合 存储 1-54张牌的信息
2.用2个数组 分别存储 4种花色 和 A ,2,3…J,Q ,K
3…将牌的具体内容放到list集合
4.发牌(发牌之前 需要将牌打乱 ,也就是随机
Collections.shuffle(list);)
5.看牌
代码示例
package Poker;
import java.util.*;
public class Game {
public static void main(String[] args) {
//存放牌信息
Map<Integer,String> map = new HashMap<>();
//存放编号 54个
List<Integer> list = new ArrayList<>();
//用2个数组存放 花色和 13张牌
String [] colors = {"♠","♥","♦","♣"};
String [] numbers={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
int index= 0; //记录编号
//将牌的信息放到 上面的集合种
// 总共循环 4*13 =52次
for(String num:numbers){
for(String col:colors){
map.put(index,col+num);
list.add(index);
index++;
}
}
System.out.println("index="+index);
//剩余2个编号就是大小王
list.add(52);
list.add(53);
map.put(52,"大王");
map.put(53,"小王");
//发牌之前 需要将牌打乱 ,也就是随机
Collections.shuffle(list);
//发牌开始 ( 3人 , 还要 留下3张底牌) 需要定义3个人 和 1个底牌 集合接收牌
List<Integer> p1 = new ArrayList<>();
List<Integer> p2 = new ArrayList<>();
List<Integer> p3 = new ArrayList<>();
List<Integer> bottom = new ArrayList<>();
//具体发送 牌
for (int i = 0; i < list.size(); i++) {
if(i<3){ // i = 0 , 1, 2 ,就是底牌了
bottom.add(list.get(i));
}else if(i%3==0){
p1.add(list.get(i));
}else if(i%3==1){
p2.add(list.get(i));
}else{
p3.add(list.get(i));
}
}
//排序到手里的牌信息
Collections.sort(p1);
Collections.sort(p2);
Collections.sort(p3);
Collections.sort(bottom);
show("杰西卡",p1,map);
show("赖熊",p2,map);
show("玺子",p3,map);
show("底牌",bottom,map);
}
private static void show(String name, List<Integer> list, Map<Integer, String> map) {
System.out.print(name+": ");
for(Integer in:list){
System.out.print(map.get(in)+" ");
}
System.out.println();
}
}