黑马程序员技术交流社区

标题: 斗地主小游戏 [打印本页]

作者: 孙广浩    时间: 2015-11-11 22:37
标题: 斗地主小游戏
  1. package com.heima.test;

  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.TreeSet;

  6. public class Test3 {
  7.         public static void main(String[] args) {
  8.                 String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
  9.                 String[] color = {"红桃","黑桃","方片","梅花"};
  10.                 HashMap<Integer, String> hm = new HashMap<>();                                       
  11.                 ArrayList<Integer> list = new ArrayList<>();                               
  12.                 int index = 0;
  13.                 for(String s1 : num) {                                                                                       
  14.                         for(String s2 : color) {                                                                       
  15.                                 hm.put(index, s2.concat(s1));
  16.                                 list.add(index);                                                                               
  17.                                 index++;
  18.                         }
  19.                 }
  20.                 hm.put(index, "小王");
  21.                 list.add(index);                                                                                               
  22.                 index++;
  23.                 hm.put(index, "大王");
  24.                 list.add(index);                                                                                               
  25.                 Collections.shuffle(list);
  26.                 TreeSet<Integer> zhourunfa = new TreeSet<>();
  27.                 TreeSet<Integer> zhouxingchi = new TreeSet<>();
  28.                 TreeSet<Integer> me = new TreeSet<>();
  29.                 TreeSet<Integer> dipai = new TreeSet<>();
  30.                
  31.                 for(int i = 0; i < list.size(); i++) {
  32.                         if(i >= list.size() - 3) {
  33.                                 dipai.add(list.get(i));                                                       
  34.                         }else if(i % 3 == 0) {
  35.                                 zhourunfa.add(list.get(i));
  36.                         }else if(i % 3 == 1) {
  37.                                 zhouxingchi.add(list.get(i));
  38.                         }else {
  39.                                 me.add(list.get(i));
  40.                         }
  41.                 }
  42.                 lookPoker(hm, gaojin, "周润发");
  43.                 lookPoker(hm, longwu, "周星驰");
  44.                 lookPoker(hm, me, "我");
  45.                 lookPoker(hm, dipai, "底牌");
  46.         }
  47.         public static void lookPoker(HashMap<Integer, String> hm,TreeSet<Integer> ts ,String name) {
  48.                 System.out.print(name + "的牌是:");
  49.                 for(Integer i : ts) {                                               
  50.                         System.out.print(hm.get(i) + " ");
  51.                 }
  52.                 System.out.println();
  53.         }
  54. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2