gpt4 book ai didi

使用 C 在 Connect N 中检查对角线获胜者

转载 作者:行者123 更新时间:2023-12-04 12:26:43 26 4
gpt4 key购买 nike

我正在使用 C 进行 Connect N 游戏,我很难尝试编写算法来检查对角线上是否有赢家。

char **BASE;
int piecesToWin, ROWS, COLS;
printf("Enter how many connected pieces to win?: ");
scanf("%d", &piecesToWin);

ROWS = 2 + piecesToWin;
COLS = 3 + piecesToWin;
BASE = (char **) malloc(ROWS * sizeof(char *)); // Allocate the row pointers
for (int i = 0; i < ROWS; i++) // Allocate each row separately
BASE[i] = (char *) malloc(COLS * sizeof(char));

// Fill all with space to start
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
BASE[i][j] = ' ';

在此之后,我在棋盘上插入一块,然后我检查对角线是否有赢家。
if (diagAscWin(ROWS, COLS, BASE, piecesToWin, 'X')) {
printf("X has won!.");
}

if (diagDescWin(ROWS, COLS, BASE, piecesToWin, 'X')) {
printf("X has won!.");
}

我试图对角线检查的内容:
bool diagAscWin(int ROWS, int COLS, char **BASE, int numToWin, char letter) {
bool isWinner = false;
for (int i=3; i<ROWS; i++){
for (int j=0; j<COLS-3; j++){
if (BASE[i][j] == letter && BASE[i-1][j+1] == letter && BASE[i-2][j+2] == letter && BASE[i-3][j+3] == letter)
isWinner = true;
}
}

return isWinner;
}

bool diagDescWin(int ROWS, int COLS, char **BASE, int numToWin, char letter) {
bool isWinner = false;
for (int i=3; i<ROWS; i++){
for (int j=3; j<COLS; j++){
if (BASE[i][j] == letter && BASE[i-1][j-1] == letter && BASE[i-2][j-2] == letter && BASE[i-3][j-3] == letter)
isWinner = true;
}
}

return isWinner;
}

这些功能不起作用。当我对角线有N个时, diagAscWindiagDescWin不要看到他们,总是返回 false .另外,我不确定是否需要初始化 ij3j<COLS-3 .我试图从另一个线程复制解决方案。

另外,我试过这个:
bool diagRightWin(int ROWS, int COLS, char **BASE, int numToWin, char letter) {
bool isWinner = false;
int count;
for (int rowStart = 0; rowStart < ROWS - 4; rowStart++) {
count = 0;
int row, col;
for (row = rowStart, col = 0; row < ROWS && col < COLS; row++, col++) {
if (BASE[row][col] == letter) {
count++;
if (count >= numToWin) isWinner = true;
} else {
count = 0;
}
}
}

return isWinner;
}

bool diagLeftWin(int ROWS, int COLS, char **BASE, int numToWin, char letter) {
bool isWinner = false;
int count;
for (int rowStart = 0; rowStart < ROWS - 4; rowStart++) {
count = 0;
int row, col;
for (row = rowStart, col = 0; row < ROWS && col < COLS; row++, col++) {
if (BASE[row][col] == letter) {
count++;
if (count >= numToWin) isWinner = true;
} else {
count = 0;
}
}
}

return isWinner;
}

这两个函数也总是返回 false .

我尝试了不同的逻辑,但没有运气。正确的做法是检查多少 numToWin件对角线表示是否有赢家。

我试图复制:
  • Connect 4 check for a win algorithm
  • Java: How to check diagonal Connect Four win in 2D array
  • Javascript - Connect Four game validation
  • Checking for a line of four (Connect Four in Java)

  • 我的目标是检查对角线(在任何方向)是否有赢家,考虑到棋盘可大可小,我们不知道大小。我们确定的是 piecesToWin这是需要放在一起才能说有赢家的件数。

    最佳答案

    主要问题是当您找到 4 个元素的匹配项时,您并未退出该函数。还需要修改函数循环,如图所示。

    您需要两个函数来检查左对角线和右对角线

       /  <-- left diagonal     right diagonal --> \
    / \
    / \
    / \

    我修改了检查函数,并添加了检查对角线中可变数量元素的功能。
    // this function checks diagnonal sloping left "/"
    //
    bool diagLeftWin(int ROWS, int COLS, char **BASE, int numToWin, char letter) {
    for (int i=0; i<ROWS-numToWin; i++){
    for (int j=0; j<COLS-numToWin; j++){
    char curr = BASE[i][j];
    if (curr == letter) {
    int k;
    for (k=1; k<numToWin; k++) {
    if (curr != BASE[i+k][j+k]) {
    break;
    }
    }
    if (k == numToWin) {
    // match found
    return true;
    }
    }
    }
    }
    return false;
    }

    // this function checks diagnonal sloping right "\"
    //
    bool diagLeftWin(int ROWS, int COLS, char **BASE, int numToWin, char letter) {
    for (int i=ROWS-numToWin; i>=0; i--){
    for (int j=0; j<COLS-numToWin; j++){
    char curr = BASE[i][j];
    if (curr == letter) {
    int k;
    for (k=1; k<numToWin; k++) {
    if (curr != BASE[i-k][j+k]) {
    break;
    }
    }
    if (k == numToWin) {
    // match found
    return true;
    }
    }
    }
    }
    return false;
    }

    这些函数每次都会检查整个电路板。您可以优化它以仅检查新添加到板上的部件。

    关于使用 C 在 Connect N 中检查对角线获胜者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58946306/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com