group3.c
void g3_mcts(const struct connect4 *game, g3_MCnode *root) {
    // For concision
    const char me = game->whoseTurn;
    //const int winCondition = me == PLAYERONE ? X_WINS : O_WINS;

    // Memory to do scratch-work in
    struct connect4 tempGame;

    g3_MCnode *nodeStack[g3_NUM_MOVES];
    int moveStack[g3_NUM_MOVES];
    int s = 0; // Stack pointer

    static int runs = 0;
    int numSims = g3_max(g3_SIMULATIONS / 26 * (26 - runs), g3_FAST_SIMULATIONS);
    runs++;

    //puts("g");
    //printf("Running %d simulations\n", numSims);

    int t; // current number of simulations
    for (t = 0; t < numSims; t++) {
        // Make a fresh copy of the game
        memcpy(&tempGame, game, sizeof(struct connect4));

        // Temp pointer to the current node in the tree
        g3_MCnode *current = root;

        // Reset stack pointer
        s = 0;

        // Play moves until the hypothetical game ends
        char winner = 0;
        while (winner == 0 && g3_movesAvailable(&tempGame)) {
            if (s >= 26)
                puts("Something's gone horribly wrong.");

            double probabilities[NUM_COLS];
            //const char currPlayer = tempGame.whoseTurn;

            // Assume both players will go for special cases
            int nextMove = g3_handleSpecialCase(&tempGame);
            if (nextMove == -1) {
                // Weight current player's path based on past success
                if (tempGame.whoseTurn == me)
                    g3_computeWeightedProbs(probabilities, current->scores, t);
                    // If there was no immediate win, give all opponent paths an equal chance
                else
                    g3_computeUniformProbs(probabilities);

                // Use probabilities to select the next node
                while (not_valid(&tempGame, nextMove))
                    nextMove = g3_chooseMove(probabilities);
            }

            // Push the move onto the stack if this is our move
            if (tempGame.whoseTurn == me) {
                //printf("Stack pointer at %d\n", s);
                //print_board(&tempGame);
                moveStack[s] = nextMove;
                nodeStack[s++] = current;
            }

            // If the move would result in a winner or the game is CATS, break the loop
            int validRow = get_row(&tempGame, nextMove);
            if (g3_is3Win(&tempGame, validRow, nextMove, tempGame.whoseTurn)) {
                winner = tempGame.whoseTurn;
            } else {
                // Otherwise, play the move and keep playing
                tempGame.board[get_row(&tempGame, nextMove)][nextMove] = tempGame.whoseTurn;
                tempGame.whoseTurn = other(tempGame.whoseTurn);
                current = g3_getNextNode(current, nextMove);
            }
        }

        // Determine if we won
        int w = (winner == me);
        // Backprop the result of the game
        g3_backpropogate(nodeStack, moveStack, s - 1, w);
    }

    return;
}
// Perform a Monte-Carlo Tree Search
Perform a Monte-Carlo Tree Search
Perform a Monte - Carlo Tree Search
void g3_mcts(const struct connect4 *game, g3_MCnode *root) {
    const char me = game->whoseTurn;
    struct connect4 tempGame;
    g3_MCnode *nodeStack[g3_NUM_MOVES];
    int moveStack[g3_NUM_MOVES];
    int s = 0; 
    static int runs = 0;
    int numSims = g3_max(g3_SIMULATIONS / 26 * (26 - runs), g3_FAST_SIMULATIONS);
    runs++;
    int t; 
    for (t = 0; t < numSims; t++) {
        memcpy(&tempGame, game, sizeof(struct connect4));
        g3_MCnode *current = root;
        s = 0;
        char winner = 0;
        while (winner == 0 && g3_movesAvailable(&tempGame)) {
            if (s >= 26)
                puts("Something's gone horribly wrong.");
            double probabilities[NUM_COLS];
            int nextMove = g3_handleSpecialCase(&tempGame);
            if (nextMove == -1) {
                if (tempGame.whoseTurn == me)
                    g3_computeWeightedProbs(probabilities, current->scores, t);
                else
                    g3_computeUniformProbs(probabilities);
                while (not_valid(&tempGame, nextMove))
                    nextMove = g3_chooseMove(probabilities);
            }
            if (tempGame.whoseTurn == me) {
                moveStack[s] = nextMove;
                nodeStack[s++] = current;
            }
            int validRow = get_row(&tempGame, nextMove);
            if (g3_is3Win(&tempGame, validRow, nextMove, tempGame.whoseTurn)) {
                winner = tempGame.whoseTurn;
            } else {
                tempGame.board[get_row(&tempGame, nextMove)][nextMove] = tempGame.whoseTurn;
                tempGame.whoseTurn = other(tempGame.whoseTurn);
                current = g3_getNextNode(current, nextMove);
            }
        }
        int w = (winner == me);
        g3_backpropogate(nodeStack, moveStack, s - 1, w);
    }
    return;
}
void g3_mcts ( const struct connect4 * game , g3_MCnode * root ) { const char me = game -> whoseTurn ; struct connect4 tempGame ; g3_MCnode * nodeStack [ g3_NUM_MOVES ] ; int moveStack [ g3_NUM_MOVES ] ; int s = 0 ; static int runs = 0 ; int numSims = g3_max ( g3_SIMULATIONS / 26 * ( 26 - runs ) , g3_FAST_SIMULATIONS ) ; runs ++ ; int t ; for ( t = 0 ; t < numSims ; t ++ ) { memcpy ( & tempGame , game , sizeof ( struct connect4 ) ) ; g3_MCnode * current = root ; s = 0 ; char winner = 0 ; while ( winner == 0 && g3_movesAvailable ( & tempGame ) ) { if ( s >= 26 ) puts ( " " ) ; double probabilities [ NUM_COLS ] ; int nextMove = g3_handleSpecialCase ( & tempGame ) ; if ( nextMove == -1 ) { if ( tempGame . whoseTurn == me ) g3_computeWeightedProbs ( probabilities , current -> scores , t ) ; else g3_computeUniformProbs ( probabilities ) ; while ( not_valid ( & tempGame , nextMove ) ) nextMove = g3_chooseMove ( probabilities ) ; } if ( tempGame . whoseTurn == me ) { moveStack [ s ] = nextMove ; nodeStack [ s ++ ] = current ; } int validRow = get_row ( & tempGame , nextMove ) ; if ( g3_is3Win ( & tempGame , validRow , nextMove , tempGame . whoseTurn ) ) { winner = tempGame . whoseTurn ; } else { tempGame . board [ get_row ( & tempGame , nextMove ) ] [ nextMove ] = tempGame . whoseTurn ; tempGame . whoseTurn = other ( tempGame . whoseTurn ) ; current = g3_getNextNode ( current , nextMove ) ; } } int w = ( winner == me ) ; g3_backpropogate ( nodeStack , moveStack , s - 1 , w ) ; } return ; }
Perform a Monte-Carlo Tree Search
Perform a Monte - Carlo Tree Search
// For concision //const int winCondition = me == PLAYERONE ? X_WINS : O_WINS; // Memory to do scratch-work in // Stack pointer //puts("g"); //printf("Running %d simulations\n", numSims); // current number of simulations // Make a fresh copy of the game // Temp pointer to the current node in the tree // Reset stack pointer // Play moves until the hypothetical game ends //const char currPlayer = tempGame.whoseTurn; // Assume both players will go for special cases // Weight current player's path based on past success // If there was no immediate win, give all opponent paths an equal chance // Use probabilities to select the next node // Push the move onto the stack if this is our move //printf("Stack pointer at %d\n", s); //print_board(&tempGame); // If the move would result in a winner or the game is CATS, break the loop // Otherwise, play the move and keep playing // Determine if we won // Backprop the result of the game