package javaEffect.ui;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.zip.DataFormatException;
import javaEffect.ErrAttack;
import javaEffect.ErrNegativeNumber;
import javaEffect.ErrNotFountOrMistake;
import javaEffect.planet.*;
import javaEffect.characters.Asari;
import javaEffect.characters.Character;
import javaEffect.characters.Human;
import javaEffect.characters.Krogan;
import javaEffect.spacecraft.*;
import javaEffect.xml.MissionsReader;
/**
* This is the user interface class
*
* @author Thibaut LOCQUET & Jack OUTRAN
* @version 0.1
*
*/
public class Ui {
private Scanner input;
Planet playerArea;
Spaceship playerSpaceship;
int playerIndex;
Character player;
Character character;
Spacecraft spacecraft;
Spaceship spaceship;
int missionId;
public int getMissionId() {
return missionId;
}
MissionsReader mission;
Character target;
/**
*
* @param playerArea where the player begins
* @param playerSpaceship the spaceship's player at the beginning
* @param mission the tree of missions
*/
public Ui(Planet playerArea, Spaceship playerSpaceship,
MissionsReader mission) {
this.input = new Scanner(System.in);
this.player = null;
this.playerArea = playerArea;
this.playerSpaceship = playerSpaceship;
this.mission = mission;
missionId = 0;
try {
this.target = mission.startMission(missionId);
} catch (ErrNotFountOrMistake e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
/**
* Method to set initial value like player name, filename,...
*
* @param firstName The player firstName
* @param lastName The player lastName
* @param race The player race
*/
public void setup(String firstName, String lastName, String race) {
/*
* System.out.println("Player informations:" );
* System.out.println("\tFirst name:" ); String firstName=input.next();
*
* System.out.println("\tLast name:" ); String lastName=input.next();
*/
if (race.equalsIgnoreCase("Human")) {
this.player = new Human(firstName, lastName, 100);
} else if (race.equalsIgnoreCase("Asari")) {
this.player = new Asari(firstName, lastName, 100);
} else if (race.equalsIgnoreCase("Krogan")) {
this.player = new Krogan(firstName, lastName, 100);
}
try {
this.playerArea.addCharacter(this.player);
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Method to manage all the user actions in the game
*/
public void action() {
int index = 0;
boolean ifInt;
boolean exit = false;
do {
System.out.println("##########################");
System.out.println("You want to interact with:");
System.out.println("\t 1->Characters");
System.out.println("\t 2->Spaceships");
System.out.println("\t 3->Player");
System.out.println("\t 4->Mission");
System.out.println("\t 5->Help");
System.out.println("\t 6->Exit");
do {
try {
index = getInt();
ifInt = true;
if (index < 1 || index > 6) {
System.out
.println("Please enter a number between 1 and 6.");
ifInt = false;
}
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ifInt = false;
System.out.println("You must enter a number! ");
input.next();
}
} while (ifInt == false);
System.out.println("##########################");
switch (index) {
case 1:// character
actionWithCharacter();
break;
case 2:// spacecraft
actionWithSpacecraft();
break;
case 3:// player
System.out.println("Player caracterictics: ");
System.out.println("-Name: " + this.player.toString());
System.out.println("-Race: " + this.player.getRace());
System.out.println("-Attack: " + this.player.getStrength());
System.out.println("-Health: " + this.player.printHealth());
System.out.println("-Credits: " + this.player.getCredit());
System.out.println("-Weapons: ");
this.player.showWeaponList();
System.out.println("");
break;
case 4:// mission
System.out.print("-Current mission: ");
try {
System.out.println(mission.getMissionTitle(missionId));
} catch (ErrNotFountOrMistake e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("-Description: ");
try {
System.out.println(mission.getMissionText(missionId));
} catch (ErrNotFountOrMistake e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("-Target: ");
System.out.println(target.toString());
System.out.println("");
break;
case 5:// help
System.out.println("Help: ");
System.out
.println("[n] symbolize your character or your own spaceship");
System.out.println("\t example: [0]-> ObiWan");
System.out.println("");
break;
case 6:// exit
exit = true;
System.out.println("GoodBye!");
break;
}
if (this.player.getHealth() == 0 || this.playerSpaceship.getArmor()==0) {
exit = true;
System.out.println(" /!\\Game Over/!\\");
}
} while (!exit);
}
/**
* Method to do action with others characters
*/
public void actionWithCharacter() {
int characterIndex = 0;
int index = 0;
boolean ifInt;
System.out.println("Other characters on the planet: ");
this.showCharactersOnArea();
if (this.playerArea.getCharacterNumber() > 1) {
System.out.println("Select the character with his number: ");
do {
try {
index = getInt();
ifInt = true;
System.out.print("Interaction with : ");
if (this.showCharacterName(index) == false) {
System.out.println("incorrect : ");
ifInt = false;
}
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ifInt = false;
System.out.println("You must enter a number! ");
input.next();
}
} while (ifInt == false);
characterIndex = index;
// Select the action to do with the character selected
System.out.println("What do you want to do? : ");
System.out.println("\t 1- Talk");
System.out.println("\t 2- Attack");
System.out.println("\t 3- Nothing");
do {
try {
index = getInt();
ifInt = true;
this.character = (Character) playerArea
.getCharacter(characterIndex);
switch (index) {
case 1:// talk to character
System.out.println("My name is " + character.toString()
+ " and I'm a " + character.getRace());
System.out.println(character.getText());
break;
case 2:// attack character
try {
this.playerAttack();
} catch (ErrNotFountOrMistake e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 3:
System.out.print("You do nothing");
break;
default:
System.out
.print("Please enter a number between 1 and 3.");
ifInt = false;
break;
}
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ifInt = false;
System.out.println("You must enter a number! ");
input.next();
}
} while (ifInt == false);
} else {
System.out.println("Sorry, you are alone... :-(");
}
}
/**
* Method to do action with spacecrafts
*/
public void actionWithSpacecraft() {
int spaceshipIndex = 0;
int index = 0;
int indexMax;
boolean ifInt;
System.out.println("Spacecrafts on the planet: ");
indexMax = this.showSpacecraftsOnArea();
System.out.println("Select the spacecrafts with his number: ");
// Select a character to interact with him
do {
try {
index = getInt();
ifInt = true;
if (index < 0 || index >= indexMax) {
System.out.println("Please enter a number between 0 and "
+ (indexMax - 1));
ifInt = false;
}
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ifInt = false;
System.out.println("You must enter a number! ");
input.next();
}
} while (ifInt == false);
spaceshipIndex = index;
// if armed, the spacecraft can attack (so it is a spaceship)
if (playerArea.getSpaceship(spaceshipIndex) instanceof javaEffect.spacecraft.Spaceship) {
this.spacecraft = null;
this.spaceship = (Spaceship) playerArea
.getSpaceship(spaceshipIndex);
} else {
this.spacecraft = (Spacecraft) playerArea
.getSpaceship(spaceshipIndex);
this.spaceship = null;
}
// Select the action to do with the spacecraft selected
System.out.println("What do you want to do? : ");
System.out.println("\t 1- Enter");
System.out.println("\t 2- Buy");
System.out.println("\t 3- Travel");
System.out.println("\t 4- Attack");
System.out.println("\t 5- Nothing");
//if it's our spaceship
if(this.spaceship != null && this.spaceship.equals(this.playerSpaceship) ) {
System.out.println("\t 6- Nurse");
}
do {
try {
index = getInt();
ifInt = true;
switch (index) {
case 1:// Enter in the spacecraft
if (this.spacecraft != null) {
indexMax = this.spacecraft.showWeaponList();
do {
try {
index = getInt();
ifInt = true;
if (index < 0 || index >= indexMax) {
System.out
.println("Please enter a number between 0 and "
+ (indexMax - 1));
ifInt = false;
}
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ifInt = false;
System.out.println("You must enter a number! ");
input.next();
}
} while (ifInt == false);
if (this.player.getCredit() >= this.spacecraft
.getWeapon(index).getCost()) {
this.player.addWeapon(this.spacecraft
.getWeapon(index));
this.player.setCredit(this.player.getCredit()
- this.spacecraft.getWeapon(index)
.getCost());
} else {
System.out.println("Too expensive for you!");
}
}
if (this.spaceship != null) {
System.out
.println("This spaceship contains no weapons.");
}
break;
case 2:// Buy the spacescraft
if (this.playerSpaceship == this.spaceship) {
System.out
.println("You can't buy this ship, it is already yours!");
break;
}
if (this.spacecraft != null) {
if (this.spacecraft.getCost() > this.player.getCredit()) {
System.out.println("Too expensive for you!");
break;
} else {
System.out
.println("Are you crazy? This spaceship has not weapon!!!");
}
}
if (this.spaceship != null) {
if (this.spaceship.getCost() > this.player.getCredit() + this.playerSpaceship
.getCost() ) {
System.out.println("Too expensive for you!");
break;
} else {
System.out.println("Good choice!");
this.player.setCredit(this.playerSpaceship
.getCost()
+ this.player.getCredit()
- this.spaceship.getCost());
this.playerSpaceship = this.spaceship;
// depreciation of the cost
try {
this.playerSpaceship
.setCost((int) (this.playerSpaceship
.getCost() * 0.8));
} catch (ErrNegativeNumber e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
break;
case 3:// travel
if (this.playerSpaceship != this.spaceship) {
System.out
.println("You can't travel with this ship, it is not yours.");
break;
}
System.out.print("Where do you want to go?");
indexMax = this.showPlanets();
do {
try {
index = getInt();
ifInt = true;
if (index < 0 || index >= indexMax) {
System.out
.println("Please enter a number between 0 and "
+ (indexMax - 1));
ifInt = false;
}
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ifInt = false;
System.out.println("You must enter a number! ");
input.next();
}
} while (ifInt == false);
this.goToPlanet(Planet.getPlanet(index));
System.out.println("Go to : " + this.playerArea.toString());
break;
case 4:// attack spacecraft
if (this.playerSpaceship == this.spaceship) {
System.out
.println("You can't attack your own spaceship!!!");
break;
}
this.playerSpaceshipAttack();
this.playerSpaceship.setShield(100);
if (this.spaceship != null) {
this.spaceship.setShield(100);
}
break;
case 5:// do nothing
break;
case 6://nurse
//if it's our spaceship
if(this.spaceship != null && this.spaceship.equals(this.playerSpaceship) ) {
this.player.setInitialHealth();
}
else {
System.out.print("Please enter a number between 1 and 5.");
}
break;
default:
System.out.print("Please enter a number between 1 and 6.");
ifInt = false;
break;
}
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ifInt = false;
System.out.println("You must enter a number! ");
input.next();
}
} while (ifInt == false);
}
/**
* Method to get integer with the scanner, and set exception if there is no
* integer
*
* @return integer
*/
private int getInt() throws DataFormatException {
if (input.hasNextInt()) {
return input.nextInt();
} else {
throw new DataFormatException("This is not a number");
}
}
/**
* Display all the character on the area.
*/
private int showCharactersOnArea() {
for (int i = 0; i < playerArea.getCharacterNumber(); i++) {
// if you want to interaction with you... impossible!!!
if (playerArea.getCharacter(i).toString()
.equals(this.player.toString()) == true) {
System.out.println("\t" + " [" + i + "]" + "-> "
+ playerArea.getCharacterName(i));
} else {
System.out.println("\t" + " " + i + " " + "-> "
+ playerArea.getCharacterName(i));
}
}
return playerArea.getCharacterNumber();
}
/**
*
* @return the list of characters on the player's planet.
*/
public ArrayList<Character> getCharactersOnArea() {
ArrayList<Character> list = new ArrayList<Character>();
for (int i = 0; i < playerArea.getCharacterNumber(); i++) {
// if you want to interaction with you... impossible!!!
if (!playerArea.getCharacter(i).toString()
.equals(this.player.toString())) {
list.add((Character) playerArea.getCharacter(i));
}
}
return list;
}
/**
* Display the name of the other characters on the player planet. And check if the selected character (with index)
* is you or not.
*
* @param index
* current index of the character on the planet
* @return False if the character is the player, else true.
*/
private boolean showCharacterName(int index) {
if (index >= 0 && index < playerArea.getCharacterNumber()) {
// if you want to interaction with you... impossible!!!
if (playerArea.getCharacter(index).toString()
.equals(this.player.toString()) == true) {
System.out.println("It is you!!!\n");
return false;
} else {
System.out.println(playerArea.getCharacter(index).toString());
return true;
}
}
System.out.println("This character doesn't exist!!!\n");
return false;
}
/**
* Method called when the player want to attack another character
* @throws ErrNotFountOrMistake
*/
private void playerAttack() throws ErrNotFountOrMistake {
int index = 0;
boolean playerAttack = true;
boolean ifInt;
boolean attack;
attack = true;
while (attack) {
System.out.println(player.toString() + "'s health: "
+ player.printHealth());
System.out.println(character.toString() + "'s health: "
+ character.printHealth());
System.out.println("----------------------------------");
if (playerAttack) {
System.out.println(player.toString() + " attacks "
+ character.toString());
player.attack(character);
System.out.println(character.toString()
+ "'s health remaining: " + character.printHealth());
} else { // when player passed his turn, then he can attack
playerAttack = true;
}
if (character.getHealth() > 0) {
System.out.println("");
System.out.println(character.toString() + " attacks "
+ player.toString());
character.attack(player);
}
System.out.println(player.toString() + "'s health remaining: "
+ player.printHealth());
if (player.getHealth() > 0 && character.getHealth() > 0) {
// Select the action to do with the character selected
System.out.println("What do you want to do? : ");
System.out.println("\t 1- Attack");
System.out.println("\t 2- Flee");
do {
try {
index = getInt();
ifInt = true;
switch (index) {
case 1:// continue to attack
attack = true;
break;
case 2:// flee
if (Math.random() <= 0.20)// 20%
{
attack = false;
playerAttack = true;
System.out.println("You succeed in fleeing");
} else {
System.out.println("You fail in fleeing");
System.out
.println("----------------------------------");
playerAttack = false;
}
break;
default:
System.out
.println("Enter a number between 1 and 2!");
break;
}
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ifInt = false;
System.out.println("You must enter a number! ");
input.next();
}
} while (ifInt == false);
}
// Someone is died
else {
if (player.getHealth() == 0) {
System.out.println(player.toString() + " is died.");
attack = false;
this.playerArea.removeCharacter(player);
} else {
this.character.setCredit(this.character.getCredit()+2000);
System.out.println(this.character.toString() + " is died.");
System.out.println("You stole "
+ this.character.getCredit() + " credit(s).");
this.player.stealCredit(this.character);
attack = false;
// if target of the mission is died
if (this.character == this.target) {
this.mission.endMission();
this.missionId++;
this.player.setCredit(this.player.getCredit());
System.out.println("Target eliminated!");
this.target = this.mission.startMission(this.missionId);
if(this.target == null){
System.out.println("It's the end of the game, you win !");
}
}
this.playerArea.removeCharacter(this.character);
}
}
}
}
/**
* Display all the spacecrafts on the area.
*/
private int showSpacecraftsOnArea() {
for (int i = 0; i < playerArea.getSpacecraftNumber(); i++) {
// if spaceship owe to player
if (this.playerSpaceship.toString().equals(
playerArea.getSpacecraftName(i))) {
System.out.println("\t" + " [" + i + "]" + "-> "
+ playerArea.getSpacecraftName(i));
} else {
System.out.println("\t" + " " + i + " " + "-> "
+ playerArea.getSpacecraftName(i));
}
}
return playerArea.getSpacecraftNumber();
}
/**
* Method called when player want to attack another spaceship
*/
private void playerSpaceshipAttack() {
int index = 0;
boolean playerAttack = true;
boolean ifInt;
boolean attack;
boolean combat;
attack = true;
combat = true;
while (attack) {
System.out.println("Your spaceship :"
+ this.playerSpaceship.toString());
if (this.spaceship != null) {
System.out.println("Ennemy :" + this.spaceship.toString());
} else {
System.out.println("Ennemy :" + this.spacecraft.toString());
}
System.out.println("----------------------------------");
// if the player attack
if (playerAttack) {
System.out.println(this.playerSpaceship.toString()
+ " attacks ");
if (this.spaceship != null) {
System.out.println(this.spaceship.toString());
}else {
System.out.println(this.spacecraft.toString());
}
try {
if (this.spaceship != null) {
this.playerSpaceship.attack(this.spaceship);
} else {
this.playerSpaceship.attack(this.spacecraft);
}
} catch (ErrAttack e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (this.spaceship != null) {
try {
System.out.println(this.spaceship.toString() + " attacks "
+ this.playerSpaceship.toString());
this.spaceship.attack(playerSpaceship);
} catch (ErrAttack e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Your spaceship :"
+ this.playerSpaceship.toString() + "'s armor remaining :"
+ this.playerSpaceship.getArmor());
//test if someone is destroyed
if (this.playerSpaceship.getArmor() == 0) {
combat = false;
}
if (this.spaceship != null) {
if (this.spaceship.getArmor() == 0) {
combat = false;
}
} else {
if (this.spacecraft.getArmor() == 0) {
combat = false;
}
}
if (combat) {
// Select the action to do with the spaceship selected
System.out.println("\nWhat do you want to do? : ");
System.out.println("\t 1- Attack");
System.out.println("\t 2- Flee");
do {
try {
index = getInt();
ifInt = true;
switch (index) {
case 1:// continue to attack
attack = true;
playerAttack = true;
break;
case 2:// flee
if (Math.random() <= 0.20)// 20%
{
attack = false;
System.out.println("You succeed in fleeing");
} else {
System.out.println("You fail in fleeing");
System.out
.println("----------------------------------");
playerAttack = false;
}
break;
default:
System.out
.println("Enter a number between 1 and 2!");
break;
}
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ifInt = false;
System.out.println("You must enter a number! ");
input.next();
}
} while (ifInt == false);
}
// Someone is died
if (combat == false) {
if (this.playerSpaceship.getArmor() == 0) {
System.out.println("\n" + this.playerSpaceship.toString()
+ " is destroyed.");
this.playerArea.removeSpacecraft(this.playerSpaceship);
attack = false;
} else {
if (this.spaceship != null) {
System.out.println("\n" + this.spaceship.toString()
+ " is destroyed.");
this.playerArea.removeSpacecraft(spaceship);
} else {
System.out.println("\n" + this.spacecraft.toString()
+ " is destroyed.");
this.playerArea.removeSpacecraft(spacecraft);
}
attack = false;
}
}
}
}
/**
*
* @return the number of planets
*/
private int showPlanets() {
int number;
System.out.println("\n");
number = Planet.printAllPlanetsAndNumbers();
return number;
}
/**
* Method to change the player's planet
*
* @param planet
* the planet where you want to go
*/
private void goToPlanet(Planet planet) {
try {
planet.addSpacecraft(this.playerSpaceship);
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
planet.addCharacter(this.player);
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.playerArea.removeSpacecraft(this.playerSpaceship);
this.playerArea.removeCharacter(this.player);
this.playerArea = planet;
this.playerSpaceship.setCurrentPlanet(planet);
}
/**
*
* @return the player
*/
public Character getPlayer() {
// TODO Auto-generated method stub
return this.player;
}
}