src/demo/TicTacToe.java
private void playGame() throws MatrixException, NeuralNetworkException, DynamicParamException, AgentException {
        PlayerRole currentHumanPlayerRole = humanPlayerRole;

        currentPlayerList = new ArrayList<>();

        int players = playerList.size();
        int noughtIndex;
        int crossIndex;

        if (players % 2 == 0) noughtIndex = 2 * random.nextInt(players / 2);
        else noughtIndex = 2 * random.nextInt((players + 1) / 2);
        crossIndex = 2 * random.nextInt(players / 2) + 1;

        currentPlayerList.add(playerList.get(noughtIndex));
        currentPlayerList.get(0).setPlayerRole(PlayerRole.NOUGHT);
        currentPlayerList.add(playerList.get(crossIndex));
        currentPlayerList.get(1).setPlayerRole(PlayerRole.CROSS);

        currentPlayerList.get(0).setAsHuman(currentHumanPlayerRole == currentPlayerList.get(0).getPlayerRole());
        currentPlayerList.get(1).setAsHuman(currentHumanPlayerRole == currentPlayerList.get(1).getPlayerRole());

        gameBoard = new GameBoard();
        gameStatus = GameStatus.ONGOING;

        panelLock.lock();
        ticTacToePanel.setHumanPlayer(currentHumanPlayerRole);
        ticTacToePanel.setGameBoard(gameBoard.getGameBoard(), gameStatus);
        panelLock.unlock();

        if (currentHumanPlayerRole == null) for (Player player : currentPlayerList) player.getAgent().enableLearning();
        else for (Player player : currentPlayerList) player.getAgent().disableLearning();

        currentPlayer = random.nextInt(currentPlayerList.size());

        for (Player player : currentPlayerList) player.getAgent().newEpisode();

        gameCount++;
        int timeStep = 0;
        while (gameStatus == GameStatus.ONGOING) {
            gameBoard.updateState();
            environmentState = new EnvironmentState(gameCount, ++timeStep, gameBoard.getState(), gameBoard.getAvailableMoves());
            panelLock.lock();
            ticTacToePanel.setGameBoard(gameBoard.getGameBoard(), gameStatus);
            panelLock.unlock();
            jFrame.revalidate();
            ticTacToePanel.paintImmediately(0, 0, boardSize * tileSize, boardSize * tileSize + 60);
            currentPlayerList.get(currentPlayer).getAgent().newStep();
            if (currentPlayerList.get(currentPlayer).isHuman()) {
                lock.lock();
                humanRow = -1;
                humanCol = -1;
                try {
                    while (!gameBoard.isValidMove(humanRow, humanCol)) humanAction.await();
                }
                catch (InterruptedException exception) {
                    Thread.currentThread().interrupt();
                    throw new RuntimeException(exception);
                }
                lock.unlock();
                currentPlayerList.get(currentPlayer).getAgent().act(gameBoard.getPos(humanRow, humanCol));
            }
            else currentPlayerList.get(currentPlayer).getAgent().act(currentHumanPlayerRole != null);

            if (gameStatus == GameStatus.ONGOING) currentPlayer = currentPlayer == 0 ? 1 : 0;
        }
        for (Player player : currentPlayerList) player.getAgent().endEpisode();

        panelLock.lock();
        ticTacToePanel.setGameBoard(gameBoard.getGameBoard(), gameStatus);
        panelLock.unlock();

        jFrame.revalidate();
        ticTacToePanel.paintImmediately(0, 0, boardSize * tileSize, boardSize * tileSize + 60);

        if (currentHumanPlayerRole != null) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException exception) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(exception);
            }
        }

    }
/**
     * Plays single episode of game.
     *
     * @throws MatrixException throws exception if matrix operation fails.
     * @throws NeuralNetworkException throws exception if neural network operation fails.
     * @throws DynamicParamException throws exception if parameter (params) setting fails.
     */
Plays single episode of game.
@throws MatrixException throws exception if matrix operation fails.
@throws NeuralNetworkException throws exception if neural network operation fails.
@throws DynamicParamException throws exception if parameter (params) setting fails.
Plays single episode of game . @throws MatrixException throws exception if matrix operation fails . @throws NeuralNetworkException throws exception if neural network operation fails . @throws DynamicParamException throws exception if parameter ( params ) setting fails .
private void playGame() throws MatrixException, NeuralNetworkException, DynamicParamException, AgentException {
        PlayerRole currentHumanPlayerRole = humanPlayerRole;
        currentPlayerList = new ArrayList<>();
        int players = playerList.size();
        int noughtIndex;
        int crossIndex;
        if (players % 2 == 0) noughtIndex = 2 * random.nextInt(players / 2);
        else noughtIndex = 2 * random.nextInt((players + 1) / 2);
        crossIndex = 2 * random.nextInt(players / 2) + 1;
        currentPlayerList.add(playerList.get(noughtIndex));
        currentPlayerList.get(0).setPlayerRole(PlayerRole.NOUGHT);
        currentPlayerList.add(playerList.get(crossIndex));
        currentPlayerList.get(1).setPlayerRole(PlayerRole.CROSS);
        currentPlayerList.get(0).setAsHuman(currentHumanPlayerRole == currentPlayerList.get(0).getPlayerRole());
        currentPlayerList.get(1).setAsHuman(currentHumanPlayerRole == currentPlayerList.get(1).getPlayerRole());
        gameBoard = new GameBoard();
        gameStatus = GameStatus.ONGOING;
        panelLock.lock();
        ticTacToePanel.setHumanPlayer(currentHumanPlayerRole);
        ticTacToePanel.setGameBoard(gameBoard.getGameBoard(), gameStatus);
        panelLock.unlock();
        if (currentHumanPlayerRole == null) for (Player player : currentPlayerList) player.getAgent().enableLearning();
        else for (Player player : currentPlayerList) player.getAgent().disableLearning();
        currentPlayer = random.nextInt(currentPlayerList.size());
        for (Player player : currentPlayerList) player.getAgent().newEpisode();
        gameCount++;
        int timeStep = 0;
        while (gameStatus == GameStatus.ONGOING) {
            gameBoard.updateState();
            environmentState = new EnvironmentState(gameCount, ++timeStep, gameBoard.getState(), gameBoard.getAvailableMoves());
            panelLock.lock();
            ticTacToePanel.setGameBoard(gameBoard.getGameBoard(), gameStatus);
            panelLock.unlock();
            jFrame.revalidate();
            ticTacToePanel.paintImmediately(0, 0, boardSize * tileSize, boardSize * tileSize + 60);
            currentPlayerList.get(currentPlayer).getAgent().newStep();
            if (currentPlayerList.get(currentPlayer).isHuman()) {
                lock.lock();
                humanRow = -1;
                humanCol = -1;
                try {
                    while (!gameBoard.isValidMove(humanRow, humanCol)) humanAction.await();
                }
                catch (InterruptedException exception) {
                    Thread.currentThread().interrupt();
                    throw new RuntimeException(exception);
                }
                lock.unlock();
                currentPlayerList.get(currentPlayer).getAgent().act(gameBoard.getPos(humanRow, humanCol));
            }
            else currentPlayerList.get(currentPlayer).getAgent().act(currentHumanPlayerRole != null);
            if (gameStatus == GameStatus.ONGOING) currentPlayer = currentPlayer == 0 ? 1 : 0;
        }
        for (Player player : currentPlayerList) player.getAgent().endEpisode();
        panelLock.lock();
        ticTacToePanel.setGameBoard(gameBoard.getGameBoard(), gameStatus);
        panelLock.unlock();
        jFrame.revalidate();
        ticTacToePanel.paintImmediately(0, 0, boardSize * tileSize, boardSize * tileSize + 60);
        if (currentHumanPlayerRole != null) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException exception) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(exception);
            }
        }
    }
private void playGame ( ) throws MatrixException , NeuralNetworkException , DynamicParamException , AgentException { PlayerRole currentHumanPlayerRole = humanPlayerRole ; currentPlayerList = new ArrayList < > ( ) ; int players = playerList . size ( ) ; int noughtIndex ; int crossIndex ; if ( players % 2 == 0 ) noughtIndex = 2 * random . nextInt ( players / 2 ) ; else noughtIndex = 2 * random . nextInt ( ( players + 1 ) / 2 ) ; crossIndex = 2 * random . nextInt ( players / 2 ) + 1 ; currentPlayerList . add ( playerList . get ( noughtIndex ) ) ; currentPlayerList . get ( 0 ) . setPlayerRole ( PlayerRole . NOUGHT ) ; currentPlayerList . add ( playerList . get ( crossIndex ) ) ; currentPlayerList . get ( 1 ) . setPlayerRole ( PlayerRole . CROSS ) ; currentPlayerList . get ( 0 ) . setAsHuman ( currentHumanPlayerRole == currentPlayerList . get ( 0 ) . getPlayerRole ( ) ) ; currentPlayerList . get ( 1 ) . setAsHuman ( currentHumanPlayerRole == currentPlayerList . get ( 1 ) . getPlayerRole ( ) ) ; gameBoard = new GameBoard ( ) ; gameStatus = GameStatus . ONGOING ; panelLock . lock ( ) ; ticTacToePanel . setHumanPlayer ( currentHumanPlayerRole ) ; ticTacToePanel . setGameBoard ( gameBoard . getGameBoard ( ) , gameStatus ) ; panelLock . unlock ( ) ; if ( currentHumanPlayerRole == null ) for ( Player player : currentPlayerList ) player . getAgent ( ) . enableLearning ( ) ; else for ( Player player : currentPlayerList ) player . getAgent ( ) . disableLearning ( ) ; currentPlayer = random . nextInt ( currentPlayerList . size ( ) ) ; for ( Player player : currentPlayerList ) player . getAgent ( ) . newEpisode ( ) ; gameCount ++ ; int timeStep = 0 ; while ( gameStatus == GameStatus . ONGOING ) { gameBoard . updateState ( ) ; environmentState = new EnvironmentState ( gameCount , ++ timeStep , gameBoard . getState ( ) , gameBoard . getAvailableMoves ( ) ) ; panelLock . lock ( ) ; ticTacToePanel . setGameBoard ( gameBoard . getGameBoard ( ) , gameStatus ) ; panelLock . unlock ( ) ; jFrame . revalidate ( ) ; ticTacToePanel . paintImmediately ( 0 , 0 , boardSize * tileSize , boardSize * tileSize + 60 ) ; currentPlayerList . get ( currentPlayer ) . getAgent ( ) . newStep ( ) ; if ( currentPlayerList . get ( currentPlayer ) . isHuman ( ) ) { lock . lock ( ) ; humanRow = - 1 ; humanCol = - 1 ; try { while ( ! gameBoard . isValidMove ( humanRow , humanCol ) ) humanAction . await ( ) ; } catch ( InterruptedException exception ) { Thread . currentThread ( ) . interrupt ( ) ; throw new RuntimeException ( exception ) ; } lock . unlock ( ) ; currentPlayerList . get ( currentPlayer ) . getAgent ( ) . act ( gameBoard . getPos ( humanRow , humanCol ) ) ; } else currentPlayerList . get ( currentPlayer ) . getAgent ( ) . act ( currentHumanPlayerRole != null ) ; if ( gameStatus == GameStatus . ONGOING ) currentPlayer = currentPlayer == 0 ? 1 : 0 ; } for ( Player player : currentPlayerList ) player . getAgent ( ) . endEpisode ( ) ; panelLock . lock ( ) ; ticTacToePanel . setGameBoard ( gameBoard . getGameBoard ( ) , gameStatus ) ; panelLock . unlock ( ) ; jFrame . revalidate ( ) ; ticTacToePanel . paintImmediately ( 0 , 0 , boardSize * tileSize , boardSize * tileSize + 60 ) ; if ( currentHumanPlayerRole != null ) { try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException exception ) { Thread . currentThread ( ) . interrupt ( ) ; throw new RuntimeException ( exception ) ; } } }
Plays single episode of game.
Plays single episode of game .