今天学习的是斗地主,主要用到了列表了for循环的方法。
代码如下:
package com.zhongruan;
import java.util.*;
public class DouDiZhu2 {
public static void main(String[] args) {
//1.拿到一副牌
Map<Integer,String> poker=new HashMap<>();
String[] nums={“A”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”};// 创建数组
String[] colors={"黑桃","红心","梅花","方块"}; //花色的数组
int index=0;
for(String color:colors){
for(String num:nums){
String pai=color+num;
index=index+1;
poker.put(index,pai);
}
}
System.out.println(index);
index++;
poker.put(index,"大王");
index++;
poker.put(index,"小王");
System.out.println(poker);
//2.洗牌
List<Integer> pokerIndexs=new ArrayList<>();
Set<Integer> integers = poker.keySet();
// System.out.println(integers);
for(Integer i:poker.keySet()){
pokerIndexs.add(i);
}
Collections.shuffle(pokerIndexs);
System.out.println(pokerIndexs);
//3.留3张牌
Set<Integer> dipaiInds=new TreeSet<>();
dipaiInds.add(pokerIndexs.remove(0));
dipaiInds.add(pokerIndexs.remove(0));
dipaiInds.add(pokerIndexs.remove(0));
System.out.println(dipaiInds);
System.out.println(pokerIndexs);
//4.发牌
Set<Integer> ct=new TreeSet<>();
Set<Integer> wwc=new TreeSet<>();
Set<Integer> sj=new TreeSet<>();
for(int i=0;i <pokerIndexs.size();i++){
int pi=pokerIndexs.get(i);
int mod=i%3;
if(mod==0){
ct.add(pi);
}else if(mod==1){
wwc.add(pi);
}else{
sj.add(pi);
}
}
System.out.println(ct);
System.out.println(wwc);
System.out.println(sj);
//5.看牌
look(poker,ct);
look(poker,wwc);
look(poker,sj);
}
public static void look(Map<Integer,String> poker,Set<Integer> indexs){
List<String> p=new ArrayList<>();
for(Integer i:indexs){
String pai=poker.get(i);
p.add(pai);
}
System.out.println(p);
}
}