Пример #1
0
// Function stores Board's in memory,
void* boardStore(board_p board, int* size)
{
    void* buffer = NULL;
    state_p header = NULL;

    ASSERT(NULL != board, "boardStore");
    ASSERT(NULL != size, "boardStore");

    *size = board->prev ? sizeof(state_11_t)
                        : sizeof(state_10_t);

    buffer = bufferAllocate(*size, NULL);

    if (board->prev) {
        state_11_p state = (state_11_p)buffer;

        state->header.version = TAG_VERSION_11;

        boardCopy(&state->current, board);
        boardCopy(&state->previous, board->prev);
    } else {
        state_10_p state = (state_10_p)buffer;

        state->header.version = TAG_VERSION_10;

        boardCopy(&state->current, board);
    }

    header = (state_p)buffer;
    header->size = *size;
    header->crc = Crc16CalcBlock(
        header + 1, *size - sizeof(*header), 0);

    return buffer;
};
Пример #2
0
// Function undos last movement
void boardUndo(board_p board)
{
    ASSERT(NULL != board, "boardUndo");

    if (board->prev)
        boardCopy(board, board->prev);
};
Пример #3
0
// Function clones a Board
board_p boardClone(board_p board)
{
    board_p clone = NULL;

    ASSERT(NULL != board, "boardClone");

    clone = boardCreate(board->seed, board->width,
                        board->height);

    boardCopy(clone, board);

    return clone;
};
Пример #4
0
// evaluate a proposed move and return its score
static int EvaluateMove(HexBoard &b, HexColor turn, unsigned int row, unsigned int col)
{
    // make a local working copy of the board
    HexBoard board(b);
    
    const int nTrials = 1000;
    int score = 0;
    
    // play the proposed move
    board.SetColor(row, col, turn);
    
    // obtain remaining blank cells
    HexCellSet hcs;
    board.GetCells(hcs, HEXBLANK);
    
    // order of turns, beginning with opponent (we already placed our first move)
    HexColor turns[2] = {((turn == HEXBLUE) ? HEXRED : HEXBLUE), turn};
    
    for (unsigned int iTrial = 0; iTrial < nTrials; iTrial++)
    {
        // make a working copy of the board to trash in this trial
        HexBoard boardCopy(board);
        
        // randomize blank cells to play them in different random order each trial
        std::random_shuffle(hcs.begin(), hcs.end());
        
        for (unsigned int i = 0; i < hcs.size(); i++)
            boardCopy.SetColor(hcs[i].row, hcs[i].col, turns[i % 2]);
            
        // check whether we (turn) won, and update stats
        if (boardCopy.Winner() == turn)
            score++;
    }
    
    return score;
}
Пример #5
0
void HexMC1Player::Move(HexBoard &board, HexColor turn, unsigned int &row, unsigned int &col)
{    
    clock_t tStart = clock();
    
    const unsigned int nTrials = 1000;
    
    // keep track of best play and best score
    int bestScore = -1;
    unsigned int bestPlay;
        
    // obtain the currently open cells
    HexCellSet hcs;
    board.GetCells(hcs, HEXBLANK);
    unsigned int nCells = hcs.size();
    
    // shuffle cells to examine them in random order
    shuffleVector(hcs, 0, nCells);   
        
    // precompute the order of moves during simulation
    HexColor turns[2];
    turns[0] = turn;
    turns[1] = ((turn == HEXBLUE) ? HEXRED : HEXBLUE);    
    
    // now evaluate each cell in turn
    for (unsigned int iCell = 0; iCell < nCells; iCell++)
    {        
        // make a working copy of the cell set
        HexCellSet hcsCopy(hcs);
        
        // store away the current cell by swapping with last cell
        swapEntries(hcsCopy, iCell, nCells - 1);
                
        // run MC simulation
        int nWins = 0;
        for (unsigned int iTrial = 0; iTrial < nTrials; iTrial++)
        {        
            // shuffle the first n-1 entries (keeping the current cell safely at the end)
            // to randomize the order in which cells are played in simulation
            shuffleVector(hcsCopy, 0, nCells-1);
            
            // make a copy of the board to run simulated game
            HexBoard boardCopy(board);
            
            // alternate turns until board is full (until no more cells to play)
            for (unsigned int iTurn = 0; iTurn < nCells; iTurn++)
            {
                // for the first move, play the cell currently under evaluation,
                // which is stored at the end of the vector.
                // for subsequent moves, play each cell in succession, starting from 0th cell
                
                unsigned int thisMove = ((iTurn == 0) ? (nCells - 1) : (iTurn - 1));
                HexColor thisColor = turns[iTurn % 2];
                
                boardCopy.SetColor(hcsCopy[thisMove].row, hcsCopy[thisMove].col, thisColor);                
            }
            
            // find out who won this simulated game
            HexColor winner = boardCopy.Winner();
            
            // if we won, update stats
            if (winner == turn)
            {
                nWins++;
            }
        }
        
        if (nWins > bestScore)
        {
            bestScore = nWins;
            bestPlay = iCell;
            
            if (nWins == nTrials)
            {
                // if this cell won all its trials, then play it
                break;
            }            
        }        
    }
    
    // return the best play we found
    clock_t tEnd = clock();
    clock_t tElapsed = (tEnd - tStart) / CLOCKS_PER_SEC;
    std::cout << "Time elapsed: " << tElapsed << "(secs) \n";
    row = hcs[bestPlay].row;
    col = hcs[bestPlay].col;
}
Пример #6
0
bool ChessMove::PutsInCheck(const ChessBoard& board) const{
    ChessBoard boardCopy(board);
    ChessPiece* pieceCopy(board.GetPiece(startX,startY));
    return (boardCopy.DoMove(*this))->InCheck(pieceCopy->GetColor());
}
void reverseSearch(int ** boardPtrs,
                   int * boardPtrsCount,
                   int * max, int * initBoard,
                   int N, int K,
                   int W)
{
    int localCount = 0;
    *boardPtrsCount = 0;
    int maxQueen = -1;
    *max = 0;
    
    Stack * stack = (Stack *) malloc(sizeof(Stack));
    createStack(stack, N*N);
    push(stack, initBoard);
    
    int * board = (int * )malloc(sizeof(int) * N*N);
    int lastSize = stack->size;
    int totalCal = 0;
    
    while (pop(stack, board)) {
        totalCal++;
        
        if(lastSize!=stack->size){
            lastSize = stack->size;
            printf("Size changed to %d\n",lastSize);
        }
        
        int * checkResult = (int *)malloc(sizeof(int) * CHECK_RESULT_SIZE);
        if(W) checkResult = checkBoardSizeAndQueenForReverseWrapAround(board, N, K);
        else checkResult = checkBoardSizeAndQueenForReverse(board, N, K);
        //printf("chkresult %d %d %d\n",checkResult[0], checkResult[1], checkResult[2]);
        if(checkResult[CHECK_BOARD_STATUS] == 0){
            free(checkResult);
            // free(board);
            continue;
        }
        
        else{
            if(checkResult[CHECK_BOARD_STAUTS]==-1){
                int * newBoard0;
                int * newBoard1;
                newBoard0 = (int *) malloc (sizeof(int)*(N*N));
                newBoard1 = (int *) malloc (sizeof(int)*(N*N));
                boardCopy(newBoard0, board, N*N);
                boardCopy(newBoard1, board, N*N);
                //allocateAndCopy(&newBoard0, board, N*N);
                //allocateAndCopy(&newBoard1, board, N*N);
                
                //alterBoardAtPosition(newBoard0, newBoard1, checkResult[CHECK_BOARD_SIZE]);
                if(checkResult[CHECK_BOARD_SIZE]==16){
                    continue;
                }
                newBoard0[checkResult[CHECK_BOARD_SIZE]] = 0;
                newBoard1[checkResult[CHECK_BOARD_SIZE]] = 1;
                
                push(stack, newBoard0);
                push(stack, newBoard1);
                free(newBoard0);
                free(newBoard1);
                
            }
            //exactly attack K queen
            
            else if(checkResult[CHECK_BOARD_STAUTS]==1){
                //find max!
                if(maxQueen==-1){
                    
                    maxQueen=checkResult[CHECK_BOARD_QUEEN];
                    boardPtrs[localCount] = (int *)malloc(sizeof(int)*N*N);
                    int idx;
                    for(idx=0;idx<N*N;++idx){
                        board[idx] = board[idx] == -1?1:board[idx];
                    }
                    boardCopy(boardPtrs[localCount], board, N*N);
                    
                    localCount++;
                    free(checkResult);
                    continue;
                }
                
                else if(maxQueen!=-1){
                    if(checkResult[CHECK_BOARD_QUEEN]<maxQueen) {
                        free(checkResult);
                        
                        continue;
                    }
                    
                    else if(checkResult[CHECK_BOARD_QUEEN]==maxQueen){
                        boardPtrs[localCount] = (int *)malloc(sizeof(int)*N*N);
                        int idx = 0;
                        for(idx=0;idx<N*N;++idx){
                            board[idx] = board[idx] == -1?1:board[idx];
                        }
                        
                        boardCopy(boardPtrs[localCount], board, N*N);
                        
                        localCount++;
                        free(checkResult);
                        continue;
                    }
                }
            }
            
            
        }
        
    }
    
    *max = maxQueen == -1?0:maxQueen;
    *boardPtrsCount = localCount;
}