Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. package hotciv.standard;
  2.  
  3. import hotciv.framework.*;
  4.  
  5. import java.util.*;
  6.  
  7. import org.junit.*;
  8. import static org.junit.Assert.*;
  9.  
  10. /** Test the utility library's methods to calculate neighborhoods
  11. * and battle factors.
  12. */
  13. public class TestUtility {
  14. private Iterator<Position> iter;
  15. private ArrayList<Position> neighborhood;
  16. private Position center, p;
  17.  
  18. Game game;
  19. @Before public void setUp() {
  20. game = new GameStubForBattleTesting();
  21. }
  22.  
  23. /** helper method to insert elements in an iterator into a list. */
  24. private ArrayList<Position> convertIteration2List(Iterator<Position> iter) {
  25. neighborhood = new ArrayList<Position>();
  26. while ( iter.hasNext() ) {
  27. p = iter.next();
  28. neighborhood.add(p);
  29. }
  30. return neighborhood;
  31. }
  32.  
  33. @Test public void shouldGive8PositionsForP8_8() {
  34. center = new Position(8,8);
  35. iter = Utility.get8NeighborhoodIterator(center);
  36. neighborhood = convertIteration2List( iter );
  37.  
  38. assertTrue( "Must contain (7,7)",
  39. neighborhood.contains( new Position(7,7)));
  40. assertTrue( "Must contain (9,9)",
  41. neighborhood.contains( new Position(9,9)));
  42. assertTrue( "Must contain (7,9)",
  43. neighborhood.contains( new Position(7,9)));
  44. assertTrue( "Must contain (8,7)",
  45. neighborhood.contains( new Position(8,7)));
  46.  
  47. assertFalse( "Must not contain center position",
  48. neighborhood.contains( center ));
  49.  
  50. assertFalse( "Must not contain (5,5) position",
  51. neighborhood.contains( new Position(5,5) ));
  52.  
  53. assertEquals( "Should be 8 positions in the iterator",
  54. 8, neighborhood.size());
  55. }
  56.  
  57. @Test public void shouldGive3PositionsForP0_0() {
  58. center = new Position(0,0);
  59. iter = Utility.get8NeighborhoodIterator(center);
  60. neighborhood = convertIteration2List( iter );
  61.  
  62. assertTrue( "Must contain (1,0)",
  63. neighborhood.contains( new Position(1,0)));
  64. assertTrue( "Must contain (1,1)",
  65. neighborhood.contains( new Position(1,1)));
  66. assertTrue( "Must contain (0,1)",
  67. neighborhood.contains( new Position(0,1)));
  68.  
  69. assertEquals( "Should be 3 positions in the iterator",
  70. 3, neighborhood.size());
  71.  
  72. }
  73. @Test public void shouldGive3PositionsForP15_15() {
  74. center = new Position(15,15);
  75. iter = Utility.get8NeighborhoodIterator(center);
  76. neighborhood = convertIteration2List( iter );
  77.  
  78. assertTrue( "Must contain (14,15)",
  79. neighborhood.contains( new Position(14,15)));
  80. assertTrue( "Must contain (14,14)",
  81. neighborhood.contains( new Position(14,14)));
  82. assertTrue( "Must contain (15,14)",
  83. neighborhood.contains( new Position(15,14)));
  84.  
  85. assertEquals( "Should be 3 positions in the iterator",
  86. 3, neighborhood.size());
  87.  
  88. }
  89.  
  90. @Test public void shouldGiveCorrectTerrainFactors() {
  91. // plains have multiplier 1
  92. assertEquals( 1, Utility.getTerrainFactor( game, new Position(0,1)));
  93. // hills have multiplier 2
  94. assertEquals( 2, Utility.getTerrainFactor( game, new Position(1,0)));
  95. // forest have multiplier 2
  96. assertEquals( 2, Utility.getTerrainFactor( game, new Position(0,0)));
  97. // cities have multiplier 3
  98. assertEquals( 3, Utility.getTerrainFactor( game, new Position(1,1)));
  99. }
  100.  
  101. @Test public void shouldGiveSum1ForBlueAtP5_5() {
  102. assertEquals("Blue unit at (5,5) should get +1 support",
  103. +1, Utility.getFriendlySupport( game, new Position(5,5), Player.BLUE));
  104. }
  105.  
  106. @Test public void shouldGiveSum0ForBlueAtP2_4() {
  107. assertEquals("Blue unit at (2,4) should get +0 support",
  108. +0, Utility.getFriendlySupport( game, new Position(2,4), Player.BLUE));
  109. }
  110. @Test public void shouldGiveSum2ForRedAtP2_4() {
  111. assertEquals("Red unit at (2,4) should get +2 support",
  112. +2, Utility.getFriendlySupport( game, new Position(2,4), Player.RED));
  113. }
  114. @Test public void shouldGiveSum3ForRedAtP2_2() {
  115. assertEquals("Red unit at (2,2) should get +3 support",
  116. +3, Utility.getFriendlySupport( game, new Position(2,2), Player.RED));
  117. }
  118. }
  119.  
  120. // ================================== TEST STUBS ===
  121. class StubTile implements Tile {
  122. private String type;
  123. public StubTile(String type, int r, int c) { this.type = type; }
  124. public String getTypeString() { return type; }
  125. }
  126.  
  127. class StubUnit implements Unit {
  128. private String type; private Player owner;
  129. public StubUnit(String type, Player owner) {
  130. this.type = type; this.owner = owner;
  131. }
  132. public String getTypeString() { return type; }
  133. public Player getOwner() { return owner; }
  134. public int getMoveCount() { return 0; }
  135. public int getDefensiveStrength() { return 0; }
  136. public int getAttackingStrength() { return 0; }
  137. }
  138.  
  139.  
  140. /** A test stub for testing the battle calculation methods in
  141. * Utility. The terrains are
  142. * 0,0 - forest;
  143. * 1,0 - hill;
  144. * 0,1 - plain;
  145. * 1,1 - city.
  146. *
  147. * Red has units on 2,3; 3,2; 3,3; blue one on 4,4
  148. */
  149. class GameStubForBattleTesting implements Game {
  150. public Tile getTileAt(Position p) {
  151. if ( p.getRow() == 0 && p.getColumn() == 0 ) {
  152. return new StubTile(GameConstants.FOREST, 0, 0);
  153. }
  154. if ( p.getRow() == 1 && p.getColumn() == 0 ) {
  155. return new StubTile(GameConstants.HILLS, 1, 0);
  156. }
  157. return new StubTile(GameConstants.PLAINS, 0, 1);
  158. }
  159. public Unit getUnitAt(Position p) {
  160. if ( p.getRow() == 2 && p.getColumn() == 3 ||
  161. p.getRow() == 3 && p.getColumn() == 2 ||
  162. p.getRow() == 3 && p.getColumn() == 3 ) {
  163. return new StubUnit(GameConstants.ARCHER, Player.RED);
  164. }
  165. if ( p.getRow() == 4 && p.getColumn() == 4 ) {
  166. return new StubUnit(GameConstants.ARCHER, Player.BLUE);
  167. }
  168. return null;
  169. }
  170. public City getCityAt(Position p) {
  171. if ( p.getRow() == 1 && p.getColumn() == 1 ) {
  172. return new City() {
  173. public Player getOwner() { return Player.RED; }
  174. public int getSize() { return 1; }
  175. public String getProduction() {
  176. return null;
  177. }
  178. public String getWorkforceFocus() {
  179. return null;
  180. }
  181. };
  182. }
  183. return null;
  184. }
  185.  
  186. // the rest is unused test stub methods...
  187. public void changeProductionInCityAt(Position p, String unitType) {}
  188. public void changeWorkForceFocusInCityAt(Position p, String balance) {}
  189. public void endOfTurn() {}
  190. public Player getPlayerInTurn() {return null;}
  191. public Player getWinner() {return null;}
  192. public int getAge() { return 0; }
  193. public boolean moveUnit(Position from, Position to) {return false;}
  194. public void performUnitActionAt( Position p ) {}
  195.  
  196. // Remove these if the Game interface for the AlphaCiv exercise is used.
  197. public void addObserver(GameObserver observer) {}
  198. public void setTileFocus(Position position) {}
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement