Esempio n. 1
0
int Board::doCaptures(Player victim, Move seed) {
    if (pieces[index(getX(seed), getY(seed))] != victim)
        return 0;

    Stone *visited = new Stone[arraySize*arraySize];
    for (int i = 0; i < arraySize*arraySize; i++) {
        visited[i] = 0;
    }
    MoveList captured;

    if (isSurrounded(victim, EMPTY, getX(seed), getY(seed), visited, captured)) {
        if (updateBoard) {
            for (unsigned int i = 0; i < captured.size(); i++) {
                Move m = captured.get(i);
                pieces[index(getX(m), getY(m))] = EMPTY;
                zobristKey ^= zobristTable[zobristIndex(victim, getX(m), getY(m))];
            }

            // Record how many pieces were captured for scoring purposes
            if (victim == BLACK)
                whiteCaptures += captured.size();
            else
                blackCaptures += captured.size();
        }
    }

    delete[] visited;
    return captured.size();
}
  bool isSurrounded(vector<vector<char>> &board,vector<vector<bool>> &haveCompare,int m,int n){

    if(board[m][n]=='X')
        return true;
    
    if(m==0||n==0||m==board.size()-1||n==board[m].size()-1)
        return false;
    
    bool temp = true;
    
    if(haveCompare[m-1][n]==false){
    
        haveCompare[m-1][n] =true;
        temp = temp&&isSurrounded(board, haveCompare, m-1, n);
    }
    
    if(haveCompare[m+1][n]==false){
        
        haveCompare[m+1][n] =true;
        temp = temp&&isSurrounded(board, haveCompare, m+1, n);
    }
    
    if(haveCompare[m][n+1]==false){
        
        haveCompare[m][n+1] =true;
        temp = temp&&isSurrounded(board, haveCompare, m, n+1);
    }
    
    if(haveCompare[m][n-1]==false){
        
        haveCompare[m][n-1] =true;
        temp = temp&&isSurrounded(board, haveCompare, m, n-1);
    }
    
    
    if(temp){
        
        board[m][n] ='X';
        return true;
    
    }else{
        return false;
    }
}
Esempio n. 3
0
// Given a coordinate as a move, and a victim color, recursively determines
// whether the victim on this square is part of a surrounded chain
// Precondition: (x, y) is of color victim
bool Board::isSurrounded(Player victim, Player open, int x, int y,
    Stone *visited, MoveList &captured) {
    visited[index(x, y)] = 1;

    Stone east = pieces[index(x+1, y)];
    // If we are next to a non-blocker and non-victim, then we are not surrounded
    if (east == open)
        return false;
    // If we next to victim, we need to recursively see if the entire group
    // is surrounded
    else if (east == victim && visited[index(x+1, y)] == 0)
        if (!isSurrounded(victim, open, x+1, y, visited, captured))
            return false;
    // Else the piece is surrounded by a blocker or edge

    Stone west = pieces[index(x-1, y)];
    if (west == open)
        return false;
    else if (west == victim && visited[index(x-1, y)] == 0)
        if (!isSurrounded(victim, open, x-1, y, visited, captured))
            return false;

    Stone north = pieces[index(x, y+1)];
    if (north == open)
        return false;
    else if (north == victim && visited[index(x, y+1)] == 0)
        if (!isSurrounded(victim, open, x, y+1, visited, captured))
            return false;

    Stone south = pieces[index(x, y-1)];
    if (south == open)
        return false;
    else if (south == victim && visited[index(x, y-1)] == 0)
        if (!isSurrounded(victim, open, x, y-1, visited, captured))
            return false;

    // If we got here, we are surrounded on all four sides
    captured.add(coordToMove(x, y));
    return true;
}
 void solve(vector<vector<char>> &board) {
     int rows = board.size();
     if(rows == 0)return;
     int cols = board[0].size();
     if(cols == 0)return;
     vector<vector<char>> isSurrounded(rows, vector<char>(cols, '0'));
     for(int i=0; i<rows; i++) {
         for(int j=0; j<cols; j++) {
             if(board[i][j]=='O' && isSurrounded[i][j] == '0') {
                 flood(i, j, isSurrounded, board);
             }
         }
     }
 }
Esempio n. 5
0
void solve(char** board, int boardRowSize, int boardColSize) {
    bool** walked = malloc(sizeof(bool*) * boardRowSize);
    int size = boardRowSize * boardColSize / 2;
    size = size < 2 ? 2 : size;
    int* xq = malloc(sizeof(int) * size);
	int* yq = malloc(sizeof(int) * size);
    int i, j;
    for (i = 0 ; i < boardRowSize ; i++) {
        walked[i] = malloc(sizeof(bool) * boardColSize);
        for (j = 0 ; j < boardColSize ; j++) {
            walked[i][j] = (board[i][j] == 'X'); // assume the 'X' is walked
        }
    }
    for (i = 0 ; i < boardRowSize ; i++) {
        for (j = 0 ; j < boardColSize ; j++) {
            if (walked[i][j] == false) {
                if (isSurrounded(board, boardRowSize, boardColSize, walked, i, j, xq, yq, size)) {
                    wipe(board, boardRowSize, boardColSize, walked, i, j, xq, yq, size);
                }
            }
        }
    }
}
void solve(vector<vector<char>> &board) {
 
    if(board.size()<=2||board[0].size()<=2)
        return;
    
    vector<vector<bool>> haveCompare;
    
    haveCompare =vector<vector<bool> >(board.size(), vector<bool>(board[0].size(), false));
    
    for(int m=1;m<board.size()-1;m++){
    
        for(int n=1;n<board[0].size()-1;n++){
        
            if (board[m][n]=='O'&&haveCompare[m][n]==false) {
                
                haveCompare[m][n] = true;
                isSurrounded(board,haveCompare, m, n);
                
            }
        }
    
    }
    
}
Esempio n. 7
0
// Counts the territory each side owns
void Board::countTerritory(int &whiteTerritory, int &blackTerritory) {
    whiteTerritory = 0;
    blackTerritory = 0;
    Stone *visited = new Stone[arraySize*arraySize];
    Stone *territory = new Stone[arraySize*arraySize];
    Stone *region = new Stone[arraySize*arraySize]; 

    // Count territory for both sides
    for (Player p = BLACK; p <= WHITE; p++) {
        // Reset the visited array
        for (int i = 0; i < arraySize*arraySize; i++)
            visited[i] = 0;

        // Main loop
        for (int j = 1; j <= boardSize; j++) {
            for (int i = 1; i <= boardSize; i++) {
                // Don't recount territory
                if (visited[index(i, j)])
                    continue;
                // Only use empty squares as seeds
                if (pieces[index(i, j)])
                    continue;

                if (isEye(p, coordToMove(i, j))) {
                    visited[index(i, j)] = 1;
                    if (p == BLACK)
                        blackTerritory++;
                    else
                        whiteTerritory++;
                    continue;
                }
                
                for (int k = 0; k < arraySize*arraySize; k++)
                    territory[k] = 0;
                int territorySize = 0;
                int boundarySize = 0;

                getTerritory(p, i, j, visited, territory, territorySize, boundarySize);

                // Check if territory was actually sectioned off
                if (territorySize + boundarySize == boardSize*boardSize)
                    continue;

                // Detect life/death of internal stones
                // Initialize region to 0 if territory is 1, and vice versa
                // This acts as our "visited" array, so that we only explore areas
                // inside the territory
                // bool isContested = false;
                // for (int n = 1; n <= boardSize; n++) {
                //     for (int m = 1; m <= boardSize; m++) {
                //         if (!territory[index(m, n)])
                //             continue;
                //         if (pieces[index(m, n)] == otherPlayer(p)) {
                //             isContested = true;
                //             break;
                //         }
                //     }
                // }

                // if (!isContested) {
                //     if (p == BLACK)
                //         blackTerritory += territorySize;
                //     else
                //         whiteTerritory += territorySize;
                // }
                
                for (int k = 0; k < arraySize*arraySize; k++)
                    region[k] = territory[k] ^ 1;
                int internalRegions = 0;

                for (int n = 1; n <= boardSize; n++) {
                    for (int m = 1; m <= boardSize; m++) {
                        if (region[index(m, n)])
                            continue;
                        if (pieces[index(m, n)])
                            continue;

                        MoveList eye;
                        if (isSurrounded(EMPTY, p, m, n, region, eye))
                            internalRegions++;
                    }
                }

                int territoryCount = 0;
                if (internalRegions == 0) {
                    territoryCount += territorySize;
                    // Score dead stones
                    for (int k = 0; k < arraySize*arraySize; k++)
                        if (territory[k] && pieces[k] == otherPlayer(p))
                            territoryCount++;
                }

                if (p == BLACK)
                    blackTerritory += territoryCount;
                else
                    whiteTerritory += territoryCount;
            }
        }
    }

    delete[] visited;
    delete[] territory;
    delete[] region;
}