From c092a561e92ef080f447f0cc12813351ddff243c Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 15 Nov 2018 14:17:38 +0800 Subject: [PATCH] =?UTF-8?q?robot=20=E5=87=BA=E7=89=8C=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ratel/landlords/client/SimpleClient.java | 2 +- .../ClientEventListener_CODE_NOT_SUPPORT.java | 16 ++ ...ntEventListener_CODE_SHOW_OPTIONS_PVE.java | 2 +- .../nico/ratel/landlords/entity/Poker.java | 2 +- .../ratel/landlords/entity/PokerSell.java | 24 +- .../org/nico/ratel/landlords/entity/Room.java | 10 + .../landlords/enums/ClientEventCode.java | 2 + .../ratel/landlords/helper/PokerHelper.java | 271 +++++++++++++----- .../robot/AbstractRobotDecisionMakers.java | 2 +- .../robot/MediumRobotDecisionMakers.java | 25 ++ .../landlords/robot/RobotDecisionMakers.java | 23 +- .../robot/SimpleRobotDecisionMakers.java | 12 +- .../nico/ratel/landlords/utils/ListUtils.java | 21 ++ ...verEventListener_CODE_ROOM_CREATE_PVE.java | 61 ++-- ...ventListener_CODE_GAME_LANDLORD_ELECT.java | 3 +- ...botEventListener_CODE_GAME_POKER_PLAY.java | 37 ++- 16 files changed, 383 insertions(+), 130 deletions(-) create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_NOT_SUPPORT.java create mode 100644 landlords-common/src/main/java/org/nico/ratel/landlords/robot/MediumRobotDecisionMakers.java create mode 100644 landlords-common/src/main/java/org/nico/ratel/landlords/utils/ListUtils.java diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java index 86ad648..3cff8ac 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java @@ -44,7 +44,7 @@ public class SimpleClient { } if(serverAddress == null || port == 0){ - String serverInfo = StreamUtils.convertToString(new URL("https://raw.githubusercontent.com/abbychau/ratel/master/serverlist.json")); + String serverInfo = StreamUtils.convertToString(new URL("https://github.com/ainilili/ratel/blob/dev-1.1.0/serverlist.json")); List serverAddressList = Noson.convert(serverInfo, new NoType>() {}); SimplePrinter.printNotice("Please select a server:"); for(int i = 0; i < serverAddressList.size(); i++) { diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_NOT_SUPPORT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_NOT_SUPPORT.java new file mode 100644 index 0000000..f5a65df --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_NOT_SUPPORT.java @@ -0,0 +1,16 @@ +package org.nico.ratel.landlords.client.event; + +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_NOT_SUPPORT extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + SimplePrinter.printNotice(data); + } + + + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVE.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVE.java index 947de36..a131e46 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVE.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVE.java @@ -31,7 +31,7 @@ public class ClientEventListener_CODE_SHOW_OPTIONS_PVE extends ClientEventListen int choose = Integer.valueOf(line); if(0 < choose && choose < 4) { - pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE_PVE, null); + pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE_PVE, String.valueOf(choose)); }else { SimplePrinter.printNotice("Invalid option, please choose again:"); call(channel, data); diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java index 38e505d..98d72fd 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java @@ -66,7 +66,7 @@ public class Poker{ @Override public String toString() { - return "Poker [level=" + level + ", type=" + type + "]"; + return String.valueOf(level.getLevel()) + " "; } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/PokerSell.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/PokerSell.java index fcd5bcf..fe8cdd7 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/PokerSell.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/PokerSell.java @@ -3,6 +3,7 @@ package org.nico.ratel.landlords.entity; import java.util.List; import org.nico.ratel.landlords.enums.SellType; +import org.nico.ratel.landlords.helper.PokerHelper; public class PokerSell { @@ -11,14 +12,24 @@ public class PokerSell { private SellType sellType; private List sellPokers; - - public PokerSell(int score, SellType sellType, List sellPokers) { - super(); - this.score = score; + + private int coreLevel; + + public PokerSell(SellType sellType, List sellPokers, int coreLevel) { + this.score = PokerHelper.parseScore(sellType, coreLevel); this.sellType = sellType; this.sellPokers = sellPokers; + this.coreLevel = coreLevel; } + public final int getCoreLevel() { + return coreLevel; + } + + public final void setCoreLevel(int coreLevel) { + this.coreLevel = coreLevel; + } + public final int getScore() { return score; } @@ -42,5 +53,10 @@ public class PokerSell { public final void setSellPokers(List sellPokers) { this.sellPokers = sellPokers; } + + @Override + public String toString() { + return "PokerSell [sellType=" + sellType + ", sellPokers=" + sellPokers + "]"; + } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java index 78ab41b..1058fa1 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java @@ -31,6 +31,8 @@ public class Room{ private int lastSellClient = -1; private int currentSellClient = -1; + + private int difficultyCoefficient; public Room() { } @@ -42,6 +44,14 @@ public class Room{ this.status = RoomStatus.BLANK; } + public final int getDifficultyCoefficient() { + return difficultyCoefficient; + } + + public final void setDifficultyCoefficient(int difficultyCoefficient) { + this.difficultyCoefficient = difficultyCoefficient; + } + public final RoomType getType() { return type; } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java index 3c06019..72f125f 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java @@ -55,6 +55,8 @@ public enum ClientEventCode implements Serializable{ CODE_GAME_POKER_PLAY_ORDER_ERROR("顺序错误"), CODE_GAME_OVER("游戏结束"), + + CODE_NOT_SUPPORT("不支持"), ; private String msg; diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java index 371e94c..3e9fdbe 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java @@ -11,14 +11,15 @@ import org.nico.ratel.landlords.entity.PokerSell; import org.nico.ratel.landlords.enums.PokerLevel; import org.nico.ratel.landlords.enums.PokerType; import org.nico.ratel.landlords.enums.SellType; +import org.nico.ratel.landlords.utils.ListUtils; public class PokerHelper { - + /** * Print the type of poker style */ public static int pokerPrinterType = 0; - + /** * The list of all pokers, by 54 */ @@ -56,7 +57,7 @@ public class PokerHelper { public static void sortPoker(List pokers){ Collections.sort(pokers, pokerComparator); } - + public static int[] getIndexes(Character[] options, List pokers) { List copyList = new ArrayList<>(pokers.size()); copyList.addAll(pokers); @@ -99,11 +100,10 @@ public class PokerHelper { } public static PokerSell checkPokerType(List pokers) { - + if(pokers != null && ! pokers.isEmpty()) { sortPoker(pokers); - int score = -1; - + int[] levelTable = new int[20]; for(Poker poker: pokers) { levelTable[poker.getLevel().getLevel()] ++; @@ -112,7 +112,7 @@ public class PokerHelper { int startIndex = -1; int endIndex = -1; int count = 0; - + int singleCount = 0; int doubleCount = 0; int threeCount = 0; @@ -148,83 +148,94 @@ public class PokerHelper { } } } - + if(singleCount == doubleCount && singleCount == threeCount && singleCount == 0 && fourCount == 1) { - score = startIndex * 4 + 999; - return new PokerSell(score, SellType.BOMB, pokers); + return new PokerSell(SellType.BOMB, pokers, startIndex); } - + if(singleCount == 2 && startIndex == PokerLevel.LEVEL_SMALL_KING.getLevel() && endIndex == PokerLevel.LEVEL_BIG_KING.getLevel()) { - score = Integer.MAX_VALUE; - return new PokerSell(score, SellType.KING_BOMB, pokers); + return new PokerSell(SellType.KING_BOMB, pokers, PokerLevel.LEVEL_SMALL_KING.getLevel()); } - + if(startIndex == endIndex) { - score = startIndex; if(levelTable[startIndex] == 1) { - return new PokerSell(score, SellType.SINGLE, pokers); + return new PokerSell(SellType.SINGLE, pokers, startIndex); }else if(levelTable[startIndex] == 2) { - return new PokerSell(score, SellType.DOUBLE, pokers); + return new PokerSell(SellType.DOUBLE, pokers, startIndex); }else if(levelTable[startIndex] == 3) { - return new PokerSell(score, SellType.THREE, pokers); + return new PokerSell(SellType.THREE, pokers, startIndex); } } if(endIndex - startIndex == count - 1 && endIndex < PokerLevel.LEVEL_2.getLevel()) { - score = endIndex; if(levelTable[startIndex] == 1 && singleCount > 4 && doubleCount + threeCount + fourCount == 0) { - return new PokerSell(score, SellType.SINGLE_STRAIGHT, pokers); + return new PokerSell(SellType.SINGLE_STRAIGHT, pokers, endIndex); }else if(levelTable[startIndex] == 2 && doubleCount > 2 && singleCount + threeCount + fourCount == 0) { - return new PokerSell(score, SellType.DOUBLE_STRAIGHT, pokers); + return new PokerSell(SellType.DOUBLE_STRAIGHT, pokers, endIndex); }else if(levelTable[startIndex] == 3 && threeCount > 1 && doubleCount + singleCount + fourCount == 0) { - return new PokerSell(score, SellType.THREE_STRAIGHT, pokers); + return new PokerSell(SellType.THREE_STRAIGHT, pokers, endIndex); }else if(levelTable[startIndex] == 4 && fourCount > 1 && doubleCount + threeCount + singleCount == 0) { - return new PokerSell(score, SellType.FOUR_STRAIGHT, pokers); + return new PokerSell(SellType.FOUR_STRAIGHT, pokers, endIndex); } } - + if(threeCount != 0) { - score = threeEndIndex; if(singleCount != 0 && singleCount == threeCount && doubleCount == 0 && fourCount == 0) { if(threeCount == 1) { - return new PokerSell(score, SellType.THREE_ZONES_SINGLE, pokers); + return new PokerSell(SellType.THREE_ZONES_SINGLE, pokers, threeEndIndex); }else { if(threeEndIndex - threeStartIndex + 1 == threeCount && threeEndIndex < PokerLevel.LEVEL_2.getLevel()) { - return new PokerSell(score, SellType.THREE_STRAIGHT_WITH_SINGLE, pokers); + return new PokerSell(SellType.THREE_STRAIGHT_WITH_SINGLE, pokers, threeEndIndex); } } }else if(doubleCount != 0 && doubleCount == threeCount && singleCount == 0 && fourCount == 0) { if(threeCount == 1) { - return new PokerSell(score, SellType.THREE_ZONES_DOUBLE, pokers); + return new PokerSell(SellType.THREE_ZONES_DOUBLE, pokers, threeEndIndex); }else { if(threeEndIndex - threeStartIndex + 1 == threeCount && threeEndIndex < PokerLevel.LEVEL_2.getLevel()) { - return new PokerSell(score, SellType.FOUR_STRAIGHT_WITH_DOUBLE, pokers); + return new PokerSell(SellType.FOUR_STRAIGHT_WITH_DOUBLE, pokers, threeEndIndex); } } } } - + if(fourCount != 0) { - score = fourEndIndex; if(singleCount != 0 && singleCount == fourCount * 2 && doubleCount == 0 && threeCount == 0) { if(fourCount == 1) { - return new PokerSell(score, SellType.FOUR_ZONES_SINGLE, pokers); + return new PokerSell(SellType.FOUR_ZONES_SINGLE, pokers, fourEndIndex); }else { if(fourEndIndex - fourStartIndex + 1 == fourCount && fourEndIndex < PokerLevel.LEVEL_2.getLevel()) { - return new PokerSell(score, SellType.FOUR_STRAIGHT_WITH_SINGLE, pokers); + return new PokerSell(SellType.FOUR_STRAIGHT_WITH_SINGLE, pokers, fourEndIndex); } } }else if(doubleCount != 0 && doubleCount == fourCount * 2 && singleCount == 0 && threeCount == 0) { if(fourCount == 1) { - return new PokerSell(score, SellType.FOUR_ZONES_DOUBLE, pokers); + return new PokerSell(SellType.FOUR_ZONES_DOUBLE, pokers, fourEndIndex); }else { if(fourEndIndex - fourStartIndex + 1 == fourCount && fourEndIndex < PokerLevel.LEVEL_2.getLevel()) { - return new PokerSell(score, SellType.FOUR_STRAIGHT_WITH_DOUBLE, pokers); + return new PokerSell(SellType.FOUR_STRAIGHT_WITH_DOUBLE, pokers, fourEndIndex); } } } } } - return new PokerSell(-1, SellType.ILLEGAL, null); + return new PokerSell(SellType.ILLEGAL, null, -1); + } + + public static int parseScore(SellType sellType, int level) { + if(sellType == SellType.BOMB) { + return level * 4 + 999; + }else if(sellType == SellType.KING_BOMB) { + return Integer.MAX_VALUE; + }else if(sellType == SellType.SINGLE || sellType == SellType.DOUBLE || sellType == SellType.THREE) { + return level; + }else if(sellType == SellType.SINGLE_STRAIGHT || sellType == SellType.DOUBLE_STRAIGHT || sellType == SellType.THREE_STRAIGHT || sellType == SellType.FOUR_STRAIGHT) { + return level; + }else if(sellType == SellType.THREE_ZONES_SINGLE || sellType == SellType.THREE_STRAIGHT_WITH_SINGLE || sellType == SellType.THREE_ZONES_DOUBLE || sellType == SellType.FOUR_STRAIGHT_WITH_DOUBLE) { + return level; + }else if(sellType == SellType.FOUR_ZONES_SINGLE || sellType == SellType.FOUR_STRAIGHT_WITH_SINGLE || sellType == SellType.FOUR_ZONES_DOUBLE || sellType == SellType.FOUR_STRAIGHT_WITH_DOUBLE) { + return level; + } + return -1; } public static List getPoker(int[] indexes, List pokers){ @@ -265,19 +276,19 @@ public class PokerHelper { public static String printPoker(List pokers) { sortPoker(pokers); switch(pokerPrinterType){ - case 0: + case 0: return buildHandStringSharp(pokers); - case 1: + case 1: return buildHandStringRounded(pokers); - case 2: + case 2: return textOnly(pokers); - case 3: + case 3: return textOnlyNoType(pokers); - default: + default: return buildHandStringSharp(pokers); } - + } private static String buildHandStringSharp(List pokers){ StringBuilder builder = new StringBuilder(); @@ -376,37 +387,7 @@ public class PokerHelper { } return builder.toString(); } - - public static List parsePokerSells(List pokers){ - List allPokerSell = new ArrayList<>(); - //single or double or three or four - int count = 0; - int lastLevel = -1; - List ps = new ArrayList<>(); - for(int index = 0; index < pokers.size(); index ++){ - int level = pokers.get(index).getLevel().getLevel(); - if(lastLevel == -1 || level == lastLevel){ - ++ count; - ps.add(pokers.get(index)); - }else{ - count = 0; - ps.clear(); - } - if(count == 1){ - allPokerSell.add(new PokerSell(level, SellType.SINGLE, ps)); - }else if(count == 2){ - allPokerSell.add(new PokerSell(level, SellType.DOUBLE, ps)); - }else if(count == 3){ - allPokerSell.add(new PokerSell(level, SellType.THREE, ps)); - }else if(count == 4){ - allPokerSell.add(new PokerSell(level + 999, SellType.BOMB, ps)); - } - } - - - return null; - } - + public static int parsePokerColligationScore(List pokers){ int score = 0; int count = 0; @@ -423,17 +404,17 @@ public class PokerHelper { if(level == lastLevel){ ++ count; }else{ - count = 0; + count = 1; } if(level < PokerLevel.LEVEL_2.getLevel() && level - 1 == lastLevel){ ++ increase; }else{ - increase = 0; + increase = 1; } - + score += (count + (increase > 4 ? increase : 0)) * level; } - + if(level == PokerLevel.LEVEL_2.getLevel()){ score += level * 2; }else if(level > PokerLevel.LEVEL_2.getLevel()){ @@ -444,4 +425,140 @@ public class PokerHelper { } return score; } + + public static List parsePokerSells(List pokers){ + List pokerSells = new ArrayList<>(); + int size = pokers.size(); + + //king boom + { + if(size > 1) { + if(pokers.get(size - 1).getLevel() == PokerLevel.LEVEL_BIG_KING && pokers.get(size - 2).getLevel() == PokerLevel.LEVEL_SMALL_KING) { + pokerSells.add(new PokerSell(SellType.KING_BOMB, ListUtils.getList(new Poker[] {pokers.get(size - 2), pokers.get(size - 1)}), PokerLevel.LEVEL_BIG_KING.LEVEL_10.getLevel())); + } + } + } + //all single or double + { + int count = 0; + int lastLevel = -1; + List sellPokers = new ArrayList<>(4); + for(int index = 0; index < pokers.size(); index ++) { + Poker poker = pokers.get(index); + int level = poker.getLevel().getLevel(); + if(lastLevel == -1) { + ++ count; + sellPokers.add(poker); + }else { + if(level == lastLevel) { + ++ count; + sellPokers.add(poker); + }else { + count = 1; + sellPokers.clear(); + } + } + if(count == 1) { + pokerSells.add(new PokerSell(SellType.SINGLE, ListUtils.getList(sellPokers), poker.getLevel().getLevel())); + }else if(count == 2){ + pokerSells.add(new PokerSell(SellType.DOUBLE, ListUtils.getList(sellPokers), poker.getLevel().getLevel())); + }else if(count == 3){ + pokerSells.add(new PokerSell(SellType.THREE, ListUtils.getList(sellPokers), poker.getLevel().getLevel())); + }else if(count == 4){ + pokerSells.add(new PokerSell(SellType.BOMB, ListUtils.getList(sellPokers), poker.getLevel().getLevel())); + } + + lastLevel = level; + } + } + //顺子 + { + parsePokerSellStraight(pokerSells, SellType.SINGLE); + parsePokerSellStraight(pokerSells, SellType.DOUBLE); + parsePokerSellStraight(pokerSells, SellType.THREE); + parsePokerSellStraight(pokerSells, SellType.BOMB); + } + + return pokerSells; + } + + private static void parsePokerSellStraight(List pokerSells, SellType sellType) { + int minLenght = -1; + int width = -1; + SellType targetSellType = null; + if(sellType == SellType.SINGLE) { + minLenght = 5; + width = 1; + targetSellType = SellType.SINGLE_STRAIGHT; + }else if(sellType == SellType.DOUBLE) { + minLenght = 3; + width = 2; + targetSellType = SellType.DOUBLE_STRAIGHT; + }else if(sellType == SellType.THREE) { + minLenght = 2; + width = 3; + targetSellType = SellType.THREE_STRAIGHT; + }else if(sellType == SellType.BOMB) { + minLenght = 2; + width = 4; + targetSellType = SellType.FOUR_STRAIGHT; + } + + int increase_1 = 0; + int lastLevel_1 = -1; + List sellPokers_1 = new ArrayList<>(4); + for(int index = 0; index < pokerSells.size(); index ++) { + PokerSell sell = pokerSells.get(index); + + if(sell.getSellType() == sellType) { + int level = sell.getCoreLevel(); + if(lastLevel_1 == -1) { + ++ increase_1; + sellPokers_1.addAll(sell.getSellPokers()); + }else { + if(level - 1 == lastLevel_1 && level != PokerLevel.LEVEL_2.getLevel()) { + ++ increase_1; + sellPokers_1.addAll(sell.getSellPokers()); + }else { + if(increase_1 >= minLenght) { + for(int s = 0; s <= increase_1 - minLenght; s ++) { + int len = minLenght + s; + for(int subIndex = 0; subIndex <= increase_1 - len; subIndex ++) { + pokerSells.add(new PokerSell(targetSellType, ListUtils.getList(sellPokers_1.subList(subIndex * width, (subIndex + len) * width)), level)); + } + } + } + increase_1 = 0; + sellPokers_1.clear(); + } + } + } + } + } + + public static void main(String[] args) { + List pokers = new ArrayList<>(); + pokers.add(new Poker(PokerLevel.LEVEL_3, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_3, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_3, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_3, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.BLANK)); + System.out.println(parsePokerSells(pokers)); + + } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/AbstractRobotDecisionMakers.java b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/AbstractRobotDecisionMakers.java index 5b695ed..b7e1ff0 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/AbstractRobotDecisionMakers.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/AbstractRobotDecisionMakers.java @@ -13,7 +13,7 @@ import org.nico.ratel.landlords.entity.PokerSell; public abstract class AbstractRobotDecisionMakers { - public abstract List howToPlayPokers(PokerSell lastPokerSell, List myPokers); + public abstract PokerSell howToPlayPokers(PokerSell lastPokerSell, List myPokers); public abstract boolean howToChooseLandlord(List leftPokers, List rightPokers, List myPokers); } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/MediumRobotDecisionMakers.java b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/MediumRobotDecisionMakers.java new file mode 100644 index 0000000..c663691 --- /dev/null +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/MediumRobotDecisionMakers.java @@ -0,0 +1,25 @@ +package org.nico.ratel.landlords.robot; + +import java.util.List; + +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.entity.PokerSell; +import org.nico.ratel.landlords.helper.PokerHelper; + +public class MediumRobotDecisionMakers extends AbstractRobotDecisionMakers{ + + @Override + public PokerSell howToPlayPokers(PokerSell lastPokerSell, List myPokers) { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean howToChooseLandlord(List leftPokers, List rightPokers, List myPokers) { + int leftScore = PokerHelper.parsePokerColligationScore(leftPokers); + int rightScore = PokerHelper.parsePokerColligationScore(rightPokers); + int myScore = PokerHelper.parsePokerColligationScore(myPokers); + return myScore >= (leftScore + rightScore)/2; + } + +} diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/RobotDecisionMakers.java b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/RobotDecisionMakers.java index 5f93a5b..7d23600 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/RobotDecisionMakers.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/RobotDecisionMakers.java @@ -1,6 +1,8 @@ package org.nico.ratel.landlords.robot; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.entity.PokerSell; @@ -12,15 +14,24 @@ import org.nico.ratel.landlords.entity.PokerSell; */ public class RobotDecisionMakers { - private static AbstractRobotDecisionMakers decisionMakers = new SimpleRobotDecisionMakers(); + private static Map decisionMakersMap = new HashMap() { + private static final long serialVersionUID = 8541568961784067309L; + { + decisionMakersMap.put(1, new SimpleRobotDecisionMakers()); + decisionMakersMap.put(2, new MediumRobotDecisionMakers()); + } + }; - public static List howToPlayPokers(PokerSell lastPokerSell, List myPokers){ - - return null; + public static boolean contains(int difficultyCoefficient) { + return decisionMakersMap.containsKey(difficultyCoefficient); } - public static boolean howToChooseLandlord(List leftPokers, List rightPokers, List myPokers) { - return decisionMakers.howToChooseLandlord(leftPokers, rightPokers, myPokers); + public static PokerSell howToPlayPokers(int difficultyCoefficient, PokerSell lastPokerSell, List myPokers){ + return decisionMakersMap.get(difficultyCoefficient).howToPlayPokers(lastPokerSell, myPokers); + } + + public static boolean howToChooseLandlord(int difficultyCoefficient, List leftPokers, List rightPokers, List myPokers) { + return decisionMakersMap.get(difficultyCoefficient).howToChooseLandlord(leftPokers, rightPokers, myPokers); } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/SimpleRobotDecisionMakers.java b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/SimpleRobotDecisionMakers.java index 6b9001b..16791d4 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/SimpleRobotDecisionMakers.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/SimpleRobotDecisionMakers.java @@ -1,10 +1,12 @@ package org.nico.ratel.landlords.robot; +import java.util.ArrayList; import java.util.List; import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.entity.PokerSell; -import org.nico.ratel.landlords.helper.PokerHelper; +import org.nico.ratel.landlords.enums.PokerLevel; +import org.nico.ratel.landlords.enums.SellType; /** * @@ -15,16 +17,14 @@ import org.nico.ratel.landlords.helper.PokerHelper; public class SimpleRobotDecisionMakers extends AbstractRobotDecisionMakers{ @Override - public List howToPlayPokers(PokerSell lastPokerSell, List myPokers) { + public PokerSell howToPlayPokers(PokerSell lastPokerSell, List myPokers) { + return null; } @Override public boolean howToChooseLandlord(List leftPokers, List rightPokers, List myPokers) { - int leftScore = PokerHelper.parsePokerColligationScore(leftPokers); - int rightScore = PokerHelper.parsePokerColligationScore(rightPokers); - int myScore = PokerHelper.parsePokerColligationScore(myPokers); - return myScore >= (leftScore + rightScore)/2; + return true; } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/utils/ListUtils.java b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/ListUtils.java new file mode 100644 index 0000000..5742e7f --- /dev/null +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/ListUtils.java @@ -0,0 +1,21 @@ +package org.nico.ratel.landlords.utils; + +import java.util.ArrayList; +import java.util.List; + +public class ListUtils { + + public static List getList(T[] array){ + List list = new ArrayList<>(array.length); + for(T t: array) { + list.add(t); + } + return list; + } + + public static List getList(List source){ + List list = new ArrayList<>(source.size()); + list.addAll(source); + return list; + } +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE_PVE.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE_PVE.java index 92effd2..1c7fde3 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE_PVE.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE_PVE.java @@ -1,12 +1,15 @@ package org.nico.ratel.landlords.server.event; +import org.nico.ratel.landlords.channel.ChannelUtils; import org.nico.ratel.landlords.entity.ClientSide; import org.nico.ratel.landlords.entity.Room; +import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.ClientRole; import org.nico.ratel.landlords.enums.ClientStatus; import org.nico.ratel.landlords.enums.RoomStatus; import org.nico.ratel.landlords.enums.RoomType; import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.robot.RobotDecisionMakers; import org.nico.ratel.landlords.server.ServerContains; public class ServerEventListener_CODE_ROOM_CREATE_PVE implements ServerEventListener{ @@ -14,34 +17,42 @@ public class ServerEventListener_CODE_ROOM_CREATE_PVE implements ServerEventList @Override public void call(ClientSide clientSide, String data) { - Room room = new Room(ServerContains.getServerId()); - room.setType(RoomType.PVE); - room.setStatus(RoomStatus.BLANK); - room.setRoomOwner(clientSide.getNickname()); - room.getClientSideMap().put(clientSide.getId(), clientSide); - room.getClientSideList().add(clientSide); - - clientSide.setRoomId(room.getId()); - ServerContains.ROOM_MAP.put(room.getId(), room); - - ClientSide preClient = clientSide; - //Add robots - for(int index = 1; index < 3; index ++) { - ClientSide robot = new ClientSide(- ServerContains.getClientId(), ClientStatus.PLAYING, null); - robot.setNickname("robot_" + index); - robot.setRole(ClientRole.ROBOT); - preClient.setNext(robot); - robot.setPre(preClient); - robot.setRoomId(room.getId()); - room.getClientSideMap().put(robot.getId(), robot); - room.getClientSideList().add(robot); + int difficultyCoefficient = Integer.valueOf(data); + if(RobotDecisionMakers.contains(difficultyCoefficient)) { + + Room room = new Room(ServerContains.getServerId()); + room.setType(RoomType.PVE); + room.setStatus(RoomStatus.BLANK); + room.setRoomOwner(clientSide.getNickname()); + room.getClientSideMap().put(clientSide.getId(), clientSide); + room.getClientSideList().add(clientSide); + room.setDifficultyCoefficient(difficultyCoefficient); - preClient = robot; + clientSide.setRoomId(room.getId()); + ServerContains.ROOM_MAP.put(room.getId(), room); + + ClientSide preClient = clientSide; + //Add robots + for(int index = 1; index < 3; index ++) { + ClientSide robot = new ClientSide(- ServerContains.getClientId(), ClientStatus.PLAYING, null); + robot.setNickname("robot_" + index); + robot.setRole(ClientRole.ROBOT); + preClient.setNext(robot); + robot.setPre(preClient); + robot.setRoomId(room.getId()); + room.getClientSideMap().put(robot.getId(), robot); + room.getClientSideList().add(robot); + + preClient = robot; + } + preClient.setNext(clientSide); + clientSide.setPre(preClient); + + ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId())); + }else { + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_NOT_SUPPORT, "The current difficulty coefficient is not supported, please pay attention to the following"); } - preClient.setNext(clientSide); - clientSide.setPre(preClient); - ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId())); } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_LANDLORD_ELECT.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_LANDLORD_ELECT.java index cb528e1..85b8d11 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_LANDLORD_ELECT.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -1,7 +1,6 @@ package org.nico.ratel.landlords.server.robot; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; @@ -40,7 +39,7 @@ public class RobotEventListener_CODE_GAME_LANDLORD_ELECT implements RobotEventLi PokerHelper.sortPoker(leftPokers); PokerHelper.sortPoker(rightPokers); - ServerEventListener.get(ServerEventCode.CODE_GAME_LANDLORD_ELECT).call(robot, String.valueOf(RobotDecisionMakers.howToChooseLandlord(leftPokers, rightPokers, landlordPokers))); + ServerEventListener.get(ServerEventCode.CODE_GAME_LANDLORD_ELECT).call(robot, String.valueOf(RobotDecisionMakers.howToChooseLandlord(room.getDifficultyCoefficient(), leftPokers, rightPokers, landlordPokers))); } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_POKER_PLAY.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_POKER_PLAY.java index 6572b73..5a0b3e1 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_POKER_PLAY.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_POKER_PLAY.java @@ -1,8 +1,17 @@ package org.nico.ratel.landlords.server.robot; +import java.util.List; + +import org.nico.ratel.landlords.channel.ChannelUtils; import org.nico.ratel.landlords.entity.ClientSide; +import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.entity.PokerSell; import org.nico.ratel.landlords.entity.Room; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ClientRole; +import org.nico.ratel.landlords.enums.SellType; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.robot.RobotDecisionMakers; import org.nico.ratel.landlords.server.ServerContains; public class RobotEventListener_CODE_GAME_POKER_PLAY implements RobotEventListener{ @@ -10,12 +19,28 @@ public class RobotEventListener_CODE_GAME_POKER_PLAY implements RobotEventListen @Override public void call(ClientSide robot, String data) { Room room = ServerContains.ROOM_MAP.get(robot.getRoomId()); - - if(room.getLastSellClient() == robot.getId()) { - - }else { - PokerSell lastPokerShell = room.getLastPokerShell(); + + PokerSell lastPokerShell = null; + if(room.getLastSellClient() != robot.getId()) { + lastPokerShell = room.getLastPokerShell(); + + PokerSell pokerSell = RobotDecisionMakers.howToPlayPokers(room.getDifficultyCoefficient(), lastPokerShell, robot.getPokers()); + if(pokerSell == null || pokerSell.getSellType() == SellType.ILLEGAL) { + ClientSide turnClient = robot.getNext(); + for(ClientSide client: room.getClientSideList()) { + String result = MapHelper.newInstance() + .put("clientId", robot.getId()) + .put("clientNickname", robot.getNickname()) + .put("nextClientId", turnClient.getId()) + .put("nextClientNickname", turnClient.getNickname()) + .json(); + if(client.getRole() == ClientRole.PLAYER) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_PASS, result); + } + } + }else { + + } } } - }