Skip to content

Commit b3d3e8a

Browse files
committed
passed tests adding betaCiv
1 parent 5e2dec0 commit b3d3e8a

9 files changed

Lines changed: 206 additions & 79 deletions

File tree

src/main/java/hotciv/framework/GameConstants.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ public class GameConstants {
4545
// Valid production balance types
4646
public static final String productionFocus = "hammer";
4747
public static final String foodFocus = "apple";
48-
public static final boolean WINNER_FOUND = false;
48+
public static boolean WINNER_FOUND = false;
49+
50+
public static Player WINNER = null;
4951
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package hotciv.framework;
2+
3+
4+
// Enum to discern what the game rules will be for a specific instantiation
5+
public enum GameType {
6+
alphaCiv, betaCiv
7+
}

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

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ public class GameImpl implements Game {
4949
private CityImpl[][] cities = new CityImpl[WORLDSIZE][WORLDSIZE];
5050
private UnitImpl[][] units = new UnitImpl[WORLDSIZE][WORLDSIZE];
5151

52+
private GameType rules;
53+
5254

5355

5456
//This constructor is currently specific to the first iteration checkoffs, but will be changed later -MAP
55-
public GameImpl() { //Constructor for GameImpl
57+
public GameImpl(GameType r) { //Constructor for GameImpl
5658
this.numberOfPlayers = 2;
5759
this.Players = new ArrayDeque<Player>(numberOfPlayers);
60+
this.rules = r;
5861

5962
//This line looks gross but IntelliJ was complaining when I used a for loop
6063
//It populates the Players queue in order depending on the number of players
@@ -133,11 +136,29 @@ public Player getPlayerInTurn() {
133136

134137
//current bodge for RED to win after 3000 BC
135138
public Player getWinner() {
136-
if( year >= -3000 ) {
137-
return Player.RED;
138-
}else
139-
return null;
140-
}
139+
switch(rules) {
140+
case alphaCiv:
141+
if (year >= -3000)
142+
WINNER = Player.RED;
143+
return WINNER;
144+
145+
146+
case betaCiv:
147+
Position city1 = new Position(1, 1);
148+
Position city2 = new Position(1, 4);
149+
if (!WINNER_FOUND && (getCityAt(city1).getOwner() == getCityAt(city2).getOwner())) {
150+
WINNER = getCityAt(city1).getOwner();
151+
WINNER_FOUND = true;
152+
return WINNER;
153+
} else
154+
return WINNER;
155+
156+
157+
default:
158+
return WINNER;
159+
}
160+
}
161+
141162

142163
public int getAge() {
143164
return year;
@@ -152,11 +173,49 @@ public boolean moveUnit( Position from, Position to ) {
152173
return false;
153174
}
154175
public void endOfTurn() {
155-
Players.addLast(Players.removeFirst()); //rotate
156-
year += 100;
157-
if( Players.peekFirst() == firstPlayer ) {
158-
this.updateCityValues();
176+
switch(rules) {
177+
case alphaCiv:
178+
Players.addLast(Players.removeFirst()); //rotate
179+
year += 100;
180+
if( Players.peekFirst() == firstPlayer ) {
181+
this.updateCityValues();
182+
}
183+
break;
184+
185+
case betaCiv:
186+
Players.addLast(Players.removeFirst()); // rotate
187+
188+
// Refer to textbook for description of aging algorithm
189+
if (year < -100) {
190+
year += 100;
191+
}
192+
else if (year == -100) {
193+
year = -1;
194+
}
195+
else if (year == -1) {
196+
year = 1;
197+
}
198+
else if (year == 1) {
199+
year = 50;
200+
}
201+
else if (year < 1750) {
202+
year +=50;
203+
}
204+
else if (year < 1900) {
205+
year +=25;
206+
}
207+
else if (year < 1970) {
208+
year +=5;
209+
}
210+
else {
211+
year += 1;
212+
}
213+
214+
if( Players.peekFirst() == firstPlayer ) {
215+
this.updateCityValues();
216+
}
159217
}
218+
160219
}
161220

162221
private void updateCityValues() {

src/test/java/hotciv/standard/TestAlphaCiv.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class TestAlphaCiv {
4242
/** Fixture for alphaciv testing. */
4343
@Before
4444
public void setUp() {
45-
game = new GameImpl();
45+
game = new GameImpl(GameType.alphaCiv);
4646
}
4747

4848
// FRS p. 455 states that 'Red is the first player to take a turn'.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package hotciv.standard;
2+
3+
import hotciv.framework.GameType;
4+
import hotciv.framework.Player;
5+
import hotciv.framework.Position;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import static org.hamcrest.CoreMatchers.is;
10+
import static org.junit.Assert.assertThat;
11+
12+
/** Skeleton class for AlphaCiv test cases
13+
14+
Updated Oct 2015 for using Hamcrest matchers
15+
16+
This source code is from the book
17+
"Flexible, Reliable Software:
18+
Using Patterns and Agile Development"
19+
published 2010 by CRC Press.
20+
Author:
21+
Henrik B Christensen
22+
Department of Computer Science
23+
Aarhus University
24+
25+
Please visit http://www.baerbak.com/ for further information.
26+
27+
Licensed under the Apache License, Version 2.0 (the "License");
28+
you may not use this file except in compliance with the License.
29+
You may obtain a copy of the License at
30+
31+
http://www.apache.org/licenses/LICENSE-2.0
32+
33+
Unless required by applicable law or agreed to in writing, software
34+
distributed under the License is distributed on an "AS IS" BASIS,
35+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36+
See the License for the specific language governing permissions and
37+
limitations under the License.
38+
39+
*/
40+
public class betaCiv_tests {
41+
private GameImpl game; //Changed Game to GameImpl -TPD
42+
43+
/**
44+
* Fixture for alphaCiv testing.
45+
*/
46+
@Before
47+
public void setUp() {
48+
game = new GameImpl(GameType.betaCiv);
49+
}
50+
51+
// FRS p. 455 states that 'Red is the first player to take a turn'.
52+
53+
@Test
54+
public void betaCiv_DynamicWorldAging() {
55+
56+
// 100 Years per turn pre- 100BC
57+
for (int i = 0; i < 39; i++) {
58+
game.endOfTurn();
59+
}
60+
assertThat(game.getAge(), is(-100));
61+
62+
// Next turn is 1BC
63+
game.endOfTurn();
64+
assertThat(game.getAge(), is(-1));
65+
66+
// Next turn is 1AD
67+
game.endOfTurn();
68+
assertThat(game.getAge(), is(1));
69+
70+
// Next turn is 50AD
71+
game.endOfTurn();
72+
assertThat(game.getAge(), is(50));
73+
74+
75+
// 50 years per turn until 1750
76+
for (int j = 0; j < 34; j++) {
77+
game.endOfTurn();
78+
}
79+
assertThat(game.getAge(), is(1750));
80+
81+
// 25 years per turn until 1900
82+
for (int h = 0; h < 6; h++) {
83+
game.endOfTurn();
84+
}
85+
assertThat(game.getAge(), is(1900));
86+
87+
// 5 years per turn until 1970
88+
for (int k = 0; k < 14; k++) {
89+
game.endOfTurn();
90+
}
91+
assertThat(game.getAge(), is(1970));
92+
93+
// 1 year per turn for the rest of the game
94+
for (int m = 0; m < 100; m++) {
95+
game.endOfTurn();
96+
}
97+
assertThat(game.getAge(), is(2070));
98+
}
99+
100+
101+
@Test
102+
public void betaCiv_FirstPlayerThatConquersWorldWins() {
103+
Position city1 = new Position(1, 1);
104+
Position city2 = new Position(1, 4);
105+
106+
game.setCityAt(city1, Player.BLUE);
107+
108+
assertThat(game.getCityAt(city1).getOwner(), is(Player.BLUE));
109+
110+
assertThat(game.getWinner(), is(Player.BLUE)); // Blue owns both cities and should win
111+
112+
game.setCityAt(city1, Player.RED);
113+
game.setCityAt(city2, Player.RED);
114+
115+
assertThat(game.getWinner(), is(Player.BLUE)); // Blue already won the game, so they remain the winner.
116+
}
117+
}
118+

src/test/java/hotciv/standard/city_tests.java

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

3+
import hotciv.framework.GameType;
34
import hotciv.framework.Player;
45
import hotciv.framework.Position;
56
import hotciv.framework.Game;
@@ -22,7 +23,7 @@ public class city_tests {
2223
*/
2324
@Before
2425
public void setUp() {
25-
game = new GameImpl();
26+
game = new GameImpl(GameType.alphaCiv);
2627
}
2728

2829

src/test/java/hotciv/standard/game_tests.java

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

33
import hotciv.framework.Game;
4+
import hotciv.framework.GameType;
45
import hotciv.framework.Player;
56
import hotciv.framework.Position;
67
import org.junit.Before;
@@ -47,7 +48,7 @@ public class game_tests {
4748
/** Fixture for alphaciv testing. */
4849
@Before
4950
public void setUp() {
50-
game = new GameImpl();
51+
game = new GameImpl(GameType.alphaCiv);
5152
}
5253

5354
// FRS p. 455 states that 'Red is the first player to take a turn'.
@@ -82,54 +83,6 @@ public void alphaCiv_Progress100YearsEveryTurn() {
8283
assertThat((turn2 - turn1), is(100));
8384
}
8485

85-
@Test
86-
public void betaCiv_DynamicWorldAging() {
87-
88-
// 100 Years per turn pre- 100BC
89-
for (int i = 0; i < 38; i++){
90-
game.endOfTurn();
91-
}
92-
assertThat(game.getAge(), is(-100));
93-
94-
// Next turn is 1BC
95-
game.endOfTurn();
96-
assertThat(game.getAge(), is(-1));
97-
98-
// Next turn is 1AD
99-
game.endOfTurn();
100-
assertThat(game.getAge(), is(1));
101-
102-
// Next turn is 50AD
103-
game.endOfTurn();
104-
assertThat(game.getAge(), is(50));
105-
106-
107-
// 50 years per turn until 1750
108-
for (int j = 0; j < 33; j++) {
109-
game.endOfTurn();
110-
}
111-
assertThat(game.getAge(), is(1750));
112-
113-
// 25 years per turn until 1900
114-
for (int h = 0; h < 5; h++) {
115-
game.endOfTurn();
116-
}
117-
assertThat(game.getAge(), is(1900));
118-
119-
// 5 years per turn until 1970
120-
for (int k = 0; k < 34; k++) {
121-
game.endOfTurn();
122-
}
123-
assertThat(game.getAge(), is(1970));
124-
125-
// 1 year per turn for the rest of the game
126-
for (int m = 0; m < 99; m++) {
127-
game.endOfTurn();
128-
}
129-
assertThat(game.getAge(), is(2070));
130-
}
131-
132-
13386
@Test
13487
public void alphaCiv_RedWinsIf3000BC() {
13588
for (int i = 0; i < 10; i++) {
@@ -139,21 +92,6 @@ public void alphaCiv_RedWinsIf3000BC() {
13992
assertThat(game.getWinner(), is(Player.RED));
14093
}
14194

142-
@Test
143-
public void betaCiv_FirstPlayerThatConquersWorldWins() {
144-
Position city1 = new Position(1,1);
145-
Position city2 = new Position(1,4);
146-
147-
game.setCityAt(city1, Player.BLUE);
148-
149-
assertThat(game.getWinner(), is(Player.BLUE)); // Blue owns both cities and should win
150-
151-
game.setCityAt(city1, Player.RED);
152-
game.setCityAt(city2, Player.RED);
153-
154-
assertThat(game.getWinner(), is(Player.BLUE)); // Blue already won the game, so they remain the winner.
155-
}
156-
15795
@Test //TPD
15896
public void attackingUnitAlwaysWins(){
15997
Position posArcher = new Position(0, 2); //Attacker

src/test/java/hotciv/standard/tile_tests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import hotciv.framework.Game;
44
import hotciv.framework.Position;
55
import hotciv.framework.Tile;
6+
import hotciv.framework.GameType;
67
import org.junit.Before;
78
import org.junit.Test;
89

@@ -47,7 +48,7 @@ public class tile_tests {
4748
/** Fixture for alphaciv testing. */
4849
@Before
4950
public void setUp() {
50-
game = new GameImpl();
51+
game = new GameImpl(GameType.alphaCiv);
5152
}
5253

5354
@Test

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import hotciv.framework.Player;
55
import hotciv.framework.Position;
66
import hotciv.framework.Game;
7+
import hotciv.framework.GameType;
78
import org.junit.Before;
89
import org.junit.Test;
910

@@ -50,7 +51,7 @@ public class unit_tests {
5051
*/
5152
@Before
5253
public void setUp() {
53-
game = new GameImpl();
54+
game = new GameImpl(GameType.alphaCiv);
5455
}
5556

5657

0 commit comments

Comments
 (0)