进阶day03-斗地主案例的分析以及Comparable&Comparator的使用
This commit is contained in:
22
javaSE-day03/src/com/inmind/comapre05/Poker.java
Normal file
22
javaSE-day03/src/com/inmind/comapre05/Poker.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.inmind.comapre05;
|
||||||
|
|
||||||
|
public class Poker implements Comparable<Poker>{
|
||||||
|
String content; //花色+数字
|
||||||
|
Integer num; //对应牌的大小的标记
|
||||||
|
|
||||||
|
public Poker(String content, Integer num) {
|
||||||
|
this.content = content;
|
||||||
|
this.num = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Poker o) {
|
||||||
|
//我-它 升序 ;它-我 降序
|
||||||
|
return o.num-this.num;
|
||||||
|
}
|
||||||
|
}
|
||||||
101
javaSE-day03/src/com/inmind/comapre05/Test16.java
Normal file
101
javaSE-day03/src/com/inmind/comapre05/Test16.java
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
package com.inmind.comapre05;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
/*
|
||||||
|
按照斗地主的规则,完成洗牌发牌的动作。
|
||||||
|
具体规则:
|
||||||
|
|
||||||
|
使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
|
||||||
|
|
||||||
|
分析:
|
||||||
|
1.准备54张牌(花色+数字)
|
||||||
|
2.洗牌(打乱顺序)
|
||||||
|
3.创建3个集合对象保存每个玩家的牌,创建1个集合保存底牌
|
||||||
|
4.打印集合的内容
|
||||||
|
|
||||||
|
注意:最后打印牌时,要有排序的效果
|
||||||
|
*/
|
||||||
|
public class Test16 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//1.准备54张牌(花色+数字)
|
||||||
|
String[] colors = {"♣", "♦", "♥", "♠"};
|
||||||
|
String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
//创建一个集合保存牌
|
||||||
|
ArrayList<Poker> pokers = new ArrayList<>();
|
||||||
|
|
||||||
|
for (String num : nums) {
|
||||||
|
for (String color : colors) {
|
||||||
|
String content = color + num;
|
||||||
|
pokers.add(new Poker(content,index++));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pokers.add(new Poker("小王",index++));
|
||||||
|
pokers.add(new Poker("大王",index++));
|
||||||
|
|
||||||
|
System.out.println(pokers);
|
||||||
|
//2.洗牌(打乱顺序)
|
||||||
|
Collections.shuffle(pokers);
|
||||||
|
System.out.println("打乱之后:"+pokers);
|
||||||
|
|
||||||
|
//3.创建3个集合对象保存每个玩家的牌,创建1个集合保存底牌
|
||||||
|
ArrayList<Poker> player1 = new ArrayList<>();
|
||||||
|
ArrayList<Poker> player2 = new ArrayList<>();
|
||||||
|
ArrayList<Poker> player3 = new ArrayList<>();
|
||||||
|
ArrayList<Poker> dipai = new ArrayList<>();
|
||||||
|
|
||||||
|
/*
|
||||||
|
按顺序,轮着抓
|
||||||
|
玩家1 0 3 6 index%3 == 0
|
||||||
|
玩家2 1 4 7 index%3 == 1
|
||||||
|
玩家3 2 5 8 index%3 == 2
|
||||||
|
|
||||||
|
最后三张牌都给底牌
|
||||||
|
*/
|
||||||
|
|
||||||
|
for (int i = 0; i < pokers.size(); i++) {
|
||||||
|
Poker poker = pokers.get(i);
|
||||||
|
//最后三张都给底牌,其他的判断下分别发给我每个玩家
|
||||||
|
if (i >= 51) {//最后三张都给底牌
|
||||||
|
dipai.add(poker);
|
||||||
|
} else {
|
||||||
|
switch (i%3) {
|
||||||
|
case 0:
|
||||||
|
player1.add(poker);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
player2.add(poker);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
player3.add(poker);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//4.打印集合的内容
|
||||||
|
//4.1对每个集合进行排序
|
||||||
|
//4.2使用比较器,对原本扑克牌,降序的排序,修改为升序
|
||||||
|
Comparator<Poker> comparator = new Comparator<Poker>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Poker o1, Poker o2) {
|
||||||
|
return o1.num - o2.num;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Collections.sort(player1,comparator);
|
||||||
|
Collections.sort(player2,comparator);
|
||||||
|
Collections.sort(player3,comparator);
|
||||||
|
Collections.sort(dipai,comparator);
|
||||||
|
|
||||||
|
System.out.println("玩家1:"+player1);
|
||||||
|
System.out.println("玩家2:"+player2);
|
||||||
|
System.out.println("玩家3:"+player3);
|
||||||
|
System.out.println("底牌:"+dipai);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user