我的游戏板问题无休止地循环。连续中的任何三个字母都将被删除,并且该过程将继续进行,直到不再有字母为止。当角色用完时它应该停止运行,但我不确定我哪里出错了。有什么帮助吗?
编辑 :问题似乎出现在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: 每当主板丢弃单元格以计算已经进行的数字移动时,主计数就会增加。关于如何计算函数执行次数的任何建议?干杯
答案 0 :(得分:0)
如果flag的值不是1,则这是一个无限循环。
while(flag != 1){
int pass3;
pass3 = checkBoard(myArray,size);
checkBoard(myArray,size);
}
答案 1 :(得分:0)
要回答关于如何计算函数执行次数的问题,请在函数中声明一个static
局部变量初始化为0,并按如下方式递增一次:
int foo() {
static int count = 0;
count++;
// Rest of the function here
}
首次运行程序时,static
变量初始化为0,并在函数调用之间保持其值 - 每次调用函数时都不会重新初始化它。由于它在每个函数调用中递增,因此它会计算调用次数。
它是一个局部变量,因此您必须在程序中放置断点并在调试器中查看其值,或者为函数提供一种返回值的方法,以便程序的其余部分可以读取其值。