Skip to content

Commit de057bf

Browse files
Merge remote-tracking branch 'origin/master'
2 parents fde13a3 + 5446590 commit de057bf

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

src/model/New_Plants/Plant.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package model.New_Plants;
22

33
import java.util.ArrayList;
4+
45
import model.New_Plants.Plant;
56

67
abstract public class Plant {
@@ -25,6 +26,14 @@ public Plant(String plantName, int healthPoint, int attackPower, int coolDown, i
2526
plants.add(this);
2627
}
2728

29+
public static Plant getPlant(String plantName) {
30+
for (int i = 0; i < Plant.getPlants().size(); i++) {
31+
if (Plant.getPlants().get(i).getPlantName().equals(plantName))
32+
return Plant.getPlants().get(i);
33+
}
34+
return null;
35+
}
36+
2837
public boolean isLilyPad() {
2938
return isLilyPad;
3039
}

src/model/battle/managers/DayBattleManager.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class DayBattleManager extends BattleManager {
1616
private SunGenerator sunGenerator = new SunGenerator();
1717

1818

19-
2019
@Override
2120
public void manage() {
2221
this.sunGenerator.generateSun();
@@ -66,8 +65,19 @@ private void generateRandomZombie() {
6665

6766
private boolean insertCardValidation() {
6867
if (coolDownCheck(((CardOfPlant) Battle.getRunningBattle().getSelectedCard()).getPlant())) {
69-
return Battle.getRunningBattle().getPlants().getSun() >= ((CardOfPlant) Battle.getRunningBattle().getSelectedCard()).getPlant().getSunUsage();
68+
if (Battle.getRunningBattle().getSelectedCell().getColumn() % 2 == 1) {
69+
if (Battle.getRunningBattle().getPlants().getSun() >= ((CardOfPlant) Battle.getRunningBattle().getSelectedCard()).getPlant().getSunUsage())
70+
return true;
71+
else {
72+
System.out.println("Not Enough Sun!");
73+
return false;
74+
}
75+
} else {
76+
System.out.println("Plants Can be planted on odd columns only!");
77+
return false;
78+
}
7079
}
80+
System.out.println("CoolDown time!");
7181
return false;
7282
}
7383

src/model/battle/managers/ZombieBattleManager.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,101 @@
11
package model.battle.managers;
22

3+
import model.Cell;
4+
import model.New_Plants.Plant;
5+
import model.New_Zombies.Zombie;
6+
import model.battle.Battle;
7+
import model.battle.PlantInGame;
8+
import model.battle.ZombieInGame;
9+
import model.card.CardOfZombie;
10+
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.Random;
15+
316
public class ZombieBattleManager extends BattleManager {
417

18+
private final int plantsCount = 18;
19+
private final HashMap<String, Integer> plantsAndTheirCounts = new HashMap<>();
20+
21+
public ZombieBattleManager() {
22+
this.initializePlantsAndTheirCountNeeded();
23+
this.setPlantsInMap();
24+
}
25+
526

627
@Override
728
public void manage() {
29+
Battle.getRunningBattle().getBattleComponents().manage();
30+
this.checkForPlantsWin();
31+
this.checkForZombiesWin();
32+
Battle.getRunningBattle().incrementTurn();
33+
}
34+
35+
private void initializePlantsAndTheirCountNeeded() {
36+
this.plantsAndTheirCounts.put("Explode-o-nut", 3);
37+
this.plantsAndTheirCounts.put("Scaredy-shroom", 6);
38+
this.plantsAndTheirCounts.put("SnowPea", 2);
39+
this.plantsAndTheirCounts.put("Cabbage-pult", 2);
40+
this.plantsAndTheirCounts.put("Threepeater", 1);
41+
this.plantsAndTheirCounts.put("GatlingPea", 1);
42+
this.plantsAndTheirCounts.put("PotatoMine", 3);
43+
}
44+
45+
private void setPlantsInMap() {
46+
ArrayList<Cell> randomCells = this.getRandomCells(this.plantsCount);
47+
for (Map.Entry element : this.plantsAndTheirCounts.entrySet()) {
48+
for (int i = 0; i < (int) element.getValue(); i++) {
49+
Plant plant = Plant.getPlant((String) element.getKey());
50+
new PlantInGame(plant, randomCells.get(0));
51+
randomCells.remove(0);
52+
}
53+
}
54+
}
55+
56+
private ArrayList<Cell> getRandomCells(int randomCellsCount) {
57+
ArrayList<Cell> randomCells = new ArrayList<>();
58+
Random random = new Random();
59+
60+
for (int i = 0; i < randomCellsCount; i++) {
61+
int randomRow = random.nextInt(6);
62+
int randomColumn = 2 * random.nextInt(9) + 1;
63+
Cell cell = Battle.getRunningBattle().getMap().getCell(randomRow, randomColumn);
64+
while (randomCells.contains(cell)) {
65+
randomRow = random.nextInt(6);
66+
randomColumn = 2 * random.nextInt(9) + 1;
67+
cell = Battle.getRunningBattle().getMap().getCell(randomRow, randomColumn);
68+
}
69+
randomCells.add(cell);
70+
}
71+
return randomCells;
72+
}
73+
74+
private void insertZombie(int count) {
75+
int coinsNeeded = count * 10 * (((CardOfZombie) Battle.getRunningBattle().getSelectedCard()).getZombie().getHealthPoint());
76+
if (Battle.getRunningBattle().getZombies().getSun() >= coinsNeeded) {
77+
if (this.getNumberOfZombiesInACellRow(Battle.getRunningBattle().getSelectedCell()) + count <= 2) {
78+
for (int i = 0; i < count; i++) {
79+
Zombie zombie = ((CardOfZombie) Battle.getRunningBattle().getSelectedCard()).getZombie();
80+
new ZombieInGame(zombie, Battle.getRunningBattle().getSelectedCell());
81+
}
82+
} else {
83+
// TODO should print a error that this row can maximum have 2 zombies
84+
System.out.println("A Row can have maximum of 2 zombies in a wave");
85+
}
86+
} else {
87+
// TODO should print a error that player doesn't have enough coins
88+
System.out.println("You Don't have enough Coins");
89+
}
90+
91+
}
892

93+
private int getNumberOfZombiesInACellRow(Cell cell) {
94+
int counter = 0;
95+
for (int i = 0; i < Battle.getRunningBattle().getMap().getCells()[cell.getRow()].length; i++) {
96+
counter += Battle.getRunningBattle().getMap().getCells()[cell.getRow()][i].getZombies().size();
97+
}
98+
return counter;
999
}
10100

11101

0 commit comments

Comments
 (0)