Exemplo n.º 1
0
/*Uses the ifBlockException function to check potential win spots that can be
 played on. It then checks the board 2 turns further to see if the
 computer has a sure win and if this spot should be played on.
 (checks for sure win 3 moves in advance) */
connectTrap(int n, char board[21][21], bool humanPlayer, char humanPlayerC, char compPlayerC) {
    int x1, y1, x2, y2, y3, x3, totalScore = 0;
    char boardCopy1[21][21], boardCopy2[21][21];
    for (x1 = 0; x1 < n; x1++)
        for (y1 = 0; y1 < n; y1++)
            if (board[x1][y1] == 'U' && ifBlockException(n, board, !humanPlayer, compPlayerC, humanPlayerC, x1, y1)) {
                resetBoardCopy(n, boardCopy1, board);
                boardCopy1[x1][y1] = compPlayerC;
                resetBoardCopy(n, boardCopy2, boardCopy1);

                for (x2 = 0; x2 < n; x2++)
                    for (y2 = 0; y2 < n; y2++) {
                        if (boardCopy2[x2][y2] == 'U' && findLongest(boardCopy2, x2, y2, !humanPlayer) == 5)
                            boardCopy2[x2][y2] = compPlayerC;
                    }
                for (x2 = 0; x2 < n; x2++)
                    for (y2 = 0; y2 < n; y2++)
                        if (boardCopy2[x2][y2] == 'U' && findLongest(boardCopy2, x2, y2, !humanPlayer) >= 6)
                            totalScore++;
                for (x2 = 0; x2 < n; x2++)
                    for (y2 = 0; y2 < n; y2++)
                        if (boardCopy1[x2][y2] == 'U' && findLongest(boardCopy1, x2, y2, !humanPlayer) >= 6)
                            totalScore++;


                if (totalScore >= 4) {
                    board[x1][y1] = compPlayerC;
                    printf("Computer moves %c at %d %d\n", compPlayerC, x1, y1);
                    printBoard(n, board);
                    return true;
                }
            }
    return false;

}
Exemplo n.º 2
0
/*Checks to see if the board has any spot that if played on can cause a segment
 of 5 in  a row which creates 2 opportunities for 6 in a row. Blocks this spot
 if it exists.*/
int block5(int n, char board[21][21], bool humanPlayer, char humanPlayerC, char compPlayerC) {
    int x1, y1, x2, y2, score, largestScore = 0, moveX, moveY;
    for (x1 = 0; x1 < n; x1++)
        for (y1 = 0; y1 < n; y1++) {
            if (board[x1][y1] == 'U' && findLongest(board, x1, y1, humanPlayer) == 5) {
                char boardCopy[21][21];
                resetBoardCopy(n, boardCopy, board);
                boardCopy[x1][y1] = humanPlayerC;
                score = 0;
                for (x2 = 0; x2 < n; x2++)
                    for (y2 = 0; y2 < n; y2++) {
                        if (board[x2][y2] == 'U' && findLongest(boardCopy, x2, y2, humanPlayer) >= 6)
                            score++;
                    }
                if (score > largestScore) {
                    largestScore = score;
                    moveX = x1;
                    moveY = y1;
                }
            }
        }
    if (largestScore >= 2) {
        board[moveX][moveY] = compPlayerC;
        printf("Computer moves %c at %d %d\n", compPlayerC, moveX, moveY);
        printBoard(n, board);
        return true;
    }
    return false;
}
 int findLongest(TreeNode* root, int len, int lastVal)
 {
     if(root == NULL)
     {
         return len;
     }
     
     int newLen = lastVal + 1 == root->val ? len + 1 : 1;
     
     return max(newLen, max(findLongest(root->left, newLen, root->val), findLongest(root->right, newLen, root->val)));
 }
 int longestConsecutive(TreeNode* root) {
     if(root == NULL)
     {
         return 0;
     }
     
     return findLongest(root, 0, root->val - 1);
 }
Exemplo n.º 5
0
//function to check if there is a winner
char checkWin()
{
    int counter=0;
    bool win=false;
    char winningColour, won='N';
    for (int i=0; i<n && !win; i++)
    {
        for (int j=0; j<n && !win; j++)
        {
            if(board[i][j]=='U')
                counter++;
        }
    }

    if (findLongest('W')>=99999)
    {
        win=true;
        winningColour='W';
    }
    if (findLongest('B')>=99999)
    {
        win=true;
        winningColour='B';
    }

    if (win)
    {
        won='Y';
        if (winningColour == 'W')
            printf("White player wins.\n");
        if (winningColour == 'B')
            printf("Black player wins.\n");
    }
    else if(counter==0)
    {
        won='D';
        printf("Draw!\n");
    }
    else
        won='N';
    return won;
}
Exemplo n.º 6
0
/*Checks to see if the board has any spot where the opponent can play and win 
 the game. Immediately blocks this spot if it exists.*/
bool block6(int n, char board[21][21], bool humanPlayer, char compPlayerC) {
    int x, y;
    for (x = 0; x < n; x++)
        for (y = 0; y < n; y++) {
            if (board[x][y] == 'U' && findLongest(board, x, y, humanPlayer) >= 6) {
                board[x][y] = compPlayerC;
                printf("Computer moves %c at %d %d\n", compPlayerC, x, y);
                printBoard(n, board);
                return true;
            }
        }
    return false;
}
Exemplo n.º 7
0
inline void findLongest(const short int &s, const short int &v, const short int &f)
{
    short int g = 0;
    for(unsigned int n = 0; n < tree[v].size(); ++ n)
    {
        g = costEdgeSecond(tree[v][n]);
        if(g == f)
            continue;

        longest[s][g] = std::max(longest[s][v], costEdgeFirst(tree[v][n]));
        findLongest(s, g, v);
    }

    return;
}
Exemplo n.º 8
0
char checkWinner(int n, char board[21][21]) {
    int x, y;
    bool found = false;
    bool spot;
    char winner = 'N';
    for (x = 0; x < n && !found; x++)
        for (y = 0; y < n && !found; y++) {
            if (board[x][y] == 'W')
                spot = true;
            else if (board[x][y] == 'B')
                spot = false;
            if ((board[x][y] == 'W' || board[x][y] == 'B') && findLongest(board, x, y, spot) >= 6)
                winner = board[x][y];
        }
    if (winner == 'B')
        printf("\nBlack player wins.\n");
    if (winner == 'W')
        printf("\nWhite player wins.\n");

    return winner;

}
Exemplo n.º 9
0
int main(void)
{
    while(scanf("%hd %d", &verts, &edges) != -1)
    {
        for(short int v = 0; v < verts; ++ v)
        {
            tree[v].clear();
            repr[v] = v;
            for(short int v2 = 0; v2 < verts; ++ v2)
                longest[v][v2] = -1;
        }

        for(int c = 0; c <= max; ++ c)
            sort[c].clear();

        max = 0;
        for(int e = 0; e < edges; ++ e)
        {
            scanf("%hd %hd %d", &start, &end, &cost);
            sort[cost].push_back(makeEdge(start - 1, end - 1));
            max = std::max(max, cost);
        }

        result = 0;
        best = 0;
        count = 0;
        for(int c = 0; c <= max && count < verts - 1; ++ c)
            for(unsigned int e = 0; e < sort[c].size(); ++ e)
            {
                int &act = sort[c][e];
                if(unionUnion(edgeFirst(act), edgeSecond(act)))
                {
                    tree[edgeFirst(act)].push_back(makeCostEdge(c, edgeSecond(act)));
                    tree[edgeSecond(act)].push_back(makeCostEdge(c, edgeFirst(act)));
                    ++ count;
                    result += c;
                }
            }

        if(count != verts - 1)
        {
            puts("disconnected");
            continue;
        }

        for(int v = 0; v < verts; ++ v)
            findLongest(v, v);

        for(int c = 0; c <= max; ++ c)
            for(unsigned int e = 0; e < sort[c].size(); ++ e)
            {
                int &act = sort[c][e];
                tmp = c + longest[edgeFirst(act)][edgeSecond(act)];
                if(tmp > best)
                    best = tmp;
            }

        printf("%d\n", result - best);
    }

    return 0;
}