/** * Random roll chooses which army will execute it's action first, sets those actions and then * executes them turn for turn. Also rolls for a chance that random event will occur. */ public void engageArmies() { while (!firstArmy.isDefeated() && !secondArmy.isDefeated()) { turns++; System.out.println("\nTURN " + turns); if (RandomEventGenerator.eventHappens()) { executeRandomEvent(); } System.out.println(firstArmy.getDetails()); System.out.println(secondArmy.getDetails()); if (firstStrikeChance() > firstStrikeChance()) { System.out.println("You have first strike!"); setPlayerAction(); setComputerAction(); executeFight(firstArmy, secondArmy); } else { System.out.println("Your opponent has first strike!"); setPlayerAction(); setComputerAction(); executeFight(secondArmy, firstArmy); } } }
/** * Defines the characteristics of a <i>TranslateTransition</i>. Each call results in ONE segment * of motion. When that segment is finished, it "chains" another call to <i>startMotion()</i> * (which is NOT recursion)! The initial call is made by the managing <i>Army</i> object; * subsequent calls are made through the "chaining" process described here. * * @param engageInCombat TODO */ public void startMotion(boolean engageInCombat) { Army opposingArmy = armyAllegiance.getOpposingArmy(); Actor opponent = opposingArmy.findNearestOpponent( this); // could legitimately return a null: 1) no one is visible 2) no Actors in // opposing army Point2D newLocation; if (opponent != null) { System.out.printf( "ToMove:[%.1f:%.1f] Opponent:[%.1f:%.1f]\n", getAvatar().getTranslateX(), getAvatar().getTranslateY(), opponent.getAvatar().getTranslateX(), opponent.getAvatar().getTranslateX()); double DISTANCE_FOR_BATTLE = 50.0; if (engageInCombat && distanceTo(opponent) < DISTANCE_FOR_BATTLE) { double h1, h2, h3, h4; // debug code h1 = this.getHealth(); h2 = opponent.getHealth(); combatRound(opponent); h3 = this.getHealth(); h4 = opponent.getHealth(); h4 = h4; if (this.getHealth() <= 0.0) { armyAllegiance.removeNowDeadActor(this); } if (opponent.getHealth() <= 0.0) { opponent.armyAllegiance.removeNowDeadActor(opponent); } } // end if (combat) newLocation = findNewLocation(opponent); } else // end if (test for null opponent) newLocation = meander(); // null opponent means we wander around close to our current location if (tt.getStatus() != Animation.Status.RUNNING) { // if NOT yet RUNNING, start . . . otherwise, do nothing. // tt.setToX(Math.random()*getAvatar().getScene().getWidth()); // tt.setToY(Math.random()*getAvatar().getScene().getHeight()); tt.setToX(validateCoordinate(newLocation).getX()); tt.setToY(validateCoordinate(newLocation).getY()); tt.setDuration( Duration.seconds(MAX_SPEED / (getSpeed() * (armyAllegiance.getSpeedControllerValue())))); tt.setOnFinished(event -> startMotion(true)); // NOT RECURSION!!!! tt .play(); // give assembled object to the render engine (of course, play() is an // object-oriented method which has access to "this" inside, and it can use // "this" to give to the render engine. } } // end startMotion()
public static void main(String[] args) { Army a = new Army(4); a.addWeapon(new Tank()); a.addWeapon(new Flighter()); a.addWeapon(new Warship()); a.addWeapon(new Warship()); a.addWeapon(new Warship()); a.moveAll(); a.attackAll(); }
public static void main(String[] args) { Army army = new Army(100); army.addWeapon(new Tank()); army.addWeapon(new Tank()); army.addWeapon(new Tank()); army.addWeapon(new Flighter()); army.addWeapon(new Flighter()); army.addWeapon(new WarShip()); army.attackAll(); army.moveAll(); }
/** * Allows player to choose his action for the turn among the possible actions. Only valid integers * are allowed as a choice. */ private void setPlayerAction() { while (true) { System.out.println("1)Attack\n2)Commander action\n0)Help"); int userChoice = getValidChoice(); if (userChoice == 0) { showActionHelp(); } else if (userChoice == 1) { firstArmy.setAction(userChoice); break; } else if (userChoice == 2) { if (firstArmy.isCommanderOnCooldown()) { System.out.printf( "%s special ability is on cooldown for another %s turns.\n", firstArmy.getName(), firstArmy.getCommanderCooldown()); } else { firstArmy.setAction(userChoice); break; } } else { System.out.println(userChoice + " is not a valid choice."); } } }
public static void main(String[] args) { // TODO Auto-generated method stub Army a=new Army(4); a.addWeapon(new Tank()); a.addWeapon(new Flighter()); a.addWeapon(new Warship()); a.addWeapon(new Warship()); a.moveAll(); a.attackAll(); }
@Test public void test() { Army forcesOfLight = new Army("Forces of Light", null, Color.RED); final int NUM_ELF = 5; forcesOfLight.populate(ActorFactory.Type.ELF, NUM_ELF); final int NUM_HOBBIT = 6; forcesOfLight.populate(ActorFactory.Type.HOBBIT, NUM_HOBBIT); final int NUM_ORC = 7; forcesOfLight.populate(ActorFactory.Type.ORC, NUM_ORC); final int NUM_WIZARD = 8; forcesOfLight.populate(ActorFactory.Type.WIZARD, NUM_WIZARD); forcesOfLight.display(); assertTrue( "Number Actors created mismatches number stored", forcesOfLight.getSize() == NUM_ELF + NUM_HOBBIT + NUM_ORC + NUM_WIZARD); }
private Point2D validateCoordinate(Point2D possibleNewLocation) { double maxY = armyAllegiance.getScene().getHeight(); double maxX = armyAllegiance.getScene().getWidth(); double myX = possibleNewLocation.getX(); double myY = possibleNewLocation.getY(); double newX = 0.0; double newY = 0.0; if ((myX < 0) && (myY < 0)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.25 * maxX), 2.0); newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.25 * maxY), 2.0); return new Point2D(newX, newY); } else if ((0 < myX) && (myX < maxX) && (myY < 0)) { newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.25 * maxY), 2.0); return new Point2D(myX, newY); } else if ((myX > maxX) && (myY < 0)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxX), 2.0); newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxY), 2.0); return new Point2D(newX, newY); } else if ((0 < myX) && (myY < maxY) && (myY > 0)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.25 * maxX), 2.0); return new Point2D(newX, myY); } else if ((myX > maxX) && (myY < maxY) && (myY > 0)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxX), 2.0); return new Point2D(newX, myY); } if ((myX < 0) && (myY > maxY)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.25 * maxX), 2.0); newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxY), 2.0); return new Point2D(newX, newY); } else if ((0 < myX) && (myX < maxX) && (myY > maxY)) { newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxY), 2.0); return new Point2D(myX, newY); } else if ((myX > maxX) && (myY > maxY)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxX), 2.0); newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxY), 2.0); return new Point2D(newX, newY); } else { return new Point2D(myX, myY); } }
// constructor public Missiles(int row, int col, Army arm, Map m) { super(new Location(row, col), arm, m); // Statistics name = "Missiles"; unitType = 6; setMoveType(MOVE_TIRE); setMove(5); price = 12000; setMaxGas(50); setMaxAmmo(6); setVision(5); minRange = 3; setMaxRange(6); starValue = 1.4; // Fills the Unit's gas and ammo setGas(getMaxGas()); setAmmo(getMaxAmmo()); // make CO adjustments arm.getCO().setChange(this); }
@Override public void actionPerformed(ActionEvent e) { if (upgrade != null && checkBox != null) { if (upgrade.removeGearList.isEmpty()) { if (upgrade.maxInSquad == -1) { upgrade.updateCostBasedOnSize(); upgrade.setActive(checkBox.isSelected()); } else upgrade.setActive(checkBox.isSelected()); } else { if (!upgrade.isActive()) { for (String s : upgrade.removeGearList) { if (s.contains("/")) { new GUI.UpgradeChoiceDialog(upgrade); return; } } } else if (upgrade.maxInSquad == -1) { upgrade.updateCostBasedOnSize(); upgrade.setActive(checkBox.isSelected()); } else upgrade.setActive(checkBox.isSelected()); } } if (e.getSource() == GUI.loadButton) { int returnVal = Main.fileChooser.showOpenDialog(GUI.saveLoad); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = Main.fileChooser.getSelectedFile(); Main.loadArmy(file); GUI.changeOption.setEnabled(true); } } if (e.getSource() == GUI.createButton || e.getSource() == GUI.changeOption) { new GUI.PointsDialog(); } if (e.getSource() == GUI.saveAsOption) { int returnVal = Main.fileChooser.showSaveDialog(GUI.saveLoad); if (returnVal == JFileChooser.APPROVE_OPTION) { File f = Main.fileChooser.getSelectedFile(); Main.saveArmy(f, true); } } if (e.getSource() == GUI.saveOption) { int returnVal; if (!Main.loadedArmy) returnVal = Main.fileChooser.showSaveDialog(GUI.saveLoad); else returnVal = -1; if (returnVal == -1) { File f = new File(Main.currentArmy.fileName); Main.saveArmy(f, false); } else if (returnVal == JFileChooser.APPROVE_OPTION) { File f = Main.fileChooser.getSelectedFile(); Main.saveArmy(f, true); } } if (e.getSource() == GUI.newOption) { Main.currentArmy = null; Main.loadedArmy = false; } if (e.getSource() == GUI.openOption) { if (Main.currentArmy != null) { String[] options = {"Continue", "Save army", "Cancel"}; int n = JOptionPane.showOptionDialog( new JPanel(), "Any unsaved changes will be lost!" + Main.lb + "Are you sure you want to load an army?", "Warning!", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[2]); if (n == JOptionPane.YES_OPTION) { int returnVal = Main.fileChooser.showOpenDialog(GUI.saveLoad); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = Main.fileChooser.getSelectedFile(); Main.loadArmy(file); } } else if (n == JOptionPane.NO_OPTION) { int returnVal; if (!Main.loadedArmy) returnVal = Main.fileChooser.showSaveDialog(GUI.saveLoad); else returnVal = -1; if (returnVal == -1) { File f = new File(Main.currentArmy.fileName); Main.saveArmy(f, false); } else if (returnVal == JFileChooser.APPROVE_OPTION) { File f = Main.fileChooser.getSelectedFile(); Main.saveArmy(f, true); } int returnVal1 = Main.fileChooser.showOpenDialog(GUI.saveLoad); if (returnVal1 == JFileChooser.APPROVE_OPTION) { File file = Main.fileChooser.getSelectedFile(); Main.loadArmy(file); } } } else { int returnVal = Main.fileChooser.showOpenDialog(GUI.saveLoad); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = Main.fileChooser.getSelectedFile(); Main.loadArmy(file); } } } if (affected.equalsIgnoreCase("racecombo")) { if (!((String) GUI.raceCombo.getSelectedItem()).equalsIgnoreCase("Pick a race!")) { GUI.raceList.setModel(Main.raceListModels.get((String) GUI.raceCombo.getSelectedItem())); CardLayout cl1 = (CardLayout) (GUI.cardPanel.getLayout()); cl1.show(GUI.cardPanel, "blank"); GUI.raceCombo.removeItem("Pick a race!"); } } else if (affected.equalsIgnoreCase("addUnit")) { Unit u1; if (GUI.raceList.getSelectedValue() != null) { u1 = new Unit(GUI.raceList.getSelectedValue()); if (Main.currentArmy != null) { Main.currentArmy.addUnit(u1); if (u1.addsUnit) { for (Unit u2 : u1.unitsToAdd) { Main.currentArmy.addUnit(new Unit(u2)); } } } else { GUI.saveOption.setEnabled(true); GUI.saveAsOption.setEnabled(true); Army newArmy = new Army(u1.getRace()).addUnit(u1); Main.currentArmy = newArmy; if (u1.addsUnit) { for (Unit u2 : u1.unitsToAdd) { newArmy.addUnit(new Unit(u2)); } } GUI.lblCurrentArmy.setVisible(true); } } else return; Main.currentArmy.update(); } else if (affected.equalsIgnoreCase("remove")) { Unit u = (Unit) GUI.armyList.getSelectedValue(); if (u.addsUnit) for (Unit u1 : u.unitsToAdd) Main.getCurrentArmy().removeUnit(u1); Main.getCurrentArmy().removeUnit(u); if (Main.getCurrentArmy().isEmpty()) { CardLayout cl1 = (CardLayout) (GUI.bottomPanel.getLayout()); cl1.show(GUI.bottomPanel, "blank"); } } else if (affected.equalsIgnoreCase("upgrade1")) { button.setSelected(false); upgrade.dialog.setVisible(false); } else if (affected.equalsIgnoreCase("edit")) { Unit w = (Unit) GUI.armyList.getSelectedValue(); if (w != null) { CardLayout cl1 = (CardLayout) (GUI.bottomPanel.getLayout()); cl1.show(GUI.bottomPanel, "add"); CardLayout cl2 = (CardLayout) (GUI.statsCard.getLayout()); cl2.show(GUI.statsCard, String.valueOf(w.getType())); GUI.modelNameLabel.setText(w.getName()); int[] stats = w.getStats(); int i = 0; for (JLabel l : GUI.statLabelList) { if (i < 8) l.setText(String.valueOf(stats[i++])); else l.setText(String.valueOf(stats[i++]) + "+"); } CardLayout cl = (CardLayout) (GUI.cardPanel.getLayout()); cl.show(GUI.cardPanel, w.getCardName()); GUI.rulesList.setModel(w.getRulesModel()); GUI.gearList.setModel(w.getGearModel()); GUI.costLabel.setText(w.getCost()); GUI.numberSpinner.setModel(w.getNumberModel()); w.update(); for (ChangeListener s : GUI.numberSpinner.getChangeListeners()) if (s instanceof SpinnerListener) ((SpinnerListener) s).setUnit(w); } } }
/** * After setting which army will execute it's action first it will execute morale modifiers * accordingly, execute action of the army with first strike and then check if the other army has * been defeated. If it has the game is over and defeat is called out. Otherwise it will execute * the action of the other army and check if the first army has been defeated. If it has the game * is over and defeat is called out. * * @param firstStrikeArmy * @param secondStrikeArmy */ private void executeFight(Army firstStrikeArmy, Army secondStrikeArmy) { firstStrikeArmy.increaseMorale(0.05f); secondStrikeArmy.reduceMorale(0.05f); if (firstStrikeArmy.getAction() == 1) { secondStrikeArmy.takeDamage(firstStrikeArmy.dealDamage(maxTurnDamage)); } else if (firstStrikeArmy.getAction() == 2) { firstStrikeArmy.executeSpecial(); } if (secondStrikeArmy.isDefeated()) { callOutDefeat(secondStrikeArmy.getName()); } else { if (secondStrikeArmy.getAction() == 1) { firstStrikeArmy.takeDamage(secondStrikeArmy.dealDamage(maxTurnDamage)); } else if (secondStrikeArmy.getAction() == 2) { secondStrikeArmy.executeSpecial(); } if (firstStrikeArmy.isDefeated()) { callOutDefeat(firstStrikeArmy.getName()); } } }
/** Uses controller to set actions for computer controlled army. */ private void setComputerAction() { secondArmy.setAction(ComputerArmyController.getAction(secondArmy)); }