s-day02-斗地主排序案例(代码)
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
package com.inmind.collections04;
|
||||||
|
|
||||||
|
public class Poker implements Comparable<Poker>{
|
||||||
|
String color;//花色
|
||||||
|
String num;//排面值
|
||||||
|
int sortValue;//排序值
|
||||||
|
|
||||||
|
public Poker(String color, String num, int sortValue) {
|
||||||
|
this.color = color;
|
||||||
|
this.num = num;
|
||||||
|
this.sortValue = sortValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.color+this.num;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Poker o) {
|
||||||
|
return this.sortValue - o.sortValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,21 +1,95 @@
|
|||||||
package com.inmind.collections04;
|
package com.inmind.collections04;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
按照斗地主的规则,完成洗牌发牌的动作。
|
按照斗地主的规则,完成洗牌发牌的动作。
|
||||||
具体规则:
|
具体规则:
|
||||||
|
|
||||||
使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
|
使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
|
||||||
|
|
||||||
分析:
|
分析:
|
||||||
1.准备54张牌(花色+数字)
|
1.准备54张牌(花色+数字)
|
||||||
2.洗牌(打乱顺序)
|
2.洗牌(打乱顺序)
|
||||||
3.创建3个集合对象保存每个玩家的牌,创建1个集合保存底牌
|
3.创建3个集合对象保存每个玩家的牌,创建1个集合保存底牌
|
||||||
4.打印集合的内容(排序后打印)
|
4.打印集合的内容(排序后打印)
|
||||||
|
|
||||||
建议:自定义一个扑克牌类,有花色属性,数字属性(排序),54个牌对象
|
建议:自定义一个扑克牌类,有花色属性,数字属性(排序),54个牌对象
|
||||||
*/
|
*/
|
||||||
public class Test16 {
|
public class Test16 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String[] colors = {"♠", "♥", "♣", "♦"};
|
//创建54张牌的集合
|
||||||
|
ArrayList<Poker> pokers = new ArrayList<>();
|
||||||
|
//创建54牌
|
||||||
|
String[] colors = {"♦","♣", "♥" ,"♠" };
|
||||||
String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
|
String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
|
||||||
|
int sortValue = 0;
|
||||||
|
for (String num : nums) {
|
||||||
|
for (String color : colors) {
|
||||||
|
Poker poker = new Poker(color, num, sortValue);
|
||||||
|
pokers.add(poker);
|
||||||
|
sortValue++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pokers.add(new Poker("", "小王", sortValue++));
|
||||||
|
pokers.add(new Poker("", "大王", sortValue++));
|
||||||
|
System.out.println(pokers);
|
||||||
|
System.out.println(pokers.size());
|
||||||
|
//洗牌(打乱顺序)
|
||||||
|
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 %3 == 0
|
||||||
|
玩家2 1 4 7 %3 == 1
|
||||||
|
玩家3 2 5 8 %3 == 2
|
||||||
|
*/
|
||||||
|
for (int i = 0; i < pokers.size(); i++) {
|
||||||
|
if (i >= 51) {
|
||||||
|
dipai.add(pokers.get(i));
|
||||||
|
}else{
|
||||||
|
switch (i % 3) {
|
||||||
|
case 0:
|
||||||
|
player1.add(pokers.get(i));
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
player2.add(pokers.get(i));
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
player3.add(pokers.get(i));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//把各自的牌整理好(排序)(自然排序)
|
||||||
|
/*Collections.sort(player1);
|
||||||
|
Collections.sort(player2);
|
||||||
|
Collections.sort(player3);
|
||||||
|
Collections.sort(dipai);*/
|
||||||
|
//使用比较器的方法
|
||||||
|
sortPokers(player1,player2,player3,dipai);
|
||||||
|
|
||||||
|
//抓完牌了,看牌
|
||||||
|
System.out.println("玩家1:"+player1);
|
||||||
|
System.out.println("玩家2:"+player2);
|
||||||
|
System.out.println("玩家3:"+player3);
|
||||||
|
System.out.println("底牌:"+dipai);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//自定义一个排序功能,对扑克牌排序(降序)
|
||||||
|
public static void sortPokers(ArrayList<Poker>... pokers) {
|
||||||
|
for (ArrayList<Poker> list : pokers) {
|
||||||
|
Collections.sort(list, new Comparator<Poker>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Poker o1, Poker o2) {
|
||||||
|
return o2.sortValue-o1.sortValue;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user