/* * GameTest.java * JUnit based test * * Created on February 10, 2007, 7:11 PM */ package chess; import java.util.Hashtable; import java.util.Vector; import junit.framework.*; import javax.crypto.NullCipher; /** * * @author Stanislav Hromek */ public class GameTest extends TestCase { public GameTest(String testName) { super(testName); } protected void setUp() throws Exception { } protected void tearDown() throws Exception { } public static Test suite() { TestSuite suite = new TestSuite(GameTest.class); return suite; } public void testMain() { System.out.println("main"); String[] args = {"1. e2-e4 e7-e5", "2. d2-d4 d7-d5"}; Game.main(args); } public void testPlayGame() { System.out.println("playGame"); Game game = new Game(); // Capablanca - Marshall, New York 1918 String[] lines = {"1. e2-e4 e7-e5", "2. Kng1-f3 Knb8-c6", "3. Bf1-b5 a7-a6", "4. Bb5-a4 Kng8-f6", "5. 0-0 Bf8-e7", "6. Rf1-e1 b7-b5", "7. Ba4-b3 0-0", "8. c2-c3 d7-d5", "9. e4xd5 Knf6xd5", "10. Knf3xe5 Knc6xe5", "11. Re1xe5 Knd5-f6", "12. Re5-e1 Be7-d6", "13. h2-h3 Knf6-g4", "14. Qd1-f3 Qd8-h4", "15. d2-d4! Kng4xf2", "16. Re1-e2! Bc8-g4", "17. h3xg4 Bd6-h2+", "18. Kg1-f1 Bh2-g3", "19. Re2xf2 Qh4-h1+", "20. Kf1-e2 Bg3xf2", "21. Bc1-d2 Bf2-h4", "22. Qf3-h3 Ra8-e8+", "23. Ke2-d3 Qh1-f1+", "24. Kd3-c2 Bh4-f2", "25. Qh3-f3 Qf1-g1", "26. Bb3-d5 c7-c5", "27. d4xc5 Bf2xc5", "28. b2-b4 Bc5-d6", "29. a2-a4 a6-a5", "30. a4xb5 a5xb4", "31. Ra1-a6 b4xc3", "32. Knb1xc3 Bd6-b4", "33. b5-b6 Bb4xc3", "34. Bd2xc3 h7-h6", "35. b6-b7 Re8-e3", "36. Bd5xf7+"}; // todo, if only one in line game.startNewGame(lines); boolean result = game.playGame(); assertEquals(true, result); } public void testPlayGame2() { System.out.println("playGame2"); Game game = new Game(); // Steinitz-Von Bardeleben, Hastings 1895 String[] lines = {"1. e2-e4 e7-e5", "2. Kng1-f3 Knb8-c6", "3. Bf1-c4 Bf8-c5", "4. c2-c3 Kng8-f6", "5. d2-d4 e5xd4", "6. c3xd4 Bc5-b4+", "7. Knb1-c3 d7-d5", "8. e4xd5 Knf6xd5", "9. 0-0 Bc8-e6", "10. Bc1-g5 Bb4-e7", "11. Bc4xd5 Be6xd5", "12. Knc3xd5 Qd8xd5", "13. Bg5xe7 Knc6xe7", "14. Rf1-e1 f7-f6", "15. Qd1-e2 Qd5-d7", "16. Ra1-c1 c7-c6?", "17. d4-d5! c6xd5", "18. Knf3-d4 Ke8-f7", "19. Knd4-e6 Rh8-c8", "20. Qe2-g4 g7-g6", "21. Kne6-g5+ Kf7-e8", "22. Re1xe7+!! Ke8-f8", "23. Re7-f7+! Kf8-g8", "24. Rf7-g7+! Kg8-h8", "25. Rg7xh7+"}; game.startNewGame(lines); boolean result = game.playGame(); assertEquals(true, result); } public void testGetBoard() { System.out.println("getBoard"); Game instance = new Game(); // nothing to test // Board expResult = null; // Board result = instance.getBoard(); // assertEquals(expResult, result); } public void testCreateMoves() { System.out.println("createMoves"); Move[] result; String[] lines = new String[2]; Game instance = new Game(); result = null; lines[0] = "1. e2-e4 e7-e5"; lines[1] = "2. Kng1-f3 Knb8-c6"; result = instance.createMoves(lines); assertEquals(2, result.length); result = null; lines[0] = "1. e2-e4 e7-e5"; lines[1] = "2. Kng1-f3 "; result = instance.createMoves(lines); assertEquals(2, result.length); result = null; lines[0] = "1.xe2-e4xe7-e5"; lines[1] = "2. Kng1-f3 "; result = instance.createMoves(lines); assertEquals(2, result.length); assertEquals(null, result[0]); } public void testIsEnd() { System.out.println("isEnd"); Move[] result; String[] lines = new String[2]; Game instance = new Game(); result = null; lines[0] = "1. e2-e4 e7-e5"; lines[1] = "2. Kng1-f3 Knb8-c6"; instance.createMoves(lines); instance.move = 0; instance.pieceColor = "white"; assertEquals(false, instance.isEnd()); result = null; lines[0] = "1. e2-e4 e7-e5"; lines[1] = "2. Kng1-f3 Rd5-d6"; result = instance.createMoves(lines); instance.move = 1; instance.pieceColor = "black"; assertEquals(false, instance.isEnd()); result = null; lines[0] = "1.xe2-e4xe7-e5"; lines[1] = "2. Kng1-f3 "; result = instance.createMoves(lines); instance.move = 0; instance.pieceColor = "white"; assertEquals(true, instance.isEnd()); result = null; lines[0] = "1.xe2-e4xe7-e5"; lines[1] = "2. Kng1-f3 "; result = instance.createMoves(lines); instance.move = 1; instance.pieceColor = "black"; assertEquals(true, instance.isEnd()); } public void testStartNewGame() { System.out.println("startNewGame"); String[] lines; Game instance = new Game(); lines = new String[2]; lines[0] = "1. e2-e4 e7-e5"; lines[1] = "2. Kng1-f3 Knb8-c6"; boolean result = instance.startNewGame(lines); assertEquals(true, result); // what is entry, if no args in main, String[0] ?, will be true anyway // lines = new String[0]; // result = instance.startNewGame(lines); // assertEquals(false, result); } public void testIsMoveOk() { System.out.println("isMoveOk"); Move[] result; String[] lines = new String[2]; Game instance = new Game(); result = null; lines[0] = "1. e2-e4 e7-e5"; lines[1] = "2. Kng1-f3 Rd5-d6"; result = instance.createMoves(lines); instance.move = 1; instance.pieceColor = "black"; assertEquals(true, instance.isMoveOk()); result = null; lines[0] = "1.xe2-e4xe7-e5"; lines[1] = "2. Kng1-f3 "; result = instance.createMoves(lines); instance.move = 0; instance.pieceColor = "white"; assertEquals(false, instance.isMoveOk()); } public void testIsMoveOk2() { System.out.println("isMoveOk2"); Move[] result; String[] lines = new String[2]; Game instance = new Game(); result = null; lines[0] = "1. e2-e4 e7-e5"; lines[1] = "2. Kng1-f3 Rd5-d6"; result = instance.createMoves(lines); instance.move = 1; instance.pieceColor = "black"; assertEquals(true, instance.isMoveOk2()); result = null; lines[0] = "1.xe2-e4xe7-e5"; lines[1] = "2. Kng1-f3 "; result = instance.createMoves(lines); instance.move = 1; instance.pieceColor = "black"; assertEquals(false, instance.isMoveOk2()); } public void testPlayOneMove() { System.out.println("playOneMove"); Move[] result; String[] lines = new String[2]; Game game = new Game(); game.getBoard().getSquare("d5").setPiece(new Rook("black")); result = null; lines[0] = "1. e2-e4 e7-e5"; lines[1] = "2. Kng1-f3 Rd5-d6"; result = game.createMoves(lines); game.move = 1; game.pieceColor = "black"; assertEquals(true, game.playOneMove()); result = null; lines[0] = "1.xe2-e4xe7-e5"; lines[1] = "2. Kng1-h8 "; result = game.createMoves(lines); game.move = 1; game.pieceColor = "white"; assertEquals(false, game.playOneMove()); } public void testParsePgnChessGame() { System.out.println("parsePgnChessGame"); Game instance = new Game(); Vector pgnGame = new Vector(); pgnGame.add("[Event \"Vienna\"]"); pgnGame.add("[Site \"Vienna\"]"); pgnGame.add("[Date \"1908.??.??\"]"); pgnGame.add("[Round \"1\"]"); pgnGame.add("[White \"Alapin, Simon\"]"); pgnGame.add("[Black \"Reti, Richard\"]"); pgnGame.add("[Result \"1-0\"]"); pgnGame.add("[WhiteElo \"\"]"); pgnGame.add("[BlackElo \"\"]"); pgnGame.add("[ECO \"C86\"]"); pgnGame.add("\n"); pgnGame.add("1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 Nf6 5.O-O Be7 6.Qe2 d6 7.c3 Bd7 8.d4 O-O"); pgnGame.add("9.Bc2 Re8 10.d5 Nb8 11.h3 Bf8 12.c4 h6 13.Nc3 g6 14.Be3 Qe7 15.Qd2 Kh7 16.Ne1 Ng8"); pgnGame.add("17.f4 exf4 18.Rxf4 Bc8 19.Qf2 f6 20.Bd4 Bg7 21.Nd3 Nd7 22.Rf3 Ne5 23.Nxe5 fxe5"); pgnGame.add("24.Rf7 Qd8 25.Be3 Rf8 26.c5 Nf6 27.Rxf8 Qxf8 28.Rf1 g5 29.cxd6 Qxd6 30.Bc5 Qd8"); pgnGame.add("31.Bd1 Bd7 32.Qc2 Be8 33.Bf3 Bg6 34.Qe2 Bf8 35.Bxf8 Qxf8 36.Qc4 Qd6 37.b4 Rf8"); pgnGame.add("38.a3 Nd7 39.Bg4 Nb6 40.Qc5 Rxf1+ 41.Kxf1 Be8 42.Qxd6 cxd6 43.Ke2 Kg7 44.Kd3 Kf6"); pgnGame.add("45.Nd1 h5 46.Be6 Bf7 47.Bxf7 Kxf7 48.Ne3 Kf6 49.g4 h4 50.Nf5 Nc8 51.b5 axb5"); pgnGame.add("52.Kc3 Ne7 53.Nxe7 Kxe7 54.Kb4 Kd7 55.Kxb5 Kc7 56.a4 Kc8 57.Kb6 Kb8 58.a5 Kc8"); pgnGame.add("59.a6 Kb8 60.a7+ Ka8 61.Kc7 b5 62.Kxd6 b4 63.Kc6 1-0"); String[] in = (String[])pgnGame.toArray(new String[0]); String[] result = instance.parsePgnChessGame(in); assertEquals("1", "[Event \"Vienna\"]" , result[0]); // "Event" assertEquals("2", "[Site \"Vienna\"]" , result[1]); // "Site" assertEquals("3", "[Date \"1908.??.??\"]" , result[2]); // "Date" assertEquals("4", "[Round \"1\"]" , result[3]); // "Round" assertEquals("5", "[White \"Alapin, Simon\"]", result[4]); // "White" assertEquals("6", "[Black \"Reti, Richard\"]", result[5]); // "Black" assertEquals("7", "[Result \"1-0\"]" , result[6]); // "Result" assertEquals("8", "[WhiteElo \"\"]" , result[7]); // "WhiteElo" assertEquals("9", "[BlackElo \"\"]" , result[8]); // "BlackElo" assertEquals("10", "[ECO \"C86\"]" , result[9]); // "ECO" Vector temp = new Vector(0); String out = "1.e4 e5 " + "2.Nf3 Nc6 " + "3.Bb5 a6 " + "4.Ba4 Nf6 " + "5.O-O Be7 " + "6.Qe2 d6 " + "7.c3 Bd7 " + "8.d4 O-O " + "9.Bc2 Re8 " + "10.d5 Nb8 " + "11.h3 Bf8 " + "12.c4 h6 " + "13.Nc3 g6 " + "14.Be3 Qe7 " + "15.Qd2 Kh7 " + "16.Ne1 Ng8 " + "17.f4 exf4 " + "18.Rxf4 Bc8 " + "19.Qf2 f6 " + "20.Bd4 Bg7 " + "21.Nd3 Nd7 " + "22.Rf3 Ne5 " + "23.Nxe5 fxe5 " + "24.Rf7 Qd8 " + "25.Be3 Rf8 " + "26.c5 Nf6 " + "27.Rxf8 Qxf8 " + "28.Rf1 g5 " + "29.cxd6 Qxd6 " + "30.Bc5 Qd8 " + "31.Bd1 Bd7 " + "32.Qc2 Be8 " + "33.Bf3 Bg6 " + "34.Qe2 Bf8 " + "35.Bxf8 Qxf8 " + "36.Qc4 Qd6 " + "37.b4 Rf8 " + "38.a3 Nd7 " + "39.Bg4 Nb6 " + "40.Qc5 Rxf1+ " + "41.Kxf1 Be8 " + "42.Qxd6 cxd6 " + "43.Ke2 Kg7 " + "44.Kd3 Kf6 " + "45.Nd1 h5 " + "46.Be6 Bf7 " + "47.Bxf7 Kxf7 " + "48.Ne3 Kf6 " + "49.g4 h4 " + "50.Nf5 Nc8 " + "51.b5 axb5 " + "52.Kc3 Ne7 " + "53.Nxe7 Kxe7 " + "54.Kb4 Kd7 " + "55.Kxb5 Kc7 " + "56.a4 Kc8 " + "57.Kb6 Kb8 " + "58.a5 Kc8 " + "59.a6 Kb8 " + "60.a7+ Ka8 " + "61.Kc7 b5 " + "62.Kxd6 b4 " + "63.Kc6 " + "1-0"; // loop ? // for (int i = 0; i < in.length; i++) { assertEquals("11", out, result[10] ); // } } public void testDivideGameToLines() { System.out.println("divideGameToLines"); String game = "1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 Nf6 5.O-O Be7 6.Qe2 d6 7.c3 Bd7 8.d4 O-O " + "9.Bc2 Re8 10.d5 Nb8 11.h3 Bf8 12.c4 h6 13.Nc3 g6 14.Be3 Qe7 15.Qd2 Kh7 16.Ne1 Ng8 " + "17.f4 exf4 18.Rxf4 Bc8 19.Qf2 f6 20.Bd4 Bg7 21.Nd3 Nd7 22.Rf3 Ne5 23.Nxe5 fxe5 " + "24.Rf7 Qd8 25.Be3 Rf8 26.c5 Nf6 27.Rxf8 Qxf8 28.Rf1 g5 29.cxd6 Qxd6 30.Bc5 Qd8 " + "31.Bd1 Bd7 32.Qc2 Be8 33.Bf3 Bg6 34.Qe2 Bf8 35.Bxf8 Qxf8 36.Qc4 Qd6 37.b4 Rf8 " + "38.a3 Nd7 39.Bg4 Nb6 40.Qc5 Rxf1+ 41.Kxf1 Be8 42.Qxd6 cxd6 43.Ke2 Kg7 44.Kd3 Kf6 " + "45.Nd1 h5 46.Be6 Bf7 47.Bxf7 Kxf7 48.Ne3 Kf6 49.g4 h4 50.Nf5 Nc8 51.b5 axb5 " + "52.Kc3 Ne7 53.Nxe7 Kxe7 54.Kb4 Kd7 55.Kxb5 Kc7 56.a4 Kc8 57.Kb6 Kb8 58.a5 Kc8 " + "59.a6 Kb8 60.a7+ Ka8 61.Kc7 b5 62.Kxd6 b4 63.Kc6 1-0"; Game instance = new Game(); String[] result = instance.divideGameToLines(game); assertEquals("1", 64, result.length); assertEquals("2", "1.e4 e5", result[0]); assertEquals("63", "62.Kxd6 b4", result[61]); assertEquals("64", "63.Kc6 ", result[62]); assertEquals("65", "1-0", result[63]); } // public void testParsePgnChessGames() { // System.out.println("parsePgnChessGames"); // // Vector pgnGame = new Vector(); // pgnGame.add("[BlackElo \"\"]"); // pgnGame.add("[ECO \"C86\"]"); // pgnGame.add("\n"); // pgnGame.add("1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 Nf6 5.O-O Be7 6.Qe2 d6 7.c3 Bd7 8.d4 O-O"); // pgnGame.add("[BlackElo \"\"]"); // pgnGame.add("[ECO \"C86\"]"); // pgnGame.add("\n"); // pgnGame.add("1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 Nf6 5.O-O Be7 6.Qe2 d6 7.c3 Bd7 8.d4 O-O"); // pgnGame.add("[BlackElo \"\"]"); // pgnGame.add("[ECO \"C86\"]"); // pgnGame.add("\n"); // pgnGame.add("1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 Nf6 5.O-O Be7 6.Qe2 d6 7.c3 Bd7 8.d4 O-O"); // String[] pgnGames = (String[])pgnGame.toArray(new String[0]); // Game instance = new Game(); // // Vector result = instance.parsePgnChessGames(pgnGames); // assertEquals("", 3, result.size()); // } /** * test full game with short notation * */ public void testPlayGame3() { System.out.println("playGame3"); Game game = new Game(); // pgn format // Capablanca - Marshall, New York 1918 String[] lines = {"1. e4 e5", "2. Knf3 Knc6", "3. Bb5 a6", "4. Ba4 Knf6", "5. 0-0 Be7", "6. Re1 b5", "7. Bb3 0-0", "8. c3 d5", "9. exd5 Knd5", "10. Kne5 Kne5", "11. Re5 Knf6", "12. Re1 Bd6", "13. h3 Kng4", "14. Qf3 Qh4", "15. d4! Knf2", "16. Re2! Bg4", "17. hxg4 Bh2+", "18. Kf1 Bg3", "19. Rxf2 Qh1+", "20. Ke2 Bxf2", "21. Bd2 Bh4", "22. Qh3 Rae8+", "23. Kd3 Qf1+", "24. Kc2 Bf2", "25. Qf3 Qg1", "26. Bd5 c5", "27. dxc5 Bxc5", "28. b4 Bd6", "29. a4 a5", "30. axb5 axb4", "31. Ra6 bxc3", "32. Knxc3 Bb4", "33. b6 Bxc3", "34. Bxc3 h6", "35. b7 Re3", "36. Bxf7+"}; // todo, if only one in line game.startNewGame(lines); boolean result = game.playGame(); System.out.println("Game.errorMessage = " + game.errorMessage); assertEquals(true, result); } public void testPlayGame4() { System.out.println("playGame4"); Move.setPiecesSymbols("K", "Q", "R", "B", "N", ""); Game game = new Game(); // Von Doery - Reti String[] lines = {"1.d4 Nf6", "2.c4 e6", "3.Nc3 Bb4", "4.Qc2 O-O", "5.e4 c5", "6.dxc5 Bxc3+", "7.bxc3 Qa5", "8.Bd3 Qxc5", "9.Be3 Qc7", "10.Nf3 d6", "11.Nd4 Nbd7", "12.Nb5 Qb8", "13.Rd1 a6", "14.Nd4 Qc7", "15.f4 Nc5", "16.O-O e5", "17.Nb3 Nxd3", "18.Qxd3 Be6", "19.Qxd6 Qxd6", "20.Rxd6 Nxe4", "21.Rb6 Bxc4", "22.Rc1 exf4", "23.Bxf4 a5", "24.a3 Bxb3", "25.Rxb3 Nc5", "26.Rb5 Rfc8", "27.Be3 Nd3", "28.Rc2 Re8", "29.Bf2 Re7", "30.Rd2 Nxf2", "31.Kxf2 h6", "32.Rdb2 Rc8", "33.Rxa5 Rxc3", "34.Ra7 Rcc7", "35.a4 g6", "36.a5 Kg7", "37.a6 Rc6", "38.g3 Rxa6", "39.Raxb7 Rxb7", "40.Rxb7 Ra2+", "41.Kg1 g5", "42.Rb1 Kg6", "43.Rf1 f5", "44.Rf2 Rxf2", "45.Kxf2 Kf6", "46.Ke3 Ke5", "47.Kf3 Kd4", "48.Ke2 Ke4", "49.Kf2 Kd3", "50.Kf3 h5", "51.h4 g4+", "52.Kf4 Ke2", "53.Kg5 f4", "54.gxf4 g3", "55.Kxh5 g2", "56.f5 g1=Q", "57.f6 Qg8", "58.Kh6 Ke3", "59.h5 Ke4", "60.f7 Qxf7", "0-1"}; // game.startNewGame(lines); boolean result = game.playGame(); System.out.println("Game.errorMessage = " + game.errorMessage); assertEquals(true, result); } }