首先需要编写maopao文件
package com; import java.util.ArrayList; public class maopao { public static void change(ArrayList<String>X){ ArrayList<Integer> newlist = new ArrayList<>(); int a = 0; for (String s : X){ if ("joker".equals(s)){ a = 500; }else if ("JOKER".equals(s)){ a = 600; }else { if (s.charAt(0) == '❤'){ a = 1; }else if (s.charAt(0) == '♣'){ a = 2; }else if (s.charAt(0) == '◇'){ a = 3; }else if (s.charAt(0) == '♠'){ a = 4; } if (s.charAt(1) == 'J'){ a += 110; }else if(s.charAt(1) == 'Q'){ a += 120; }else if(s.charAt(1) == 'K'){ a += 130; }else if (s.charAt(1) == 'A'){ a += 140; }else if (s.charAt(1) == '2'){ a += 150; }else if (s.charAt(1) == '1' && s.charAt(2) == '0'){ a += 100; }else{ a += (s.charAt(1) - '0') * 10; } } newlist.add(a); } sawp(newlist,X); System.out.println(X); } private static void sawp(ArrayList<Integer> list,ArrayList<String> X) { String temp1;int temp2;//定义一个临时变量 for(int i=0;i < list.size()-1;i++){//冒泡趟数 for(int j=0;j < list.size()-1;j++){ if(list.get(j + 1) < list.get(j)){ temp1 = X.get(j); X.set(j, X.get(j + 1)); X.set(j + 1, temp1); temp2 = list.get(j); list.set(j, list.get(j + 1)); list.set(j + 1, temp2); } } } } }
再进行斗地主的实现
package com; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class DouDiZhu { public static void main(String[] args) { List<String> pokes = new ArrayList<>(); List<String> colors = new ArrayList<>(); List<String> numbers = new ArrayList<>(); colors.add("❤"); colors.add("♣"); colors.add("◇"); colors.add("♠"); numbers.add("A"); for(int i = 2;i <= 10 ; i++){ numbers.add(i+""); } numbers.add("J"); numbers.add("Q"); numbers.add("K"); for (String C:colors){ for (String N:numbers){ String pai = C + N; pokes.add(pai); } } pokes.add("joker"); pokes.add("JOKER"); Collections.shuffle(pokes); int i; for (i=0;i<pokes.size();i++){ if (i % 13 == 0){ System.out.println(); } System.out.print(pokes.get(i)+"\t"); } ArrayList<String> play1 = new ArrayList<>(); ArrayList<String> play2 = new ArrayList<>(); ArrayList<String> play3 = new ArrayList<>(); ArrayList<String> dipai = new ArrayList<>(); for (i = 0;i<pokes.size();i++){ String pai = pokes.get(i); if (i>=51){ dipai.add(pai); }else{ if(i % 3 == 0){ play1.add(pai); }else if (i % 3 == 1){ play2.add(pai); }else{ play3.add(pai); } } } System.out.println(); System.out.println(); System.out.println(play1); System.out.println(play2); System.out.println(play3); System.out.println(dipai); System.out.println(); System.out.println(); maopao.change(play1); maopao.change(play2); maopao.change(play3); maopao.change(dipai); } }