예제 #1
0
  private void redStrategy(final Agent a) {

    // get the creditchecker and register
    CreditChecker.addOrResetCalculatingThread(a.getId(), a.getInitialCredits());

    // EVERYTHING SHOULD HAPPEN IN THIS NEW THREAD BLOCK
    new Thread(
            new Runnable() {
              public void run() {

                if (redTeamPathStrategy.equals(AGENT_STRATEGY.ASTARWALKER)) {
                  // simpleAStarStrategy(a);
                  advancedStrategy(a);

                } else if (redTeamPathStrategy.equals((AGENT_STRATEGY.RANDOM_WALKER))) {
                  simpleRandomStrategy(a);

                }
                // ADD OTHER STRATEGIES HERE

                // *****
                else {
                  System.err.println("Illegal red path strategy.");
                  System.exit(-1);
                }
              }
            })
        .start();
  }
예제 #2
0
  private void simpleAStarStrategy(final Agent a) {

    if (a.getCarrying() > -1) {
      a.calcPath(
          calcXFromCoor(a.getCampLocation()),
          calcYFromCoor(a.getCampLocation())); // if carrying a Tilia, go to camp
    } else { // go pick up a random tilia
      ArrayList<Integer> coins = new ArrayList<Integer>(coinLocations.keySet());
      int coin;

      if (coins.size() > 1) {
        coin = coins.get(new Random().nextInt(coinLocations.size() - 1));
      } else if (coins.size() == 1) {
        coin = coins.get(0);
      } else {
        coin = a.getCampLocation();
      }
      a.calcPath(calcXFromCoor(coin), calcYFromCoor(coin));
    }
  }
예제 #3
0
  private void advancedStrategy(
      Agent a) { // afstand wordt bevoordeeld tov padlengte omwille van complexiteit

    if (a.getCarrying() > -1) {
      a.calcPath(
          calcXFromCoor(a.getCampLocation()),
          calcYFromCoor(a.getCampLocation())); // if carrying a Tilia, go to camp
    } else { // go pick up a tilia closest to position
      int coin = 0;
      if (a.isRed()) {
        if (coins_red.size() >= 1) {
          coin = coins_red.extractMin().getlocation();
        }

      } else {
        if (coins_blue.size() >= 1) {
          coin = coins_blue.extractMin().getlocation();
        }
      }
      if (coin == 0) {
        coin = a.getCampLocation();
      }
      a.calcPath(calcXFromCoor(coin), calcYFromCoor(coin));
    }
  }
예제 #4
0
  private void hierarchicalStrategy(Agent a) {

    if (a.getCarrying() > -1) {
      a.calcPath(
          calcXFromCoor(a.getCampLocation()),
          calcYFromCoor(a.getCampLocation())); // if carrying a Tilia, go to camp
    } else { // go pick up a tilia closest to position
      int coin = 0;
      if (a.isRed()) {
        if (coins_red.size() >= 1) {
          coin = coins_red.extractMin().getlocation();
        }

      } else {
        if (coins_blue.size() >= 1) {
          coin = coins_blue.extractMin().getlocation();
        }
      }
      if (coin == 0) {
        coin = a.getCampLocation();
      }
      a.calcPath(calcXFromCoor(coin), calcYFromCoor(coin));
    }
  }
예제 #5
0
  private boolean executeMovementAgent(Agent a, int wantsToMove) {

    int currentLocation = a.getLocation();
    int xCurrent = this.calcXFromCoor(currentLocation);
    int yCurrent = this.calcYFromCoor(currentLocation);
    int xWants = this.calcXFromCoor(wantsToMove);
    int yWants = this.calcYFromCoor(wantsToMove);

    boolean isRed = a.isRed();

    // check if this is a valid movement request, otherwhise crash the game
    // check if wantsToMove is in bounds

    if (wantsToMove < 0 || wantsToMove >= this.gridIdentifier.length) {
      System.err.println("REQUESTED MOVE OUT OF BOUNDS. EXITING.");
      System.exit(-1);
    }

    // we can either move up, down, left or right, just check the four cases
    // crash the game if the move is not one of these choices
    if (!((xCurrent == xWants - 1 && yCurrent == yWants)
        || (xCurrent == xWants + 1 && yCurrent == yWants)
        || (yCurrent == yWants + 1 && xCurrent == xWants)
        || (yCurrent == yWants - 1 && xCurrent == xWants))) {

      System.err.println(xCurrent);
      System.err.println(yCurrent);
      System.err.println(xWants);
      System.err.println(yWants);
      System.err.println("REQUESTED MOVE NOT UP,DOWN,RIGHT OR LEFT.");
      System.exit(-1);
    }

    // move is not legal because there is a rock
    if (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.ROCK_TILE)) {
      System.err.println("REQUESTED TO MOVE ON ROCK SQUARE.");
      System.exit(-1);
    }

    // move is also illegal if you're tyring to move into the enemies camp

    if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_CAMP_TILE) && isRed)
        || (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_CAMP_TILE) && !isRed)) {
      System.err.println("REQUESTED TO MOVE ON ENEMIES CAMP.");
      System.exit(-1);
    }

    // I guess if we get here, we can actually move!
    // Then there are three options, either the goto tile is a coin or else
    // its a grass field or its own camp

    // if its a camp, the sprite shouldnt move, but if is carrying a coin it
    // should drop of the coin and the movement list should go empty
    if (isRed && (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_CAMP_TILE))) {

      int coin = a.getCarrying();

      if (coin != -1) {
        coinDrop(isRed, a.getCarrying());
        a.setCarrying(-1);
      }
      a.clearMovementList();
      return false;
    } else if (!isRed
        && (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_CAMP_TILE))) {
      int coin = a.getCarrying();

      if (coin != -1) {
        coinDrop(isRed, a.getCarrying());
        a.setCarrying(-1);
      }

      a.clearMovementList();
      return false;
    }

    // if its a coin, two things can happen, either the actor is already
    // carrying a coin, in which case the coin being held is dropped at the
    // current location of the actor, then the actor moves to destination
    // and removed the coin there and puts it in inventory
    // else if he is not carrying anything, he removes the coin and moves to
    // the target square
    if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.COIN_TILE))) {
      int carrying = a.getCarrying();

      // if he is carrying something, drop the coint
      if (carrying != -1) {
        // current location becomes a coin
        gridIdentifier[currentLocation] = mapper.get(Tile.COIN_TILE);
        // update coint location of coin dropped
        this.coinLocations.put(currentLocation, carrying);
      } else {
        gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE);
      }
      // targetlocation becomes sprite (red or blue)
      if (isRed) {
        gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE);
      } else {
        gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE);
      }

      // pick up the coin
      int pickup = this.coinLocations.get(wantsToMove);
      // give it to sprite
      a.setCarrying(pickup);
      // remove it from coint map
      this.coinLocations.remove(wantsToMove);
      // remove the action
      a.popMovement();
      a.setLocation(wantsToMove);

      return true;
    }

    if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.GRASS_TILE))) {

      if (isRed) {
        gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE);
      } else {
        gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE);
      }

      gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE);
      a.popMovement();

      a.setLocation(wantsToMove);

      return true;
    }

    // this is still fatal if there is a head on collision, it is easier to
    // just allow for passing movements
    if (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_SPRITE_TILE)
        || gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_SPRITE_TILE)) {

      if (isRed) {
        gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE);
      } else {
        gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE);
      }

      gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE);
      a.popMovement();
      a.setLocation(wantsToMove);

      return true;
    }

    throw new RuntimeErrorException(new Error("Ran out of options."));
  }
예제 #6
0
  private void executeGameLogic() {

    // do processing logic

    // check if there is something in the queue and we are not processing
    // anything
    // note that at this stage, no threads can be running as we specifically
    // wait until a thread signals it is either
    // finished or at is stuck because it has no credits left
    // as this is the case we should not worry about other threads updating
    // these variables!

    // first check if something is finished processing for blue
    if (this.blueToAddScore > -1) {
      // add the score
      this.scoreBlue += this.blueToAddScore;
      // indicate that nothing is processing
      this.blueToAddScore = -1;
      this.processingBlue = -1;
    }

    // next check if something is finished processing for red
    if (this.redToAddScore > -1) {
      // add the score
      this.scoreRed += this.redToAddScore;
      // indicate that nothing is processing
      this.redToAddScore = -1;
      this.processingRed = -1;
    }

    // next check if blue can start processing something or it is in need of
    // new credits
    if (this.processingBlue == -1) {
      // check if there is something in the queue
      if (!this.queueBlue.isEmpty()) {
        // items are always processed in the order they were submitted
        this.processingBlue = this.queueBlue.poll();
        blueProcStrategy(collectionBlue.getGrid(this.processingBlue));
      }
    } else {
      CreditChecker.giveNewCredit(CreditChecker.BLUE_PROCESSING_THREAD);
    }

    // next check if red can start processing something or it is in need of
    // new credits
    if (this.processingRed == -1) {
      // check if there is something in the queue
      if (!this.queueRed.isEmpty()) {
        // items are always processed in the order they were submitted
        this.processingRed = this.queueRed.poll();
        redProcStrategy(collectionRed.getGrid(this.processingRed));
      }
    } else {
      CreditChecker.giveNewCredit(CreditChecker.RED_PROCESSING_THREAD);
    }

    // first decide the order of the agents to perform actions
    Collections.shuffle(this.allSprites);

    boolean[] actionPerformed = new boolean[this.allSprites.size()];
    for (int i = 0; i < actionPerformed.length; i++) {
      actionPerformed[i] = false;
    }

    // first check all the movements
    for (int i = 0; i < this.allSprites.size(); i++) {
      Agent a = this.allSprites.get(i);
      // check if agent wants to move
      int wantsToMove = a.wantsToMove();
      // yes it does want to move , then spent this turn moving
      if (wantsToMove != -1) {
        executeMovementAgent(a, wantsToMove);
        actionPerformed[i] = true;
      }
    }

    // now reset all the sprites as there is a bug with overlap
    for (int i = 0; i < this.allSprites.size(); i++) {
      Agent a = this.allSprites.get(i);
      if (a.isRed()) {
        gridIdentifier[a.getLocation()] = mapper.get(Tile.RED_SPRITE_TILE);
      } else {
        gridIdentifier[a.getLocation()] = mapper.get(Tile.BLUE_SPRITE_TILE);
      }
    }

    // now, all the agents which did not move and are not performing an
    // action can get a new command
    for (int i = 0; i < this.allSprites.size(); i++) {
      if (actionPerformed[i]) {
        continue;
      }
      Agent a = this.allSprites.get(i);
      Boolean bool = CreditChecker.isIdling(a.getId());
      if (bool) {
        CreditChecker.giveNewCredit(a.getId());
      } else {
        if (a.isRed()) {
          redStrategy(a);
        } else {
          blueStrategy(a);
        }
      }
    }
  }
예제 #7
0
  private void simpleRandomStrategy(final Agent a) {

    a.calcPath(0, 0);
  }
예제 #8
0
  private void readFromFile(Tile[] tbs, String file) {

    int height = squaresY;
    int width = squaresX;
    int countSprites = 0;

    BufferedReader reader = null;
    int i = 0;
    int lines = 0;
    try {
      reader = new BufferedReader(new FileReader(file));
      String line = reader.readLine();
      lines++;

      while (lines <= height) {
        String[] tokens = line.split("\t");
        if (tokens.length != width) {
          throw new IOException();
        }

        for (String x : tokens) {

          Tile tile = mapper.get(x);

          // if the tile did not map, it should be an int value (and
          // as such a coin)
          if (tile == null) {
            int value = Integer.parseInt(x);
            this.coinLocations.put(i, value);
            tile = mapper.get(Tile.COIN_TILE);
          } else if (tile.getIdentifier().equals(Tile.BLUE_CAMP_TILE)) {
            this.blueCamp = i;
          } else if (tile.getIdentifier().equals(Tile.RED_CAMP_TILE)) {
            this.redCamp = i;
          } else if (tile.getIdentifier().equals(Tile.BLUE_SPRITE_TILE)) {
            countSprites++;
            Agent a = null;
            if (this.blueTeamPathStrategy == AGENT_STRATEGY.RANDOM_WALKER) {
              a = new RandomWalker("" + countSprites, false, this, pathCredits);
            } else if (this.blueTeamPathStrategy == AGENT_STRATEGY.ASTARWALKER) {
              a = new AStarWalker("" + countSprites, false, this, pathCredits);
            }
            // ADD OTHER STRATEGIES FOR BLUE HERE
            else if (this.blueTeamPathStrategy == AGENT_STRATEGY.HIERARCHICALWALKER) {
              a = new HierarchicalWalker("" + countSprites, false, this, pathCredits);
            }

            // *****
            else {
              System.err.println("Illegal walker type");
              System.exit(-1);
            }
            a.setLocation(i);
            this.blueTeam.add(a);
            this.allSprites.add(a);
          } else if (tile.getIdentifier().equals(Tile.RED_SPRITE_TILE)) {
            countSprites++;
            Agent a = null;
            if (this.redTeamPathStrategy == AGENT_STRATEGY.RANDOM_WALKER) {
              a = new RandomWalker("" + countSprites, true, this, pathCredits);
            } else if (this.redTeamPathStrategy == AGENT_STRATEGY.ASTARWALKER) {
              a = new AStarWalker("" + countSprites, true, this, pathCredits);
            }
            // add other strategies for red team here

            // *****
            else {
              System.err.println("Illegal walker type");
              System.exit(-1);
            }
            this.redTeam.add(a);
            a.setLocation(i);
            this.allSprites.add(a);
          }
          tbs[i] = tile;
          i++;
        }
        line = reader.readLine();
        lines++;
      }

    } catch (IOException e) {
      System.err.println("Error reading  map file");
      e.printStackTrace();
      System.exit(-1);
    } finally {
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
예제 #9
0
  public GameLogic(
      int totalSteps,
      int pathCredits,
      int calcCredits,
      TiliaCollection collectionBlue,
      TiliaCollection collectionRed,
      GUI gui,
      String map,
      double speed,
      GameLogic.AGENT_STRATEGY blueTeamPathStrategy,
      GameLogic.AGENT_STRATEGY redTeamPathStrategy,
      GameLogic.PROCESSING_STRATEGY blueProcStrategy,
      GameLogic.PROCESSING_STRATEGY redProcStrategy) {

    this.totalSteps = totalSteps;
    this.pathCredits = pathCredits;
    this.calcCredits = calcCredits;
    this.collectionBlue = collectionBlue;
    this.collectionRed = collectionRed;
    this.gui = gui;
    this.map = map;
    this.speed = speed;
    this.blueTeamPathStrategy = blueTeamPathStrategy;
    this.redTeamPathStrategy = redTeamPathStrategy;
    this.blueProcStrategy = blueProcStrategy;
    this.redProcStrategy = redProcStrategy;

    this.coinLocations = new HashMap<Integer, Integer>();
    for (String x : Tile.tiletypes) {
      mapper.put(x, new Tile(x));
    }

    this.squaresX = gui.getButtonsX();
    this.squaresY = gui.getButtonsY();
    this.gridIdentifier = new Tile[squaresX * squaresY];

    // read map from file
    readFromFile(gridIdentifier, map);

    for (Agent a : redTeam) {
      a.setCampLocation(redCamp);
    }

    for (Agent a : blueTeam) {
      a.setCampLocation(blueCamp);
    }

    this.processingBlue = -1;
    this.processingRed = -1;

    initialize_coins();
    // map preprocessing
    cluster1Nodes = new ArrayList<Node>();
    cluster2Nodes = new ArrayList<Node>();
    cluster3Nodes = new ArrayList<Node>();
    cluster4Nodes = new ArrayList<Node>();
    cluster5Nodes = new ArrayList<Node>();
    cluster6Nodes = new ArrayList<Node>();
    cluster7Nodes = new ArrayList<Node>();
    cluster8Nodes = new ArrayList<Node>();
    cluster9Nodes = new ArrayList<Node>();
    cluster10Nodes = new ArrayList<Node>();
    cluster11Nodes = new ArrayList<Node>();
    cluster12Nodes = new ArrayList<Node>();
    cluster13Nodes = new ArrayList<Node>();
    cluster14Nodes = new ArrayList<Node>();
    cluster15Nodes = new ArrayList<Node>();
    cluster16Nodes = new ArrayList<Node>();
    cluster17Nodes = new ArrayList<Node>();
    cluster18Nodes = new ArrayList<Node>();
    cluster19Nodes = new ArrayList<Node>();
    cluster20Nodes = new ArrayList<Node>();
    allNodes = new ArrayList<ArrayList<Node>>();
  }