Skip to content

Commit

Permalink
changed the move command constructors to take in a game object and gr…
Browse files Browse the repository at this point in the history
…ab that data in the abstract class. you still need to manually pass in whose making the move (#55)
  • Loading branch information
Philip Ross authored and Jernik committed May 13, 2016
1 parent 65a42eb commit 31cfee5
Show file tree
Hide file tree
Showing 21 changed files with 347 additions and 412 deletions.
9 changes: 3 additions & 6 deletions src/ai/Ai.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public MoveCommand generateMove() {
// you can't swap
pushPiecePlace = this.generateRandomDirection(randomDirection);
}
MoveCommand pushMove = new PushMove(this.game.getBoardState(), pieceCoor, randomDirection, pushPiecePlace,
this.owner, this.game.getNumMoves());
MoveCommand pushMove = new PushMove(this.game, pieceCoor, randomDirection, pushPiecePlace, this.owner);
if (pushMove.isValidMove()) {
return pushMove;
}
Expand All @@ -84,17 +83,15 @@ public MoveCommand generateMove() {
// you can't swap
pullPiecePlace = this.generateRandomDirection(randomDirection);
}
MoveCommand pullMove = new PullMove(this.game.getBoardState(), pieceCoor, randomDirection, pullPiecePlace,
this.owner, this.game.getNumMoves());
MoveCommand pullMove = new PullMove(this.game, pieceCoor, randomDirection, pullPiecePlace, this.owner);
if (pullMove.isValidMove()) {
return pullMove;
}
// wasn't a valid pull, try again
return this.generateMove();
}
// use regular method
MoveCommand regularMove = new RegularMove(this.game.getBoardState(), pieceCoor, randomDirection, this.owner,
this.game.getNumMoves());
MoveCommand regularMove = new RegularMove(this.game, pieceCoor, randomDirection, this.owner);
if (regularMove.isValidMove()) {
return regularMove;
}
Expand Down
13 changes: 6 additions & 7 deletions src/listeners/MovementListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void mousePressed(MouseEvent e) {
if (game.isAiTurn()) {
return;
}

Coordinate coor = new Coordinate((sourceX - 10) / 80, (sourceY - 10) / 80);
// Beginning movement, nothing yet selected
// Selecting piece to interact with
Expand All @@ -70,8 +70,7 @@ public void mousePressed(MouseEvent e) {
// AKA move
else if (isSelectedPieceAndEmptySpaceClicked(coor)) {
// Using move to check for valid move
RegularMove m = new RegularMove(game.getBoardState(), this.selectedPieceCoord, coor,
this.game.getPlayerTurn(), game.getNumMoves());
RegularMove m = new RegularMove(game, this.selectedPieceCoord, coor, this.game.getPlayerTurn());
if (game.move(m)) {
gui.rerenderBoard();
}
Expand All @@ -92,11 +91,11 @@ else if (pieceSelectedAndSecondPieceClicked(coor)) {
} else if (twoPieceSelectedAndEmptySpaceClicked(coor)) {
MoveCommand move = null;
if (this.secondSelectedPieceCoord.isOrthogonallyAdjacentTo(coor)) {
move = new PushMove(game.getBoardState(), this.selectedPieceCoord, this.secondSelectedPieceCoord,
coor, game.getPlayerTurn(), game.getNumMoves());
move = new PushMove(game, this.selectedPieceCoord, this.secondSelectedPieceCoord, coor,
game.getPlayerTurn());
} else {
move = new PullMove(game.getBoardState(), this.selectedPieceCoord, coor,
this.secondSelectedPieceCoord, game.getPlayerTurn(), game.getNumMoves());
move = new PullMove(game, this.selectedPieceCoord, coor, this.secondSelectedPieceCoord,
game.getPlayerTurn());
}
if (game.move(move)) {
gui.rerenderBoard();
Expand Down
10 changes: 5 additions & 5 deletions src/move_commands/MoveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import board.BoardState;
import board.Coordinate;
import game.Game;
import piece.Owner;

public abstract class MoveCommand implements Serializable {
Expand All @@ -18,16 +19,15 @@ public abstract class MoveCommand implements Serializable {
protected Owner turn;
protected int movesLeft;

protected MoveCommand(BoardState board, Coordinate originalPosition, Coordinate newPosition, Owner turn,
int movesLeft) {
this.originalBoard = new BoardState(board);
this.newBoard = board;
protected MoveCommand(Game game, Coordinate originalPosition, Coordinate newPosition, Owner turn) {
this.originalBoard = new BoardState(game.getBoardState());
this.newBoard = game.getBoardState();

this.originalPosition = originalPosition;
this.newPosition = newPosition;

this.turn = turn;
this.movesLeft = movesLeft;
this.movesLeft = game.getNumMoves();
}

abstract public BoardState execute();
Expand Down
7 changes: 4 additions & 3 deletions src/move_commands/PullMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import board.BoardState;
import board.Coordinate;
import game.Game;
import piece.AbstractPiece;
import piece.Owner;

Expand All @@ -15,9 +16,9 @@ public class PullMove extends MoveCommand {
public static final int NUMBER_OF_MOVES = 2;
private Coordinate pullPiecePosition;

public PullMove(BoardState board, Coordinate originalPosition, Coordinate newPosition, Coordinate pullPiecePosition,
Owner turn, int movesLeft) {
super(board, originalPosition, newPosition, turn, movesLeft);
public PullMove(Game game, Coordinate originalPosition, Coordinate newPosition, Coordinate pullPiecePosition,
Owner turn) {
super(game, originalPosition, newPosition, turn);
this.pullPiecePosition = pullPiecePosition;
}

Expand Down
7 changes: 4 additions & 3 deletions src/move_commands/PushMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import board.BoardState;
import board.Coordinate;
import game.Game;
import piece.AbstractPiece;
import piece.Owner;

Expand All @@ -15,9 +16,9 @@ public class PushMove extends MoveCommand {
public static final int NUMBER_OF_MOVES = 2;
private Coordinate pushPiecePosition;

public PushMove(BoardState board, Coordinate originalPosition, Coordinate newPosition, Coordinate pushPiecePosition,
Owner turn, int movesLeft) {
super(board, originalPosition, newPosition, turn, movesLeft);
public PushMove(Game game, Coordinate originalPosition, Coordinate newPosition, Coordinate pushPiecePosition,
Owner turn) {
super(game, originalPosition, newPosition, turn);
this.pushPiecePosition = pushPiecePosition;
}

Expand Down
6 changes: 3 additions & 3 deletions src/move_commands/RegularMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import board.BoardState;
import board.Coordinate;
import game.Game;
import piece.AbstractPiece;
import piece.Owner;
import piece.Rabbit;
Expand All @@ -10,9 +11,8 @@ public class RegularMove extends MoveCommand {
private static final long serialVersionUID = 4434841689278271636L;
public static final int NUMBER_OF_MOVES = 1;

public RegularMove(BoardState board, Coordinate originalPosition, Coordinate newPosition, Owner turn,
int movesLeft) {
super(board, originalPosition, newPosition, turn, movesLeft);
public RegularMove(Game game, Coordinate originalPosition, Coordinate newPosition, Owner turn) {
super(game, originalPosition, newPosition, turn);
}

@Override
Expand Down
36 changes: 12 additions & 24 deletions test/ai/TestGenerateRandomMoveOnlyIfEnoughMoves.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,51 +32,39 @@ private void testOnlyRegularMoves(Ai ai) {
@Test
public void testNormalNoPushOrPullIf1MoveLeft() {
Game game = this.normalAi.getGame();
game.move(new RegularMove(game.getBoardState(), new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game.getBoardState(), new Coordinate(1, 2), new Coordinate(1, 1), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game.getBoardState(), new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game, new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn()));
game.move(new RegularMove(game, new Coordinate(1, 2), new Coordinate(1, 1), game.getPlayerTurn()));
game.move(new RegularMove(game, new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn()));
testOnlyRegularMoves(this.normalAi);
}

@Test
public void testKatLoverNoPushOrPullIf1MoveLeft() {
Game game = this.catLoverAi.getGame();
game.move(new RegularMove(game.getBoardState(), new Coordinate(0, 1), new Coordinate(0, 0), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game.getBoardState(), new Coordinate(0, 0), new Coordinate(0, 1), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game.getBoardState(), new Coordinate(0, 1), new Coordinate(0, 0), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game, new Coordinate(0, 1), new Coordinate(0, 0), game.getPlayerTurn()));
game.move(new RegularMove(game, new Coordinate(0, 0), new Coordinate(0, 1), game.getPlayerTurn()));
game.move(new RegularMove(game, new Coordinate(0, 1), new Coordinate(0, 0), game.getPlayerTurn()));
testOnlyRegularMoves(this.catLoverAi);
}

@Test
public void testNotManyMovesNoPushOrPullIf1MoveLeft() {
Game game = this.notManyMovesAi.getGame();
game.move(new RegularMove(game.getBoardState(), new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game, new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn()));
game.getBoardState().movePiece(new Coordinate(1, 2), new Coordinate(1, 1));
game.move(new RegularMove(game.getBoardState(), new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game, new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn()));
game.getBoardState().movePiece(new Coordinate(1, 2), new Coordinate(1, 1));
game.move(new RegularMove(game.getBoardState(), new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game, new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn()));
game.getBoardState().movePiece(new Coordinate(1, 2), new Coordinate(1, 1));
testOnlyRegularMoves(this.notManyMovesAi);
}

@Test
public void testStartingNoPushOrPullIf1MoveLeft() {
Game game = this.startingAi.getGame();
game.move(new RegularMove(game.getBoardState(), new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game.getBoardState(), new Coordinate(1, 2), new Coordinate(1, 3), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game.getBoardState(), new Coordinate(1, 3), new Coordinate(1, 4), game.getPlayerTurn(),
game.getNumMoves()));
game.move(new RegularMove(game, new Coordinate(1, 1), new Coordinate(1, 2), game.getPlayerTurn()));
game.move(new RegularMove(game, new Coordinate(1, 2), new Coordinate(1, 3), game.getPlayerTurn()));
game.move(new RegularMove(game, new Coordinate(1, 3), new Coordinate(1, 4), game.getPlayerTurn()));
game.getBoardState().movePiece(new Coordinate(1, 4), new Coordinate(1, 1));
testOnlyRegularMoves(this.startingAi);
}
Expand Down
61 changes: 26 additions & 35 deletions test/game/TestEndGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public void testLoseWithNoMoves1() {
noMovesP1.put(new Coordinate(5, 5), new Rabbit(Owner.Player2));
Game game = new Game(new BoardState(noMovesP1));

assertTrue(game.move(new RegularMove(game.getBoardState(), new Coordinate(4, 3), new Coordinate(4, 4),
game.getPlayerTurn(), game.getNumMoves())));
assertTrue(game.move(new RegularMove(game, new Coordinate(4, 3), new Coordinate(4, 4), game.getPlayerTurn())));

assertTrue(game.hasNoMoves(game.getPlayerTurn()));
assertEquals(Owner.Player2, game.getWinner());
Expand All @@ -97,10 +96,8 @@ public void testLoseWithNoMovesIfOnlyPushAvaliableButWeaker() {

Game game = new Game(new BoardState(p));

assertTrue(game.move(new RegularMove(game.getBoardState(), new Coordinate(4, 3), new Coordinate(4, 4),
game.getPlayerTurn(), game.getNumMoves())));
assertTrue(game.move(new RegularMove(game.getBoardState(), new Coordinate(4, 2), new Coordinate(4, 3),
game.getPlayerTurn(), game.getNumMoves())));
assertTrue(game.move(new RegularMove(game, new Coordinate(4, 3), new Coordinate(4, 4), game.getPlayerTurn())));
assertTrue(game.move(new RegularMove(game, new Coordinate(4, 2), new Coordinate(4, 3), game.getPlayerTurn())));

assertTrue(game.hasNoMoves(game.getPlayerTurn()));
assertEquals(Owner.Player2, game.getWinner());
Expand Down Expand Up @@ -128,10 +125,8 @@ public void testLoseWithNoMovesIfOnlyPushAvaliableButEqualStrength() {
Game game = new Game(new BoardState(p));
game.incrementTurn();

assertTrue(game.move(new RegularMove(game.getBoardState(), new Coordinate(4, 5), new Coordinate(4, 4),
game.getPlayerTurn(), game.getNumMoves())));
assertTrue(game.move(new RegularMove(game.getBoardState(), new Coordinate(4, 6), new Coordinate(4, 5),
game.getPlayerTurn(), game.getNumMoves())));
assertTrue(game.move(new RegularMove(game, new Coordinate(4, 5), new Coordinate(4, 4), game.getPlayerTurn())));
assertTrue(game.move(new RegularMove(game, new Coordinate(4, 6), new Coordinate(4, 5), game.getPlayerTurn())));

assertTrue(game.hasNoMoves(Owner.Player2));
assertEquals(Owner.Player1, game.getWinner());
Expand Down Expand Up @@ -159,21 +154,18 @@ public void testLoseWithNoMovesIfOnlyPushAvaliableAndNotEnoughMovesLeft() {
p.put(new Coordinate(3, 4), new Rabbit(Owner.Player2));

Game game = new Game(new BoardState(p));
assertTrue(game.move(new RegularMove(game.getBoardState(), new Coordinate(4, 0), new Coordinate(5, 0),
game.getPlayerTurn(), game.getNumMoves())));
assertTrue(game.move(new RegularMove(game, new Coordinate(4, 0), new Coordinate(5, 0), game.getPlayerTurn())));
assertEquals(Owner.Nobody, game.getWinner());

assertTrue(game.move(new RegularMove(game.getBoardState(), new Coordinate(5, 0), new Coordinate(5, 1),
game.getPlayerTurn(), game.getNumMoves())));
assertTrue(game.move(new RegularMove(game, new Coordinate(5, 0), new Coordinate(5, 1), game.getPlayerTurn())));
assertEquals(Owner.Nobody, game.getWinner());

assertTrue(game.move(new RegularMove(game.getBoardState(), new Coordinate(5, 1), new Coordinate(5, 2),
game.getPlayerTurn(), game.getNumMoves())));
assertTrue(game.move(new RegularMove(game, new Coordinate(5, 1), new Coordinate(5, 2), game.getPlayerTurn())));

assertTrue(game.hasNoMoves(Owner.Player1));
assertEquals(Owner.Player2, game.getWinner());
}

@Test
public void testPlayer1WinThroughtRabbitElimination() {
HashMap<Coordinate, AbstractPiece> p = new HashMap<Coordinate, AbstractPiece>();
Expand All @@ -183,11 +175,11 @@ public void testPlayer1WinThroughtRabbitElimination() {
Game game = new Game(new BoardState(p));
assertEquals(Owner.Nobody, game.getWinner());

assertTrue(game.move(new RegularMove(game.getBoardState(), new Coordinate(3, 3), new Coordinate(3, 4),
game.getPlayerTurn(), game.getNumMoves())));

assertTrue(game.move(new RegularMove(game, new Coordinate(3, 3), new Coordinate(3, 4), game.getPlayerTurn())));

assertEquals(Owner.Player1, game.getWinner());
}

@Test
public void testPlayer2WinThroughtRabbitElimination() {
HashMap<Coordinate, AbstractPiece> p = new HashMap<Coordinate, AbstractPiece>();
Expand All @@ -197,9 +189,8 @@ public void testPlayer2WinThroughtRabbitElimination() {
Game game = new Game(new BoardState(p));
assertEquals(Owner.Nobody, game.getWinner());

assertTrue(game.move(new RegularMove(game.getBoardState(), new Coordinate(3, 3), new Coordinate(3, 4),
game.getPlayerTurn(), game.getNumMoves())));

assertTrue(game.move(new RegularMove(game, new Coordinate(3, 3), new Coordinate(3, 4), game.getPlayerTurn())));

assertEquals(Owner.Player2, game.getWinner());
}

Expand All @@ -210,7 +201,7 @@ public void testPlayer2Win() {
Coordinate start = new Coordinate(1, 1);
Coordinate end = start.up();
Owner owner = game.getPlayerTurn();
MoveCommand move = new RegularMove(game.getBoardState(), start, end, owner, game.getNumMoves());
MoveCommand move = new RegularMove(game, start, end, owner);
assertTrue(game.move(move));
assertEquals(Owner.Player2, game.getWinner());
}
Expand All @@ -220,7 +211,7 @@ public void testPlayer1Win() {
Coordinate start = new Coordinate(1, 6);
Coordinate end = start.down();
Owner owner = game.getPlayerTurn();
MoveCommand move = new RegularMove(game.getBoardState(), start, end, owner, game.getNumMoves());
MoveCommand move = new RegularMove(game, start, end, owner);
assertTrue(game.move(move));
assertEquals(Owner.Player1, game.getWinner());
}
Expand All @@ -230,7 +221,7 @@ public void testWinWhenP1HasNoRabbits() {
Coordinate start = new Coordinate(1, 1);
Coordinate end = start.down();
Owner owner = game2.getPlayerTurn();
MoveCommand move = new RegularMove(game2.getBoardState(), start, end, owner, game2.getNumMoves());
MoveCommand move = new RegularMove(game2, start, end, owner);
game2.move(move);
assertEquals(Owner.Player2, game2.getWinner());
}
Expand All @@ -240,7 +231,7 @@ public void testCheckFriendlyAdjacentDownCase() {
Coordinate start = new Coordinate(5, 1);
Coordinate end = start.right();
Owner owner = game2.getPlayerTurn();
MoveCommand move = new RegularMove(game2.getBoardState(), start, end, owner, game2.getNumMoves());
MoveCommand move = new RegularMove(game2, start, end, owner);
assertTrue(game2.move(move));
}

Expand All @@ -250,42 +241,42 @@ public void testEndMove() {
Coordinate start = new Coordinate(0, 1);
Coordinate end = start.down();
Owner owner = g.getPlayerTurn();
MoveCommand move = new RegularMove(g.getBoardState(), start, end, owner, g.getNumMoves());
MoveCommand move = new RegularMove(g, start, end, owner);
assertTrue(g.move(move));
start = new Coordinate(0, 2);
end = start.down();
owner = g.getPlayerTurn();
move = new RegularMove(g.getBoardState(), start, end, owner, g.getNumMoves());
move = new RegularMove(g, start, end, owner);
assertTrue(g.move(move));
start = new Coordinate(0, 3);
end = start.down();
owner = g.getPlayerTurn();
move = new RegularMove(g.getBoardState(), start, end, owner, g.getNumMoves());
move = new RegularMove(g, start, end, owner);
assertTrue(g.move(move));
start = new Coordinate(0, 4);
end = start.down();
owner = g.getPlayerTurn();
move = new RegularMove(g.getBoardState(), start, end, owner, g.getNumMoves());
move = new RegularMove(g, start, end, owner);
assertTrue(g.move(move));
start = new Coordinate(1, 6);
end = start.up();
owner = g.getPlayerTurn();
move = new RegularMove(g.getBoardState(), start, end, owner, g.getNumMoves());
move = new RegularMove(g, start, end, owner);
assertTrue(g.move(move));
start = new Coordinate(1, 5);
end = start.up();
owner = g.getPlayerTurn();
move = new RegularMove(g.getBoardState(), start, end, owner, g.getNumMoves());
move = new RegularMove(g, start, end, owner);
assertTrue(g.move(move));
start = new Coordinate(1, 4);
end = start.up();
owner = g.getPlayerTurn();
move = new RegularMove(g.getBoardState(), start, end, owner, g.getNumMoves());
move = new RegularMove(g, start, end, owner);
assertTrue(g.move(move));
start = new Coordinate(1, 3);
end = start.up();
owner = g.getPlayerTurn();
move = new RegularMove(g.getBoardState(), start, end, owner, g.getNumMoves());
move = new RegularMove(g, start, end, owner);
assertTrue(g.move(move));
}
}
Loading

0 comments on commit 31cfee5

Please sign in to comment.