Skip to content

Commit

Permalink
Small updates and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurelien Thieriot authored and Aurelien Thieriot committed Jul 19, 2017
1 parent 521ec5b commit fdadef4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ Documentation URL: [http://localhost:8080/swagger-ui.html](http://localhost:8080

This will create a new Kalah(6, 6) game and pick a first player randomly

curl -X POST \
--header "Content-Type: application/json" \
"http://localhost:8080/game" | jq
curl -X POST "http://localhost:8080/game" | jq

```
{
Expand All @@ -54,9 +52,7 @@ It's player's 2 turn so he starts and chose to sow the fifth house of his zone.

See the last part of the path: `/play/2/5`. It's for player 2, house 5 (Choice from 1 to 6)

curl -X POST \
--header "Content-Type: application/json" \
"http://localhost:8080/game/77903be2-dfbb-4550-94d2-2526fc71fbc6/play/2/5"
curl -X POST "http://localhost:8080/game/77903be2-dfbb-4550-94d2-2526fc71fbc6/play/2/5"

```
{
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/github/athieriot/api/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ public GameController(EngineRepository engines) {
this.engines = engines;
}

//TODO: Allow to configure the game. Ex: Start Kalah(6,4) instead of Kalah(6, 6)
@PostMapping
@ApiOperation(value = "Create a new game of Kalah",
notes = "This will create a new Kalah(6, 6) game and pick a first player randomly")
@ApiOperation(
value = "Create a new game of Kalah",
notes = "This will create a new Kalah(6, 6) game and pick a first player randomly"
)
//TODO: Allow to configure the game. Ex: Start Kalah(6,4) instead of Kalah(6, 6)
public ResponseEntity<Engine> newGame() {
Engine engine = new Engine(6, 6);

Expand Down
45 changes: 24 additions & 21 deletions src/main/java/com/github/athieriot/engine/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,36 @@ public class Board {
checkLegalMoves(houseNbr, houseIdx);
pickSeeds(houseIdx);

int startIdx = houseIdx + 1;
int lastIdx = startIdx;

while (hand > 0) {
lastIdx = sowSeedsFrom(player, startIdx);
startIdx = 0;
}
return sowSeedsInHand(player, houseIdx);
}

return lastIdx;
private int fromHouseNbrToIdx(int player, int houseNbr) {
return player == 1 ? houseNbr - 1 : houseNbr + houses;
}

private void checkLegalMoves(int houseNbr, int houseIdx) {
if (houseNbr < 1 || houseNbr > houses) { throw new InvalidParameterException("This is a party with " + houses + " houses"); }
else if (board[houseIdx] == 0) { throw new IllegalMoveException("Empty house. Not a valid move"); }
}

private int fromHouseNbrToIdx(int player, int houseNbr) {
return player == 1 ? houseNbr - 1 : houseNbr + houses;
}

private void pickSeeds(int house) {
synchronized (board) {
hand = board[house];
board[house] = 0;
}
}

private int sowSeedsInHand(int player, int houseIdx) {
int startIdx = houseIdx + 1;
int lastIdx = startIdx;

while (hand > 0) {
lastIdx = sowSeedsFrom(player, startIdx);
startIdx = 0;
}
return lastIdx;
}

private int sowSeedsFrom(int player, int startIdx) {
int i;
int opponentStoreIdx = player == 1 ? northStoreIdx : southStoreIdx;
Expand All @@ -85,7 +88,7 @@ private int sowSeedsFrom(int player, int startIdx) {
}

/* package */ void capture(int player, int idx) {
int opponentIdx = opponentIdx(idx);
int opponentIdx = oppositeIdx(idx);
int playerStoreIdx = playerStoreIdx(player);
int treasure = board[idx] + board[opponentIdx];

Expand All @@ -99,7 +102,7 @@ private int sowSeedsFrom(int player, int startIdx) {
/* package */ void collectSeeds(int player) {
board[playerStoreIdx(player)] = board[playerStoreIdx(player)] + seedsLeftFor(player);

streamOf(player).forEach(i -> board[i] = 0);
streamHousesOf(player).forEach(i -> board[i] = 0);
}

/* package */ boolean isPlayersHouse(int player, int idx) {
Expand All @@ -108,14 +111,14 @@ private int sowSeedsFrom(int player, int startIdx) {
}

/* package */ int seedsLeftFor(int player) {
return streamOf(player).map(i -> board[i]).sum();
return streamHousesOf(player).map(i -> board[i]).sum();
}

/* package */ int playerStoreIdx(int player) {
return player == 1 ? southStoreIdx : northStoreIdx;
}

/* package */ int opponentIdx(int idx) {
/* package */ int oppositeIdx(int idx) {
if (idx == northStoreIdx) { return southStoreIdx; }
else if (idx == southStoreIdx) { return northStoreIdx; }

Expand All @@ -126,17 +129,17 @@ private int sowSeedsFrom(int player, int startIdx) {
return board[idx];
}

private IntStream streamOf(int player) {
/* package */ int[] toArray() {
return board;
}

private IntStream streamHousesOf(int player) {
int start = player == 1 ? southStartIdx : northStartIdx;
int end = start + houses;

return range(start, end);
}

public int[] list() {
return board;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/athieriot/engine/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public UUID id() {

@JsonProperty
public int[] board() {
return board.list();
return board.toArray();
}

@JsonProperty
Expand Down Expand Up @@ -118,7 +118,7 @@ private void togglePlayersTurn() {
}

private boolean captureConditions(int player, int idx) {
int opponentIdx = board.opponentIdx(idx);
int opponentIdx = board.oppositeIdx(idx);

return board.isPlayersHouse(player, idx)
&& board.seeds(idx) == 1
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/github/athieriot/engine/BoardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ public void test_capture_from_player_1() {
public void test_opponent_idx() {
Board board = new Board(6, 6);

assertThat(board.opponentIdx(0)).isEqualTo(12);
assertThat(board.opponentIdx(5)).isEqualTo(7);
assertThat(board.opponentIdx(3)).isEqualTo(9);
assertThat(board.opponentIdx(6)).isEqualTo(13);
assertThat(board.opponentIdx(13)).isEqualTo(6);
assertThat(board.oppositeIdx(0)).isEqualTo(12);
assertThat(board.oppositeIdx(5)).isEqualTo(7);
assertThat(board.oppositeIdx(3)).isEqualTo(9);
assertThat(board.oppositeIdx(6)).isEqualTo(13);
assertThat(board.oppositeIdx(13)).isEqualTo(6);
}

@Test
Expand Down

0 comments on commit fdadef4

Please sign in to comment.