From 70b698c22eca48a67248b1a9380169261094a578 Mon Sep 17 00:00:00 2001 From: KahootChampion Date: Mon, 25 Feb 2019 19:33:48 -0700 Subject: [PATCH] Java Docs BOIZ for ShipWarafare --- .idea/workspace.xml | 207 ++++++++++++++++++-------------------- src/ShipWarfare.java | 231 ++++++++++++++++++++++++++++--------------- 2 files changed, 248 insertions(+), 190 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index dd86bd4..1129f0e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,7 @@ - + @@ -479,13 +426,13 @@ - - + - + @@ -500,7 +447,7 @@ - + @@ -520,6 +467,42 @@ + + + + + + @@ -550,26 +533,7 @@ - - - - - - - - - - - - - - - - - - - - + @@ -591,24 +555,6 @@ - - - - - - - - - - - - - - - - - - @@ -633,6 +579,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/ShipWarfare.java b/src/ShipWarfare.java index 49efdca..9ea3055 100644 --- a/src/ShipWarfare.java +++ b/src/ShipWarfare.java @@ -2,33 +2,47 @@ import java.util.Scanner; import java.util.Random; import java.util.concurrent.TimeUnit; -public class ShipWarfare { + +public class ShipWarfare extends Player { private int numOfPeasantShips = 0; private int numOfLittyShips = 0; private boolean userAttacks = true; private int startingPeasantShips = 0; - private int startingLittyShips =0; + private int startingLittyShips = 0; private int howMuchRun = 0; private String pirateName = "Liu Yen"; private Player player; - public ShipWarfare(Player player){ + public ShipWarfare(Player player) { Player playerDummy = new Player(player); this.player = playerDummy; } + /** + * setter method for player + * @param player object of the class Player + */ + public void setPlayer(Player player) { Player playerDummy = new Player(player); this.player = playerDummy; } - public Player getPlayer(){ + /** + * getter method for obtaining a player object. + * @return returns player object + */ + + public Player getPlayer() { Player playerDummy = new Player(player); return playerDummy; } - - //This fleet is easy to defeat as a maximum of 15 ships can run away each volley, they can not tank hits + + /** + * This fleet is easy to defeat as a maximum of 15 ships can run away each volley, they can not tank hits + * @throws Exception in case of errors due to the delay + */ public void peasantFleetAttack() throws Exception { Scanner userResponse = new Scanner(System.in); setNumOfPeasantShips(numOfShips()); @@ -41,6 +55,7 @@ public class ShipWarfare { if (response.equalsIgnoreCase("f")) { userAttacks = true; System.out.println("Ohh, fight ehh?"); + delayForSeconds(1); boolean winOrLose = destroyPeasantShipsOrEscape(); if (winOrLose == true) { break; @@ -54,6 +69,7 @@ public class ShipWarfare { break; } else { System.out.println("Phew! Got away safely"); + delayForSeconds(2); break; } @@ -63,7 +79,11 @@ public class ShipWarfare { } - //This fleet is difficult to defeat as a maximum of 10 ships can run away each volley, they can tank hits + + /** + * This fleet is difficult to defeat as a maximum of 10 ships can run away each volley, they can tank hits + * @throws Exception in case of errors due to the delay + */ public void littyFleetAttack() throws Exception { Scanner userResponse = new Scanner(System.in); setNumOfLittyShips(numOfShips()); @@ -83,10 +103,12 @@ public class ShipWarfare { } else if (response.equalsIgnoreCase("r")) { if (runFromShips() == false) { System.out.println("Couldn't run away!"); + delayForSeconds(1); if (destroyLittyShipsOrEscape()) break; } else { System.out.println("Phew! Got away safely"); + delayForSeconds(2); break; } @@ -97,34 +119,49 @@ public class ShipWarfare { } + /** + * Asks user if they would like to fight or run against ships + */ public void fightOrRunMessage() { - System.out.printf("What do you want to do? Enter \"f\" to fight, and \"r\" to run (we have %d guns)", player.getGuns()); - - } - - public int numOfPeasantShips() { - return numOfPeasantShips; + System.out.printf("What do you want to do? Enter \"f\" to fight, and \"r\" to run (we have %d guns)\n", player.getGuns()); } + /** + * setter method that takes in an integer as an argument + * @param numOfLittyShips the number of ships to be used in the litty fleet attack + */ public void setNumOfLittyShips(int numOfLittyShips) { this.numOfLittyShips = numOfLittyShips; startingLittyShips = numOfLittyShips; } + /** + * setter method that takes in an integer as an argument + * @param numOfPeasantShips the number of ships to be used in the peasant fleet attack + */ + public void setNumOfPeasantShips(int numOfPeasantShips) { this.numOfPeasantShips = numOfPeasantShips; startingPeasantShips = numOfPeasantShips; } - public void delayForASecond() throws Exception { - TimeUnit.SECONDS.sleep(1); + /** + * delays for a specific amount of seconds, takes an integer as an argument + * @param num the seconds to delay + * @throws Exception in case of errors due to the delay + */ + public void delayForSeconds(int num) throws Exception { + TimeUnit.SECONDS.sleep(num); } - //The number of ships which attack is based on the amount of money one has on hand + /** + * The number of ships that attack is based on the amount of money one has on hand + * @return the number of ships which will attack + */ public int numOfShips() { int numOfShipsAttacking = 0; @@ -148,7 +185,11 @@ public class ShipWarfare { } - //One in two chance of running away + /** + * One in two chance of running away + * @return true if the user is allowed to run, false if not, the "default" is false + */ + public boolean runFromShips() { userAttacks = false; Random randomValue = new Random(); @@ -161,6 +202,11 @@ public class ShipWarfare { return false; } + /** + * The user faces off against the litty ships and either prevails, dies, or runs away + * @return true if the user wins, loses, or flees, it returns false otherwise + * @throws Exception in case of errors due to the delay + */ //The loot for defeating a litty fleet is much higher than that of a peasant one public boolean destroyLittyShipsOrEscape() throws Exception { int calculateLoot = 0; @@ -173,29 +219,34 @@ public class ShipWarfare { //Player volley while (exitValue == 0) { - for (int j = 0; j < player.getGuns(); j++) { - if (userAttacks == true) { - int hitOrMiss = randomValue.nextInt(3) + 1; - if (hitOrMiss == 1) { - numOfLittyShips--; - if (numOfLittyShips <= 0) { - exitValue = 1; - break; + if (player.getGuns() > 0) { + for (int j = 0; j < player.getGuns(); j++) { + if (userAttacks == true) { + int hitOrMiss = randomValue.nextInt(3) + 1; + if (hitOrMiss == 1) { + numOfLittyShips--; + if (numOfLittyShips <= 0) { + exitValue = 1; + break; + } + System.out.println("Got eem"); + delayForSeconds(1); + } else if (hitOrMiss == 2) { + System.out.printf("ARRG! We missed %s\n", getName()); + delayForSeconds(1); + } else { + System.out.println("Darn! Their fleet tanked our attack"); + delayForSeconds(1); } - System.out.println("Got eem"); - delayForASecond(); - } else if (hitOrMiss == 2) { - System.out.printf("ARRG! We missed %s\n", player.getName()); - delayForASecond(); + + } else { - System.out.println("Darn! Their fleet tanked our attack"); - delayForASecond(); + continue; } - - - } else { - continue; } + } else { + System.out.printf("%s! We don't have any GUNS!!!!\n",player.getName()); + delayForSeconds(1); } @@ -203,24 +254,26 @@ public class ShipWarfare { exitValue = 1; break; } - chanceOfEnemyRun = randomValue.nextInt(2) + 1; - if (chanceOfEnemyRun == 2) { - howMuchRun = randomValue.nextInt(10) + 1; - if (howMuchRun != 0 && howMuchRun < numOfLittyShips) { + if (player.getGuns() > 0) { + if (chanceOfEnemyRun == 2) { + chanceOfEnemyRun = randomValue.nextInt(2) + 1; + howMuchRun = randomValue.nextInt(10) + 1; + if (howMuchRun != 0 && howMuchRun < numOfLittyShips) { - setNumOfLittyShips(numOfLittyShips - howMuchRun); - if (userAttacks == true) { - System.out.printf("Cowards! %d ships ran away %s!\n", howMuchRun, player.getName()); - } else { - System.out.printf("Escaped %d of them!\n", howMuchRun); + setNumOfLittyShips(numOfLittyShips - howMuchRun); + if (userAttacks == true) { + System.out.printf("Cowards! %d ships ran away %s!\n", howMuchRun, getName()); + } else { + System.out.printf("Escaped %d of them!\n", howMuchRun); + } } } } System.out.printf("%d ships remaining\n", numOfLittyShips); System.out.println("Oh no, they are taking the offensive!"); - delayForASecond(); + delayForSeconds(1); //Computer volley int takeGunChance = randomValue.nextInt(4) + 1; if (takeGunChance == 1 && player.getGuns() > 0) { @@ -234,7 +287,7 @@ public class ShipWarfare { break; } System.out.printf("EEK, our current ship status is %d%% \n", player.getHP()); - delayForASecond(); + delayForSeconds(1); if (userAttacks == false) { userAttacks = true; } @@ -245,8 +298,10 @@ public class ShipWarfare { if (response.equalsIgnoreCase("r")) { if (runFromShips() == false) { System.out.println("Couldn't run away"); + delayForSeconds(1); } else { - exitValue = 3; + System.out.println("Phew! Got away safely"); + delayForSeconds(2); break; } } @@ -257,15 +312,18 @@ public class ShipWarfare { if (exitValue == 1) { System.out.printf("\nGot eem\nVictory!\nIt appears we have defeated the enemy fleet and made it out at %d%% ship status\n", player.getHP()); + delayForSeconds(1); calculateLoot = (randomValue.nextInt(startingLittyShips) + startingLittyShips) * 300; player.setMoney(player.getMoney() + calculateLoot); System.out.printf("We got $%,d!\n", calculateLoot); + delayForSeconds(2); return true; } else if (exitValue == 2) { - player.gameOver(); + gameOver(); return true; } else if (exitValue == 3) { System.out.printf("We made it out at %d%% ship status!\n", player.getHP()); + delayForSeconds(2); return true; } return false; @@ -273,6 +331,11 @@ public class ShipWarfare { } + /** + * The user faces off against the peasant ships and either prevails, dies, or runs away + * @return true if the user wins, loses, or flees, it returns false otherwise + * @throws Exception in case of errors due to the delay + */ public boolean destroyPeasantShipsOrEscape() throws Exception { int calculateLoot = 0; @@ -285,51 +348,62 @@ public class ShipWarfare { //Player volley while (exitValue == 0) { - for (int j = 0; j < player.getGuns(); j++) { - if (userAttacks == true) { - int hitOrMiss = randomValue.nextInt(2) + 1; - if (hitOrMiss == 2) { - numOfPeasantShips--; - if (numOfPeasantShips <= 0) { - exitValue = 1; - break; + if (player.getGuns() > 0) { + + for (int j = 0; j < player.getGuns(); j++) { + if (userAttacks == true) { + int hitOrMiss = randomValue.nextInt(2) + 1; + if (hitOrMiss == 2) { + numOfPeasantShips--; + if (numOfPeasantShips <= 0) { + exitValue = 1; + break; + } + System.out.println("Got eem"); + delayForSeconds(1); + } else { + System.out.printf("ARRG! We missed %s\n", getName()); + delayForSeconds(1); } - System.out.println("Got eem"); - delayForASecond(); + + } else { - System.out.printf("ARRG! We missed %s\n", player.getName()); - delayForASecond(); + continue; } - - - } else { - continue; } } + else{ + System.out.printf("%s! We don't have any GUNS!!!!\n", player.getName()); + delayForSeconds(1); + + } if (numOfPeasantShips <= 0) { exitValue = 1; break; } - chanceOfEnemyRun = randomValue.nextInt(2) + 1; - if (chanceOfEnemyRun == 2) { - howMuchRun = randomValue.nextInt(15) + 1; - if (howMuchRun != 0 && howMuchRun < numOfPeasantShips) { + if (player.getGuns() > 0) { + chanceOfEnemyRun = randomValue.nextInt(2) + 1; + if (chanceOfEnemyRun == 2) { + howMuchRun = randomValue.nextInt(15) + 1; + if (howMuchRun != 0 && howMuchRun < numOfPeasantShips) { - setNumOfPeasantShips(numOfPeasantShips - howMuchRun); - if (userAttacks == true) { - System.out.printf("Ahhh, %d ships ran away %s!\n", howMuchRun, player.getName()); - } else { - System.out.printf("Escaped %d of them!\n", howMuchRun); + setNumOfPeasantShips(numOfPeasantShips - howMuchRun); + if (userAttacks == true) { + System.out.printf("Ahhh, %d ships ran away %s!\n", howMuchRun, getName()); + } else { + System.out.printf("Escaped %d of them!\n", howMuchRun); + } } } } System.out.printf("%d ships remaining\n", numOfPeasantShips); + delayForSeconds(1); System.out.println("Oh no, they are taking the offensive!"); - delayForASecond(); + delayForSeconds(1); //Computer volley int takeGunChance = randomValue.nextInt(4) + 1; if (takeGunChance == 1 && player.getGuns() > 0) { @@ -343,7 +417,7 @@ public class ShipWarfare { break; } System.out.printf("EEK, our current ship status is %d%% \n", player.getHP()); - delayForASecond(); + delayForSeconds(1); if (userAttacks == false) { userAttacks = true; } @@ -366,15 +440,18 @@ public class ShipWarfare { if (exitValue == 1) { System.out.printf("\nGot eem\nVictory!\nIt appears we have defeated the enemy fleet and made it out at %d%% ship status\n", player.getHP()); + delayForSeconds(1); calculateLoot = (randomValue.nextInt(startingPeasantShips) + startingPeasantShips) * 100; player.setMoney(player.getMoney() + calculateLoot); System.out.printf("We got $%,d!", calculateLoot); + delayForSeconds(2); return true; } else if (exitValue == 2) { - player.gameOver(); + gameOver(); return true; } else if (exitValue == 3) { System.out.printf("We made it out at %d%% ship status!\n", player.getHP()); + delayForSeconds(2); return true; } return false;