本帖最后由 CristianoCaruro 于 2018-11-15 09:50 编辑
昨天老师上课把斗地主的案例重新升级,感觉程序又变得有趣了,回去又细细的品味一下老师的代码,自己又写了一遍,对为什么要对集合索引操作有了深刻认识,感觉还是有趣,完成如下:import java.util.ArrayList;
import java.util.Collections;
public class DouDiZhu {
public static void main(String[] args) {
String[] huaSe = {"♥","♦","♠","♣"};
String[] mianZhi = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
ArrayList<String> pai = new ArrayList<>();
ArrayList<Integer> indexs = new ArrayList<>();
int index = 0;
for (int i = 0; i < mianZhi.length; i++) {
for (int j = 0; j < huaSe.length; j++) {
pai.add(huaSe[j]+mianZhi);
indexs.add(index++);
}
}
pai.add("小鬼");
indexs.add(index++);
pai.add("大鬼");
indexs.add(index);
ArrayList<Integer> 小陈 = new ArrayList<>();
ArrayList<Integer> 小红 = new ArrayList<>();
ArrayList<Integer> 小丽 = new ArrayList<>();
Collections.shuffle(indexs);
for (int i = 0; i < indexs.size()-3; i++) {
if(i%3==0){
小陈.add(indexs.get(i));
}
if(i%3==1){
小红.add(indexs.get(i));
}
if(i%3==2){
小丽.add(indexs.get(i));
}
}
System.out.println(小陈);
System.out.println(小红);
System.out.println(小丽);
Collections.sort(小陈);
Collections.sort(小红);
Collections.sort(小丽);
System.out.println("小陈手牌是:");
for (int i = 0; i < 小陈.size(); i++) {
System.out.print(pai.get(小陈.get(i))+" ");
}
System.out.println();
System.out.println("小红手牌是:");
for (int i = 0; i < 小红.size(); i++) {
System.out.print(pai.get(小红.get(i))+" ");
}
System.out.println();
System.out.println("小丽:");
for (int i = 0; i < 小丽.size(); i++) {
System.out.print(pai.get(小丽:get(i))+" ");
}
System.out.println();
System.out.println("底牌:");
for (int i = pai.size()-3 ; i < pai.size(); i++) {
System.out.print(pai.get(indexs.get(i))+" ");
}
System.out.println();
}
}
接下来每个用户是如何打牌的呢,以后是否还要继续深入学习完善这个案例,期待接下来的学习和领悟吧
快一个的学习,痛并快乐着!为自己加油!
2018.11.12 星期四 9:48 cd黑马 |
|