Solution
Transcription
Solution
Pirates (Sea Battle) File: SeaBattle.java | Project: Pirates 11TG /** * Simulates a simple battle of two ships on the open sea... * * @author gamca174 (Gamboa Carlos) / olial319 (Olinger Alex) * @version 06/05/2014 14:16:28 * Classe: 11TG */ public class SeaBattle { private Ship blackPearl; public void playGame() { Ship pirate = new Ship("Rackam", 11); Ship admiral = new Ship("Licorne", 18); System.out.println(pirate); System.out.println(admiral); pirate.encounter(admiral); //blackPearl = admiral; //blackPearl.encounter(pirate); } } admiral.encounter(admiral); /** * The game... */ public static void main(String[] args) { SeaBattle battle = new SeaBattle(); battle.playGame(); } Solution 1 / 2 Pirates (Sea Battle) /** * Simulates * * @author * @version * Classe: */ public class { private private File: Ship.java | Project: Pirates 11TG a simple ship. gamca174 (Gamboa Carlos) / olial319 (Olinger Alex) 06/05/2014 14:16:28 11TG Ship String name; int lifePoints; public Ship(String pName, int pLifePoints) { name = pName; lifePoints = pLifePoints; } public String getName() { return name; } public int getLifePoints() { return lifePoints; } /* méthode: toString - état du bateau sous forme de texte - pas de paramètres nous voulons (par exemple pour le bateau nommé: Licorne) un résultat comme celui-ci: Licorne -> 7 points par contre s'il n'a plus de points de vie il faut marquer: */ Licorne -> destroyed!! public String toString() { String res = " -> "; if (lifePoints == 0) res = res + "destroyed !!"; else res = res + lifePoints +" points"; return res; } public void doDamage(int pDamage) { System.out.println(name+" - status before attack "+toString()); lifePoints = lifePoints - pDamage; if (lifePoints < 0) lifePoints = 0; } System.out.println(name+" - receives "+pDamage+" damage points"); System.out.println(name+" - status after attack "+toString()); /* méthode: encounter - pas de résultat - un bateau comme paramètre appel de la méthode doDamage sur les 2 bateaux ayant pour paramètre un dommage aléatoire entre 2 et 10 points (limites comprise!) */ } public void encounter(Ship pOther) { System.out.println(name+" attacks "+pOther.getName()); doDamage((int)(Math.random()*9)+2); pOther.doDamage((int)(Math.random()*9)+2); System.out.println(); } Solution 2 / 2