上干货~
--------------------------------------------------------------------------------------------------------------
今天最后 冯佳老师讲了一个斗地主.
挺好玩的.
于是乎,自己也写了 一个, 是时候展现真正的技术了.~
赌神 高进 颤抖吧~~~吼吼吼~
----------------------------------------------------------------------------------------------------------------
代码区:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class 斗地主 {
public static void main(String[] args) {
String [] str ={"3","4","5","6","7","8","9","10","j","Q","K","A","2"};
String [] str1 = {"红桃","方片","梅花","黑桃"};
ArrayList<String> list = new ArrayList<>();//总牌
for (String string : str) {
for (String string1 : str1) {
list.add(string1.concat(string));
}
}
list.add("大王");//添加大王,小王
list.add("小王");
HashMap<Integer, String> doudizhu = new HashMap<>();
for(int i=0;i<list.size();i++) {
doudizhu.put(i ,list.get(i));
}
//System.out.println(doudizhu);
ArrayList<Integer> arrlist = new ArrayList<>();
for (Integer integer : doudizhu.keySet()) {
arrlist.add(integer);
}
System.out.println(arrlist);
ArrayList<Integer> gaojin = new ArrayList<>();
ArrayList<Integer> xiaodao = new ArrayList<>();
ArrayList<Integer> me = new ArrayList<>();
ArrayList<Integer> dipai = new ArrayList<>();
Collections.shuffle(arrlist);
System.out.println(arrlist);
for(int i = 0; i<arrlist.size();i++) {
if (i>=arrlist.size()-3) {
dipai.add(arrlist.get(i));
}
if (i%3==0) {
gaojin.add(arrlist.get(i));
}
if (i%3==1) {
xiaodao.add(arrlist.get(i));
}
if (i%3==2) {
me.add(arrlist.get(i));
}
}
/* ArrayList<String> gaojin1 = new ArrayList<>();
ArrayList<String> xiaodao1 = new ArrayList<>();
ArrayList<String> me1 = new ArrayList<>();
ArrayList<String> dipai1 = new ArrayList<>();*/
System.out.print("底牌是:");
for (Integer integer : dipai) {
// dipai1.add(doudizhu.get(integer));
System.out.print(doudizhu.get(integer)+" ");
}
System.out.println();
System.out.print("我的是:");
for (Integer integer : me) {
// me1.add(doudizhu.get(integer));
System.out.print(doudizhu.get(integer)+" ");
}
System.out.println();
System.out.print("小刀的:");
for (Integer integer : xiaodao) {
// xiaodao1.add(doudizhu.get(integer));
System.out.print(doudizhu.get(integer)+" ");
}
System.out.println();
System.out.print("高进的:");
for (Integer integer : gaojin) {
// gaojin1.add(doudizhu.get(integer));
System.out.print(doudizhu.get(integer)+" ");
}
/*System.out.println();
System.out.println(dipai1);
System.out.println(xiaodao1);
System.out.println(gaojin1);
System.out.println(me1);*/ }
}
-------------------------------------------------------------------------------------------------------
总结一下:
首先我 将1-K 和四种花色 进行匹配,
然后通过 HashMap 给他 搞进去
然后通过 HashMap.keyset(),方法,将他的键全部拿出去变成集合.
然后对这个集合进行 重新洗牌
然后 分给三个人.
这时候分的还是 数字,
但是通过map.get(),方法可以获取到值
这样就可以 愉快的 看高进的 底牌了,
其实我早就看穿了你的底牌~ 颤抖吧.
|
|