WIP CaptureTheFlag

This commit is contained in:
joachimschmidt557 2019-03-22 15:11:56 +01:00
parent ba24a0e430
commit 07e292a5e1

View file

@ -1,6 +1,9 @@
package game.goals; package game.goals;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import java.util.Random;
import game.Game; import game.Game;
import game.Goal; import game.Goal;
@ -9,13 +12,20 @@ import game.map.Castle;
public class CaptureTheFlagGoal extends Goal { public class CaptureTheFlagGoal extends Goal {
final int ROUND_FOR_DISTRIBUTION = 2; final static int ROUND_FOR_DISTRIBUTION = 2;
final int MIN_ROUND_FOR_WIN = 2; final static int MIN_ROUND_FOR_WIN = 2;
final static int NUM_FLAG_CASTLES = 4;
List<Castle> flagCastles; List<Castle> flagCastles;
private static Random random = new Random();
private static int newRandomInt(int min, int max) {
return min + random.nextInt(max - min + 1);
}
public CaptureTheFlagGoal() { public CaptureTheFlagGoal() {
super("Capture the flag", ""); super("Capture the flag", "Derjenige Spieler gewinnt, der zuerst"
+ " alle " + NUM_FLAG_CASTLES + " Flaggen-Schlösser erobert.");
} }
@Override @Override
@ -40,11 +50,22 @@ public class CaptureTheFlagGoal extends Goal {
if (game.getRound() < MIN_ROUND_FOR_WIN) if (game.getRound() < MIN_ROUND_FOR_WIN)
return null; return null;
if (flagCastles.size() < 1)
return null;
return
// Check if all flag castles belong // Check if all flag castles belong
// to exactly one player // to exactly one player
flagCastles.stream()
.map(x -> x.getOwner())
.reduce(flagCastles.get(0).getOwner(), (x, y) -> {
if (x == null || y == null)
return null; return null;
else if (!x.equals(y))
return null;
else
return x;
});
} }
@ -59,12 +80,67 @@ public class CaptureTheFlagGoal extends Goal {
} }
/** /**
* Distribute the flag castles * Distribute the flag castles evenly to
* all players
*/ */
private void distributeFlagCastles() { private void distributeFlagCastles() {
Game game = this.getGame();
List<Player> players = game.getPlayers();
int numCastlesToDistribute =
/*if*/ NUM_FLAG_CASTLES < players.size() ?
/*then*/ players.size() :
/*else*/ NUM_FLAG_CASTLES;
for (int i = 0; i < players.size(); i++) {
int playersLeft = players.size() - i;
int numCastlesForPlayer = numCastlesToDistribute / playersLeft;
if (numCastlesForPlayer > players.get(i).getCastles(game).size())
numCastlesForPlayer = players.get(i).getCastles(game).size();
pickRandomCastles(players.get(i).getCastles(game), numCastlesForPlayer)
.stream()
.forEach(x -> flagCastles.add(x));
numCastlesToDistribute -= numCastlesForPlayer;
}
}
/**
* Pick a number of random castles
* @param castles All castles
* @param num The number of castles to choose
* @return The picked castles
*/
private List<Castle> pickRandomCastles(List<Castle> castles, int num) {
int min = 0;
int max = castles.size();
List<Integer> nums = new ArrayList<Integer>();
if (num >= max)
return new ArrayList<Castle>(castles);
for (int i = 0; i < num; i++) {
int newNum = newRandomInt(min, max);
while (nums.contains(newNum))
newNum = newRandomInt(min, max);
nums.add(newNum);
}
return nums.stream().map(x -> castles.get(x)).collect(Collectors.toList());
} }
} }