Skip to content

Commit 2f5ce6b

Browse files
committed
Added more unit tests, fixed more than one unit per tile
1 parent a50d7f7 commit 2f5ce6b

4 files changed

Lines changed: 58 additions & 17 deletions

File tree

src/main/java/hotciv/framework/Game.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package hotciv.framework;
22

3+
import hotciv.standard.UnitImpl;
4+
35
/** Game is the central interface allowing a client to access and
46
* modify the state of a HotCiv game.
57
@@ -133,5 +135,6 @@ public interface Game {
133135
* @param p the position of a unit that must perform its action.
134136
* Nothing happens in case the unit has no associated action.
135137
*/
136-
public void performUnitActionAt( Position p );
138+
public void performUnitActionAt( Position p );
139+
137140
}

src/main/java/hotciv/standard/GameImpl.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public GameImpl() { //Constructor for GameImpl
6767
for (int j = 0; j < WORLDSIZE; j++)
6868
world[i][j] = new TileImpl();
6969

70+
for (int i = 0; i < WORLDSIZE; i++)
71+
for (int j = 0; j < WORLDSIZE; j++)
72+
units[i][j] = null;
73+
74+
for (int i = 0; i < WORLDSIZE; i++)
75+
for (int j = 0; j < WORLDSIZE; j++)
76+
cities[i][j] = null;
77+
7078
//set the special tiles
7179
world[1][0].setTerrain(OCEANS);
7280
world[0][1].setTerrain(HILLS);
@@ -83,27 +91,30 @@ public GameImpl() { //Constructor for GameImpl
8391
Position posArcher = new Position(0,2);
8492
Position posSettler = new Position(3, 4);
8593
Position posLegion = new Position(2,3);
86-
setUnitAt(posArcher, ARCHER, Player.RED);
87-
setUnitAt(posSettler, SETTLER, Player.RED);
88-
setUnitAt(posLegion, LEGION, Player.BLUE);
94+
createUnitAt(posArcher, ARCHER, Player.RED);
95+
createUnitAt(posSettler, SETTLER, Player.RED);
96+
createUnitAt(posLegion, LEGION, Player.BLUE);
8997

9098

9199
this.year = -4000;
92-
93100
}
94101

95102

96103
public Tile getTileAt( Position p ) {
97104
return world[p.getColumn()][p.getRow()];
98105
}
106+
99107
public Unit getUnitAt( Position p ) {
100108
return this.units[p.getColumn()][p.getRow()];
101109
}
102110

103111
//This will be changed later to account for the conditions needed to buy and place units -MAP
104-
public boolean setUnitAt( Position p, String unitType, Player owner ) {
105-
this.units[p.getColumn()][p.getRow()] = new UnitImpl(unitType, owner);
106-
return true;
112+
public boolean createUnitAt( Position p, String unitType, Player owner ) {
113+
if(this.units[p.getColumn()][p.getRow()] != null) {
114+
return false;
115+
}
116+
this.units[p.getColumn()][p.getRow()] = new UnitImpl(unitType, owner);
117+
return true;
107118
}
108119

109120
public City getCityAt( Position p ) {
@@ -133,7 +144,7 @@ public int getAge() {
133144
}
134145

135146
public boolean moveUnit( Position from, Position to ) {
136-
if(units[from.getColumn()][from.getRow()] != null) {
147+
if(units[from.getColumn()][from.getRow()] != null && units[to.getColumn()][to.getRow()] == null) {
137148
units[to.getColumn()][to.getRow()] = units[from.getColumn()][from.getRow()];
138149
units[from.getColumn()][from.getRow()] = null;
139150
return true;
@@ -172,9 +183,13 @@ public void performUnitActionAt( Position p ) {
172183

173184

174185
//----------------- True / False Queries ---------------------//
186+
175187
public boolean isPlayerInGame(Player player) {
176188
return Players.contains(player);
177189
}
178190

179191

192+
193+
194+
180195
}

src/main/java/hotciv/standard/UnitImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public UnitImpl(String type, Player owner) {
3232
* least those listed in GameConstants, particular variants
3333
* may define more strings to be valid.
3434
*/
35+
36+
//---------------------Getters---------------------//
3537
public String getTypeString() {
3638
return this.type.toString();
3739
}
@@ -87,8 +89,7 @@ public static boolean valid_unit_type(String type){
8789
} catch (IllegalArgumentException e) {
8890
return false;
8991
}
90-
91-
return true;
92-
}
92+
return true;
93+
}
9394

9495
}

src/test/java/hotciv/standard/unit_tests.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hotciv.standard;
22

33

4+
import hotciv.framework.Player;
45
import hotciv.framework.Position;
56
import hotciv.framework.Game;
67
import org.junit.Before;
@@ -44,7 +45,9 @@
4445
public class unit_tests {
4546
private Game game;
4647

47-
/** Fixture for alphaciv testing. */
48+
/**
49+
* Fixture for alphaciv testing.
50+
*/
4851
@Before
4952
public void setUp() {
5053
game = new GameImpl();
@@ -53,11 +56,30 @@ public void setUp() {
5356

5457
@Test
5558
public void startingUnits() {
56-
Position posArcher = new Position(0,2);
57-
Position posSettler = new Position(3,4);
58-
Position posLegion = new Position(2,3);
59+
Position posArcher = new Position(0, 2);
60+
Position posSettler = new Position(3, 4);
61+
Position posLegion = new Position(2, 3);
5962
assertThat(game.getUnitAt(posArcher).getTypeString(), is(ARCHER));
6063
assertThat(game.getUnitAt(posSettler).getTypeString(), is(SETTLER));
6164
assertThat(game.getUnitAt(posLegion).getTypeString(), is(LEGION));
6265
}
63-
}
66+
67+
68+
@Test
69+
public void oneUnitPerTile() {
70+
Position posArcher = new Position(0, 2);
71+
Position posSettler = new Position(3, 4);
72+
Position posLegion = new Position(2, 3);
73+
assertThat(game.getUnitAt(posArcher).getTypeString(), is(ARCHER));
74+
assertThat(game.getUnitAt(posSettler).getTypeString(), is(SETTLER));
75+
assertThat(game.getUnitAt(posLegion).getTypeString(), is(LEGION));
76+
77+
boolean oneUnitPerTile = game.moveUnit(posArcher, posLegion);
78+
boolean oneUnitPerTile2 = game.moveUnit(posLegion, posArcher);
79+
boolean oneUnitPerTile3 = game.moveUnit(posSettler, posSettler);
80+
81+
boolean unitsPlacedOverEachOther = oneUnitPerTile || oneUnitPerTile2 || oneUnitPerTile3;
82+
83+
assertThat(unitsPlacedOverEachOther, is(false));
84+
}
85+
}

0 commit comments

Comments
 (0)