Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In Boardklasse pasten
- public Board(byte board[][]) {
- this.n = board.length;
- this.board = new byte[n][n];
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < n; j++) {
- doMove(new Position(j, i), board[i][j]);
- }
- }
- this.freeFields = n*n;
- }
- //Boardtest
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.Test;
- import java.io.ByteArrayOutputStream;
- import java.io.PrintStream;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.InputMismatchException;
- class BoardTest {
- @Test
- void testBoard() {
- assertThrows(InputMismatchException.class, () -> {
- new Board(0);
- });
- assertThrows(InputMismatchException.class, () -> {
- new Board(11);
- });
- }
- @Test
- void testGetN() {
- Board test = new Board(3);
- assertEquals(3, test.getN());
- test = new Board(1);
- assertEquals(1, test.getN());
- test = new Board(6);
- assertEquals(6, test.getN());
- }
- @Test
- void testNFreeFields() {
- int n = 3;
- Board test = new Board(n);
- test.doMove(new Position(0, 0), 1);
- assertEquals(n * n - 1, test.nFreeFields());
- test.undoMove(new Position(0, 0));
- assertEquals(n * n, test.nFreeFields());
- test.undoMove(new Position(0, 0));
- assertEquals(n * n, test.nFreeFields());
- test.doMove(new Position(0, 1), -1);
- assertEquals(n * n - 1, test.nFreeFields());
- test = new Board(1);
- assertEquals(1, test.nFreeFields());
- }
- @Test
- void testGetField() {
- Board test = new Board(2);
- test.doMove(new Position(1, 0), 1);
- assertEquals(test.getField(new Position(1, 0)), 1);
- test = new Board(new byte[][]{{0, 1}, {0, 0}});
- assertEquals(test.getField(new Position(1, 0)), 1);
- }
- @Test
- void testSetField() {
- Board test;
- test = new Board(new byte[][]{{1, -1}, {0, 0}});
- test.setField(new Position(1, 1), 1);
- test.setField(new Position(1, 0), 0);
- test.setField(new Position(0, 1), -1);
- assertEquals(1, test.getField(new Position(0, 0)));
- assertEquals(-1, test.getField(new Position(0, 1)));
- assertEquals(0, test.getField(new Position(1, 0)));
- assertEquals(1, test.getField(new Position(1, 1)));
- }
- @Test
- void testDoMove() {
- Board test = new Board(3);
- test.doMove(new Position(1, 0), -1);
- test.doMove(new Position(2, 2), 1);
- }
- @Test
- void testUndoMove() {
- Board test;
- test = new Board(new byte[][]{{0, -1}, {0, 0}});
- test.undoMove(new Position(1, 0));
- assertEquals(0, test.getField(new Position(1, 0)));
- }
- @Test
- void testIsGameWon() {
- testGame(new byte[][]{{0, 0, 0}, {1, 1, 1}, {0, 0, 0}}, true); // row
- testGame(new byte[][]{{0, 0, 1}, {0, 0, 1}, {0, 0, 1}}, true); // column
- testGame(new byte[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, true); // diagonal
- testGame(new byte[][]{{0, 0, 1}, {0, 1, 0}, {1, 0, 0}}, true); // diagonal
- testGame(new byte[][]{{-1, 0, 1}, {0, -1, 0}, {-1, 1, 0}}, false); // random
- }
- static void testGame(byte[][] board, boolean won) {
- Board test = new Board(board);
- assertEquals(won, test.isGameWon());
- }
- @Test
- void testValidMoves() {
- Board test = new Board(new byte[][]{{0, 0, 0}, {1, 0, 1}, {-1, -1, -1}});
- Collection<Position> valid = (Collection<Position>) test.validMoves();
- assertEquals(4, valid.size());
- }
- @Test
- void testPrint() {
- fail("Not yet implemented");
- }
- }
- //TicTacToeTest
- import static junit.framework.Assert.fail;
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.Test;
- import java.io.ByteArrayOutputStream;
- import java.io.PrintStream;
- class TicTacToeTest {
- @Test
- void testAlphaBetaBoardInt() {
- //1 == x, -1 == o
- // Win in one move
- boolean show1 = false;
- testEval(new byte[][]{{-1, 0, -1}, {0, 0, 0}, {0, 0, 0}}, -1, 7, show1);
- testEval(new byte[][]{{1, 0, 0}, {0, 0, 1}, {1, 0, 0}}, 1, 6, show1);
- testEval(new byte[][]{{1, 1, 0}, {0, 0, 0}, {0, 0, 1}}, 1, 6, show1);
- testEval(new byte[][]{{1, 0, 1}, {0, 1, -1}, {-1, 0, -1}}, 1, 3, show1); // Stellung (6)
- testEval(new byte[][]{{-1, 0, -1}, {0, 1, 0}, {-1, 0, 0}}, -1, 5, show1);
- testEval(new byte[][]{{1, 0}, {0, 0}}, -1, -2, show1);
- testEval(new byte[][]{{1, 0}, {0, 0}}, 1, 3, show1);
- // Lose in two moves
- boolean show2 = false;
- testEval(new byte[][]{{-1, 0, -1}, {0, 0, 0}, {-1, 0, 0}}, 1, -5, show2);
- testEval(new byte[][]{{1, 0, 1}, {0, 0, 0}, {1, 0, -1}}, -1, -4, show2);
- // Win in 3 moves
- testEval(new byte[][]{{1, 0, 0}, {0, 1, -1}, {0, 0, -1}}, 1, 3, false); // Stellung (4)
- // Lose in 4 moves
- testEval(new byte[][]{{1, 0, 0}, {0, 1, -1}, {0, 0, 0}}, -1, -3, false); // Stellung (3)
- testEval(new byte[][]{{0, 0, 0}, {0, 1, -1}, {0, 0, 0}}, 1, 3, false); // Stellung (2)
- // draw
- testEval(new byte[][]{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}, -1, 0, false); // Stellung (1)
- // Edge cases
- testEval(new byte[][]{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, -1, -1, false);
- testEval(new byte[][]{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, 1, 0, false);
- }
- static void testEval(byte[][] board, int playerToAct, int Evaluation, boolean print) {
- Board test = new Board(board);
- int eval = TicTacToe.alphaBeta(test, playerToAct);
- if (print) {
- System.out.println(test);
- if (playerToAct == 1) {
- System.out.println("X: " + eval);
- } else {
- System.out.println("O: " + eval);
- }
- System.out.println();
- }
- assertEquals(Evaluation, eval);
- }
- @Test
- void testEvaluatePossibleMoves() {
- // 1 == x, -1 == o
- Board test;
- test = new Board(new byte[][]{{0, 0}, {0, 0}});
- testEvalPossMoves(test, -1, "Evaluation for player 'o':\n 2 2\n 2 2\n");
- test = new Board(new byte[][]{{0, -1, 0}, {0, 0, 0}, {0, 0, 1}}); // Beispiel 1 (PDF)
- testEvalPossMoves(test, 1, "Evaluation for player 'x':\n 0 o 3\n 0 3 -2\n 3 0 x\n");
- test = new Board(3); // test case (results.txt)
- test.doMove(new Position(1, 0), -1);
- test.doMove(new Position(2, 2), 1);
- testEvalPossMoves(test, 1, "Evaluation for player 'x':\n 0 o 3\n 0 3 -2\n 3 0 x\n");
- test = new Board(3);
- testEvalPossMoves(test, 1, "Evaluation for player 'x':\n 0 0 0\n 0 0 0\n 0 0 0\n");
- }
- void testEvalPossMoves(Board test, int player, String expected) {
- final PrintStream originalOut = System.out;
- ByteArrayOutputStream outContent = new ByteArrayOutputStream();
- System.setOut(new PrintStream(outContent));
- TicTacToe.evaluatePossibleMoves(test, player);
- assertEquals(expected, outContent.toString());
- System.setOut(originalOut);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement