コード例 #1
0
ファイル: Engine.java プロジェクト: Bloged/conquest-engine
  // Moves have already been checked if they are legal
  private void executePlaceArmies() {
    for (PlaceArmiesMove move : moveQueue.placeArmiesMoves) {
      if (move.getIllegalMove().equals("")) // the com.theaigames.move is not illegal
      move.getRegion().setArmies(move.getRegion().getArmies() + move.getArmies());

      Map mapCopy = map.getMapCopy();
      fullPlayedGame.add(new MoveResult(move, mapCopy));
      if (map.visibleRegionsForPlayer(player1).contains(move.getRegion())) {
        player1PlayedGame.add(
            new MoveResult(move, map.getVisibleMapCopyForPlayer(player1))); // for the game file
        if (move.getPlayerName().equals(player2.getName()))
          opponentMovesPlayer1.add(move); // for the opponent_moves output
      }
      if (map.visibleRegionsForPlayer(player2).contains(move.getRegion())) {
        player2PlayedGame.add(
            new MoveResult(move, map.getVisibleMapCopyForPlayer(player2))); // for the game file
        if (move.getPlayerName().equals(player1.getName()))
          opponentMovesPlayer2.add(move); // for the opponent_moves output
      }
    }
  }
コード例 #2
0
ファイル: Engine.java プロジェクト: Bloged/conquest-engine
  public void distributeStartingRegions() {
    ArrayList<Region> pickableRegions = new ArrayList<Region>();
    int nrOfStartingRegions = 3;
    int regionsAdded = 0;

    // pick semi random regions to start with
    for (SuperRegion superRegion : map.getSuperRegions()) {
      int nrOfRegions = superRegion.getSubRegions().size();
      while (regionsAdded < 2) {
        double rand = Math.random();
        int randomRegionId = (int) (rand * nrOfRegions);
        Region randomRegion =
            superRegion
                .getSubRegions()
                .get(randomRegionId); // get one random subregion from superRegion
        if (!pickableRegions.contains(randomRegion)) {
          pickableRegions.add(randomRegion);
          regionsAdded++;
        }
      }
      regionsAdded = 0;
    }

    // get the preferred starting regions from the players
    ArrayList<Region> p1Regions =
        parser.parsePreferredStartingRegions(
            player1.getBot().getPreferredStartingArmies(2000, pickableRegions),
            pickableRegions,
            player1);
    ArrayList<Region> p2Regions =
        parser.parsePreferredStartingRegions(
            player2.getBot().getPreferredStartingArmies(2000, pickableRegions),
            pickableRegions,
            player2);
    ArrayList<Region> givenP1Regions = new ArrayList<Region>();
    ArrayList<Region> givenP2Regions = new ArrayList<Region>();

    // if the com.theaigames.bot did not correctly return his starting regions, get some random ones
    if (p1Regions == null) {
      p1Regions = new ArrayList<Region>();
    }
    if (p2Regions == null) {
      p2Regions = new ArrayList<Region>();
    }

    p1Regions.addAll(getRandomStartingRegions(pickableRegions));
    p2Regions.addAll(getRandomStartingRegions(pickableRegions));

    // distribute the starting regions
    int i1, i2, n;
    i1 = 0;
    i2 = 0;
    n = 0;

    while (n < nrOfStartingRegions) {
      Region p1Region = p1Regions.get(i1);
      Region p2Region = p2Regions.get(i2);

      if (givenP2Regions.contains(
          p1Region)) { // preferred region for player1 is not given to player2 already
        i1++;
      } else if (givenP1Regions.contains(
          p2Region)) { // preferred region for player2 is not given to player1 already
        i2++;
      } else if (p1Region != p2Region) {
        p1Region.setPlayerName(player1.getName());
        p2Region.setPlayerName(player2.getName());
        givenP1Regions.add(p1Region);
        givenP2Regions.add(p2Region);
        n++;
        i1++;
        i2++;
      } else { // random player gets the region if same preference
        double rand = Math.random();
        if (rand < 0.5) {
          i1++;
        } else {
          i2++;
        }
      }
    }

    fullPlayedGame.add(new MoveResult(null, map.getMapCopy()));
    player1PlayedGame.add(new MoveResult(null, map.getVisibleMapCopyForPlayer(player1)));
    player2PlayedGame.add(new MoveResult(null, map.getVisibleMapCopyForPlayer(player2)));
  }
コード例 #3
0
ファイル: Engine.java プロジェクト: Bloged/conquest-engine
  private void executeAttackTransfer() {
    LinkedList<Region> visibleRegionsPlayer1Map = map.visibleRegionsForPlayer(player1);
    LinkedList<Region> visibleRegionsPlayer2Map = map.visibleRegionsForPlayer(player2);
    LinkedList<Region> visibleRegionsPlayer1OldMap = visibleRegionsPlayer1Map;
    LinkedList<Region> visibleRegionsPlayer2OldMap = visibleRegionsPlayer2Map;
    ArrayList<ArrayList<Integer>> usedRegions = new ArrayList<ArrayList<Integer>>();
    for (int i = 0; i <= 42; i++) {
      usedRegions.add(new ArrayList<Integer>());
    }
    Map oldMap = map.getMapCopy();

    int moveNr = 1;
    Boolean previousMoveWasIllegal = false;
    String previousMovePlayer = "";
    while (moveQueue.hasNextAttackTransferMove()) {
      AttackTransferMove move =
          moveQueue.getNextAttackTransferMove(moveNr, previousMovePlayer, previousMoveWasIllegal);

      if (move.getIllegalMove().equals("")) // the com.theaigames.move is not illegal
      {
        Region fromRegion = move.getFromRegion();
        Region oldFromRegion = oldMap.getRegion(move.getFromRegion().getId());
        Region toRegion = move.getToRegion();
        Player player = getPlayer(move.getPlayerName());

        if (fromRegion.ownedByPlayer(
            player.getName())) // check if the fromRegion still belongs to this player
        {
          if (!usedRegions
              .get(fromRegion.getId())
              .contains(
                  toRegion
                      .getId())) // between two regions there can only be attacked/transfered once
          {
            if (oldFromRegion.getArmies() > 1) // there are still armies that can be used
            {
              if (oldFromRegion.getArmies() < fromRegion.getArmies()
                  && oldFromRegion.getArmies() - 1
                      < move
                          .getArmies()) // not enough armies on fromRegion at the start of the
                                        // round?
              move.setArmies(
                    oldFromRegion.getArmies() - 1); // com.theaigames.move the maximal number.
              else if (oldFromRegion.getArmies() >= fromRegion.getArmies()
                  && fromRegion.getArmies() - 1
                      < move.getArmies()) // not enough armies on fromRegion currently?
              move.setArmies(fromRegion.getArmies() - 1); // com.theaigames.move the maximal number.

              // not needed anymore as regions can only be used once now
              oldFromRegion.setArmies(
                  oldFromRegion.getArmies()
                      - move.getArmies()); // update oldFromRegion so new armies cannot be used yet

              if (toRegion.ownedByPlayer(player.getName())) // transfer
              {
                if (fromRegion.getArmies() > 1) {
                  fromRegion.setArmies(fromRegion.getArmies() - move.getArmies());
                  toRegion.setArmies(toRegion.getArmies() + move.getArmies());
                  usedRegions.get(fromRegion.getId()).add(toRegion.getId());
                } else
                  move.setIllegalMove(
                      move.getFromRegion().getId() + " transfer " + "only has 1 army");
              } else // attack
              {
                int destroyedArmies = doAttack(move);
                oldFromRegion.setArmies(
                    oldFromRegion.getArmies()
                        - destroyedArmies); // armies that are destroyed and replaced in the same
                                            // turn cannot be used to attack/transfer with
                usedRegions.get(fromRegion.getId()).add(toRegion.getId());
              }
            } else
              move.setIllegalMove(
                  move.getFromRegion().getId()
                      + " attack/transfer "
                      + "has used all available armies");
          } else
            move.setIllegalMove(
                move.getFromRegion().getId()
                    + " attack/transfer "
                    + "has already attacked/transfered to this region");
        } else
          move.setIllegalMove(
              move.getFromRegion().getId() + " attack/transfer " + "was taken this round");
      }

      visibleRegionsPlayer1Map = map.visibleRegionsForPlayer(player1);
      visibleRegionsPlayer2Map = map.visibleRegionsForPlayer(player2);

      fullPlayedGame.add(new MoveResult(move, map.getMapCopy()));
      if (visibleRegionsPlayer1Map.contains(move.getFromRegion())
          || visibleRegionsPlayer1Map.contains(move.getToRegion())
          || visibleRegionsPlayer1OldMap.contains(move.getToRegion())) {
        player1PlayedGame.add(
            new MoveResult(move, map.getVisibleMapCopyForPlayer(player1))); // for the game file
        if (move.getPlayerName().equals(player2.getName()))
          opponentMovesPlayer1.add(move); // for the opponent_moves output
      }
      if (visibleRegionsPlayer2Map.contains(move.getFromRegion())
          || visibleRegionsPlayer2Map.contains(move.getToRegion())
          || visibleRegionsPlayer2OldMap.contains(move.getToRegion())) {
        player2PlayedGame.add(
            new MoveResult(move, map.getVisibleMapCopyForPlayer(player2))); // for the game file
        if (move.getPlayerName().equals(player1.getName()))
          opponentMovesPlayer2.add(move); // for the opponent_moves output
      }

      visibleRegionsPlayer1OldMap = visibleRegionsPlayer1Map;
      visibleRegionsPlayer2OldMap = visibleRegionsPlayer2Map;

      // set some stuff to know what next com.theaigames.move to get
      if (move.getIllegalMove().equals("")) {
        previousMoveWasIllegal = false;
        moveNr++;
      } else {
        previousMoveWasIllegal = true;
      }
      previousMovePlayer = move.getPlayerName();
    }
  }