在判断牌的类型的时候,比如判断387654的牌型的时候,需要首先对牌进行排序,这样才能判断这6张牌是个顺子。
本篇简要介绍下 如何对一手牌或很多牌进行排序。
在前几篇定义牌Card的属性的时候,有个grade字段,这个字段就是用来对牌进行比较和排序的。
比如大王的grade是17,小王的grade是16,这样大王>小王,其它单张牌的比较是类似的。
1.根据牌的id,获得一张牌的等级
- /**
- * 根据牌的id,获得一张牌的等级
- *
- * @param id
- * 牌的id
- * @return 与牌数字对应的等级
- */
- public static int getGrade(int id) {
- if (id < 1 || id > 54) {
- throw new RuntimeException("牌的数字不合法");
- }
- int grade = 0;
- // 2个王必须放在前边判断
- if (id == 53) {
- grade = 16;
- } else if (id == 54) {
- grade = 17;
- }
- else {
- int modResult = id % 13;
- if (modResult == 1) {
- grade = 14;
- } else if (modResult == 2) {
- grade = 15;
- } else if (modResult == 3) {
- grade = 3;
- } else if (modResult == 4) {
- grade = 4;
- } else if (modResult == 5) {
- grade = 5;
- } else if (modResult == 6) {
- grade = 6;
- } else if (modResult == 7) {
- grade = 7;
- } else if (modResult == 8) {
- grade = 8;
- } else if (modResult == 9) {
- grade = 9;
- } else if (modResult == 10) {
- grade = 10;
- } else if (modResult == 11) {
- grade = 11;
- } else if (modResult == 12) {
- grade = 12;
- } else if (modResult == 0) {
- grade = 13;
- }
- }
- return grade;
- }
/** * 根据牌的id,获得一张牌的等级 * * @param id * 牌的id * @return 与牌数字对应的等级 */ public static int getGrade(int id) { if (id < 1 || id > 54) { throw new RuntimeException("牌的数字不合法"); } int grade = 0; // 2个王必须放在前边判断 if (id == 53) { grade = 16; } else if (id == 54) { grade = 17; } else { int modResult = id % 13; if (modResult == 1) { grade = 14; } else if (modResult == 2) { grade = 15; } else if (modResult == 3) { grade = 3; } else if (modResult == 4) { grade = 4; } else if (modResult == 5) { grade = 5; } else if (modResult == 6) { grade = 6; } else if (modResult == 7) { grade = 7; } else if (modResult == 8) { grade = 8; } else if (modResult == 9) { grade = 9; } else if (modResult == 10) { grade = 10; } else if (modResult == 11) { grade = 11; } else if (modResult == 12) { grade = 12; } else if (modResult == 0) { grade = 13; } } return grade; }
2.对牌进行排序,从小到大,使用冒泡排序
- /**
- * 对牌进行排序,从小到大,使用冒泡排序,此种方法不是很好
- *
- * @param cards
- * 牌
- */
- public static boolean bubbleSortCards(List<Card> cards) {
- if (cards == null) {
- return false;
- }
- int size = cards.size();
- // 冒泡排序,从左到右,从小到大
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size – i – 1; j++) {
- int gradeOne = cards.get(j).grade;
- int gradeTwo = cards.get(j + 1).grade;
- boolean isExchange = false;
- if (gradeOne > gradeTwo) {
- isExchange = true;
- } else if (gradeOne == gradeTwo) {
- // 2张牌的grade相同
- CardBigType type1 = cards.get(j).bigType;
- CardBigType type2 = cards.get(j + 1).bigType;
- // 从做到右,方块、梅花、红桃、黑桃
- if (type1.equals(CardBigType.HEI_TAO)) {
- isExchange = true;
- } else if (type1.equals(CardBigType.HONG_TAO)) {
- if (type2.equals(CardBigType.MEI_HUA)
- || type2.equals(CardBigType.FANG_KUAI)) {
- isExchange = true;
- }
- } else if (type1.equals(CardBigType.MEI_HUA)) {
- if (type2.equals(CardBigType.FANG_KUAI)) {
- isExchange = true;
- }
- }
- }
- if (isExchange) {
- Card cardOne = cards.get(j);
- Card cardTwo = cards.get(j + 1);
- // 交换
- cards.set(j + 1, cardOne);
- cards.set(j, cardTwo);
- }
- }
- }
- return true;
- }
/** * 对牌进行排序,从小到大,使用冒泡排序,此种方法不是很好 * * @param cards * 牌 */ public static boolean bubbleSortCards(List<Card> cards) { if (cards == null) { return false; } int size = cards.size(); // 冒泡排序,从左到右,从小到大 for (int i = 0; i < size; i++) { for (int j = 0; j < size – i – 1; j++) { int gradeOne = cards.get(j).grade; int gradeTwo = cards.get(j + 1).grade; boolean isExchange = false; if (gradeOne > gradeTwo) { isExchange = true; } else if (gradeOne == gradeTwo) { // 2张牌的grade相同 CardBigType type1 = cards.get(j).bigType; CardBigType type2 = cards.get(j + 1).bigType; // 从做到右,方块、梅花、红桃、黑桃 if (type1.equals(CardBigType.HEI_TAO)) { isExchange = true; } else if (type1.equals(CardBigType.HONG_TAO)) { if (type2.equals(CardBigType.MEI_HUA) || type2.equals(CardBigType.FANG_KUAI)) { isExchange = true; } } else if (type1.equals(CardBigType.MEI_HUA)) { if (type2.equals(CardBigType.FANG_KUAI)) { isExchange = true; } } } if (isExchange) { Card cardOne = cards.get(j); Card cardTwo = cards.get(j + 1); // 交换 cards.set(j + 1, cardOne); cards.set(j, cardTwo); } } } return true; }
3.使用JDK自带的类库进行排序
- /**
- * 对牌进行排序,从小到大,比较器为CardComparator
- *
- * @param cards
- * 牌的集合
- */
- public static void sortCards(List<Card> cards) {
- // 策略模式;复用已有类;
- Collections.sort(cards, new CardComparator());
- }
- public class CardComparator implements Comparator<Card> {
- public int compare(Card card1, Card card2) {
- int result = -1;
- int grade1 = card1.grade;
- int grade2 = card2.grade;
- if (grade1 > grade2) {
- result = 1;
- } else if (grade1 < grade2) {
- result = -1;
- } else {
- // 等级相同的情况,比如都是9
- CardBigType bigType1 = card1.bigType;
- CardBigType bigType2 = card2.bigType;
- // 从左到右,方块、梅花、红桃、黑桃
- if (bigType1.equals(CardBigType.HEI_TAO)) {
- result = 1;
- } else if (bigType1.equals(CardBigType.HONG_TAO)) {
- if (bigType2.equals(CardBigType.MEI_HUA)
- || bigType2.equals(CardBigType.FANG_KUAI)) {
- result = 1;
- }
- } else if (bigType1.equals(CardBigType.MEI_HUA)) {
- if (bigType2.equals(CardBigType.FANG_KUAI)) {
- result = 1;
- }
- }
- // 2张牌的等级不可能完全相同,程序内部采用这种设计
- else {
- result = -1;
- }
- }
- return result;
- }
- }
/** * 对牌进行排序,从小到大,比较器为CardComparator * * @param cards * 牌的集合 */ public static void sortCards(List<Card> cards) { // 策略模式;复用已有类; Collections.sort(cards, new CardComparator()); } public class CardComparator implements Comparator<Card> { public int compare(Card card1, Card card2) { int result = -1; int grade1 = card1.grade; int grade2 = card2.grade; if (grade1 > grade2) { result = 1; } else if (grade1 < grade2) { result = -1; } else { // 等级相同的情况,比如都是9 CardBigType bigType1 = card1.bigType; CardBigType bigType2 = card2.bigType; // 从左到右,方块、梅花、红桃、黑桃 if (bigType1.equals(CardBigType.HEI_TAO)) { result = 1; } else if (bigType1.equals(CardBigType.HONG_TAO)) { if (bigType2.equals(CardBigType.MEI_HUA) || bigType2.equals(CardBigType.FANG_KUAI)) { result = 1; } } else if (bigType1.equals(CardBigType.MEI_HUA)) { if (bigType2.equals(CardBigType.FANG_KUAI)) { result = 1; } } // 2张牌的等级不可能完全相同,程序内部采用这种设计 else { result = -1; } } return result; } }
相关阅读: