Update Travel.java

This commit is contained in:
Vikramb987
2019-02-21 22:08:26 -07:00
committed by GitHub
parent 1540323985
commit b78ba180f5

View File

@@ -1,64 +1,79 @@
import java.util.Random; import java.util.Random;
import java.util.Scanner; import java.util.Scanner;
public class Travel extends Player { public class Travel {
private void seaAtlas(int locationOfTravel){ private Player player;
public void Player(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
public Player getPlayer() {
Player playerDummy = new Player(player);
return playerDummy;
}
public Travel(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
private void seaAtlas(int locationOfTravel) {
switch (locationOfTravel) { switch (locationOfTravel) {
case 1: case 1:
System.out.println("\nArriving at Hong Kong"); System.out.println("\nArriving at Hong Kong");
setLocation(1); player.setLocation(1);
break; break;
case 2: case 2:
System.out.println("\nArriving at Shanghai"); System.out.println("\nArriving at Shanghai");
setLocation(2); player.setLocation(2);
break; break;
case 3: case 3:
System.out.println("\nArriving at Nagasaki"); System.out.println("\nArriving at Nagasaki");
setLocation(3); player.setLocation(3);
break; break;
case 4: case 4:
System.out.println("\nArriving at Saigon"); System.out.println("\nArriving at Saigon");
setLocation(4); player.setLocation(4);
break; break;
case 5: case 5:
System.out.println("\nArriving at Manila"); System.out.println("\nArriving at Manila");
setLocation(5); player.setLocation(5);
break; break;
case 6: case 6:
System.out.println("\nArriving at Singapore"); System.out.println("\nArriving at Singapore");
setLocation(6); player.setLocation(6);
break; break;
case 7: case 7:
System.out.println("\nArriving at Batavia"); System.out.println("\nArriving at Batavia");
setLocation(7); player.setLocation(7);
break; break;
} }
} }
private void randomEventSea(int locationOfTravel) throws Exception { private void randomEventSea(int locationOfTravel) throws Exception {
ShipWarfare attackShip = new ShipWarfare(); ShipWarfare attackShip = new ShipWarfare(player);
Random rand = new Random(); Random rand = new Random();
int randGenNum = rand.nextInt(3) + 1; int randGenNum = rand.nextInt(3) + 1;
if(randGenNum == 1){ if (randGenNum == 1) {
attackShip.peasantFleetAttack(); attackShip.peasantFleetAttack();
} } else if (randGenNum == 2) {
else if(randGenNum == 2){
disaster(locationOfTravel); disaster(locationOfTravel);
} }
System.out.println("We made it!"); System.out.println("We made it!");
} }
private void disaster(int locationOfTravel){ private void disaster(int locationOfTravel) {
System.out.print("Storm "+getName()+"! "); System.out.print("Storm " + player.getName() + "! ");
Random rand = new Random(); Random rand = new Random();
int randGenNum = rand.nextInt(5) + 1; int randGenNum = rand.nextInt(5) + 1;
if(randGenNum <= 2){ if (randGenNum <= 2) {
System.out.println(" We made it through!"); System.out.println(" We made it through!");
} } else {
else{ while (randGenNum == locationOfTravel) {
while(randGenNum == locationOfTravel){
randGenNum = rand.nextInt(7) + 1; randGenNum = rand.nextInt(7) + 1;
if (randGenNum != locationOfTravel) { if (randGenNum != locationOfTravel) {
System.out.println("We've been blown off course!"); System.out.println("We've been blown off course!");
@@ -68,36 +83,31 @@ public class Travel extends Player {
} }
} }
public void travelTo(){ public void travelTo() {
Scanner keyboard = new Scanner(System.in); Scanner keyboard = new Scanner(System.in);
String response; String response;
int tempInt; int tempInt;
boolean hasTraveled = false; boolean hasTraveled = false;
while (true) { while (true) {
System.out.println("\n" + getName() + ", do you wish to go to:\n"); System.out.println("\n" + player.getName() + ", do you wish to go to:\n");
System.out.println("1) Hong Kong, 2) Shanghai, 3) Nagasaki,\n4) Saigon, 5) Manila, 6) Singapore, or 7) Batavia?"); System.out.println("1) Hong Kong, 2) Shanghai, 3) Nagasaki,\n4) Saigon, 5) Manila, 6) Singapore, or 7) Batavia?");
response = keyboard.nextLine(); response = keyboard.nextLine();
try { try {
tempInt = Integer.parseInt(response); tempInt = Integer.parseInt(response);
System.out.println(tempInt + " " + getLocation()); System.out.println(tempInt + " " + player.getLocation());
if(tempInt != getLocation()){ if (tempInt != player.getLocation()) {
randomEventSea(tempInt); randomEventSea(tempInt);
seaAtlas(tempInt); seaAtlas(tempInt);
hasTraveled = true; hasTraveled = true;
} } else System.out.println("\nYou're already here " + player.getName() + ".");
else System.out.println("\nYou're already here " + getName() + "."); } catch (Exception e) {
System.out.print("\nSorry, " + player.getName() + " could you say that again?");
} }
catch (Exception e){ if (hasTraveled) {
System.out.print("\nSorry, " + getName() + " could you say that again?"); break;
} }
if(hasTraveled){break;}
} }
} }
public static void main(String[] args){
Travel ship = new Travel();
ship.travelTo();
}
} }