package Humans;
public class BackPacker extends Human {
// toggles weather the BackPacker thinks majority is better vs. minority is better
private static final boolean goWithMajority = false;
@Override
public final 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 (String nextRound : roundVotes) {
boolean didGoodWin = didGoodWin(nextRound, players);
for (int player = 0; player < nextRound.length(); player++) {
boolean playerVotedGood = nextRound.charAt(player) == '1';
winningPlayers[player] += didPlayerWin(didGoodWin, playerVotedGood);
}
}
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 int didPlayerWin(boolean didGoodWin, boolean playerVotedGood) {
if(goWithMajority) {
return ((didGoodWin && goodplayerVotedGood) || (!didGoodWin && !goodplayerVotedGood)) ? 1 : 0;
} else {
return ((!didGoodWin && goodplayerVotedGood) || (didGoodWin && !goodplayerVotedGood)) ? 1 : 0;
}
}
private boolean didGoodWin(String round, int players) {
int good = 0;
for (char next : round.toCharArray())
good += next == '1' ? 1 : 0;
return (good * 2) > players;
}
}
package Humans;
public class CrowdFollower extends Human {
// toggles weather the FrontPacker thinks majority is better vs. minority is better
private static final boolean goWithMajority = true;
@Override
public final 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 (String nextRound : roundVotes) {
boolean didGoodWin = didGoodWin(nextRound, players);
for (int player = 0; player < nextRound.length(); player++) {
boolean playerVotedGood = nextRound.charAt(player) == '1';
winningPlayers[player] += didPlayerWin(didGoodWin, playerVotedGood);
}
}
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 int didPlayerWin(boolean didGoodWin, boolean playerVotedGood) {
if(goWithMajority) {
return ((didGoodWin && goodplayerVotedGood) || (!didGoodWin && !goodplayerVotedGood)) ? 1 : 0;
} else {
playerVotedGood return ((!didGoodWin && good) || (didGoodWin && !goodplayerVotedGood)) ? 1 : 0;
}
}
private boolean didGoodWin(String round, int players) {
int good = 0;
for (char next : round.toCharArray())
good += next == '1' ? 1 : 0;
return (good * 2) > players;
}
}