diff --git a/core/app/Controller.java b/core/app/Controller.java index 3d7c36a654e3336e7ea5901aac49ddfa3c63b41c..4ec4dab15b827349d19870ad5ab8e187b05bf5c0 100644 --- a/core/app/Controller.java +++ b/core/app/Controller.java @@ -40,8 +40,8 @@ import view.ZPMView; public class Controller { private View view; - private int colonelLife; - private int jaffaLife; + private int colonelInitLife; + private int jaffaInitLife; private int maxLvl; private boolean gameStarted = false; @@ -82,8 +82,8 @@ public class Controller { */ public void startGame(int colonelLife, int jaffaLife, int maxLvl) { - this.colonelLife = colonelLife; - this.jaffaLife = jaffaLife; + this.colonelInitLife = colonelLife; + this.jaffaInitLife = jaffaLife; this.maxLvl = maxLvl; // invoke main thread to run game @@ -96,8 +96,13 @@ public class Controller { int failure = 0; InitController.generateValidPairs(); - InitController.startColonelZPMs = 0; - InitController.startJaffaZPMs = 0; + + // ZPM counts of each player in a + // specific order + int[] playerZPMs = new int[] { + InitController.startColonelZPMs, + InitController.startJaffaZPMs + }; for(; actualLvl <= maxLvl; actualLvl++) { @@ -114,8 +119,9 @@ public class Controller { uploadMapDatas(); GameController game = new GameController(view, InitController.map.length, InitController.map[0].length); - game.startGameProcess(); - //actualLvl++; + + // Boxing: GameController updates ZPM count through reference + game.startGameProcess( playerZPMs ); } catch (FileNotFoundException e) { System.err.println("Map with path " + mapPath + " not found"); continue; @@ -137,7 +143,7 @@ public class Controller { } // end of game - this.view.finishGame(InitController.startColonelZPMs, InitController.startJaffaZPMs); + this.view.finishGame(playerZPMs[0], playerZPMs[1]); System.exit(0); } @@ -173,12 +179,12 @@ public class Controller { private void updateLifeSettings() { if(InitController.colonel != null) - InitController.colonel.setPlayerLives(colonelLife); + InitController.colonel.setPlayerLives(colonelInitLife); else System.err.println("No colonel found when game starts"); if(InitController.jaffa != null) - InitController.jaffa.setPlayerLives(jaffaLife); + InitController.jaffa.setPlayerLives(jaffaInitLife); else System.err.println("No jaffa found when game starts"); } @@ -197,13 +203,13 @@ public class Controller { if(InitController.colonel != null && InitController.jaffa != null) { - view.showGame(InitController.map[0].length, InitController.map.length, colonelLife, jaffaLife, + view.showGame(InitController.map[0].length, InitController.map.length, colonelInitLife, jaffaInitLife, InitController.startColonelZPMs + Model.getInstance().getColonel().getZpmCount(), InitController.startJaffaZPMs + Model.getInstance().getJaffa().getZpmCount()); } else { - view.showGame(InitController.map[0].length, InitController.map.length, colonelLife, jaffaLife, 0, 0); + view.showGame(InitController.map[0].length, InitController.map.length, colonelInitLife, jaffaInitLife, 0, 0); if(InitController.colonel == null) System.err.println("No colonel found when game starts"); if(InitController.jaffa == null) diff --git a/core/app/GameController.java b/core/app/GameController.java index eb7218f704d9c195fe65d2a100d024c42d7edc98..7a4fb2fa70fc5e0a18ecb4d47bd9d920d35e107a 100644 --- a/core/app/GameController.java +++ b/core/app/GameController.java @@ -19,7 +19,7 @@ import view.ViewElement; public class GameController implements PropertyChangeListener{ private View view; private ArrayList<IElement>[][] map; - + @SuppressWarnings("unchecked") public GameController(View view, int width, int height) { @@ -27,16 +27,27 @@ public class GameController implements PropertyChangeListener{ map = new ArrayList[InitController.map.length][InitController.map[0].length]; Model.getInstance().addPropertyChangeListener(this); } - - public void startGameProcess() throws GameRunningException + + /** + * Starts and runs through a game process with a map. + * @param rZPMCount Global ZPM counts which have to be updated. + * <ul> + * <li> 0 - colonel</li> + * <li> 1 - jaffa </li> + * </ul> + * @throws GameRunningException + */ + public void startGameProcess( int[] rZPMCount) throws GameRunningException { Model.getInstance().restartMap(); - + int failure = 0; int counter = 0; + + // previous values of the players lives Integer tmpColonelLives = Model.getInstance().getColonel().getPlayerLives(); Integer tmpJaffaLives = Model.getInstance().getJaffa().getPlayerLives(); - + while(true) { try { @@ -52,7 +63,7 @@ public class GameController implements PropertyChangeListener{ throw new GameRunningException(); } } - + if(counter < 4) { counter++; @@ -65,10 +76,14 @@ public class GameController implements PropertyChangeListener{ } counter = 0; } - + Integer lastKey = view.getLastKey(); if(lastKey != null) { + // previous values of the players ZPM counts + int prevColonelZpm = Model.getInstance().getColonel().getZpmCount(); + int prevJaffaZpm = Model.getInstance().getJaffa().getZpmCount(); + switch(lastKey) { // Colonel controls @@ -191,24 +206,27 @@ public class GameController implements PropertyChangeListener{ boxUp(Model.getInstance().getJaffa()); break; } - + if(Model.getInstance().isMapFinished()) { break; } - } - } - - endProcess(); - + + updateZPMCount( rZPMCount, prevColonelZpm, prevJaffaZpm ); + } // end of if key pressed + + } // end of while + + + return; // Map finished } - + private void updateViewToolbar(Integer beforeColonelLives, Integer beforeJaffaLives) { int afterColonelLives = Model.getInstance().getColonel().getPlayerLives(); int afterJaffaLives = Model.getInstance().getJaffa().getPlayerLives(); - + if(beforeColonelLives.intValue() != afterColonelLives) { view.setColonelLivesDisplayed(afterColonelLives); @@ -220,44 +238,67 @@ public class GameController implements PropertyChangeListener{ beforeJaffaLives = afterJaffaLives; } } - + private void move(Player player) { player.step(); } - + private void turn(Player player, Direction dir) { player.setDirection(dir); } - + private void shoot(Player player, PortalColor color) { player.fire(color); } - + private void boxDown(Player player) { player.putBoxDown(); } - + private void boxUp(Player player) { player.getBoxUp(); } - + private void moveReplicator() { Model.getInstance().getReplicator().setDirection(Direction.getRandomDir()); Model.getInstance().getReplicator().step(); } - - private void endProcess() + + /** + * updates global ZPM count for each player. + * @param rZPMCount ZPM count of each player + * <ul> + * <li> 0 - colonel</li> + * <li> 1 - jaffa </li> + * </ul> + * @param prevJaffaZpm the previous ZPM count of jaffa + * on the current map + * @param prevColonelZpm the previous ZPM count of + * the colonel on the current map + */ + private void updateZPMCount( int[] rZPMCount, int prevColonelZpm, int prevJaffaZpm) { - InitController.startColonelZPMs += Model.getInstance().getColonel().getZpmCount(); - InitController.startJaffaZPMs += Model.getInstance().getJaffa().getZpmCount(); + if( rZPMCount == null) + rZPMCount = new int[] {0, 0}; + + // update global ZPM counts + rZPMCount[0] += Model.getInstance().getColonel().getZpmCount() - prevColonelZpm; + rZPMCount[1] += Model.getInstance().getJaffa().getZpmCount() - prevJaffaZpm; + + // update previous values of ZPM's on the current map + prevColonelZpm = Model.getInstance().getColonel().getZpmCount(); + prevJaffaZpm = Model.getInstance().getJaffa().getZpmCount(); + + this.view.setColonelZPMDisplayed(rZPMCount[0]); + this.view.setJaffaZPMDisplayed(rZPMCount[1]); } - + @Override public void propertyChange(PropertyChangeEvent evt) { if(evt.getPropertyName().equals("error")) // update the whole map as before @@ -265,25 +306,25 @@ public class GameController implements PropertyChangeListener{ updateGameControllerMap(); ArrayList<ViewElement>[][] viewMap = uploadToView(); view.setMap(viewMap); - + return; } - + // update only the changed field int coord = (int)evt.getNewValue(); int xCoord = coord / map[0].length; int yCoord = coord % map[0].length; Field changedField = (Field)evt.getOldValue(); ArrayList<ViewElement> fieldView = new ArrayList<>(); - + for(int i = 0; i < changedField.getElementNum(); i++) { fieldView.add(Controller.getViewOf(changedField.getElement(i))); } - + view.setElement(yCoord, xCoord, fieldView); } - + @SuppressWarnings("unchecked") private void updateGameControllerMap() { @@ -293,7 +334,7 @@ public class GameController implements PropertyChangeListener{ tmp = Model.class.getDeclaredField("map"); tmp.setAccessible(true); ArrayList<Field> tmpMap = (ArrayList<Field>)tmp.get(Model.getInstance()); - + int rowLength = map[0].length; int size = map.length * map[0].length; for(int i = 0; i < size; i++) @@ -310,7 +351,7 @@ public class GameController implements PropertyChangeListener{ // omg } } - + @SuppressWarnings("unchecked") private ArrayList<ViewElement>[][] uploadToView() { diff --git a/core/view/GameWindow.java b/core/view/GameWindow.java index de7608984aa16856f8d53fbaf30eb39465e26680..34ef2bc7dd9bd70a5e6ee1aa587e12b5c648bb9b 100644 --- a/core/view/GameWindow.java +++ b/core/view/GameWindow.java @@ -136,7 +136,7 @@ class GameWindow extends JFrame implements KeyListener { "Jaffa lives: "+this.currJaffaLives+" ZPM: " + this.currJaffaZPM); } public void setJaffaZPM(int zpm) { - this.setJaffaZPM(zpm); + this.currJaffaZPM = zpm; this.jaffaStatus.setText( "Jaffa lives: "+this.currJaffaLives+" ZPM: "+this.currJaffaZPM); }