Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.41 KB | None | 0 0
  1.  
  2. ---------- Forwarded message ----------
  3. From: Aman Plahe <amandeep.plahe@hotmail.com>
  4. Date: Sun, Mar 18, 2018 at 8:10 PM
  5. Subject:
  6. To: "shivampatel118@hotmail.co.uk" <shivampatel118@hotmail.co.uk>, Nathaniel Yanney <nathaniel.ayanney@gmail.com>
  7.  
  8.  
  9.  
  10. package uk.ac.brunel.cs1702.Cylons;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Optional;
  14. public class ResurrectionShipFactory implements ShipFinder {
  15. private String shipsCreatedChecker = "";
  16. List<ResurrectionShip> shipsCreated = new ArrayList<ResurrectionShip>();
  17. private static ResurrectionShipFactory instance;
  18. private ResurrectionShipFactory(){}
  19. public static ResurrectionShipFactory getInstance(){
  20. if (instance == null) {
  21. instance = new ResurrectionShipFactory();
  22. }
  23. return instance;
  24. }
  25. public ResurrectionShip getNewShip(int shipID) throws CylonException {
  26. if((shipID <= 0) || (shipID > Constants.MAX_NUMBER_OF_SHIPS)) throw new CylonException();
  27. if(!shipsCreatedChecker.contains(Integer.toString(shipID)))
  28. shipsCreatedChecker += Integer.toString(shipID) + " ";
  29. else
  30. throw new CylonException();
  31. ResurrectionShip currentShip = new ResurrectionShip(shipID);
  32. shipsCreated.add(currentShip);
  33. return currentShip;
  34. }
  35. public ResurrectionShip findYourShip(Cylon cylon) {
  36. Optional<ResurrectionShip> ship = shipsCreated.stream().filter(s -> s.hasModel(cylon.getModel())).findFirst();
  37. if (ship.isPresent()) {
  38. return ship.get();
  39. }
  40. return null;
  41. }
  42. }
  43. // public ResurrectionShip findYourShip(Cylon cylon) {
  44. // // returns the correct ID of the ship that serves the given Cylon in the parameter, for resurrection.
  45. // int cylonsPerShip = Constants.NUMBER_OF_CYLON_MODELS / Constants.MAX_NUMBER_OF_SHIPS;
  46. //
  47. // // formulae to return the shipID the cylon belongs to
  48. // int returnShipID = cylon.getModel()-1 / cylonsPerShip;
  49. //
  50. // return new ResurrectionShip(returnShipID+1);
  51. // }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. package uk.ac.brunel.cs1702;
  65. import uk.ac.brunel.cs1702.Cylons.*;
  66. public class CylonArmy {
  67. private Cylon[][] thisArmy;
  68. private Cylon[][] shipsArmy;
  69. public CylonArmy(int[] modelNoCount) throws CylonException {
  70. if (modelNoCount.length != Constants.NUMBER_OF_CYLON_MODELS) throw new CylonException();
  71. for(int thisModel : modelNoCount)
  72. if(thisModel < 1 || thisModel > Constants.MAX_NUMBER_OF_BODIES_PER_MODEL_IN_RESURRECTION_SHIP)
  73. throw new CylonException();
  74. thisArmy = new Cylon[modelNoCount.length][0];
  75. for (int i = 1; i <= Constants.MAX_NUMBER_OF_SHIPS; i++) {
  76. shipsArmy = ResurrectionShipFactory.getInstance().getNewShip(i).getCylonArmy(modelNoCount);
  77. for (int j = 0; j < shipsArmy.length; j++) {
  78. if (shipsArmy[j].length > 0) thisArmy[j] = shipsArmy[j];
  79. }
  80. }
  81. }
  82. public Cylon[][] getArmy() {
  83. // return this class's current two dimentional army array
  84. return thisArmy;
  85. }
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. package uk.ac.brunel.cs1702.Cylons;
  96. import java.util.ArrayList;
  97. import java.util.HashMap;
  98. import java.util.List;
  99. import java.util.Map;
  100. public class ResurrectionShip {
  101. private int shipID;
  102. private List<Integer> models = new ArrayList<Integer>();
  103. private Map<Integer, Integer> bodiesPerModel = new HashMap<Integer, Integer>();
  104. protected ResurrectionShip(int shipID){
  105. this.shipID = shipID;
  106. int nrOfModels = Constants.NUMBER_OF_CYLON_MODELS / Constants.MAX_NUMBER_OF_SHIPS;
  107. int modelStart = (shipID - 1) * nrOfModels;
  108. for (int i = modelStart; i < modelStart + nrOfModels; i++) {
  109. models.add(i + 1);
  110. bodiesPerModel.put(i + 1,
  111. Constants.MAX_NUMBER_OF_BODIES_PER_MODEL_IN_RESURRECTION_SHIP);
  112. }
  113. }
  114. protected int getBodyCount(int modelNo) {
  115. if (models.contains(modelNo)) {
  116. return bodiesPerModel.get(modelNo);
  117. }
  118. return 0;
  119. }
  120. public Cylon[][] getCylonArmy(int[] modelNoCount) throws CylonException{
  121. if (modelNoCount.length != Constants.NUMBER_OF_CYLON_MODELS) {
  122. throw new CylonException();
  123. }
  124. Cylon[][] armyToReturn = new Cylon[modelNoCount.length][0];
  125. for (int i = 0; i < modelNoCount.length; i++) {
  126. int modelNo = i + 1;
  127. if (models.contains(modelNo)) {
  128. armyToReturn[i] = handleCylonArmyGetterForModel(modelNo, modelNoCount[i]);
  129. }
  130. }
  131. return armyToReturn;
  132. }
  133. private Cylon[] handleCylonArmyGetterForModel(int modelNo, int wantToGet)
  134. throws CylonException {
  135. Cylon[] armyToReturn = new Cylon[0];
  136. if (wantToGet < 0 || !hasEnoughBodies(modelNo, wantToGet)) {
  137. throw new CylonException();
  138. }
  139. armyToReturn = createCylonArmyWithModel(modelNo, wantToGet);
  140. updateBodiesForModel(modelNo, wantToGet);
  141. return armyToReturn;
  142. }
  143. private boolean hasEnoughBodies(int modelNo, int wantToGet) {
  144. int availableForModel = bodiesPerModel.get(modelNo);
  145. if (availableForModel >= wantToGet) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. private Cylon[] createCylonArmyWithModel(int modelNo, int wantToGet) throws CylonException {
  151. Cylon[] returnArmy = new Cylon[wantToGet];
  152. for (int i = 0; i < wantToGet; i++) {
  153. returnArmy[i] = new Cylon(modelNo);
  154. }
  155. return returnArmy;
  156. }
  157. private void updateBodiesForModel(int modelNo, int wantToGet) {
  158. int availableForModel = bodiesPerModel.get(modelNo);
  159. bodiesPerModel.put(modelNo, availableForModel - wantToGet);
  160. }
  161. protected int resurrect(Cylon cylon) throws CylonException{
  162. if (cylon.isInfected()) {
  163. for (int i = 0; i < models.size(); i++) {
  164. bodiesPerModel.put(models.get(i), 0);
  165. }
  166. }
  167. int modelNo = cylon.getModel();
  168. int bodiesLeftForModel = bodiesPerModel.get(modelNo);
  169. if (bodiesLeftForModel > 0) {
  170. bodiesPerModel.put(modelNo, bodiesLeftForModel - 1);
  171. return modelNo;
  172. }
  173. throw new CylonException();
  174. }
  175. protected int getShipID() {
  176. return this.shipID;
  177. }
  178. protected boolean hasModel(int modelNo) {
  179. return models.contains(modelNo);
  180. }
  181. }
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. package uk.ac.brunel.cs1702.Cylons;
  190. public class Cylon {
  191. private int modelNumber;
  192. private int resCount = 0;
  193. private boolean dead = false;
  194. private boolean infected = false;
  195. protected Cylon(int modelNo) throws CylonException{
  196. if (modelNo < 1 || modelNo > Constants.NUMBER_OF_CYLON_MODELS) throw new CylonException();
  197. modelNumber = modelNo;
  198. }
  199. public int getModel() {
  200. return modelNumber;
  201. }
  202. public int getResurrectionCount() {
  203. return resCount;
  204. }
  205. public boolean isDeadForever() {
  206. return dead;
  207. }
  208. public void killed() throws CylonException {
  209. try {
  210. ResurrectionShip ship = ResurrectionShipFactory.getInstance().findYourShip(this);
  211. if (ship != null) {
  212. ship.resurrect(this);
  213. resCount++;
  214. }
  215. else throw new CylonException();
  216. }
  217. catch (CylonException e) {
  218. dead = true;
  219. }
  220. }
  221. public void setInfected(){
  222. infected = true;
  223. }
  224. public boolean isInfected(){
  225. if(infected == true) return true;
  226. else return false;
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement