-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue #10] created assassin victory post round handler.
- Loading branch information
andrewglowacki
committed
Oct 2, 2013
1 parent
b055d15
commit 6ccd39a
Showing
10 changed files
with
225 additions
and
12 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
baseline/src/main/java/theresistance/baseline/handler/AssassinVictoryHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
baseline/src/test/java/theresistance/baseline/handler/AssassinVictoryHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,9 @@ public boolean isGood() | |
{ | ||
return GOOD.equals(this); | ||
} | ||
|
||
public boolean isEvil() | ||
{ | ||
return EVIL.equals(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,5 @@ public interface Role | |
* @return alignment of the role | ||
*/ | ||
Alignment getAlignment(); | ||
|
||
} |