Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------- Forwarded message ----------
- From: Aman Plahe <amandeep.plahe@hotmail.com>
- Date: Sun, Mar 18, 2018 at 8:10 PM
- Subject:
- To: "shivampatel118@hotmail.co.uk" <shivampatel118@hotmail.co.uk>, Nathaniel Yanney <nathaniel.ayanney@gmail.com>
- package uk.ac.brunel.cs1702.Cylons;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Optional;
- public class ResurrectionShipFactory implements ShipFinder {
- private String shipsCreatedChecker = "";
- List<ResurrectionShip> shipsCreated = new ArrayList<ResurrectionShip>();
- private static ResurrectionShipFactory instance;
- private ResurrectionShipFactory(){}
- public static ResurrectionShipFactory getInstance(){
- if (instance == null) {
- instance = new ResurrectionShipFactory();
- }
- return instance;
- }
- public ResurrectionShip getNewShip(int shipID) throws CylonException {
- if((shipID <= 0) || (shipID > Constants.MAX_NUMBER_OF_SHIPS)) throw new CylonException();
- if(!shipsCreatedChecker.contains(Integer.toString(shipID)))
- shipsCreatedChecker += Integer.toString(shipID) + " ";
- else
- throw new CylonException();
- ResurrectionShip currentShip = new ResurrectionShip(shipID);
- shipsCreated.add(currentShip);
- return currentShip;
- }
- public ResurrectionShip findYourShip(Cylon cylon) {
- Optional<ResurrectionShip> ship = shipsCreated.stream().filter(s -> s.hasModel(cylon.getModel())).findFirst();
- if (ship.isPresent()) {
- return ship.get();
- }
- return null;
- }
- }
- // public ResurrectionShip findYourShip(Cylon cylon) {
- // // returns the correct ID of the ship that serves the given Cylon in the parameter, for resurrection.
- // int cylonsPerShip = Constants.NUMBER_OF_CYLON_MODELS / Constants.MAX_NUMBER_OF_SHIPS;
- //
- // // formulae to return the shipID the cylon belongs to
- // int returnShipID = cylon.getModel()-1 / cylonsPerShip;
- //
- // return new ResurrectionShip(returnShipID+1);
- // }
- package uk.ac.brunel.cs1702;
- import uk.ac.brunel.cs1702.Cylons.*;
- public class CylonArmy {
- private Cylon[][] thisArmy;
- private Cylon[][] shipsArmy;
- public CylonArmy(int[] modelNoCount) throws CylonException {
- if (modelNoCount.length != Constants.NUMBER_OF_CYLON_MODELS) throw new CylonException();
- for(int thisModel : modelNoCount)
- if(thisModel < 1 || thisModel > Constants.MAX_NUMBER_OF_BODIES_PER_MODEL_IN_RESURRECTION_SHIP)
- throw new CylonException();
- thisArmy = new Cylon[modelNoCount.length][0];
- for (int i = 1; i <= Constants.MAX_NUMBER_OF_SHIPS; i++) {
- shipsArmy = ResurrectionShipFactory.getInstance().getNewShip(i).getCylonArmy(modelNoCount);
- for (int j = 0; j < shipsArmy.length; j++) {
- if (shipsArmy[j].length > 0) thisArmy[j] = shipsArmy[j];
- }
- }
- }
- public Cylon[][] getArmy() {
- // return this class's current two dimentional army array
- return thisArmy;
- }
- }
- package uk.ac.brunel.cs1702.Cylons;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class ResurrectionShip {
- private int shipID;
- private List<Integer> models = new ArrayList<Integer>();
- private Map<Integer, Integer> bodiesPerModel = new HashMap<Integer, Integer>();
- protected ResurrectionShip(int shipID){
- this.shipID = shipID;
- int nrOfModels = Constants.NUMBER_OF_CYLON_MODELS / Constants.MAX_NUMBER_OF_SHIPS;
- int modelStart = (shipID - 1) * nrOfModels;
- for (int i = modelStart; i < modelStart + nrOfModels; i++) {
- models.add(i + 1);
- bodiesPerModel.put(i + 1,
- Constants.MAX_NUMBER_OF_BODIES_PER_MODEL_IN_RESURRECTION_SHIP);
- }
- }
- protected int getBodyCount(int modelNo) {
- if (models.contains(modelNo)) {
- return bodiesPerModel.get(modelNo);
- }
- return 0;
- }
- public Cylon[][] getCylonArmy(int[] modelNoCount) throws CylonException{
- if (modelNoCount.length != Constants.NUMBER_OF_CYLON_MODELS) {
- throw new CylonException();
- }
- Cylon[][] armyToReturn = new Cylon[modelNoCount.length][0];
- for (int i = 0; i < modelNoCount.length; i++) {
- int modelNo = i + 1;
- if (models.contains(modelNo)) {
- armyToReturn[i] = handleCylonArmyGetterForModel(modelNo, modelNoCount[i]);
- }
- }
- return armyToReturn;
- }
- private Cylon[] handleCylonArmyGetterForModel(int modelNo, int wantToGet)
- throws CylonException {
- Cylon[] armyToReturn = new Cylon[0];
- if (wantToGet < 0 || !hasEnoughBodies(modelNo, wantToGet)) {
- throw new CylonException();
- }
- armyToReturn = createCylonArmyWithModel(modelNo, wantToGet);
- updateBodiesForModel(modelNo, wantToGet);
- return armyToReturn;
- }
- private boolean hasEnoughBodies(int modelNo, int wantToGet) {
- int availableForModel = bodiesPerModel.get(modelNo);
- if (availableForModel >= wantToGet) {
- return true;
- }
- return false;
- }
- private Cylon[] createCylonArmyWithModel(int modelNo, int wantToGet) throws CylonException {
- Cylon[] returnArmy = new Cylon[wantToGet];
- for (int i = 0; i < wantToGet; i++) {
- returnArmy[i] = new Cylon(modelNo);
- }
- return returnArmy;
- }
- private void updateBodiesForModel(int modelNo, int wantToGet) {
- int availableForModel = bodiesPerModel.get(modelNo);
- bodiesPerModel.put(modelNo, availableForModel - wantToGet);
- }
- protected int resurrect(Cylon cylon) throws CylonException{
- if (cylon.isInfected()) {
- for (int i = 0; i < models.size(); i++) {
- bodiesPerModel.put(models.get(i), 0);
- }
- }
- int modelNo = cylon.getModel();
- int bodiesLeftForModel = bodiesPerModel.get(modelNo);
- if (bodiesLeftForModel > 0) {
- bodiesPerModel.put(modelNo, bodiesLeftForModel - 1);
- return modelNo;
- }
- throw new CylonException();
- }
- protected int getShipID() {
- return this.shipID;
- }
- protected boolean hasModel(int modelNo) {
- return models.contains(modelNo);
- }
- }
- package uk.ac.brunel.cs1702.Cylons;
- public class Cylon {
- private int modelNumber;
- private int resCount = 0;
- private boolean dead = false;
- private boolean infected = false;
- protected Cylon(int modelNo) throws CylonException{
- if (modelNo < 1 || modelNo > Constants.NUMBER_OF_CYLON_MODELS) throw new CylonException();
- modelNumber = modelNo;
- }
- public int getModel() {
- return modelNumber;
- }
- public int getResurrectionCount() {
- return resCount;
- }
- public boolean isDeadForever() {
- return dead;
- }
- public void killed() throws CylonException {
- try {
- ResurrectionShip ship = ResurrectionShipFactory.getInstance().findYourShip(this);
- if (ship != null) {
- ship.resurrect(this);
- resCount++;
- }
- else throw new CylonException();
- }
- catch (CylonException e) {
- dead = true;
- }
- }
- public void setInfected(){
- infected = true;
- }
- public boolean isInfected(){
- if(infected == true) return true;
- else return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement