我的代码一直因分段错误而失败,但我没有分配任何内存

问题描述 投票:0回答:1

我的代码在几个输入下继续工作,然后因分段错误而结束。我不做任何动态内存分配。这应该是一种黑白棋游戏(又名奥赛罗)。 include lab8part1 包含 stdbool 并声明一些函数。

#include <stdio.h>
#include <stdlib.h>
//#include "lab8part1.h"
#include <stdbool.h>

void printBoard(char board[][26], int n);
bool positionInBounds(int n, int row, int col);
bool checkLegalInDirection(char board[][26], int n, int row, int col, char colour, int deltaRow, int deltaCol);
bool validMove(char board[][26], int n, int row, int col, char colour);
bool computerMove(char board[][26], int n, char colour, char row, char col);
bool UserMove(char board[][26], int n, char colour, char row, char col);
int checkBestMoves(char board[][26], int n, int row, int col, char colour, int deltaRow, int deltaCol);
bool checkifmove(char board[][26], int n, char color);

int
main(void)
{
    char board[26][26],
     color,
     row,
     col;

    printf("Enter the board dimension: ");
    int n;

    scanf(" %d", &n);
    int start = n / 2;

    for (int m = 0; m < n; m++) {
        for (int j = 0; j < n; j++) {
            if ((m == start - 1 && j == start - 1) ||
                (m == start && j == start)) {
                board[m][j] = 'W';

            }
            else if ((m == start - 1 && j == start) ||
                (m == start && j == start - 1)) {
                board[m][j] = 'B';

            }
            else {
                board[m][j] = 'U';

            }
        }
    }

    printf("Computer plays (B/W): ");
    scanf(" %c", &color);
    char color1;

    if (color == 'B') {
        color1 = 'W';
    }
    else {
        color1 = 'B';
    }
    printBoard(board, n);
    printf("\n");
    char turn = 'B';
    bool validmove = true;

    while ((checkifmove(board, n, color) == true) ||
        (checkifmove(board, n, color1) == true)) {
        validmove = true;

        if (turn == color) {
            validmove = computerMove(board, n, color, row, col);
        }
        else {
            UserMove(board, n, color1, row, col);
        }

        if (validmove == false) {
            break;
        }

        if (turn == 'W') {
            turn = 'B';
        }
        else {
            turn = 'W';
        }

    }
    int whitwin = 0;
    int blacwin = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (board[i][j] == 'W') {
                whitwin += 1;
            }
            else {
                blacwin += 1;
            }
        }
    }
    if (whitwin > blacwin) {
        printf("W player wins.");
    }
    else if (whitwin < blacwin) {
        printf("B player wins.");
    }
    else {
        printf("Draw.");
    }
    return 0;
}

bool
computerMove(char board[][26], int n, char colour, char row, char col)
{

    int movesrow[100] = { 0 };
    int movescol[100] = { 0 };
    int count = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (board[i][j] == 'U') {
                if (checkLegalInDirection(board, n, i, j, colour, -1, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

                if (checkLegalInDirection(board, n, i, j, colour, -1, 0)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }
                if (checkLegalInDirection(board, n, i, j, colour, -1, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;
                }

                if (checkLegalInDirection(board, n, i, j, colour, 0, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

                if (checkLegalInDirection(board, n, i, j, colour, 0, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

                if (checkLegalInDirection(board, n, i, j, colour, 1, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }
                if (checkLegalInDirection(board, n, i, j, colour, 1, 0)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }
                if (checkLegalInDirection(board, n, i, j, colour, 1, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

            }

        }
        count += 1;
    }

    int bestMoves[600] = { 0 };
    int tracker = 0;
    int tot = 0;

    for (int i = 0; i < count; i++) {

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }
        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, 0);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {

            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 0, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 0, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, 0);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }
    }

    // if computer runs out of moves
    if (bestMoves[0] == 0) {
        printf("%c player has no valid move.", colour);
        return false;
    }

    int counter = 0;
    int bigNum = bestMoves[0];
    int duplicates[tracker];

    for (int i = 1; i < tracker; i++) {
        if (bestMoves[i] > bigNum) {
            bigNum = bestMoves[i];
            duplicates[0] = i;
            counter = 1;
        }
        else if (bestMoves[i] == bigNum) {
            duplicates[counter] = i;
            counter++;
        }
    }
    int rowtemp = 0,
        coltemp = 0;

    for (int i = 0; i < counter; i++) {
        for (int j = 0; j < n; j++) {
            if ((movesrow[duplicates[i]] < movesrow[duplicates[i + j]]) &&
                movescol[duplicates[i]] < movescol[duplicates[i + j]]) {
                rowtemp = movesrow[duplicates[i]];
                coltemp = movescol[duplicates[i]];
            }
            else {
                rowtemp = movesrow[duplicates[i + j]];
                coltemp = movescol[duplicates[i + j]];
            }
        }
    }
    row = rowtemp;
    col = coltemp;
    if (validMove(board, n, (row), (col), colour)) {
        board[row][col] = colour;
        printf("\nComputer places %c at %c%c\n", colour, (row + 'a'), (col + 'a'));
        printBoard(board, n);
        return true;

    }
    else {
        return false;
    }
}

bool
UserMove(char board[][26], int n, char colour, char row, char col)
{
    int movesrow[100] = { 0 };
    int movescol[100] = { 0 };
    int count = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (board[i][j] == 'U') {
                if (checkLegalInDirection(board, n, i, j, colour, -1, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

                if (checkLegalInDirection(board, n, i, j, colour, -1, 0)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }
                if (checkLegalInDirection(board, n, i, j, colour, -1, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

                if (checkLegalInDirection(board, n, i, j, colour, 0, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

                if (checkLegalInDirection(board, n, i, j, colour, 0, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

                if (checkLegalInDirection(board, n, i, j, colour, 1, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }
                if (checkLegalInDirection(board, n, i, j, colour, 1, 0)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }
                if (checkLegalInDirection(board, n, i, j, colour, 1, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

            }

        }
        count += 1;
    }

    int bestMoves[100] = { 0 };
    int tracker = 0;
    int tot = 0;

    for (int i = 0; i < count; i++) {

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }
        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, 0);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {

            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 0, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 0, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, 0);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }
    }

    // if player runs out of moves
    if (bestMoves[0] == 0) {
        printf("%c player has no valid move.", colour);
        return false;
    }

    printf("\nEnter a move for colour %c (RowCol): ", colour);
    scanf(" %c%c", &row, &col);
    if (validMove(board, n, (row - 'a'), (col - 'a'), colour)) {
        board[row - 'a'][col - 'a'] = colour;
        printBoard(board, n);
    }
}

bool
validMove(char board[][26], int n, int row, int col, char colour)
{
    int score = 0;

    if (checkLegalInDirection(board, n, row, col, colour, -1, -1)) {
        score++;
        int i = 1;

        while (board[row + (i * -1)][col + (i * -1)] != colour) {
            board[row + (i * -1)][col + (i * -1)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, -1, 0)) {
        score++;

        int i = 1;

        while (board[row + (i * -1)][col + (i * 0)] != colour) {
            board[row + (i * -1)][col + (i * 0)] = colour;
            i++;
        }

    }
    if (checkLegalInDirection(board, n, row, col, colour, -1, 1)) {
        score++;

        int i = 1;

        while (board[row + (i * -1)][col + (i * 1)] != colour) {
            board[row + (i * -1)][col + (i * 1)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, 0, -1)) {
        score++;

        int i = 1;

        while (board[row + (i * 0)][col + (i * -1)] != colour) {
            board[row + (i * 0)][col + (i * -1)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, 0, 1)) {
        score++;

        int i = 1;

        while (board[row + (i * 0)][col + (i * 1)] != colour) {
            board[row + (i * 0)][col + (i * 1)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, 1, -1)) {
        score++;

        int i = 1;

        while (board[row + (i * 1)][col + (i * -1)] != colour) {
            board[row + (i * 1)][col + (i * -1)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, 1, 0)) {
        score++;

        int i = 1;

        while (board[row + (i * 1)][col + (i * 0)] != colour) {
            board[row + (i * 1)][col + (i * 0)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, 1, 1)) {
        score++;

        int i = 1;

        while (board[row + (i * 1)][col + (i * 1)] != colour) {
            board[row + (i * 1)][col + (i * 1)] = colour;
            i++;
        }
    }

    if (score > 0) {

        return true;
    }
    else {
        return false;
    }
}

void
printBoard(char board[][26], int n)
{
    printf("  ");
    for (int i = 0; i < n; i++) {
        printf("%c", 'a' + i);
    }

    for (int m = 0; m < n; m++) {
        printf("\n%c ", 'a' + m);
        for (int j = 0; j < n; j++) {
            printf("%c", board[m][j]);
        }
    }
}

bool
positionInBounds(int n, int row, int col)
{
    if (row >= 0 && row < n && col >= 0 && col < n) {
        return true;
    }
    else {
        return false;
    }
}

bool
checkLegalInDirection(char board[][26], int n, int row, int col, char colour,
    int deltaRow, int deltaCol)
{

    if (positionInBounds(n, row, col) == false) {
        return false;
    }

    if (board[row][col] != 'U') {
        return false;
    }

    row += deltaRow;
    col += deltaCol;

    if (positionInBounds(n, row, col) == false) {
        return false;
    }

    if (board[row][col] == colour || board[row][col] == 'U') {
        return false;
    }

    while ((positionInBounds(n, row, col)) == true) {
        if (board[row][col] == colour) {
            return true;
        }
        if (board[row][col] == 'U') {
            return false;
        }

        row += deltaRow;
        col += deltaCol;
    }
    return false;
}

int
checkBestMoves(char board[][26], int n, int row, int col, char colour,
    int deltaRow, int deltaCol)
{
    int tiles = 0;

    if (positionInBounds(n, row, col) == false) {
        return false;
    }

    if (board[row][col] != 'U') {
        return false;
    }

    row += deltaRow;
    col += deltaCol;

    if (positionInBounds(n, row, col) == false) {
        return false;
    }

    if (board[row][col] == colour || board[row][col] == 'U') {
        return false;
    }

    while ((positionInBounds(n, row, col)) == true) {
        if (board[row][col] == colour) {
            return tiles;
        }
        if (board[row][col] == 'U') {
            return false;
        }
        tiles += 1;

        row += deltaRow;
        col += deltaCol;

    }
    return false;
}

bool
checkifmove(char board[][26], int n, char color)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (board[i][j] == 'U') {
                if (checkLegalInDirection(board, n, i, j, color, -1, -1) ||
                    checkLegalInDirection(board, n, i, j, color, -1, 0) ||
                    checkLegalInDirection(board, n, i, j, color, -1, 1) ||
                    checkLegalInDirection(board, n, i, j, color, 0, -1) ||
                    checkLegalInDirection(board, n, i, j, color, 0, 1) ||
                    checkLegalInDirection(board, n, i, j, color, 1, -1) ||
                    checkLegalInDirection(board, n, i, j, color, 1, 0) ||
                    checkLegalInDirection(board, n, i, j, color, 1, 1)) {
                    return true;
                }
            }
        }
    }
    return false;
}

我搞乱了缩短和扩大数组大小,但似乎没有任何效果。我也尝试过使用 malloc 但遇到了同样的问题。

c segmentation-fault
1个回答
0
投票

一些问题...

  1. 对于
    -Wall
    ,参数
    col
    row
    char
    并且编译器会抱怨这一点(转换为
    int
  2. row
    col
    已统一化(在
    main
    中),但 computerMove
    UserMove
    永远不会使用
    (设置为零以消除警告)。他们不需要需要争论。

当电路板尺寸为

8
且计算机为
B
时,在
computerMove
中,消毒剂抱怨
if
:

for (int i = 0; i < counter; i++) {
    for (int j = 0; j < n; j++) {
        dbgprt("i=%d j=%d tracker=%d\n",i,j,tracker);
        if ((movesrow[duplicates[i]] < movesrow[duplicates[i + j]]) &&
            movescol[duplicates[i]] < movescol[duplicates[i + j]]) {
            rowtemp = movesrow[duplicates[i]];
            coltemp = movescol[duplicates[i]];
        }
        else {
            rowtemp = movesrow[duplicates[i + j]];
            coltemp = movescol[duplicates[i + j]];
        }
    }
}

我添加的调试

printf
显示:

i=0 j=0 tracker=4
i=0 j=1 tracker=4
i=0 j=2 tracker=4
i=0 j=3 tracker=4
i=0 j=4 tracker=4

数组

duplicates
的维度为
tracker
(即 4),因此
i + j
变得太大并溢出
duplicates
。这是 UB(未定义行为)。


这是重构后的代码。注释为:

#include <stdio.h>
#include <stdlib.h>
#if 0
#include "lab8part1.h"
#else
#include <stdbool.h>
#endif

#if DEBUG
#define dbgprt(_fmt...) \
    printf(_fmt)
#else
#define dbgprt(_fmt...)
    do { } while (0)
#endif

void printBoard(char board[][26], int n);
bool positionInBounds(int n, int row, int col);
bool checkLegalInDirection(char board[][26], int n, int row, int col, char colour, int deltaRow, int deltaCol);
bool validMove(char board[][26], int n, int row, int col, char colour);
#if 0
bool computerMove(char board[][26], int n, char colour, char row, char col);
bool UserMove(char board[][26], int n, char colour, char row, char col);
#else
bool computerMove(char board[][26], int n, char colour, int row, int col);
bool UserMove(char board[][26], int n, char colour, int row, int col);
#endif
int checkBestMoves(char board[][26], int n, int row, int col, char colour, int deltaRow, int deltaCol);
bool checkifmove(char board[][26], int n, char color);

int
main(void)
{
#if 0
    char board[26][26], color, row, col;
#else
    char board[26][26], color;
    int row, col;
#endif

    printf("Enter the board dimension: ");
    int n;

    scanf(" %d", &n);
    int start = n / 2;

    for (int m = 0; m < n; m++) {
        for (int j = 0; j < n; j++) {
            if ((m == start - 1 && j == start - 1) ||
                (m == start && j == start)) {
                board[m][j] = 'W';

            }
            else if ((m == start - 1 && j == start) ||
                (m == start && j == start - 1)) {
                board[m][j] = 'B';

            }
            else {
                board[m][j] = 'U';

            }
        }
    }

    printf("Computer plays (B/W): ");
    scanf(" %c", &color);
    char color1;

    if (color == 'B') {
        color1 = 'W';
    }
    else {
        color1 = 'B';
    }
    printBoard(board, n);
    printf("\n");
    char turn = 'B';
    bool validmove = true;

// NOTE/BUG: these are unitialized, but the neither computerMove nor UserMove
// use them. they do _not_ need to be arguments
#if 1
    row = 0;
    col = 0;
#endif

    while ((checkifmove(board, n, color) == true) ||
        (checkifmove(board, n, color1) == true)) {
        validmove = true;

        if (turn == color) {
            validmove = computerMove(board, n, color, row, col);
        }
        else {
            UserMove(board, n, color1, row, col);
        }

        if (validmove == false) {
            break;
        }

        if (turn == 'W') {
            turn = 'B';
        }
        else {
            turn = 'W';
        }

    }
    int whitwin = 0;
    int blacwin = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (board[i][j] == 'W') {
                whitwin += 1;
            }
            else {
                blacwin += 1;
            }
        }
    }
    if (whitwin > blacwin) {
        printf("W player wins.");
    }
    else if (whitwin < blacwin) {
        printf("B player wins.");
    }
    else {
        printf("Draw.");
    }
    return 0;
}

#if 0
bool
computerMove(char board[][26], int n, char colour, char row, char col)
#else
bool
computerMove(char board[][26], int n, char colour, int row, int col)
#endif
{

    int movesrow[100] = { 0 };
    int movescol[100] = { 0 };
    int count = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (board[i][j] == 'U') {
                if (checkLegalInDirection(board, n, i, j, colour, -1, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

                if (checkLegalInDirection(board, n, i, j, colour, -1, 0)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }
                if (checkLegalInDirection(board, n, i, j, colour, -1, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;
                }

                if (checkLegalInDirection(board, n, i, j, colour, 0, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

                if (checkLegalInDirection(board, n, i, j, colour, 0, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

                if (checkLegalInDirection(board, n, i, j, colour, 1, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }
                if (checkLegalInDirection(board, n, i, j, colour, 1, 0)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }
                if (checkLegalInDirection(board, n, i, j, colour, 1, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;

                }

            }

        }
        count += 1;
    }

    int bestMoves[600] = { 0 };
    int tracker = 0;
    int tot = 0;

    for (int i = 0; i < count; i++) {

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }
        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, 0);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {

            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 0, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 0, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, 0);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }
    }

    // if computer runs out of moves
    if (bestMoves[0] == 0) {
        printf("%c player has no valid move.", colour);
        return false;
    }

    int counter = 0;
    int bigNum = bestMoves[0];
    int duplicates[tracker];

    for (int i = 1; i < tracker; i++) {
        if (bestMoves[i] > bigNum) {
            bigNum = bestMoves[i];
            duplicates[0] = i;
            counter = 1;
        }
        else if (bestMoves[i] == bigNum) {
            duplicates[counter] = i;
            counter++;
        }
    }
    int rowtemp = 0,
        coltemp = 0;

    for (int i = 0; i < counter; i++) {
        for (int j = 0; j < n; j++) {
            dbgprt("i=%d j=%d tracker=%d\n",i,j,tracker);
// NOTE/BUG: i + j will exceed tracker -- this is UB
            if ((movesrow[duplicates[i]] < movesrow[duplicates[i + j]]) &&
                movescol[duplicates[i]] < movescol[duplicates[i + j]]) {
                rowtemp = movesrow[duplicates[i]];
                coltemp = movescol[duplicates[i]];
            }
            else {
                rowtemp = movesrow[duplicates[i + j]];
                coltemp = movescol[duplicates[i + j]];
            }
        }
    }
    row = rowtemp;
    col = coltemp;
    if (validMove(board, n, (row), (col), colour)) {
        board[row][col] = colour;
        printf("\nComputer places %c at %c%c\n", colour, (row + 'a'), (col + 'a'));
        printBoard(board, n);
        return true;

    }
    else {
        return false;
    }
}

#if 0
bool
UserMove(char board[][26], int n, char colour, char row, char col)
#else
bool
UserMove(char board[][26], int n, char colour, int row, int col)
#endif
{
    int movesrow[100] = { 0 };
    int movescol[100] = { 0 };
    int count = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (board[i][j] == 'U') {
                if (checkLegalInDirection(board, n, i, j, colour, -1, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;
                }

                if (checkLegalInDirection(board, n, i, j, colour, -1, 0)) {
                    movesrow[count] = i;
                    movescol[count] = j;
                }

                if (checkLegalInDirection(board, n, i, j, colour, -1, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;
                }

                if (checkLegalInDirection(board, n, i, j, colour, 0, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;
                }

                if (checkLegalInDirection(board, n, i, j, colour, 0, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;
                }

                if (checkLegalInDirection(board, n, i, j, colour, 1, -1)) {
                    movesrow[count] = i;
                    movescol[count] = j;
                }

                if (checkLegalInDirection(board, n, i, j, colour, 1, 0)) {
                    movesrow[count] = i;
                    movescol[count] = j;
                }

                if (checkLegalInDirection(board, n, i, j, colour, 1, 1)) {
                    movesrow[count] = i;
                    movescol[count] = j;
                }
            }
        }
        count += 1;
    }

    int bestMoves[100] = { 0 };
    int tracker = 0;
    int tot = 0;

    for (int i = 0; i < count; i++) {

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }
        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, 0);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, -1, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {

            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 0, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 0, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, -1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, 0);
            if (tot != 0) {
                bestMoves[tracker] = tot;

            }

        }

        if (tot != 0) {
            tracker += 1;
        }
        tot = 0;
        for (int j = 0; j < count; j++) {
            tot += checkBestMoves(board, n, movesrow[i], movescol[i], colour, 1, 1);
            if (tot != 0) {
                bestMoves[tracker] = tot;
            }

        }
    }

    // if player runs out of moves
    if (bestMoves[0] == 0) {
        printf("%c player has no valid move.", colour);
        return false;
    }

    printf("\nEnter a move for colour %c (RowCol): ", colour);
#if 0
    scanf(" %c%c", &row, &col);
#else
    char c_row, c_col;
    scanf(" %c%c", &c_row, &c_col);
    row = c_row;
    col = c_col;
#endif
    if (validMove(board, n, (row - 'a'), (col - 'a'), colour)) {
        board[row - 'a'][col - 'a'] = colour;
        printBoard(board, n);
    }
    return true;
}

bool
validMove(char board[][26], int n, int row, int col, char colour)
{
    int score = 0;

    if (checkLegalInDirection(board, n, row, col, colour, -1, -1)) {
        score++;
        int i = 1;

        while (board[row + (i * -1)][col + (i * -1)] != colour) {
            board[row + (i * -1)][col + (i * -1)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, -1, 0)) {
        score++;

        int i = 1;

        while (board[row + (i * -1)][col + (i * 0)] != colour) {
            board[row + (i * -1)][col + (i * 0)] = colour;
            i++;
        }

    }
    if (checkLegalInDirection(board, n, row, col, colour, -1, 1)) {
        score++;

        int i = 1;

        while (board[row + (i * -1)][col + (i * 1)] != colour) {
            board[row + (i * -1)][col + (i * 1)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, 0, -1)) {
        score++;

        int i = 1;

        while (board[row + (i * 0)][col + (i * -1)] != colour) {
            board[row + (i * 0)][col + (i * -1)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, 0, 1)) {
        score++;

        int i = 1;

        while (board[row + (i * 0)][col + (i * 1)] != colour) {
            board[row + (i * 0)][col + (i * 1)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, 1, -1)) {
        score++;

        int i = 1;

        while (board[row + (i * 1)][col + (i * -1)] != colour) {
            board[row + (i * 1)][col + (i * -1)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, 1, 0)) {
        score++;

        int i = 1;

        while (board[row + (i * 1)][col + (i * 0)] != colour) {
            board[row + (i * 1)][col + (i * 0)] = colour;
            i++;
        }
    }
    if (checkLegalInDirection(board, n, row, col, colour, 1, 1)) {
        score++;

        int i = 1;

        while (board[row + (i * 1)][col + (i * 1)] != colour) {
            board[row + (i * 1)][col + (i * 1)] = colour;
            i++;
        }
    }

    if (score > 0) {

        return true;
    }
    else {
        return false;
    }
}

void
printBoard(char board[][26], int n)
{
    printf("  ");
    for (int i = 0; i < n; i++) {
        printf("%c", 'a' + i);
    }

    for (int m = 0; m < n; m++) {
        printf("\n%c ", 'a' + m);
        for (int j = 0; j < n; j++) {
            printf("%c", board[m][j]);
        }
    }
}

bool
positionInBounds(int n, int row, int col)
{
    if (row >= 0 && row < n && col >= 0 && col < n) {
        return true;
    }
    else {
        return false;
    }
}

bool
checkLegalInDirection(char board[][26], int n, int row, int col, char colour,
    int deltaRow, int deltaCol)
{

    if (positionInBounds(n, row, col) == false) {
        return false;
    }

    if (board[row][col] != 'U') {
        return false;
    }

    row += deltaRow;
    col += deltaCol;

    if (positionInBounds(n, row, col) == false) {
        return false;
    }

    if (board[row][col] == colour || board[row][col] == 'U') {
        return false;
    }

    while ((positionInBounds(n, row, col)) == true) {
        if (board[row][col] == colour) {
            return true;
        }
        if (board[row][col] == 'U') {
            return false;
        }

        row += deltaRow;
        col += deltaCol;
    }
    return false;
}

int
checkBestMoves(char board[][26], int n, int row, int col, char colour,
    int deltaRow, int deltaCol)
{
    int tiles = 0;

    if (positionInBounds(n, row, col) == false) {
        return false;
    }

    if (board[row][col] != 'U') {
        return false;
    }

    row += deltaRow;
    col += deltaCol;

    if (positionInBounds(n, row, col) == false) {
        return false;
    }

    if (board[row][col] == colour || board[row][col] == 'U') {
        return false;
    }

    while ((positionInBounds(n, row, col)) == true) {
        if (board[row][col] == colour) {
            return tiles;
        }
        if (board[row][col] == 'U') {
            return false;
        }
        tiles += 1;

        row += deltaRow;
        col += deltaCol;

    }
    return false;
}

bool
checkifmove(char board[][26], int n, char color)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (board[i][j] == 'U') {
                if (checkLegalInDirection(board, n, i, j, color, -1, -1) ||
                    checkLegalInDirection(board, n, i, j, color, -1, 0) ||
                    checkLegalInDirection(board, n, i, j, color, -1, 1) ||
                    checkLegalInDirection(board, n, i, j, color, 0, -1) ||
                    checkLegalInDirection(board, n, i, j, color, 0, 1) ||
                    checkLegalInDirection(board, n, i, j, color, 1, -1) ||
                    checkLegalInDirection(board, n, i, j, color, 1, 0) ||
                    checkLegalInDirection(board, n, i, j, color, 1, 1)) {
                    return true;
                }
            }
        }
    }
    return false;
}

在上面的代码中,我使用

cpp
条件来表示旧代码与新代码:

#if 0
// old code
#else
// new code
#endif

#if 1
// new code
#endif

注意:这可以通过运行文件来清理

unifdef -k

© www.soinside.com 2019 - 2024. All rights reserved.