Advertisement
Guest User

Untitled

a guest
May 26th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.56 KB | None | 0 0
  1. //In Boardklasse pasten  
  2. public Board(byte board[][]) {
  3.         this.n = board.length;
  4.         this.board = new byte[n][n];
  5.         for(int i = 0; i < n; i++) {
  6.             for(int j = 0; j < n; j++) {
  7.                 doMove(new Position(j, i), board[i][j]);
  8.             }
  9.         }
  10.         this.freeFields = n*n;
  11.     }
  12.  
  13. //Boardtest
  14. import static org.junit.jupiter.api.Assertions.*;
  15. import org.junit.jupiter.api.Test;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.PrintStream;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collection;
  21. import java.util.InputMismatchException;
  22.  
  23. class BoardTest {
  24.  
  25.     @Test
  26.     void testBoard() {
  27.         assertThrows(InputMismatchException.class, () -> {
  28.             new Board(0);
  29.         });
  30.  
  31.         assertThrows(InputMismatchException.class, () -> {
  32.             new Board(11);
  33.         });
  34.     }
  35.  
  36.     @Test
  37.     void testGetN() {
  38.         Board test = new Board(3);
  39.         assertEquals(3, test.getN());
  40.  
  41.         test = new Board(1);
  42.         assertEquals(1, test.getN());
  43.  
  44.         test = new Board(6);
  45.         assertEquals(6, test.getN());
  46.     }
  47.  
  48.     @Test
  49.     void testNFreeFields() {
  50.         int n = 3;
  51.         Board test = new Board(n);
  52.         test.doMove(new Position(0, 0), 1);
  53.         assertEquals(n * n - 1, test.nFreeFields());
  54.         test.undoMove(new Position(0, 0));
  55.         assertEquals(n * n, test.nFreeFields());
  56.         test.undoMove(new Position(0, 0));
  57.         assertEquals(n * n, test.nFreeFields());
  58.         test.doMove(new Position(0, 1), -1);
  59.         assertEquals(n * n - 1, test.nFreeFields());
  60.  
  61.         test = new Board(1);
  62.         assertEquals(1, test.nFreeFields());
  63.     }
  64.  
  65.     @Test
  66.     void testGetField() {
  67.         Board test = new Board(2);
  68.         test.doMove(new Position(1, 0), 1);
  69.         assertEquals(test.getField(new Position(1, 0)), 1);
  70.  
  71.         test = new Board(new byte[][]{{0, 1}, {0, 0}});
  72.         assertEquals(test.getField(new Position(1, 0)), 1);
  73.     }
  74.  
  75.     @Test
  76.     void testSetField() {
  77.         Board test;
  78.         test = new Board(new byte[][]{{1, -1}, {0, 0}});
  79.         test.setField(new Position(1, 1), 1);
  80.         test.setField(new Position(1, 0), 0);
  81.         test.setField(new Position(0, 1), -1);
  82.  
  83.         assertEquals(1, test.getField(new Position(0, 0)));
  84.         assertEquals(-1, test.getField(new Position(0, 1)));
  85.         assertEquals(0, test.getField(new Position(1, 0)));
  86.         assertEquals(1, test.getField(new Position(1, 1)));
  87.     }
  88.  
  89.     @Test
  90.     void testDoMove() {
  91.         Board test = new Board(3);
  92.         test.doMove(new Position(1, 0), -1);
  93.         test.doMove(new Position(2, 2), 1);
  94.     }
  95.  
  96.     @Test
  97.     void testUndoMove() {
  98.         Board test;
  99.         test = new Board(new byte[][]{{0, -1}, {0, 0}});
  100.         test.undoMove(new Position(1, 0));
  101.         assertEquals(0, test.getField(new Position(1, 0)));
  102.     }
  103.  
  104.     @Test
  105.     void testIsGameWon() {
  106.         testGame(new byte[][]{{0, 0, 0}, {1, 1, 1}, {0, 0, 0}}, true);    // row
  107.         testGame(new byte[][]{{0, 0, 1}, {0, 0, 1}, {0, 0, 1}}, true);    // column
  108.         testGame(new byte[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, true);    // diagonal
  109.         testGame(new byte[][]{{0, 0, 1}, {0, 1, 0}, {1, 0, 0}}, true);    // diagonal
  110.         testGame(new byte[][]{{-1, 0, 1}, {0, -1, 0}, {-1, 1, 0}}, false);    // random
  111.     }
  112.  
  113.     static void testGame(byte[][] board, boolean won) {
  114.         Board test = new Board(board);
  115.         assertEquals(won, test.isGameWon());
  116.     }
  117.  
  118.     @Test
  119.     void testValidMoves() {
  120.         Board test = new Board(new byte[][]{{0, 0, 0}, {1, 0, 1}, {-1, -1, -1}});
  121.         Collection<Position> valid = (Collection<Position>) test.validMoves();
  122.         assertEquals(4, valid.size());
  123.     }
  124.  
  125.  
  126.     @Test
  127.     void testPrint() {
  128.         fail("Not yet implemented");
  129.     }
  130.  
  131. }
  132.  
  133.  
  134.  
  135. //TicTacToeTest
  136. import static junit.framework.Assert.fail;
  137. import static org.junit.jupiter.api.Assertions.*;
  138. import org.junit.jupiter.api.Test;
  139.  
  140. import java.io.ByteArrayOutputStream;
  141. import java.io.PrintStream;
  142.  
  143. class TicTacToeTest {
  144.  
  145.     @Test
  146.     void testAlphaBetaBoardInt() {
  147.         //1 == x, -1 == o
  148.         // Win in one move
  149.         boolean show1 = false;
  150.         testEval(new byte[][]{{-1, 0, -1}, {0, 0, 0}, {0, 0, 0}}, -1, 7, show1);
  151.         testEval(new byte[][]{{1, 0, 0}, {0, 0, 1}, {1, 0, 0}}, 1, 6, show1);
  152.         testEval(new byte[][]{{1, 1, 0}, {0, 0, 0}, {0, 0, 1}}, 1, 6, show1);
  153.         testEval(new byte[][]{{1, 0, 1}, {0, 1, -1}, {-1, 0, -1}}, 1, 3, show1);    // Stellung (6)
  154.         testEval(new byte[][]{{-1, 0, -1}, {0, 1, 0}, {-1, 0, 0}}, -1, 5, show1);
  155.         testEval(new byte[][]{{1, 0}, {0, 0}}, -1, -2, show1);
  156.         testEval(new byte[][]{{1, 0}, {0, 0}}, 1, 3, show1);
  157.         // Lose in two moves
  158.         boolean show2 = false;
  159.         testEval(new byte[][]{{-1, 0, -1}, {0, 0, 0}, {-1, 0, 0}}, 1, -5, show2);
  160.         testEval(new byte[][]{{1, 0, 1}, {0, 0, 0}, {1, 0, -1}}, -1, -4, show2);
  161.         // Win in 3 moves
  162.         testEval(new byte[][]{{1, 0, 0}, {0, 1, -1}, {0, 0, -1}}, 1, 3, false);    // Stellung (4)
  163.         // Lose in 4 moves
  164.         testEval(new byte[][]{{1, 0, 0}, {0, 1, -1}, {0, 0, 0}}, -1, -3, false);    // Stellung (3)
  165.         testEval(new byte[][]{{0, 0, 0}, {0, 1, -1}, {0, 0, 0}}, 1, 3, false);    // Stellung (2)
  166.         // draw
  167.         testEval(new byte[][]{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}, -1, 0, false);    // Stellung (1)
  168.         // Edge cases
  169.         testEval(new byte[][]{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, -1, -1, false);
  170.         testEval(new byte[][]{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, 1, 0, false);
  171.     }
  172.  
  173.     static void testEval(byte[][] board, int playerToAct, int Evaluation, boolean print) {
  174.         Board test = new Board(board);
  175.         int eval = TicTacToe.alphaBeta(test, playerToAct);
  176.         if (print) {
  177.             System.out.println(test);
  178.             if (playerToAct == 1) {
  179.                 System.out.println("X: " + eval);
  180.             } else {
  181.                 System.out.println("O: " + eval);
  182.             }
  183.             System.out.println();
  184.         }
  185.         assertEquals(Evaluation, eval);
  186.     }
  187.  
  188.     @Test
  189.     void testEvaluatePossibleMoves() {
  190.         // 1 == x, -1 == o
  191.         Board test;
  192.         test = new Board(new byte[][]{{0, 0}, {0, 0}});
  193.         testEvalPossMoves(test, -1, "Evaluation for player 'o':\n  2  2\n  2  2\n");
  194.  
  195.         test = new Board(new byte[][]{{0, -1, 0}, {0, 0, 0}, {0, 0, 1}});    // Beispiel 1 (PDF)
  196.         testEvalPossMoves(test, 1, "Evaluation for player 'x':\n  0  o  3\n  0  3 -2\n  3  0  x\n");
  197.  
  198.         test = new Board(3);    // test case (results.txt)
  199.         test.doMove(new Position(1, 0), -1);
  200.         test.doMove(new Position(2, 2), 1);
  201.         testEvalPossMoves(test, 1, "Evaluation for player 'x':\n  0  o  3\n  0  3 -2\n  3  0  x\n");
  202.  
  203.         test = new Board(3);
  204.         testEvalPossMoves(test, 1, "Evaluation for player 'x':\n  0  0  0\n  0  0  0\n  0  0  0\n");
  205.     }
  206.  
  207.     void testEvalPossMoves(Board test, int player, String expected) {
  208.         final PrintStream originalOut = System.out;
  209.         ByteArrayOutputStream outContent = new ByteArrayOutputStream();
  210.         System.setOut(new PrintStream(outContent));
  211.         TicTacToe.evaluatePossibleMoves(test, player);
  212.         assertEquals(expected, outContent.toString());
  213.         System.setOut(originalOut);
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement