Skip to content

Commit

Permalink
[Issue #10] created assassin victory post round handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewglowacki committed Oct 2, 2013
1 parent b055d15 commit 6ccd39a
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package theresistance.baseline.handler;

import theresistance.baseline.role.Assassin;
import theresistance.baseline.role.Merlin;
import theresistance.core.Alignment;
import theresistance.core.Game;
import theresistance.core.Player;

/**
* A post round victory handler that checks to see
* if one of the sides have won on rounds or not. If the evil
* side has won on rounds, then evil wins. If the good side
* does, then the {@link AssassinVictoryHandlerTest#WAITING_FOR_ASSASSINATION}
* property is set to true in the game's extra info.
*/
public class AssassinVictoryHandler extends RoundVictoryHandler
{
/**
* Game extra info property indicating that an
* the assassin should try to assassinate Merlin.
*/
public static final String WAITING_FOR_ASSASSINATION = "waiting.for.assassination";

/**
* The class of the role to be assassinated.
*/
public static final Class<?> ROLE_TO_ASSASSINATE = Merlin.class;

/**
* The class of the role that does the assassinating.
*/
public static final Class<?> ASSASSIN = Assassin.class;

Game game;

@Override
public void init(Game game)
{
super.init(game);
this.game = game;
game.getExtraInfo().put(WAITING_FOR_ASSASSINATION, false);
}

@Override
public void roundFinished()
{
Alignment winner = getWinner();
if (winner != Alignment.NEITHER)
{
if (winner == Alignment.EVIL)
{
game.setWinners(winner);
}
else
{
game.getExtraInfo().put(WAITING_FOR_ASSASSINATION, true);
}
}
}

/**
* Sets the winner of the game based on the player that was assassinated.
*
* @param game
* Game to set the winner of.
* @param assassinated
* The player that was assassinated.
*/
public static void setWinnerBasedOnAssassination(Game game, Player assassinated)
{
if (assassinated.getRole().getClass().equals(ROLE_TO_ASSASSINATE))
{
game.setWinners(Alignment.EVIL);
}
else
{
game.setWinners(Alignment.GOOD);
}
game.getExtraInfo().put(WAITING_FOR_ASSASSINATION, false);
}

public static boolean isGameWaitingForAssassination(Game game)
{
return game.getExtraInfo().getBoolean(WAITING_FOR_ASSASSINATION, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* The post round handler that checks both sides for a victory (majority of the
* rounds won)
*/
public class VictoryHandler implements PostRoundEventHandler
{
public class RoundVictoryHandler implements PostRoundEventHandler
{
private static final int GOOD_INDEX = 0;
private static final int EVIL_INDEX = 1;

Expand All @@ -27,17 +27,27 @@ public void init(Game game)

@Override
public void roundFinished()
{
Alignment winner = getWinner();
if (winner != Alignment.NEITHER) {
game.setWinners(winner);
}
}

protected Alignment getWinner()
{
int[] scores = getScores();

if (scores[GOOD_INDEX] >= numWins)
{
game.setWinners(Alignment.GOOD);
return Alignment.GOOD;
}
else if (scores[EVIL_INDEX] >= numWins)
{
game.setWinners(Alignment.EVIL);
return Alignment.EVIL;
}

return Alignment.NEITHER;
}

private int[] getScores()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package theresistance.baseline.handler;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import theresistance.baseline.role.Assassin;
import theresistance.baseline.role.Merlin;
import theresistance.core.Game;
import theresistance.core.Mission;
import theresistance.core.Mission.Result;
import theresistance.core.Player;
import theresistance.core.Proposal;
import theresistance.core.config.GameConfig;

public class AssassinVictoryHandlerTest
{
Game game;
private final Player p1 = new Player("p1");
private final Player p2 = new Player("p2");

@Before
public void setup()
{
GameConfig config = new GameConfig();
config.setHandlers(new AssassinVictoryHandler());
config.setMissions(new Mission(1, 1), new Mission(1, 1), new Mission(1, 1));
config.setRoles(new Merlin(), new Assassin());
config.setPlayers(p1, p2);
game = config.create();
}

@Test
public void testNotEndOfGame()
{
Proposal proposal = game.propose(p1);
Mission mission = game.send(proposal);
mission.setResults(Result.PASS);
game.completeRound();

Assert.assertFalse(game.isOver());
Assert.assertFalse(AssassinVictoryHandler.isGameWaitingForAssassination(game));
}

@Test
public void testEndOfGameEvilWinOnRounds()
{
Proposal proposal = game.propose(p1);
Mission mission = game.send(proposal);
mission.setResults(Result.FAIL);
game.completeRound();

Assert.assertFalse(game.isOver());
Assert.assertFalse(AssassinVictoryHandler.isGameWaitingForAssassination(game));

proposal = game.propose(p1);
mission = game.send(proposal);
mission.setResults(Result.FAIL);
game.completeRound();

Assert.assertTrue(game.isOver());
Assert.assertFalse(AssassinVictoryHandler.isGameWaitingForAssassination(game));
Assert.assertTrue(game.getWinners().isEvil());
}

@Test
public void testEndOfGameMisAssassination()
{
passFirstTwoRounds();
Player assassin = (p1.getRole() instanceof Merlin ? p2 : p1);

AssassinVictoryHandler.setWinnerBasedOnAssassination(game, assassin);

Assert.assertTrue(game.isOver());
Assert.assertFalse(AssassinVictoryHandler.isGameWaitingForAssassination(game));
Assert.assertTrue(game.getWinners().isGood());
}

@Test
public void testEndOfGameMerlinAssassinated()
{
passFirstTwoRounds();
Player merlin = (p1.getRole() instanceof Merlin ? p1 : p2);

AssassinVictoryHandler.setWinnerBasedOnAssassination(game, merlin);

Assert.assertTrue(game.isOver());
Assert.assertFalse(AssassinVictoryHandler.isGameWaitingForAssassination(game));
Assert.assertTrue(game.getWinners().isEvil());
}

protected void passFirstTwoRounds()
{
Proposal proposal = game.propose(p1);
Mission mission = game.send(proposal);
mission.setResults(Result.PASS);
game.completeRound();

Assert.assertFalse(game.isOver());
Assert.assertFalse(AssassinVictoryHandler.isGameWaitingForAssassination(game));

proposal = game.propose(p1);
mission = game.send(proposal);
mission.setResults(Result.PASS);
game.completeRound();

Assert.assertFalse(game.isOver());
Assert.assertTrue(AssassinVictoryHandler.isGameWaitingForAssassination(game));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import theresistance.core.Proposal;
import theresistance.core.config.GameConfig;

public class VictoryHandlerTest
public class RoundVictoryHandlerTest
{
private Game game;
private final Player p1 = new Player("p1");
Expand All @@ -27,7 +27,7 @@ public void setup()
1));
config.setPlayers(p1, p2);
config.setRoles(new LoyalServant(), new Minion());
config.setHandlers(new VictoryHandler());
config.setHandlers(new RoundVictoryHandler());

game = config.create();
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/theresistance/core/Alignment.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public boolean isGood()
{
return GOOD.equals(this);
}

public boolean isEvil()
{
return EVIL.equals(this);
}
}
6 changes: 3 additions & 3 deletions core/src/main/java/theresistance/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Game
{
private final Round[] rounds;
private final Player[] players;
private ExtraInfoBag extraInfo;
private ExtraInfoBag extraInfo = new ExtraInfoBag();
private final PostRoundEventHandler[] handlers;

private int curRound = 0;
Expand All @@ -25,7 +25,7 @@ public Game(Player[] players, Round[] rounds,
this.rounds = rounds;
this.handlers = handlers;
}

/**
* make the next proposal
*
Expand Down Expand Up @@ -73,7 +73,7 @@ public void completeRound()

public boolean isOver()
{
return winners != null;
return winners != Alignment.NEITHER;
}

public Alignment getWinners()
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/theresistance/core/Mission.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum Result
PASS, FAIL
}

private ExtraInfoBag extraInfo;
private ExtraInfoBag extraInfo = new ExtraInfoBag();
private final int numParticipants;
private final int requiredFails;
private Player[] participants;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/theresistance/core/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Player implements Comparable<Player>
{
private String name;
private Role role;
private ExtraInfoBag extraInfo;
private ExtraInfoBag extraInfo = new ExtraInfoBag();

public Player(String name)
{
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/theresistance/core/Proposal.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum Vote
}

private Player leader;
private ExtraInfoBag extraInfo;
private ExtraInfoBag extraInfo = new ExtraInfoBag();
private int totalPlayers;
private Player[] participants;
private Map<Player, Vote> votes = new TreeMap<Player, Vote>();
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/theresistance/core/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public interface Role
* @return alignment of the role
*/
Alignment getAlignment();

}

0 comments on commit 6ccd39a

Please sign in to comment.