GameManagerTest.java
package Tests;
import Game.*;
import Pieces.BoardSquare;
import Pieces.Pawn;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class GameManagerTest {
@Test
public void testHistory() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
BoardSquare square = new Pawn(1, 2, Player.BLACK);
game.addToMoveHist(1, 2, 3, 4, square);
Command command = (Command)game.MoveHist.pop();
assertEquals(command.getSourceX(), 1);
assertEquals(command.getSourceY(), 2);
assertEquals(command.getDestX(), 3);
assertEquals(command.getDestY(), 4);
assertEquals(command.getPlayer(), Player.BLACK);
}
@Test
public void testUndo() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
assertEquals(game.undo(), false);
assertEquals(game.Move(4, 1, 4, 3), GameManager.MoveStatus.NONE);
assertEquals(game.undo(), true);
assertEquals(game.Move(4, 1, 4, 3), GameManager.MoveStatus.NONE);
}
//@todo stalemate.
@Test
public void testStaleMate() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
CLI.Print(game.gameBoard);
assertEquals(game.Move(2, 1, 2, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(7, 6, 7, 4), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(7, 1, 7, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 6, 0, 4), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(3, 0, 0, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 7, 0, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 3, 0, 4), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 5, 7, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 4, 2, 6), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(5, 6, 5, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(2, 6, 3, 6), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 7, 5, 6), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(3, 6, 1, 6), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(3, 7, 3, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(1, 6, 1, 7), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(3, 2, 7, 6), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(1, 7, 2, 7), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(5, 6, 6, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(2, 7, 4, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.testStalemate(), true);
}
@Test
public void testMove() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
assertEquals(game.Move(0, 0, -55, -55), GameManager.MoveStatus.ERROR);
assertEquals(game.Move(-88, 88, 100, -55), GameManager.MoveStatus.ERROR);
assertEquals(game.Move(0, 0, 0, 0), GameManager.MoveStatus.ERROR);
}
@Test
public void testValidMove() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
assertEquals(game.validMove(0, 0, -55, -55), GameManager.MoveStatus.ERROR);
assertEquals(game.validMove(-88, 88, 100, -55), GameManager.MoveStatus.ERROR);
assertEquals(game.validMove(0, 0, 0, 0), GameManager.MoveStatus.ERROR);
assertEquals(game.validMove(4, 4, 4, 3), GameManager.MoveStatus.ERROR);
}
//@new test
@Test
public void testPawnHop() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 1, 4, 3), GameManager.MoveStatus.NONE);
game.nextTurn();
CLI.Print(game.gameBoard);
assertEquals(game.Move(3, 0, 5, 2), GameManager.MoveStatus.NONE);
game.nextTurn();
CLI.Print(game.gameBoard);
assertEquals(game.Move(5, 2, 5, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(5, 6, 5, 4), GameManager.MoveStatus.ERROR);
}
//@new test
@Test
public void testKingLimit() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 1, 4, 3), GameManager.MoveStatus.NONE);
game.nextTurn();
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 0, 4, 1), GameManager.MoveStatus.NONE);
game.nextTurn();
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 1, 4, 2), GameManager.MoveStatus.NONE);
game.nextTurn();
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 2, 4, 0), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 2, 0, 2), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 2, 1, 5), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 2, 7, 4), GameManager.MoveStatus.ERROR);
}
//@new test, testing for checkmate was inadvertently moving pieces.
@Test
public void testCheckMate_notInCheckMate() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
//boolean firstmove = true;
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 1, 4, 3), GameManager.MoveStatus.NONE);
assertEquals(game.testCheck(true), Player.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 6, 0, 5), GameManager.MoveStatus.NONE);
assertEquals(game.testCheck(true), Player.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(5, 0, 2, 3), GameManager.MoveStatus.NONE);
assertEquals(game.testCheck(true), Player.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 5, 0, 4), GameManager.MoveStatus.NONE);
assertEquals(game.testCheck(true), Player.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(3, 0, 5, 2), GameManager.MoveStatus.NONE);
assertEquals(game.testCheck(true), Player.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 4, 0, 3), GameManager.MoveStatus.NONE);
assertEquals(game.testCheck(true), Player.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(2, 3, 5, 6), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
assertEquals(game.testCheck(false), Player.BLACK);
assertEquals(game.Move(0, 3, 0, 2), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
//game.testCheck(true);
assertEquals(game.testCheck(true), Player.BLACK);
CLI.Print(game.gameBoard);
}
@Test
public void testCheckMate() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
//boolean firstmove = true;
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 1, 4, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 6, 0, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(5, 0, 2, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 5, 0, 4), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(3, 0, 5, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 4, 0, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(2, 3, 5, 6), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
assertEquals(game.testCheck(false), Player.BLACK);
assertEquals(game.Move(0, 3, 0, 2), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
//game.testCheck(true);
assertEquals(game.testCheck(true), Player.BLACK);
CLI.Print(game.gameBoard);
System.out.println("Done.");
}
@Test
public void testCheck() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
//boolean firstmove = true;
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 1, 4, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 6, 0, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(5, 0, 2, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 5, 0, 4), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(3, 0, 5, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 6, 4, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(2, 3, 4, 5), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 4, 0, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 5, 5, 6), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
assertEquals(game.testCheck(false), Player.BLACK);
assertEquals(game.Move(0, 3, 0, 2), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
//game.testCheck(true);
assertEquals(game.testCheck(true), Player.NONE);
CLI.Print(game.gameBoard);
System.out.println("Done.");
}
@Test
public void testMoveIntoCheck() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
//boolean firstmove = true;
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 1, 4, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 6, 0, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(5, 0, 2, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 5, 0, 4), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(3, 0, 5, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 4, 0, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(5, 2, 5, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.testCheck(false), Player.NONE);
assertEquals(game.Move(4, 6, 4, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 1, 0, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 7, 4, 6), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
//game.testCheck(true);
assertEquals(game.testCheck(true), Player.NONE);
CLI.Print(game.gameBoard);
System.out.println("Done.");
}
@Test
public void testFindPieces() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
//int ret;
BoardSquare startSquare = game.gameBoard.getPiece(0, 0);
BoardSquare destSquare = game.gameBoard.getPiece(7, 7);
Assert.assertTrue(game.gameBoard.findPieces(startSquare, destSquare, 6));
startSquare = game.gameBoard.getPiece(0, 0);
destSquare = game.gameBoard.getPiece(7, 7);
Assert.assertTrue(game.gameBoard.findPieces(startSquare, destSquare, 2));
startSquare = game.gameBoard.getPiece(0, 0);
destSquare = game.gameBoard.getPiece(0, 0);
Assert.assertFalse(game.gameBoard.findPieces(startSquare, destSquare, 1));
startSquare = game.gameBoard.getPiece(0, 0);
destSquare = game.gameBoard.getPiece(0, 7);
Assert.assertTrue(game.gameBoard.findPieces(startSquare, destSquare, 2));
startSquare = game.gameBoard.getPiece(0, 0);
destSquare = game.gameBoard.getPiece(0, 7);
Assert.assertTrue(game.gameBoard.findPieces(startSquare, destSquare, 6));
startSquare = game.gameBoard.getPiece(7, 7);
destSquare = game.gameBoard.getPiece(0, 0);
Assert.assertTrue(game.gameBoard.findPieces(startSquare, destSquare, 2));
startSquare = game.gameBoard.getPiece(7, 7);
destSquare = game.gameBoard.getPiece(0, 0);
Assert.assertTrue(game.gameBoard.findPieces(startSquare, destSquare, 6));
startSquare = game.gameBoard.getPiece(7, 0);
destSquare = game.gameBoard.getPiece(0, 7);
Assert.assertTrue(game.gameBoard.findPieces(startSquare, destSquare, 6));
startSquare = game.gameBoard.getPiece(0, 7);
destSquare = game.gameBoard.getPiece(7, 0);
Assert.assertTrue(game.gameBoard.findPieces(startSquare, destSquare, 6));
startSquare = game.gameBoard.getPiece(0, 0);
destSquare = game.gameBoard.getPiece(7, 0);
Assert.assertTrue(game.gameBoard.findPieces(startSquare, destSquare, 6));
}
@Test
public void testKnightMove() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
assertEquals(game.Move(1, 0, 0, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(0, 2, 2, 1), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
//game.playerTurn = ! game.playerTurn;
assertEquals(game.Move(0, 2, 1, 0), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(1, 0, 2, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(2, 2, 1, 0), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(1, 0, 2, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(2, 2, 4, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(4, 3, 2, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(2, 2, 0, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(0, 3, 1, 1), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 3, 2, 4), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(2, 4, 4, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(4, 3, 2, 4), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(2, 4, 0, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
}
@Test
public void testPawnMove() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
assertEquals(game.Move(0, 1, 0, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 6, 0, 4), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(1, 1, 1, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(1, 6, 1, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(1, 2, 1, 4), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
assertEquals(game.Move(1, 2, 1, 1), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
}
@Test
public void testPawnCapture() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
assertEquals(game.Move(0, 1, 0, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(1, 6, 1, 4), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(0, 3, 1, 4), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
}
@Test
public void testBerolinaPawnMove_andCapture() throws Exception {
GameManager game = new GameManager(GameType.CUSTOM);
assertEquals(game.Move(0, 1, 0, 3), GameManager.MoveStatus.ERROR);
assertEquals(game.Move(0, 1, 3, 4), GameManager.MoveStatus.ERROR);
assertEquals(game.Move(0, 1, 2, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 6, 3, 5), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(2, 3, 0, 5), GameManager.MoveStatus.ERROR);
assertEquals(game.Move(2, 3, 3, 2), GameManager.MoveStatus.ERROR);
assertEquals(game.Move(2, 3, 2, 2), GameManager.MoveStatus.ERROR);
assertEquals(game.Move(1, 1, 1, 2), GameManager.MoveStatus.ERROR);
assertEquals(game.Move(1, 1, 0, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(3, 5, 2, 4), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
assertEquals(game.Move(2, 3, 2, 4), GameManager.MoveStatus.CAPTURE);
CLI.Print(game.gameBoard);
}
@Test
public void testBishopMove() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
assertEquals(game.Move(1, 1, 1, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(2, 0, 0, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
}
@Test
public void testQueenMove() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
assertEquals(game.Move(2, 1, 2, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(3, 0, 0, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
}
@Test
public void testKingMove() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
assertEquals(game.Move(3, 1, 3, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(4, 0, 0, 4), GameManager.MoveStatus.ERROR);
CLI.Print(game.gameBoard);
assertEquals(game.Move(4, 0, 3, 1), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
}
@Test
public void testRookMove() throws Exception {
GameManager game = new GameManager(GameType.STANDARD);
assertEquals(game.Move(0, 1, 0, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(0, 0, 0, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(0, 2, 7, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
}
@Test
public void testWazirMove() throws Exception {
GameManager game = new GameManager(GameType.CUSTOM);
assertEquals(game.Move(0, 1, 2, 3), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(0, 0, 0, 2), GameManager.MoveStatus.ERROR);
assertEquals(game.Move(0, 0, 0, 1), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(0, 1, 0, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
game.nextTurn();
assertEquals(game.Move(0, 2, 2, 2), GameManager.MoveStatus.ERROR);
assertEquals(game.Move(0, 2, 1, 2), GameManager.MoveStatus.NONE);
CLI.Print(game.gameBoard);
}
}