55public class FlowerPoker
extends Gamble {
58 super(host, opponent);
62 public String toString() {
63 return "Flower Poker";
67 public void gamble() {
68 getHost().getGambling().getFlowers().clear();
69 getOpponent().getGambling().getFlowers().clear();
71 getHost().getGambling().getGameFlowers().clear();
72 getOpponent().getGambling().getGameFlowers().clear();
74 for (
int i = 0; i < 5; i++) {
86 getHost().getGambling().getFlowers().add(hostFlower);
87 getOpponent().getGambling().getFlowers().add(opponentFlower);
89 final Ranking hostResult = getRank(getHost());
90 final Ranking opponentResult = getRank(getOpponent());
92 System.out.println(
"Looking for place to play");
93 boolean[] canPlay = {
false,
false };
96 int maxX = GAMBLING_ZONE.getMaximumX() - GAMBLING_ZONE.getMinimumX();
97 int maxY = GAMBLING_ZONE.getMaximumY() - GAMBLING_ZONE.getMinimumY();
98 System.out.println(
"maxX: " + maxX +
", " + maxY);
99 positionOne =
new Position(GAMBLING_ZONE.getMinimumX() +
Utility.random(maxX), GAMBLING_ZONE.getMinimumY() +
Utility.random(maxY));
100 positionTwo = positionOne.
create(positionOne.
getX() + 1, positionOne.
getY());
101 canPlay[0] = getHost().getGambling().canPlayFlowerPokerAtPositon(getHost(), positionTwo);
102 canPlay[1] = getHost().getGambling().canPlayFlowerPokerAtPositon(getHost(), positionOne);
104 System.out.println(canPlay[0] +
" - " + canPlay[1]);
105 }
while(canPlay[0] ==
false || canPlay[1] ==
false);
107 System.out.println(
"Found a place to play");
109 Position finalPositionOne = positionOne;
110 Position finalPositionTwo = positionTwo;
115 protected void execute() {
116 if(getHost().getGambling().getStage() !=
GambleStage.IN_PROGRESS) {
122 getHost().move(finalPositionOne);
123 getOpponent().move(finalPositionTwo);
129 getHost().speak(
"I got " + hostResult.name().toLowerCase().replaceAll(
"_",
" "));
130 getOpponent().speak(
"I got " + opponentResult.name().toLowerCase().replaceAll(
"_",
" "));
133 getHost().getGambling().finish(getHost(), getOpponent(), hostResult.ordinal(), opponentResult.ordinal());
142 private void plant(
Task gambleTask) {
146 Flowers hostFlower = getHost().getGambling().getFlowers().get(type);
147 Flowers opponentFlower = getOpponent().getGambling().getFlowers().get(type);
149 Player[] players =
new Player[] { getHost(), getOpponent() };
152 protected void execute() {
153 if(getHost().getGambling().getStage() !=
GambleStage.IN_PROGRESS) {
160 for(
Player player : players)
164 hostFlower = getHost().getGambling().getFlowers().get(type);
165 opponentFlower = getOpponent().getGambling().getFlowers().get(type);
167 if (hostFlower ==
null || opponentFlower ==
null) {
172 for(
Player player : players) {
173 Flowers flowers = player.equals(getHost()) ? hostFlower : opponentFlower;
177 player.getGambling().getGameFlowers().add(gameFlower);
179 player.movement.walkTo(player.getPosition().south());
184 for(
Player player : players) {
185 Flowers flowers = player.equals(getHost()) ? hostFlower : opponentFlower;
186 player.speak(
"I planted a " +
Utility.formatText(flowers.name().toLowerCase()) +
" flower.");
191 if(hostFlower.name().equalsIgnoreCase(
"WHITE") || hostFlower.name().equalsIgnoreCase(
"BLACK") ||
192 opponentFlower.name().equalsIgnoreCase(
"WHITE") || opponentFlower.name().equalsIgnoreCase(
"BLACK")) {
193 getHost().getGambling().finish(getHost(), getOpponent(), 0, 0);
213private enum Ranking {
223 private static Ranking getRank(
Player player) {
224 ArrayList<Flowers> flowers =
new ArrayList<Flowers>();
225 flowers.addAll(player.getGambling().getFlowers());
226 Collections.sort(flowers);
227 Map<Integer, Integer> pairs = getPairs(flowers);
228 for (
int i = 0; i < pairs.size(); i++) {
229 if (pairs.get(i) ==
null) {
232 if (pairs.get(i).intValue() == 5) {
233 return Ranking.ROYAL_KIND;
236 if (pairs.size() == 2) {
237 if ((pairs.get(0).intValue() == 3 && pairs.get(1).intValue() == 2) || (pairs.get(1).intValue() == 3 && pairs.get(0).intValue() == 2)) {
238 return Ranking.FULL_HOUSE;
242 for (
int i = 0; i < pairs.size(); i++) {
243 if (pairs.get(i) ==
null) {
246 if (pairs.get(i).intValue() == 3) {
247 return Ranking.THREE_OF_KIND;
249 if (pairs.get(i).intValue() == 2) {
253 if (totalPairs == 2) {
254 return Ranking.TWO_PAIR;
256 if (totalPairs == 1) {
259 return Ranking.NOTHING;
262 private static Map<Integer, Integer> getPairs(ArrayList<Flowers> list) {
263 Map<Integer, Integer> finalPairs =
new HashMap<Integer, Integer>();
264 int[] pairs =
new int[14];
266 pairs[flower.ordinal()]++;
269 for (
int i = 0; i < pairs.length; i++) {
271 finalPairs.put(slot, pairs[i]);