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(); }
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); } } } }