Skip to content

Commit 17c4be5

Browse files
authored
Merge pull request #26 from RedSoxFan/king_special_move_test
Adds missing tests for moving the king to special squares
2 parents acec42f + 1a6b18f commit 17c4be5

File tree

2 files changed

+283
-1
lines changed

2 files changed

+283
-1
lines changed

src/main/java/cowards/Board.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ private void handleEndMove(int row, int col) throws GridOutOfBoundsException {
296296
// Check to see if move was winning move.
297297
if (isGameOver()) {
298298
// King was captured.
299-
} else if (square(selRow, selCol).isKing() && inCornerLocation(row, col)) {
299+
} else if (square(row, col).isKing() && inCornerLocation(row, col)) {
300300
// If the king escaped we won.
301301
setGameOver(true);
302302
} else {

src/test/java/MovementTest.java

+282
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import static org.junit.Assert.*;
22

3+
import cowards.BadAsciiBoardFormatException;
34
import cowards.Board;
45
import cowards.GridOutOfBoundsException;
56
import org.junit.Test;
@@ -834,6 +835,287 @@ public void moveDownBarricadeAsDefenderTest() {
834835
return;
835836
}
836837

838+
/**
839+
* Retrieve a board with only one king.
840+
*/
841+
private Board getBoardWithOneKing() throws BadAsciiBoardFormatException {
842+
Board board = new Board();
843+
board.loadBoardFromChar(new char[][]{
844+
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
845+
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
846+
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
847+
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
848+
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
849+
{'K', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
850+
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
851+
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
852+
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
853+
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
854+
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}
855+
});
856+
return board;
857+
}
858+
859+
/**
860+
Verify that the king can move to the uppper left corner.
861+
*/
862+
@Test
863+
public void moveKingToTopLeft() {
864+
try {
865+
Board board = getBoardWithOneKing();
866+
board.setAttackerTurn(false);
867+
868+
board.select(5, 0);
869+
assertTrue(board.move(0, 0));
870+
} catch (BadAsciiBoardFormatException exception) {
871+
fail();
872+
} catch (GridOutOfBoundsException ex) {
873+
// This should not happen.
874+
System.out.println("A GridOutOfBoundsException has been thrown");
875+
fail();
876+
}
877+
878+
// Pass.
879+
return;
880+
}
881+
882+
/**
883+
Verify that the king can move to the uppper right corner.
884+
*/
885+
@Test
886+
public void moveKingToTopRight() {
887+
try {
888+
Board board = getBoardWithOneKing();
889+
board.setAttackerTurn(false);
890+
891+
board.select(5, 0);
892+
board.move(5, 10);
893+
894+
board.setAttackerTurn(false);
895+
896+
board.select(5, 10);
897+
assertTrue(board.move(0, 10));
898+
} catch (BadAsciiBoardFormatException exception) {
899+
fail();
900+
} catch (GridOutOfBoundsException ex) {
901+
// This should not happen.
902+
System.out.println("A GridOutOfBoundsException has been thrown");
903+
fail();
904+
}
905+
906+
// Pass.
907+
return;
908+
}
909+
910+
/**
911+
Verify that the king can move to the lower left corner.
912+
*/
913+
@Test
914+
public void moveKingToBottomLeft() {
915+
try {
916+
Board board = getBoardWithOneKing();
917+
board.setAttackerTurn(false);
918+
919+
board.select(5, 0);
920+
assertTrue(board.move(10, 0));
921+
} catch (BadAsciiBoardFormatException exception) {
922+
fail();
923+
} catch (GridOutOfBoundsException ex) {
924+
// This should not happen.
925+
System.out.println("A GridOutOfBoundsException has been thrown");
926+
fail();
927+
}
928+
929+
// Pass.
930+
return;
931+
}
932+
933+
/**
934+
Verify that the king can move to the lower right corner.
935+
*/
936+
@Test
937+
public void moveKingToBottomRight() {
938+
try {
939+
Board board = getBoardWithOneKing();
940+
board.setAttackerTurn(false);
941+
942+
board.select(5, 0);
943+
board.move(5, 10);
944+
945+
board.setAttackerTurn(false);
946+
947+
board.select(5, 10);
948+
assertTrue(board.move(10, 10));
949+
} catch (BadAsciiBoardFormatException exception) {
950+
fail();
951+
} catch (GridOutOfBoundsException ex) {
952+
// This should not happen.
953+
System.out.println("A GridOutOfBoundsException has been thrown");
954+
fail();
955+
}
956+
957+
// Pass.
958+
return;
959+
}
960+
961+
/**
962+
Verify that the king can move to the throne.
963+
*/
964+
@Test
965+
public void moveKingToThrone() {
966+
try {
967+
Board board = getBoardWithOneKing();
968+
board.setAttackerTurn(false);
969+
970+
board.select(5, 0);
971+
assertTrue(board.move(5, 5));
972+
} catch (BadAsciiBoardFormatException exception) {
973+
fail();
974+
} catch (GridOutOfBoundsException ex) {
975+
// This should not happen.
976+
System.out.println("A GridOutOfBoundsException has been thrown");
977+
fail();
978+
}
979+
980+
// Pass.
981+
return;
982+
}
983+
984+
/**
985+
Verify that the king causes a game over when moved the top left.
986+
*/
987+
@Test
988+
public void gameKingToTopLeft() {
989+
try {
990+
Board board = getBoardWithOneKing();
991+
board.setAttackerTurn(false);
992+
993+
board.select(5, 0);
994+
board.move(0, 0);
995+
996+
assertTrue(board.isGameOver());
997+
} catch (BadAsciiBoardFormatException exception) {
998+
fail();
999+
} catch (GridOutOfBoundsException ex) {
1000+
// This should not happen.
1001+
System.out.println("A GridOutOfBoundsException has been thrown");
1002+
fail();
1003+
}
1004+
1005+
// Pass.
1006+
return;
1007+
}
1008+
1009+
/**
1010+
Verify that the king causes a game over when moved to the top right.
1011+
*/
1012+
@Test
1013+
public void gameKingToTopRight() {
1014+
try {
1015+
Board board = getBoardWithOneKing();
1016+
board.setAttackerTurn(false);
1017+
1018+
board.select(5, 0);
1019+
board.move(5, 10);
1020+
1021+
board.setAttackerTurn(false);
1022+
1023+
board.select(5, 10);
1024+
board.move(0, 10);
1025+
1026+
assertTrue(board.isGameOver());
1027+
} catch (BadAsciiBoardFormatException exception) {
1028+
fail();
1029+
} catch (GridOutOfBoundsException ex) {
1030+
// This should not happen.
1031+
System.out.println("A GridOutOfBoundsException has been thrown");
1032+
fail();
1033+
}
1034+
1035+
// Pass.
1036+
return;
1037+
}
1038+
1039+
/**
1040+
Verify that the king causes a game over when moved to the bottom left.
1041+
*/
1042+
@Test
1043+
public void gameKingToBottomLeft() {
1044+
try {
1045+
Board board = getBoardWithOneKing();
1046+
board.setAttackerTurn(false);
1047+
1048+
board.select(5, 0);
1049+
board.move(10, 0);
1050+
1051+
assertTrue(board.isGameOver());
1052+
} catch (BadAsciiBoardFormatException exception) {
1053+
fail();
1054+
} catch (GridOutOfBoundsException ex) {
1055+
// This should not happen.
1056+
System.out.println("A GridOutOfBoundsException has been thrown");
1057+
fail();
1058+
}
1059+
1060+
// Pass.
1061+
return;
1062+
}
1063+
1064+
/**
1065+
Verify that the king causes a game over when moved to the bottom right.
1066+
*/
1067+
@Test
1068+
public void gameKingToBottomRight() {
1069+
try {
1070+
Board board = getBoardWithOneKing();
1071+
board.setAttackerTurn(false);
1072+
1073+
board.select(5, 0);
1074+
board.move(5, 10);
1075+
1076+
board.setAttackerTurn(false);
1077+
1078+
board.select(5, 10);
1079+
board.move(10, 10);
1080+
1081+
assertTrue(board.isGameOver());
1082+
} catch (BadAsciiBoardFormatException exception) {
1083+
fail();
1084+
} catch (GridOutOfBoundsException ex) {
1085+
// This should not happen.
1086+
System.out.println("A GridOutOfBoundsException has been thrown");
1087+
fail();
1088+
}
1089+
1090+
// Pass.
1091+
return;
1092+
}
1093+
1094+
/**
1095+
Verify that the king does not cause a game over when moved to the throne.
1096+
*/
1097+
@Test
1098+
public void noGameKingToThrone() {
1099+
try {
1100+
Board board = getBoardWithOneKing();
1101+
board.setAttackerTurn(false);
1102+
1103+
board.select(5, 0);
1104+
board.move(5, 5);
1105+
1106+
assertFalse(board.isGameOver());
1107+
} catch (BadAsciiBoardFormatException exception) {
1108+
fail();
1109+
} catch (GridOutOfBoundsException ex) {
1110+
// This should not happen.
1111+
System.out.println("A GridOutOfBoundsException has been thrown");
1112+
fail();
1113+
}
1114+
1115+
// Pass.
1116+
return;
1117+
}
1118+
8371119
/**
8381120
Verify attacking side cannot move to upper left corner.
8391121
*/

0 commit comments

Comments
 (0)