mirror of
https://github.com/tiennm99/caro.git
synced 2026-07-30 00:20:09 +00:00
牌的类型校验算法
This commit is contained in:
@@ -7,6 +7,7 @@ import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
|
||||
import org.nico.ratel.landlords.enums.RoomStatus;
|
||||
import org.nico.ratel.landlords.enums.SellType;
|
||||
|
||||
public class Room implements Serializable{
|
||||
|
||||
@@ -26,6 +27,8 @@ public class Room implements Serializable{
|
||||
|
||||
private List<Poker> lastSellPokers;
|
||||
|
||||
private SellType sellType;
|
||||
|
||||
private int lastSellClient;
|
||||
|
||||
public Room(int id) {
|
||||
@@ -35,6 +38,14 @@ public class Room implements Serializable{
|
||||
this.status = RoomStatus.BLANK;
|
||||
}
|
||||
|
||||
public final SellType getSellType() {
|
||||
return sellType;
|
||||
}
|
||||
|
||||
public final void setSellType(SellType sellType) {
|
||||
this.sellType = sellType;
|
||||
}
|
||||
|
||||
public int getLastSellClient() {
|
||||
return lastSellClient;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.nico.ratel.landlords.enums;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public enum SellType implements Serializable{
|
||||
|
||||
ILLEGAL("非合法"),
|
||||
|
||||
ROCKET("火箭"),
|
||||
|
||||
BOMB("炸弹"),
|
||||
|
||||
KING_BOMB("王炸"),
|
||||
|
||||
SINGLE("单个牌"),
|
||||
|
||||
DOUBLE("对子牌"),
|
||||
|
||||
THREE("三张牌"),
|
||||
|
||||
THREE_ZONES_A("三带一"),
|
||||
|
||||
THREE_ZONES_TWO("三带二"),
|
||||
|
||||
SINGLE_STRAIGHT("单顺子"),
|
||||
|
||||
DOUBLE_STRAIGHT("双顺子"),
|
||||
|
||||
THREE_STRAIGHT("三顺子"),
|
||||
|
||||
FOUR_STRAIGHT("四顺子"),
|
||||
|
||||
THREE_STRAIGHT_WITH_SINGLE("飞机带单牌"),
|
||||
|
||||
THREE_STRAIGHT_WITH_DOUBLE("飞机带对牌"),
|
||||
|
||||
FOUR_STRAIGHT_WITH_SINGLE("四顺子带单"),
|
||||
|
||||
FOUR_STRAIGHT_WITH_DOUBLE("四顺子带对"),
|
||||
;
|
||||
|
||||
private String msg;
|
||||
|
||||
private SellType(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public final String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public final void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -18,7 +18,7 @@ public class ServerEventListener_CODE_PLAY_ROUND implements ServerEventListener<
|
||||
Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId());
|
||||
int[] indexes = serverTransferData.getData();
|
||||
|
||||
if(PokerHelper.checkPoker(indexes, clientSide.getPokers())){
|
||||
if(PokerHelper.checkPokerIndex(indexes, clientSide.getPokers())){
|
||||
if(room.getLastSellClient() != clientSide.getId() && room.getLastSellPokers() != null){
|
||||
// Compare the brand
|
||||
}else{
|
||||
|
||||
+77
-13
@@ -8,22 +8,23 @@ import java.util.List;
|
||||
import org.nico.ratel.landlords.entity.Poker;
|
||||
import org.nico.ratel.landlords.enums.PokerLevel;
|
||||
import org.nico.ratel.landlords.enums.PokerType;
|
||||
import org.nico.ratel.landlords.enums.SellType;
|
||||
|
||||
public class PokerHelper {
|
||||
|
||||
private static List<Poker> basePokers = new ArrayList<Poker>(54);
|
||||
|
||||
|
||||
private static Comparator<Poker> pokerComparator = new Comparator<Poker>() {
|
||||
@Override
|
||||
public int compare(Poker o1, Poker o2) {
|
||||
return o1.getLevel().getLevel() - o2.getLevel().getLevel();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
static {
|
||||
PokerLevel[] pokerLevels = PokerLevel.values();
|
||||
PokerType[] pokerTypes = PokerType.values();
|
||||
|
||||
|
||||
for(PokerLevel level: pokerLevels) {
|
||||
if(level == PokerLevel.LEVEL_BIG_KING) {
|
||||
basePokers.add(new Poker(level, PokerType.BLANK));
|
||||
@@ -41,12 +42,12 @@ public class PokerHelper {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void sortPoker(List<Poker> pokers){
|
||||
Collections.sort(pokers, pokerComparator);
|
||||
}
|
||||
|
||||
public static boolean checkPoker(int[] indexes, List<Poker> pokers){
|
||||
|
||||
public static boolean checkPokerIndex(int[] indexes, List<Poker> pokers){
|
||||
boolean access = true;
|
||||
for(int index: indexes){
|
||||
if(index >= pokers.size()){
|
||||
@@ -55,7 +56,70 @@ public class PokerHelper {
|
||||
}
|
||||
return access;
|
||||
}
|
||||
|
||||
|
||||
public static SellType checkPokerType(List<Poker> pokers) {
|
||||
if(pokers != null && pokers.isEmpty()) {
|
||||
sortPoker(pokers);
|
||||
|
||||
int[] levelTable = new int[20];
|
||||
for(Poker poker: pokers) {
|
||||
levelTable[poker.getLevel().getLevel()] ++;
|
||||
}
|
||||
|
||||
int startIndex = -1;
|
||||
int endIndex = -1;
|
||||
int count = -1;
|
||||
|
||||
int singleCount = 0;
|
||||
int doubleCount = 0;
|
||||
int threeCount = 0;
|
||||
int fourCount = 0;
|
||||
for(int index = 0; index < levelTable.length; index ++) {
|
||||
int value = levelTable[index];
|
||||
if(value != 0) {
|
||||
endIndex = index;
|
||||
count ++;
|
||||
if(startIndex == -1) {
|
||||
startIndex = index;
|
||||
}
|
||||
if(value == 1) {
|
||||
singleCount ++;
|
||||
}else if(value == 2) {
|
||||
doubleCount ++;
|
||||
}else if(value == 3) {
|
||||
threeCount ++;
|
||||
}else if(value == 4) {
|
||||
fourCount ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(count == 1) {
|
||||
if(levelTable[startIndex] == 1) {
|
||||
return SellType.SINGLE;
|
||||
}else if(levelTable[startIndex] == 2) {
|
||||
return SellType.DOUBLE;
|
||||
}else if(levelTable[startIndex] == 3) {
|
||||
return SellType.THREE;
|
||||
}else if(levelTable[startIndex] == 4) {
|
||||
return SellType.BOMB;
|
||||
}
|
||||
}
|
||||
if(endIndex - startIndex == count - 1 && endIndex < PokerLevel.LEVEL_2.getLevel()) {
|
||||
if(levelTable[startIndex] == 1) {
|
||||
return SellType.SINGLE_STRAIGHT;
|
||||
}else if(levelTable[startIndex] == 2) {
|
||||
return SellType.DOUBLE_STRAIGHT;
|
||||
}else if(levelTable[startIndex] == 3) {
|
||||
return SellType.THREE_STRAIGHT;
|
||||
}else if(levelTable[startIndex] == 4) {
|
||||
return SellType.FOUR_STRAIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return SellType.ILLEGAL;
|
||||
}
|
||||
|
||||
public static List<Poker> getPoker(int[] indexes, List<Poker> pokers){
|
||||
List<Poker> resultPokers = new ArrayList<>(indexes.length);
|
||||
for(int index: indexes){
|
||||
@@ -64,12 +128,12 @@ public class PokerHelper {
|
||||
sortPoker(resultPokers);
|
||||
return resultPokers;
|
||||
}
|
||||
|
||||
|
||||
public static boolean comparePoker(List<Poker> pres, List<Poker> currents){
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static List<List<Poker>> distributePoker(){
|
||||
Collections.shuffle(basePokers);
|
||||
List<List<Poker>> pokersList = new ArrayList<List<Poker>>();
|
||||
@@ -90,12 +154,12 @@ public class PokerHelper {
|
||||
}
|
||||
return pokersList;
|
||||
}
|
||||
|
||||
|
||||
public static String unfoldPoker(List<Poker> pokers, boolean serialFlag) {
|
||||
sortPoker(pokers);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
if(pokers != null && pokers.size() > 0) {
|
||||
|
||||
|
||||
for(int index = 0; index < pokers.size(); index ++) {
|
||||
if(index == 0) {
|
||||
builder.append("Poker: ┌──┐");
|
||||
@@ -144,7 +208,7 @@ public class PokerHelper {
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<List<Poker>> pokersList = distributePoker();
|
||||
System.out.println(unfoldPoker(pokersList.get(3), false));
|
||||
|
||||
Reference in New Issue
Block a user