Skip to content

Instantly share code, notes, and snippets.

@QuantumFractal
Created May 5, 2014 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuantumFractal/3c1a9029767372fcb5a2 to your computer and use it in GitHub Desktop.
Save QuantumFractal/3c1a9029767372fcb5a2 to your computer and use it in GitHub Desktop.
Spaceship simulator
package galaxy;
import java.util.ArrayList;
public class Battle {
public static void main(String[] args)
{
//Make an arraylist of ships!
ArrayList<Spaceship> ships = new ArrayList<Spaceship>();
ships.add(new Kestrel());
ships.add(new Daliong());
ships.add(new Kestrel());
ships.add(new IMC());
printHealth(ships);
System.out.println("The Kestrel attacked the Daliong!");
ships.get(0).attack(ships.get(1));
printHealth(ships);
}
public static void printHealth(ArrayList<Spaceship> ships){
System.out.println("\n<<<<<Health Report>>>>>>");
for(Spaceship s: ships){
System.out.println("Health of "+s.getName()+" is "+s.getHealth());
}
System.out.println("<<<<<END Health Report>>>>>>\n");
}
}
package galaxy;
import java.awt.Point;
public abstract class Cruiser implements Spaceship {
private int attack;
private int HP;
private int speed;
private Point position = new Point();
public Cruiser (int givenAttack, int givenHP){
attack = givenAttack;
HP = givenHP;
speed = 20;
position.setLocation(0, 0);
}
public Cruiser (int givenAttack, int givenHP, int xPos, int yPos){
attack = givenAttack;
HP = givenHP;
speed = 20;
position.setLocation(xPos, yPos);
}
@Override
public void fly(int x, int y) {
position.setLocation(x,y);
}
public int getHealth(){
return HP;
}
public void removeHealth(int attack){
HP -= attack;
}
@Override
public void attack(Spaceship target) {
if(target.isDestroyed())
System.out.println("That Ship is destroyed sir!!");
else
target.removeHealth(attack);
}
@Override
public boolean isDestroyed(){
if(HP < 0)
return true;
else
return false;
}
@Override
public abstract String getName();
}
package galaxy;
public class Daliong extends Frigate {
public Daliong() {
super(5, 9);
}
public Daliong(int x, int y) {
super(5,9,x,y);
}
@Override
public String getName() {
return "Daliong";
}
}
package galaxy;
import java.awt.Point;
public abstract class Frigate implements Spaceship {
private int attack;
private int HP;
private int speed;
private Point position = new Point();
public Frigate (int givenAttack, int givenHP){
attack = givenAttack;
HP = givenHP;
speed = 15;
position.setLocation(0, 0);
}
public Frigate (int givenAttack, int givenHP, int xPos, int yPos){
attack = givenAttack;
HP = givenHP;
speed = 15;
position.setLocation(xPos, yPos);
}
@Override
public void fly(int x, int y) {
position.setLocation(x,y);
}
public int getHealth(){
return HP;
}
public void removeHealth(int attack){
HP -= attack;
}
@Override
public void attack(Spaceship target) {
if(target.isDestroyed())
System.out.println("That Ship is destroyed sir!!");
else
target.removeHealth(attack);
}
@Override
public boolean isDestroyed(){
if(HP < 0)
return true;
else
return false;
}
@Override
public abstract String getName();
}
package galaxy;
public class IMC extends Cruiser {
public IMC() {
super(2, 13);
}
public IMC(int x, int y) {
super(2,13,x,y);
}
@Override
public String getName() {
return "IMC";
}
}
package galaxy;
public class Kestrel extends Frigate {
public Kestrel() {
super(2, 13);
}
public Kestrel(int x, int y) {
super(2,13,x,y);
}
@Override
public String getName() {
return "Kestrel";
}
}
package galaxy;
public interface Spaceship {
public void fly(int x, int y);
public void attack(Spaceship target);
public int getHealth();
public void removeHealth(int amt);
public boolean isDestroyed();
public String getName();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment