循环没有完成它在C中的迭代

时间:2018-03-15 01:17:52

标签: c loops

但是我的游戏循环遇到了麻烦!目前,当进行无效移动时,循环正确迭代。但是,当进行有效移动时,我的程序会显示已更改的板,但是在循环中间似乎会冻结!我有它冻结的位置(代码中的测试点2),但我不知道为什么。我的代码所需的输出是继续迭代,直到没有可用的移动。 (我还没有创建没有动作的检查,因为我无法让我的代码在正常的计数循环中运行!)

代码无效时有效,有效时有效,如下所示。 here

问题发生的主要功能

int main(void)
{
    int n;
    int p;
    char oppChar;
    char playChar;
    int i;
    int pTurn;
    int deltaRow;
    int deltaCol;
    int endGame = 999;

    printf("Enter the board dimension: ");
    fflush(stdout);
    scanf("%d", &n);

    printf("Computer plays (B/W) ");
    fflush(stdout);
    scanf(" %c", &oppChar);

    if (oppChar == 'B') {
        pTurn = 0;
        playChar = 'W';
    } else {
        pTurn = 1;
        playChar = 'B';
    }

    char board[n][26];
    int movesAvalB[n][26];
    int movesAvalW[n][26];
    int AIBoard[n][26];

    for (i = 0; i < n; i++) {
        for (p = 0; p < n; p++) {
            board[i][p] = 'U';
        }
    }

    board[(n / 2) - 1][(n / 2) - 1] = 'W';
    board[(n / 2) - 1][(n / 2)] = 'B';
    board[(n / 2)][(n / 2) - 1] = 'B';
    board[n / 2][n / 2] = 'W';
    printBoard(board, n);


    int q = 0;
    do {
        q++;
        // testing
        printf(" \n");
        printf("Turn Number %d \n", pTurn);


        // variable decleration
        int k = 0;
        int y = 0;
        int x = 0;
        int max = 0;
        int temp = 0;
        char c[3] = " ";

        // arrays are all 0
        for (i = 0; i < n; i++) {
            for (p = 0; p < n; p++) {
                AIBoard[i][p] = 0;
                movesAvalB[i][p] = 0;
                movesAvalW[i][p] = 0;
            }
        }

        // moves avaliable to W
        for (y = 0; y < n; y++) {
            for (x = 0; x < n; x++) {
                if (moves(n, x, y, board, 'W')) {
                    movesAvalW[y][x] = 1;
                }
            }
        }
        printf("Test Spot 1\n");

        // moves avaliable to B
        for (y = 0; y < n; y++) {
            for (x = 0; x < n; x++) {
                if (moves(n, x, y, board, 'B')) {
                    printf("Test Spot 1.5\n");
                    movesAvalB[y][x] = 1;
                }
            }
        }
        fflush(stdout);
        printf("Test Spot 2\n");

        // pTurn = pTurn%2;
        fflush(stdout);
        printf("Enter a move for colour %c (RowCol): \n", playChar);
        fflush(stdout);
        scanf("%s", &c);

        if (positionInBounds(n, c[0], c[1])) {
            int y = c[0] - 97;
            int x = c[1] - 97;

            if (playChar == 'B') {
                if (movesAvalB[y][x] == 1) {
                    for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
                        for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
                            if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) {
                                i = 1;

                                while ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
                                       (board[y + (i * deltaRow)][x + (i * deltaCol)] == 'W')) {

                                    i++;
                                    if ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
                                        (board[y + (i * deltaRow)][x + (i * deltaCol)] == 'B')) {
                                        while (i != 0) {
                                            i--;
                                            board[y + (i * deltaRow)][x + (i * deltaCol)] = 'B';
                                        }
                                    }
                                }
                            }
                        }
                    }
                    for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
                        for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
                            if (board[y + deltaRow][x + deltaCol] == 'W') {
                                board[y + deltaRow][x + deltaCol] == 'B';
                            }
                        }
                    }

                    printBoard(board, n);

                } else {
                    printf("Invalid Move.");
                }
            }

            if (playChar == 'W') {
                if (movesAvalW[y][x] == 1) {
                    for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
                        for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
                            if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) {
                                i = 1;

                                while ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
                                       (board[y + (i * deltaRow)][x + (i * deltaCol)] == 'B')) {

                                    i++;
                                    if ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
                                        (board[y + (i * deltaRow)][x + (i * deltaCol)] == 'W')) {
                                        while (i != 0) {
                                            i--;
                                            board[y + (i * deltaRow)][x + (i * deltaCol)] = 'W';
                                        }
                                    }
                                }
                            }
                        }
                    }
                    for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
                        for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
                            if (board[y + deltaRow][x + deltaCol] == 'B') {
                                board[y + deltaRow][x + deltaCol] == 'W';
                            }
                        }
                    }
                    printBoard(board, n);

                } else {
                    printf("Invalid Move.");
                }
            }
        } else {
            printf("Invalid Move.");
        }
        pTurn++;

    } while (q <= 5);
    printf("were out");
}

您可能不需要但可能

的辅助功能
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

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

    int p;
    int q;
    for (p = 0; p < n; p++) {
        printf("\n");
        printf("%c", alpha[p]);
        for (q = 0; q < n; q++) {
            printf("%c", board[p][q]);
        }
    }
}

bool positionInBounds(int n, char row, char col)
{
    int p = row - 97;
    int d = col - 97;

    if (p > n) {
        return false;
    }
    if (d > n) {
        return false;
    }
    if (0 > p) {
        return false;
    }
    if (0 > d) {
        return false;
    }

    return true;
}

bool positionIntBounds(int n, int row, int col)
{
    if (row > n) {
        return false;
    }
    if (col > n) {
        return false;
    }
    if (0 > row) {
        return false;
    }
    if (0 > col) {
        return false;
    }

    return true;
}

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

    while ((positionIntBounds(n, (row + (i * deltaRow)), (col + (i * deltaCol)))) &&
           (board[row + (i * deltaRow)][col + (i * deltaCol)] != colour) &&
           (board[row + (i * deltaRow)][col + (i * deltaCol)] != 'U')) {
        i++;
        if ((positionIntBounds(n, (row + (i * deltaRow)), (col + (i * deltaCol)))) &&
            (board[row + (i * deltaRow)][col + (i * deltaCol)] == colour)) {

            return true;
        }
    }
    return false;
}

bool moves(int n, int x, int y, char board[][26], char colour)
{

    int deltaRow;
    int deltaCol;

    if (board[y][x] == 'U') {

        for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
            for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
                if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) {

                    if (checkLegalInDirection(board, n, (x + deltaRow), (y + deltaCol), colour, deltaRow, deltaCol)) {

                        return true;
                    }
                }
            }
        }
    }
    return false;
}

(我被告知在我的scanf语句之前放fflush(stdout),但这并没有解决问题)

0 个答案:

没有答案