The BackPacker
Determinates a player that has won the most, yet and chooses his last vote.
public class BackPacker extends Human {
public String takeSides(String history) {
if (history == null || history.equals(""))
return "evil";
String[] roundVotes = history.split(",");
int players = roundVotes[0].length();
int[] winningPlayers = new int[players];
for (int round = 0; round < roundVotes.length; round++) {
String nextRound = roundVotes[round];
boolean didGoodWin = didGoodWin(nextRound, players);
for (int player = 0; player < nextRound.length(); player++) {
boolean good = nextRound.charAt(player) == '1';
winningPlayers[player] += ((!didGoodWin && good) || (didGoodWin && !good)) ? 1 : 0;
}
}
int bestScore = -1;
for (int nextPlayer : winningPlayers)
if (bestScore < nextPlayer)
bestScore = nextPlayer;
int bestPlayer = 0;
for (int ii = 0; ii < players; ii++) {
if (winningPlayers[ii] == bestScore) {
bestPlayer = ii;
break;
}
}
if (roundVotes[roundVotes.length - 1].charAt(bestPlayer) == '1')
return "good";
return "evil";
}
private boolean didGoodWin(String round, int players) {
int good = 0;
for (char next : round.toCharArray())
good += next == '1' ? 1 : 0;
return (good * 2) > players;
}
}