Skip to content
Snippets Groups Projects
Commit d3504822 authored by Nils Breuer's avatar Nils Breuer
Browse files

Add Legendäre_Vögel source-code

parent 8c308014
Branches
No related merge requests found
Showing
with 405 additions and 0 deletions
import ninja.Ninja;
import pokemon.*;
import teams.Team;
public abstract class Main {
public static void main(String[] args) {
typedPokemonFight();
teamFightPokemon();
teamFightNinja();
}
static void typedPokemonFight() {
var g = new Glurak(123, 5);
var s = new Schiggy(50, 3);
if(g.isStrongAgainst(s))
System.out.println("Glurak is strong against Schiggy");
if(s.isStrongAgainst(g))
System.out.println("Schiggy is strong against Glurak");
g.fight(s);
}
static void teamFightPokemon() {
var ash = new Team<Pokemon>();
ash.addMember(new Schiggy(30, 8));
ash.addMember(new Taubsi(25, 6));
ash.addMember(new Glurak(80, 10));
var gary = new Team<Pokemon>();
gary.addMember(new Bisasam(40, 7));
gary.addMember(new Habitak(50, 10));
gary.addMember(new Karpador(10, 1));
System.out.println(ash);
System.out.println(gary);
ash.fight(gary);
System.out.println(ash);
System.out.println(gary);
}
static void teamFightNinja() {
var n1 = new Team<Ninja>();
n1.addMember(new Ninja(100, 10));
var n2 = new Team<Ninja>();
n2.addMember(new Ninja(20, 30));
n2.addMember(new Ninja(20, 30));
n2.addMember(new Ninja(20, 30));
n1.fight(n2);
System.out.println(n1);
System.out.println(n2);
// Note: It would not possible to execute ash.fight(n1), because ash.fight() requires a parameter of
// type Team<Pokemon> and n1 would be of type Team<Ninja>.
// Compiler error:
// java: incompatible types: Team<Pokemon> cannot be converted to Team<Ninja>
}
}
package ninja;
import teams.Fightable;
public class Ninja implements Fightable<Ninja> {
private int hp, attack;
public Ninja(int hp, int attack) {
this.hp = hp;
this.attack = attack;
}
@Override
public boolean fight(Ninja other) {
while(isAlive() && other.isAlive()) {
other.hp = Math.max(0, other.hp - attack);
if(other.isAlive())
hp = Math.max(0, hp - other.attack);
}
return isAlive();
}
@Override
public boolean isAlive() {
return hp > 0;
}
@Override
public String toString() {
return String.format("A ninja (%d HP)", hp);
}
}
package pokemon;
public class Arktos extends Pokemon implements FlyingType, IceType {
public Arktos(int hp, int attack) {
super("Arktos", hp, attack);
}
@Override
public boolean isWeakAgainst(Pokemon other) {
return FlyingType.super.isWeakAgainst(other) ||
other instanceof FireType || other instanceof WaterType;
}
@Override
public boolean isStrongAgainst(Pokemon other) {
return FlyingType.super.isStrongAgainst(other) || other instanceof FlyingType;
}
}
package pokemon;
public class Bisasam extends Pokemon implements GrassType {
public Bisasam(int hp, int attack) {
super("Bisasam", hp, attack);
}
}
package pokemon;
public class Glaziola extends Pokemon implements IceType {
public Glaziola(int hp, int attack) {
super("Glaziola", hp, attack);
}
}
package pokemon;
/**
* This is a Pokemon that implements two interfaces FireType and FlyingType.
* Both of these interfaces have the same methods with default implementation.
* Hence, this class would not compile without overriding the two methods.
*/
public class Glurak extends Pokemon implements FireType, FlyingType {
public Glurak(int hp, int attack) {
super("Glurak", hp, attack);
}
@Override
public boolean isWeakAgainst(Pokemon other) {
// Only weakness of FireType, because the FlyingType weakness (grass) is cancelled out by Fire-Type.
return FireType.super.isWeakAgainst(other);
// Or:
// return other instanceof WaterType;
}
@Override
public boolean isStrongAgainst(Pokemon other) {
return FireType.super.isStrongAgainst(other) || FlyingType.super.isStrongAgainst(other);
}
}
package pokemon;
public class Habitak extends Pokemon implements FlyingType {
public Habitak(int hp, int attack) {
super("Habitak", hp, attack);
}
}
package pokemon;
public class Karpador extends Pokemon implements WaterType {
public Karpador(int hp, int attack) {
super("Karpador", hp, attack);
}
}
package pokemon;
public class Lavados extends Pokemon implements FlyingType, FireType {
public Lavados(int hp, int attack) {
super("Lavados", hp, attack);
}
@Override
public boolean isWeakAgainst(Pokemon other) {
return FlyingType.super.isWeakAgainst(other) || other instanceof WaterType;
}
@Override
public boolean isStrongAgainst(Pokemon other) {
return FlyingType.super.isStrongAgainst(other) || other instanceof IceType;
}
}
\ No newline at end of file
package pokemon;
public class Pikachu extends Pokemon implements ElectricType {
public Pikachu(int hp, int attack) {
super("Pikachu", hp, attack);
}
}
package pokemon;
import teams.Fightable;
public abstract class Pokemon implements PokemonType, Fightable<Pokemon> {
private String name;
private int hp;
private int attack;
public Pokemon(String name, int hp, int attack) {
this.name = name;
this.hp = hp;
this.attack = attack;
}
private void attack(Pokemon other) {
int attackPoints = attack;
if(isStrongAgainst(other))
attackPoints *= 2;
if(isWeakAgainst(other))
attackPoints /= 2;
other.hp = Math.max(0, other.hp - attackPoints);
System.out.println(String.format("%s attacked %s: -%d (remaining HP: %d)",
name, other.name, attackPoints, other.hp));
}
public boolean fight(Pokemon other) {
attack(other);
if (other.hp > 0) {
return !other.fight(this);
} else {
System.out.println(other.name + " fainted!");
System.out.println();
return true;
}
}
public boolean isAlive() {
return hp > 0;
}
@Override
public String toString() {
return String.format("%s has %d HP", name, hp);
}
}
package pokemon;
public class Schiggy extends Pokemon implements WaterType {
public Schiggy(int hp, int attack) {
super("Schiggy", hp, attack);
}
}
package pokemon;
public class Taubsi extends Pokemon implements FlyingType {
public Taubsi(int hp, int attack) {
super("Taubsi", hp, attack);
}
}
package pokemon;
/*
Customization of the types orientated from the table of the PokeWiki
https://www.pokewiki.de/Typen
*/
interface PokemonType {
boolean isWeakAgainst(Pokemon other);
boolean isStrongAgainst(Pokemon other);
}
interface FlyingType extends PokemonType {
default boolean isWeakAgainst(Pokemon other) {
return other instanceof ElectricType;
}
default boolean isStrongAgainst(Pokemon other) {
return other instanceof GrassType;
}
}
interface FireType extends PokemonType {
default boolean isWeakAgainst(Pokemon other) {
return other instanceof FireType || other instanceof WaterType;
}
default boolean isStrongAgainst(Pokemon other) {
return other instanceof GrassType || other instanceof IceType;
}
}
interface WaterType extends PokemonType {
default boolean isWeakAgainst(Pokemon other) {
return other instanceof WaterType || other instanceof GrassType;
}
default boolean isStrongAgainst(Pokemon other) {
return other instanceof FireType;
}
}
interface GrassType extends PokemonType {
default boolean isWeakAgainst(Pokemon other) {
return other instanceof FlyingType || other instanceof FireType || other instanceof GrassType;
}
default boolean isStrongAgainst(Pokemon other) {
return other instanceof WaterType;
}
}
/* Implementation of ElectricType and IceType */
interface ElectricType extends PokemonType {
default boolean isWeakAgainst(Pokemon other) { return other instanceof GrassType || other instanceof ElectricType; }
default boolean isStrongAgainst(Pokemon other) { return other instanceof FlyingType || other instanceof WaterType; }
}
interface IceType extends PokemonType {
default boolean isWeakAgainst(Pokemon other) { return other instanceof FireType || other instanceof WaterType || other instanceof IceType; }
default boolean isStrongAgainst(Pokemon other) { return other instanceof FlyingType || other instanceof GrassType; }
}
package pokemon;
public class Zapdos extends Pokemon implements FlyingType, ElectricType {
public Zapdos(int hp, int attack) {
super("Zapdos", hp, attack);
}
@Override
public boolean isWeakAgainst(Pokemon other) {
return false;
}
@Override
public boolean isStrongAgainst(Pokemon other) {
return other instanceof FlyingType;
}
}
\ No newline at end of file
package teams;
public interface Fightable<T> {
boolean fight(T other);
boolean isAlive();
}
\ No newline at end of file
package teams;
import java.util.ArrayList;
/*
* A team of fighters. Two teams of the same type (T) can fight against each other.
*/
public class Team<T extends Fightable<T>> {
private ArrayList<T> members;
public Team() {
members = new ArrayList<>();
}
public void addMember(T member) {
members.add(member);
}
public T getMember(int index) {
assert(index >= 0);
assert(index < members.size());
return members.get(index);
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("TEAM MEMBERS:");
result.append(System.lineSeparator());
for (var p : members) {
result.append("- ");
result.append(p.toString());
result.append(System.lineSeparator());
}
return result.toString();
}
public T nextFighter() {
for(int i=0; i<members.size(); i++) {
var member = members.get(i);
if(member.isAlive())
return member;
}
return null;
}
enum FightResult { WIN, LOOSE }
public FightResult fight(Team<T> other) {
T fighter = nextFighter();
while(fighter != null) {
T enemy = other.nextFighter();
if(enemy == null) {
return FightResult.WIN;
}
fighter.fight(enemy);
fighter = nextFighter();
}
return FightResult.LOOSE;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment