gpt4 book ai didi

c - 我的游戏板无限循环

转载 作者:行者123 更新时间:2023-11-30 17:29:42 24 4
gpt4 key购买 nike

我的游戏板出现无休止循环的问题。连续的任何三个字母都会被删除,并继续该过程,直到没有更多的字母为止。当角色用完时,它应该停止运行,但我不确定我哪里出错了。有什么帮助吗?

编辑:问题似乎出在 updateBoard 函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX 100

void command(int argc) {
if (argc != 3) { // Takes in 3 arguments. File name/Size/Number of letters
printf("You need 3 arguments");
} else {
}
}

int args(char* arg) {
int size;
size = atoi(arg); // Convert string to int
return size; // Return int
}

int checkBoard(char myArray[MAX][MAX], int size) {
int i, j; // Loop through board
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
if (myArray[i][j] == myArray[i + 1][j] &&
myArray[i + 1][j] == myArray[i + 2][j]) {
myArray[i][j] = ' '; // Change 3 cells to blanks
myArray[i + 1][j] = ' ';
myArray[i + 2][j] = ' ';
}
if (myArray[i][j] == myArray[i][j + 1] &&
myArray[i][j + 1] == myArray[i][j + 2]) {
myArray[i][j] = ' '; // Change cells to blanks
myArray[i][j + 1] = ' ';
myArray[i][j + 2] = ' ';
}
}
}

int pass2;
pass2 = updateBoard(myArray, size); // Pass array board to next function

updateBoard(myArray, size);
}

int updateBoard(char myArray[MAX][MAX], int size) {
int i, j;
for (i = 0; i < size; i++) // Loops through and prints board
{
for (j = 0; j < size; j++) {
if (myArray[i][j] == ' ' && myArray[i + 1][j] == ' ' &&
myArray[i + 2][j] == ' ') {
myArray[i][j] =
myArray[i][j - 1]; // Drop cell if existing cells are blank
myArray[i + 1][j] = myArray[i][j - 1];
myArray[i + 2][j] = myArray[i][j - 1];
}
if (myArray[i][j] == ' ' && myArray[i][j + 1] == ' ' &&
myArray[i][j + 2] == ' ') {
myArray[i][j] = myArray[i][j - 4]; // Same as above
myArray[i][j + 1] = myArray[i][j - 4];
myArray[i][j + 2] = myArray[i][j - 4];
}
}
}
for (i = 0; i < size; i++) // Loop through and print updated board
{
for (j = 0; j < size; j++) {
printf("%c ", myArray[i][j]);
}
printf("\n");
}
printf("\n");

int flag;
if (myArray[MAX][MAX]) {
flag = 1;
} else {
flag = 0;
}

while (flag != 1) {
int pass3;
pass3 = checkBoard(myArray, size);
checkBoard(myArray, size);
}
}

int main(int argc, char* argv[]) {
int i, j;
int size, sarg; // Two arguments

command(argc); // Call command function

printf("test1\n");
size = args(argv[1]); // Passing arguments
printf("test2\n");
sarg = args(argv[2]);
printf("test3\n");
printf("%s\n", argv[1]);
printf("%s\n", argv[2]);
printf("Arg1: %d\nArg2: %d\n\n", size, sarg);

srand(time(NULL)); // Use of time to generate random characters

static char myArray[MAX][MAX];
char letter =
'A' + (rand() % sarg); // 26 possible letters for second argument

for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
myArray[i][j] = 'a' + (rand() % sarg); // Loop through and fill
// board
}
}

for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("%c ", myArray[i][j]); // Loop through and print board
}
printf("\n");
}
printf("\n");

int pass; // Pass initial board to checkboard function
pass = checkBoard(myArray, size);

int count;

checkBoard(myArray, size); // Call function to generate updated board

printf("The number of moves made is %d: ", count);
}

PS:每次棋盘掉落单元格时,main 中的计数都会增加,以计算已进行的移动次数。关于如何计算函数执行次数的任何建议?干杯

最佳答案

如果flag的值不为1,则这是一个无限循环。

 while(flag != 1){
int pass3;
pass3 = checkBoard(myArray,size);
checkBoard(myArray,size);
}

关于c - 我的游戏板无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25554761/

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