Java语言基础
集合框架
- 发好的牌没有排序
import java.util.ArrayList;
import java.util.Collections;
public class MyTest {
public static void main(String[] args) {
/* 模拟斗地主洗牌和发牌,发好的牌没有排序*/
//首先得有一副牌
//花色 A-K
//创建一个牌盒子
ArrayList<String> pokerBox = new ArrayList<>();
//生成54张牌,放进排行
String[] colors={"♥","♠","♣","♦"};
String[] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (String color : colors) {
for (String num : nums) {
pokerBox.add(color.concat(num));
}
}
//手动添加大小王
pokerBox.add("☀");//大王
pokerBox.add("🌙");//小王
//洗牌(洗三次)
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);
//发牌 三个人 人手一副 留三张底牌
ArrayList<String> 高进 = new ArrayList<>();
ArrayList<String> 刀仔 = new ArrayList<>();
ArrayList<String> 星仔 = new ArrayList<>();
ArrayList<String> 底牌 = new ArrayList<>();
//一张一张发牌的方法:
//高进 0 3 6 9 %3==0
//刀仔 1 4 7 10 %3==1
//星仔 2 5 8 11 %3==2
for (int i = 0; i < pokerBox.size(); i++) {
if(i>=pokerBox.size()-3){
底牌.add(pokerBox.get(i));
}else if(i%3 == 0){
高进.add(pokerBox.get(i));
}else if(i % 3 ==1 ){
刀仔.add(pokerBox.get(i));
}else{
星仔.add(pokerBox.get(i));
}
}
//看牌
lookPoker("高进", 高进);
lookPoker("刀仔",刀仔);
lookPoker("星仔", 星仔);
lookPoker("底牌", 底牌);
}
private static void lookPoker(String name, ArrayList<String> list) {
System.out.println(name);
for (String s : list) {
System.out.print(s+"\t");
}
System.out.println();
}
}
/*结果:(每次运行结果都不一样)
高进
♣8 ♦7 ♥10 🌙 ♠5 ♣A ♣5 ♠6 ♦Q ♣J ♥K ♠2 ♣10 ♣7 ♣K ♦9 ♣2
刀仔
♠K ♦8 ♥6 ♣4 ♠A ♠3 ♣6 ♥J ♦6 ♥2 ♦2 ☀ ♠7 ♦4 ♦J ♠10 ♥9
星仔
♠Q ♥Q ♥A ♥4 ♦10 ♠8 ♣3 ♦A ♠J ♥3 ♥5 ♠4 ♦3 ♦K ♦5 ♥8 ♥7
底牌
♣Q ♠9 ♣9
*/
- 发好的牌自动排序
利用TreeSet、HashMap集合的特点
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class MyTest {
public static void main(String[] args) {
/*模拟斗地主洗牌和发牌,发好的牌自动排好序*/
HashMap<Integer,String> hashMap = new HashMap<>();
//创建一个牌盒;
ArrayList<Integer> pokerBox = new ArrayList<>();
//生成54张牌,放进牌盒中;
String[] colors={"♥","♠","♣","♦"};
String[] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
int index=0;
for (String num : nums) {
for (String color: colors) {
String s = color.concat(num);
hashMap.put(index,s);
pokerBox.add(index);
index++;
}
}
//放大小王
hashMap.put(index,"☀");
pokerBox.add(index);
index++;
hashMap.put(index,"🌙");
pokerBox.add(index);
//洗牌(洗三次)
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);
//发牌
TreeSet<Integer> 高进 = new TreeSet<>();
TreeSet<Integer> 刀仔 = new TreeSet<>();
TreeSet<Integer> 星仔 = new TreeSet<>();
TreeSet<Integer> 底牌 = new TreeSet<>();
for (int i = 0; i < pokerBox.size(); i++) {
if(i>=pokerBox.size()-3){
底牌.add(pokerBox.get(i));
}else if(i%3 == 0){
高进.add(pokerBox.get(i));
}else if(i % 3 ==1 ){
刀仔.add(pokerBox.get(i));
}else{
星仔.add(pokerBox.get(i));
}
}
//看牌
lookPoker("高进", 高进,hashMap);
lookPoker("刀仔",刀仔,hashMap);
lookPoker("星仔", 星仔,hashMap);
lookPoker("底牌", 底牌,hashMap);
}
private static void lookPoker(String name, TreeSet<Integer> treeSet, HashMap<Integer,String> hashMap) {
System.out.println(name+"的牌是:");
for (Integer key : treeSet) {
String string = hashMap.get(key);
System.out.print(string+"\t");
}
System.out.println();
}
}
/*结果:
高进的牌是:
♥A ♣2 ♥3 ♣3 ♥5 ♠5 ♣5 ♦5 ♠6 ♦7 ♣8 ♦8 ♣10 ♦10 ♠J ♦J ♦K
刀仔的牌是:
♠A ♦2 ♥4 ♠4 ♦4 ♣6 ♦6 ♣9 ♦9 ♥10 ♠10 ♥J ♠Q ♥K ♠K ♣K 🌙
星仔的牌是:
♣A ♦A ♥2 ♠3 ♣4 ♥6 ♠7 ♣7 ♥8 ♠8 ♥9 ♠9 ♣J ♥Q ♣Q ♦Q ☀
底牌的牌是:
♠2 ♦3 ♥7
*/