Game loop and fight
This commit is contained in:
parent
228c410a1a
commit
a982311f46
3 changed files with 144 additions and 1 deletions
115
src/Game.java
115
src/Game.java
|
@ -2,6 +2,7 @@ package it.arnaldo.unibs.tamagolem;
|
|||
|
||||
import it.kibo.fp.lib.Menu;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class Game {
|
||||
|
@ -9,6 +10,15 @@ public class Game {
|
|||
private final Player player2;
|
||||
private final ElementGraph worldBalance;
|
||||
private boolean restart = false;
|
||||
private Modes mode;
|
||||
private int numberOfElements;
|
||||
private int numberOfStones;
|
||||
private int numberOfTamaGolems;
|
||||
private int totalNumberOfStones;
|
||||
private int numberOfStonesPerElement;
|
||||
private int totalPower;
|
||||
private int lifePoints;
|
||||
private HashMap<Element, Integer> stonesPerElement;
|
||||
|
||||
public Game(Player player1, Player player2, ElementGraph worldBalance) {
|
||||
this.player1 = player1;
|
||||
|
@ -37,7 +47,69 @@ public class Game {
|
|||
this.restart = restart;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
private void loop(){
|
||||
List<Element> elementsNames = worldBalance.getElementsNames();
|
||||
|
||||
// setto la mappa con le pietre ancora disponibili
|
||||
for (Element element : elementsNames) {
|
||||
stonesPerElement.put(element, numberOfStonesPerElement);
|
||||
}
|
||||
|
||||
// aggiungo i tamagolem
|
||||
for (int i = 0; i < numberOfTamaGolems; i++) {
|
||||
player1.addTamaGolem(new TamaGolem(lifePoints));
|
||||
player2.addTamaGolem(new TamaGolem(lifePoints));
|
||||
}
|
||||
|
||||
|
||||
while(!player1.getTamaGolems().isEmpty() && !player2.getTamaGolems().isEmpty()) {
|
||||
// setup del primo tamagolem
|
||||
setupGolem(player1.getNextTamaGolem());
|
||||
setupGolem(player2.getNextTamaGolem());
|
||||
|
||||
FightEsit esito = fight();
|
||||
|
||||
switch (esito) {
|
||||
case GOLEM1 -> System.out.println(player1.getName() + " won the first fight");
|
||||
case GOLEM2 -> System.out.println(player2.getName() + " won the first fight");
|
||||
}
|
||||
|
||||
// dopo il fight i tamagolem morti vengono rimossi
|
||||
checkDeathsTamagolems(player1, player2);
|
||||
}
|
||||
|
||||
if (player1.getTamaGolems().isEmpty()) {
|
||||
System.out.println(player2.getName() + " won the game");
|
||||
} else {
|
||||
System.out.println(player1.getName() + " won the first game");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
System.out.println("GameLoop");
|
||||
}
|
||||
|
||||
private void setupGolem(TamaGolem golem) {
|
||||
List<Element> elementsNames = worldBalance.getElementsNames();
|
||||
for (int i = 0; i < numberOfStones; i++) {
|
||||
Menu stonesMenu = new Menu("TamaGolem", getMenuOptions(), true, true, false);
|
||||
int firstmenuChoise = stonesMenu.choose();
|
||||
golem.addElementalStone(elementsNames.get(firstmenuChoise - 1));
|
||||
stonesPerElement.put(elementsNames.get(firstmenuChoise - 1), stonesPerElement.get(elementsNames.get(firstmenuChoise - 1)) - 1);
|
||||
}
|
||||
|
||||
golem.print();
|
||||
}
|
||||
|
||||
|
||||
private void checkDeathsTamagolems(Player p1, Player p2) {
|
||||
p1.getTamaGolems().removeIf(tg -> !tg.isAlive());
|
||||
p2.getTamaGolems().removeIf(tg -> !tg.isAlive());
|
||||
}
|
||||
|
||||
|
||||
private void setup() {
|
||||
// N
|
||||
numberOfElements = worldBalance.getElements().size();
|
||||
|
||||
|
@ -67,4 +139,45 @@ public class Game {
|
|||
worldBalance.printGraph();
|
||||
}
|
||||
|
||||
private boolean checkVictory(){
|
||||
return !player1.isAlive() || !player2.isAlive();
|
||||
}
|
||||
|
||||
private String[] getMenuOptions() {
|
||||
List<Element> elementsNames = worldBalance.getElementsNames();
|
||||
String[] menuOptions = new String[numberOfElements];
|
||||
|
||||
for (int i = 0; i < numberOfElements; i++) {
|
||||
menuOptions[i] = elementsNames.get(i) + " -> (" + stonesPerElement.get(elementsNames.get(i)) + ")";
|
||||
}
|
||||
|
||||
return menuOptions;
|
||||
}
|
||||
|
||||
public FightEsit fight() {
|
||||
TamaGolem golem1 = player1.getNextTamaGolem();
|
||||
TamaGolem golem2 = player2.getNextTamaGolem();
|
||||
|
||||
while (golem1.isAlive() && golem2.isAlive()) {
|
||||
Element firstStone = golem1.useElementalStone();
|
||||
Element secondStone = golem2.useElementalStone();
|
||||
int interaction = worldBalance.getInteractionBetween(firstStone, secondStone);
|
||||
|
||||
System.out.println(player1.getName() + "'s tamagolem throws " + firstStone.name() + " and " + player2.getName() + "'s tamagolem throws " + secondStone.name());
|
||||
|
||||
if (interaction > 0){
|
||||
golem2.getDamage(interaction);
|
||||
System.out.println(player2.getName() + "'s tamagolem gets " + interaction + " damage");
|
||||
} else {
|
||||
golem1.getDamage(-1 * interaction);
|
||||
System.out.println(player1.getName() + "'s tamagolem gets " + -1 * interaction + " damage");
|
||||
}
|
||||
}
|
||||
|
||||
if (golem1.isAlive()){
|
||||
return FightEsit.GOLEM1;
|
||||
}
|
||||
return FightEsit.GOLEM2;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,4 +18,32 @@ public class Player {
|
|||
public ArrayList<TamaGolem> getTamaGolems() {
|
||||
return tamaGolems;
|
||||
}
|
||||
|
||||
public TamaGolem getNextTamaGolem() {
|
||||
if (tamaGolems.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
return tamaGolems.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
public void addTamaGolem(TamaGolem t) {
|
||||
tamaGolems.add(t);
|
||||
}
|
||||
|
||||
public void removeTamaGolem(TamaGolem t) {
|
||||
tamaGolems.remove(t);
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
for (TamaGolem golem : tamaGolems) {
|
||||
if (golem.getLifePoints() > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -44,12 +44,14 @@ public class WorldBuilder {
|
|||
for (int i = 0; i < numElements; i++) {
|
||||
Element e1 = selectedElements.get(i);
|
||||
int[] row = new int[pairIndex.size()];
|
||||
|
||||
for (Element e2 : selectedElements) {
|
||||
if (e1 == e2) continue;
|
||||
int dir = linkDirections.get(e1).get(e2);
|
||||
int pos = pairIndex.get(Set.of(e1, e2));
|
||||
row[pos] = dir;
|
||||
}
|
||||
|
||||
A[i] = row;
|
||||
b[i] = 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue