[英]My game board loops endlessly
我的游戲板不斷循環出現問題。 連續刪除任何三個字母,然后繼續進行此過程,直到沒有更多的字母為止。 當字符用完時,應該停止運行,但是我不確定我要去哪里。 有什么幫助嗎?
編輯 :問題似乎出在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);
}
要回答有關如何計算函數執行次數的問題,請在函數中聲明一個初始化為0的static
局部變量,並按如下方式遞增一次:
int foo() {
static int count = 0;
count++;
// Rest of the function here
}
首次運行程序時, static
變量將初始化為0,並在兩次函數調用之間保持其值-每次調用函數時均不會重新初始化它。 由於在每個函數調用中遞增,因此它計算調用次數。
這是一個局部變量,因此您必須在程序中放置一個斷點並在調試器中查看其值,或者為函數提供一種返回值的方式,以便程序的其余部分可以讀取其值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.