小言_互联网的博客

玩扑克牌的代码

406人阅读  评论(0)

package landlords;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Landlords {
public static void main(String[] args) {
Map<Integer,String> pokers = new HashMap<>();
List list = new ArrayList<>();
String[] colors = {“♥”,“♣”,“♠”,“♦”};
String[] numbers = {“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”,“A”,“2”};
int num = 0;
for(String number : numbers) {
for(String color : colors) {
String poker = color + number;
pokers.put(num, poker);
list.add(num);
num++;
}
}
pokers.put(num, “小王”);
list.add(num++);
pokers.put(num, “大王”);
list.add(num );
System.out.println(“所有的牌:” + pokers);
System.out.println(“牌的编号:” +list);
Collections.shuffle(list);
System.out.println(“洗好牌后,编号为” + list);
List liuyifei = new ArrayList<>();
List zhaoliying = new ArrayList<>();
List xiaohei = new ArrayList<>();
List dipai = new ArrayList<>();
for(int i = 0; i < list.size(); i++ ) {
Integer pokerNum = list.get(i);
if(i >= list.size() - 3) {
dipai.add(pokerNum);
}else if(i % 3 == 0 ) {
liuyifei.add(pokerNum);
}else if(i % 3 == 1 ) {
zhaoliying.add(pokerNum);
}else if(i % 3 == 2 ) {
xiaohei.add(pokerNum);
}

}

// System.out.println(“liuyifei” + liuyifei);
// System.out.println(“zhaoliying” + zhaoliying);
// System.out.println(“xiaohei” + xiaohei);
// System.out.println(“dipai” + dipai);
//
System.out.println("-----------");
System.out.println(“liuyifei” + printPoker(liuyifei,pokers));
System.out.println(“zhaoliying” + printPoker(zhaoliying,pokers));
System.out.println(“xiaohei” + printPoker(xiaohei,pokers));
System.out.println(“dipai” + printPoker(dipai,pokers));

}
public static String printPoker(List nums, Map<Integer,String>pokers) {

**Collections.sort(nums);
StringBuilder sb = new StringBuilder();
for(Integer num : nums) {
	String poker = pokers.get(num);
	sb.append(poker + " ");
}
String str = sb.toString();
return str.trim();**

}
}


转载:https://blog.csdn.net/qq_53887167/article/details/115289089
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场