コード例 #1
1
 @Override
 public void acceptTrade() {
   try {
     if (game.isTradeActive()) {
       Trade currentOffer = game.getCurrentOffer();
       if (currentOffer.getPackage1().getResources().contains(ResourceType.SHEEP)
           && !currentOffer.getPackage2().getResources().contains(ResourceType.SHEEP)) {
         List<ResourceType> sending = currentOffer.getPackage2().getResources();
         final int ownedOre = getNumberOfType(ResourceType.ORE);
         final int ownedBrick = getNumberOfType(ResourceType.BRICK);
         final int ownedWheat = getNumberOfType(ResourceType.WHEAT);
         final int ownedWood = getNumberOfType(ResourceType.WOOD);
         int oreAsked = 0;
         int brickAsked = 0;
         int wheatAsked = 0;
         int woodAsked = 0;
         for (ResourceType resource : sending) {
           switch (resource) {
             case ORE:
               oreAsked++;
               if (oreAsked > ownedOre) {
                 game.acceptTrade(getPlayerIndex(), false);
                 return;
               }
               break;
             case BRICK:
               brickAsked++;
               if (brickAsked > ownedBrick) {
                 game.acceptTrade(getPlayerIndex(), false);
                 return;
               }
               break;
             case WHEAT:
               wheatAsked++;
               if (wheatAsked > ownedWheat) {
                 game.acceptTrade(getPlayerIndex(), false);
                 return;
               }
               break;
             case WOOD:
               woodAsked++;
               if (woodAsked > ownedWood) {
                 game.acceptTrade(getPlayerIndex(), false);
                 return;
               }
               break;
             default:
               break;
           }
         }
         game.acceptTrade(getPlayerIndex(), true);
       } else {
         game.acceptTrade(getPlayerIndex(), false);
       }
     }
   } catch (Exception | InvalidTypeException e) {
     e.printStackTrace();
   }
 }
コード例 #2
1
 @Override
 public void rob() {
   // try and place robber on sheep hex where aiPlayer doesn't have a building itself
   try {
     for (HexLocation sheepHex : sheepHexes) {
       ArrayList<Integer> potentialVictims = getPlayers(sheepHex);
       if (potentialVictims.size() > 0
           && !potentialVictims.contains(getPlayerIndex())
           && !game.getMap().getRobber().getLocation().equals(sheepHex)) {
         game.rob(getPlayerIndex(), potentialVictims.get(0), sheepHex);
         return;
       }
     }
     // this is if the aiPlayer has a building on every single sheep hex.  Now it will just pick a
     // random hex where it doesn't have a building
     java.util.Map<HexLocation, Hex> hexes = game.getMap().getHexes();
     for (java.util.Map.Entry<HexLocation, Hex> entry : hexes.entrySet()) {
       if (entry.getValue().getType() != HexType.WATER) {
         ArrayList<Integer> potentialVictims = getPlayers(entry.getKey());
         if (potentialVictims.size() != 0
             && !potentialVictims.contains(getPlayerIndex())
             && !game.getMap().getRobber().getLocation().equals(entry.getKey())) {
           // this robs a random person on a random hex where the aiPlayer doesn't have a building
           // there
           int victim = potentialVictims.remove(0);
           game.rob(getPlayerIndex(), victim, entry.getKey());
           return;
         }
       }
     }
     // this is if the aiPlayer has a building on every hex that has a building.  Now it will just
     // not rob anyone
     for (java.util.Map.Entry<HexLocation, Hex> entry : hexes.entrySet()) {
       if (entry.getValue().getType() != HexType.WATER) {
         if (!game.getMap().getRobber().getLocation().equals(entry.getKey())) {
           game.rob(getPlayerIndex(), getPlayerIndex(), entry.getKey());
           return;
         }
       }
     }
   } catch (Exception | InvalidTypeException e) {
     e.printStackTrace();
   }
 }
コード例 #3
1
 @Override
 public void play() {
   try {
     ArrayList<Integer> choices = new ArrayList<>();
     for (int i = 0; i < 6; i++) {
       choices.add(i);
     }
     while (choices.size() > 0) {
       int random = choices.remove(new Random().nextInt(choices.size()));
       switch (random) {
         case 0:
           playRoad();
           break;
         case 1:
           playSettlement();
           break;
         case 2:
           playCity();
           break;
         case 3:
           getDevCard();
           break;
         case 4:
           playDevCard();
           break;
         case 5:
           maritimeTrade();
           break;
         default:
           break;
       }
     }
     sendChat();
     trade();
   } catch (Exception | InvalidTypeException e) {
     e.printStackTrace();
   }
 }
コード例 #4
1
 @Override
 public void discard() {
   try {
     if (!hasDiscarded()) {
       ArrayList<ResourceType> unwanted = new ArrayList<>();
       unwanted.add(ResourceType.BRICK);
       unwanted.add(ResourceType.ORE);
       unwanted.add(ResourceType.WOOD);
       unwanted.add(ResourceType.WHEAT);
       ArrayList<ResourceType> resources = new ArrayList<>();
       int totalToDiscard =
           game.getPlayerManager().getPlayerByIndex(getPlayerIndex()).getNumberResourceCards() / 2;
       int brick =
           game.getPlayerManager()
               .getPlayerByIndex(getPlayerIndex())
               .getNumberOfType(ResourceType.BRICK);
       int ore =
           game.getPlayerManager()
               .getPlayerByIndex(getPlayerIndex())
               .getNumberOfType(ResourceType.ORE);
       int wheat =
           game.getPlayerManager()
               .getPlayerByIndex(getPlayerIndex())
               .getNumberOfType(ResourceType.WHEAT);
       int wood =
           game.getPlayerManager()
               .getPlayerByIndex(getPlayerIndex())
               .getNumberOfType(ResourceType.WOOD);
       while (totalToDiscard != 0 && unwanted.size() != 0) {
         ResourceType random = unwanted.remove(new Random().nextInt(unwanted.size()));
         switch (random) {
           case ORE:
             while (ore != 0 && totalToDiscard != 0) {
               resources.add(random);
               ore--;
               totalToDiscard--;
             }
             break;
           case WOOD:
             while (wood != 0 && totalToDiscard != 0) {
               resources.add(random);
               wood--;
               totalToDiscard--;
             }
             break;
           case WHEAT:
             while (wheat != 0 && totalToDiscard != 0) {
               resources.add(random);
               wheat--;
               totalToDiscard--;
             }
             break;
           case BRICK:
             while (brick != 0 && totalToDiscard != 0) {
               resources.add(random);
               brick--;
               totalToDiscard--;
             }
             break;
           default:
             break;
         }
       }
       while (totalToDiscard != 0) {
         resources.add(ResourceType.SHEEP);
         totalToDiscard--;
       }
       game.discardCards(getPlayerIndex(), resources);
     }
   } catch (Exception | InvalidTypeException e) {
     e.printStackTrace();
   }
 }