模拟斗地主
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Main { public static void main(String[] args) { ArrayListlist = new ArrayList (); String [] s1= {"♦","♣","♥","♠"}; String [] s2= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; for (String hua : s1) { for(String shu :s2 ) { String z=hua+shu; list.add(z); } } list.add("大王"); list.add("小王"); Collections.shuffle(list); ArrayList< String > men1=new ArrayList<>(); //玩家一 ArrayList< String > men2=new ArrayList<>(); //玩家二 ArrayList< String > men3=new ArrayList<>(); //玩家三 ArrayList< String > dipai=new ArrayList<>(); //底牌 for(int i=0;i 斗地主进阶 ~将玩家手里的牌排序显示:
思路:将牌用编号1~54表示,由HashMap存编号和对应的牌。将编号排序,再输出。 import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; public class Main { public static void main(String[] args) { HashMaphashMap = new HashMap<>(); // 存整副牌 String[] s1 = { "♦", "♣", "♥", "♠" }; //花色 String[] s2 = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; //数字 ArrayList list = new ArrayList<>(); // 存编号 int count = 1; for (String shu : s2) { for (String hua : s1) { String z = hua + shu; hashMap.put(count, z); list.add(count); count++; } } hashMap.put(53, "小王"); list.add(53); hashMap.put(54, "大王"); list.add(54); Collections.shuffle(list); // 随机打乱 ArrayList men1 = new ArrayList (); // 玩家一 ArrayList men2 = new ArrayList (); // 玩家二 ArrayList men3 = new ArrayList (); // 玩家三 ArrayList dipai = new ArrayList (); // 底牌 //分牌 for (int i = 0; i < list.size(); i++) { if (i < 3) { dipai.add(list.get(i)); } else if (i < 20) { men1.add(list.get(i)); } else if (i < 37) { men2.add(list.get(i)); } else { men3.add(list.get(i)); } } // 将编号从小到大排序 Comparator comparator = new Comparator () { @Override public int compare(Integer o1, Integer o2) { // TODO 自动生成的方法存根 return o1 - o2; } }; men1.sort(comparator); men2.sort(comparator); men3.sort(comparator); dipai.sort(comparator); System.out.print("玩家一:"); for (Integer one : men1) { System.out.print(hashMap.get(one) + " "); } System.out.print("n玩家二:"); for (Integer tow : men2) { System.out.print(hashMap.get(tow) + " "); } System.out.print("n玩家三:"); for (Integer three : men3) { System.out.print(hashMap.get(three) + " "); } System.out.print("n底牌 :"); for (Integer d : dipai) { System.out.print(hashMap.get(d) + " "); } } } 编译结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)