博客> Java Collections附加模拟斗地主小Demo
Java Collections附加模拟斗地主小Demo
2017-08-18 04:17 评论:0 阅读:116 西单_夜未央
Java模拟斗地主 collections
/*
 *  Collection和Collections的区别?
 *  Collection:是Collection集合的顶层接口,定义了Collection集合的共性方法。
 *  Collections:是一个类,定义了针对Collection集合操作的功能。有排序,查找,反转等。
 *  
 *  Collections的功能:
 *  排序:public static void sort(List list)
 *  二分查找:public static <T> int binarySearch(List list,T key)
 *  反转:public static void reverse(List list)
 *  最大值:public static  T max(Collection coll)
 *  随机置换:public static void shuffle(List list)
 */

测试:

 public static void main(String[] args) {

  ArrayList<Integer> al = new ArrayList<Integer>();

  al.add(20);
  al.add(120);
  al.add(990);
  al.add(56);
  al.add(87);
  al.add(36);
  al.add(105);
  al.add(4);

  System.out.println(al);
//  //排序
//  Collections.sort(al);
//  System.out.println(al);
//  
//  //二分查找
//  int index = Collections.binarySearch(al, 36);
//  System.out.println(index);

  //反转
  Collections.reverse(al);
  System.out.println(al);

  //最大值
  System.out.println(Collections.max(al));
  //随机置换
  Collections.shuffle(al);
  System.out.println(al);

}

//模拟斗地主

public static void main(String[] args) {

String[] numArray = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; String[] colorArray = {"黑桃","梅花","方块","红桃"}; //一盒完整的牌 ArrayList cardsArray = new ArrayList(); cardsArray.add("大王"); cardsArray.add("小王"); for (String color : colorArray) { for (String num : numArray) { cardsArray.add(color+num); } } //System.out.println(cardsArray); //洗牌 Collections.shuffle(cardsArray); //三个打牌人 ArrayList huiArray = new ArrayList(); ArrayList liArray = new ArrayList(); ArrayList mingArray = new ArrayList(); ArrayList bottomArray = new ArrayList();

//发牌 for (int i = 0; i < cardsArray.size()-3; i++) { String card = cardsArray.get(i); if (i % 3 == 0) { huiArray.add(card); } else if (i % 3 == 1){ liArray.add(card); }else{ mingArray.add(card); } }

//底牌 for (int i = cardsArray.size()-3; i < cardsArray.size(); i++) { String bottomCard = cardsArray.get(i); bottomArray.add(bottomCard); }

System.out.println("辉哥的牌:"+huiArray); System.out.println("宝贝的牌:"+liArray); System.out.println("自己的牌:"+mingArray); System.out.println("底牌:"+bottomArray);

}

收藏
0
sina weixin mail 回到顶部