为什么会发生这种情况(C编程运行时错误)?

时间:2019-04-11 12:10:38

标签: c

当我为单词搜索游戏运行此代码时,它会打印菜单并允许用户输入数字,但是只是坐着却什么也不做。

WordSearch Game

C代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define WIDTH 16
#define HEIGHT 16
#define NWORDS 6

char wordsearch[HEIGHT][WIDTH];

/* horizontaly */
int canPlaceH(const char * word, int i, int j) {
    if ((strlen(word) + j) > WIDTH)
        return 0;
    do {
        if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != * word))
            return 0;
        j += 1;
    } while ( * ++word);
    return 1;
}

void placeH(const char * word, int i, int j) {
    do {
        wordsearch[i][j++] = * word;
    } while ( * ++word);
}

/* verticaly */
int canPlaceV(const char * word, int i, int j) {
    if ((strlen(word) + i) > HEIGHT)
        return 0;
    do {
        if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != * word))
            return 0;
        i += 1;
    } while ( * ++word);
    return 1;
}

void placeV(const char * word, int i, int j) {
    do {
        wordsearch[i++][j] = * word;
    } while ( * ++word);
}

/* diagonal up */
int canPlaceDU(const char * word, int i, int j) {
    int ln = strlen(word);
    if (((ln + j) > WIDTH) || ((i - ln) < 0))
        return 0;
    do {
        if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != * word))
            return 0;
        i -= 1;
        j += 1;
    } while ( * ++word);
    return 1;
}

void placeDU(const char * word, int i, int j) {
    do {
        wordsearch[i--][j++] = * word;
    } while ( * ++word);
}

/* diagonal down */
int canPlaceDD(const char * word, int i, int j) {
    int ln = strlen(word);
    if (((ln + j) > WIDTH) || ((i + ln) > HEIGHT))
        return 0;
    do {
        if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != * word))
            return 0;
        i += 1;
        j += 1;
    } while ( * ++word);
    return 1;
}

void placeDD(const char * word, int i, int j) {
    do {
        wordsearch[i++][j++] = * word;
    } while ( * ++word);
}

void fillWordsearch(const char ** a, int sz) {
    /* first step add words from a */
    const char * used[NWORDS - 1] = {
        NULL
    };
    /* to not get two times the same word */
    for (int w = 0; w != NWORDS; ++w) {
        /* random choice of a word not yet used */
        const char * word;
        for (;;) {
            word = a[rand() % sz];
            int i;
            /* check not yet used */
            for (i = 0; i != w; ++i)
                if (!strcmp(used[i], word))
                    break;
            if (i == w) {
                /* not yet used */
                used[w] = word;
                break;
            }
        }

        /* random placement */
        int i, j, d;
        typedef int( * canFunc)(const char * , int, int);
        typedef void( * placeFunc)(const char * , int, int);
        const canFunc canPlace[] = {
            canPlaceH,
            canPlaceV,
            canPlaceDU,
            canPlaceDD
        };
        const placeFunc place[] = {
            placeH,
            placeV,
            placeDU,
            placeDD
        };

        do {
            i = rand() % HEIGHT;
            j = rand() % WIDTH;
            d = rand() % 4;
        } while (!( * canPlace[d])(word, i, j));
        ( * place[d])(word, i, j);

#ifdef DEBUG
  for (int i = 0; i != HEIGHT; ++i) {
    for (int j = 0; j != WIDTH; ++j)
      putchar((wordsearch[i][j] == 0) ? '.' : wordsearch[i][j]);
    putchar('\n');
  }
  putchar('\n');
#endif

  /* second step fill not yet set characters with random lowercase letters */
  int q,w;

  for (q = 0; q < HEIGHT; q++)
    for (w = 0; w != WIDTH; ++w)
      if (wordsearch[q][w] == 0)
        wordsearch[q][w] = 'a' + rand() % ('z' - 'a' + 1);
    }
}

int main() {
  const char *animalArray[] = {
    "lynx",
    "kitten",
    "cheetah",
    "ape",
    "doe",
    "reindeer",
    "whale",
    "baboon",
    "skunk",
    "dugong",
    "elephant",
    "anteater",
    "chameleon",
    "lizard",
    "horse"
  };

  const char *colourArray[] = {
    "red",
    "green",
    "blue",
    "black",
    "pink",
    "yellow",
    "brown",
    "orange",
    "purple",
    "black",
    "white",
    "cyan",
    "maroon",
    "magenta",
    "gray"
  };

  const char *videogameArray[] = {
    "fortnite",
    "fifa",
    "hytale",
    "soma",
    "prey",
    "destiny",
    "titanfall",
    "woldenstein",
    "battlefield",
    "fallout",
    "tekken",
    "skyrim",
    "dishonored",
    "uncharted",
    "anthem"
  };

  const char *sportsArray[] = {
    "basketball",
    "football",
    "cricket",
    "wrestling",
    "fencing",
    "rowing",
    "volleyball",
    "baseball",
    "hockey",
    "racing",
    "golf",
    "bobsleigh",
    "curling",
    "snowboarding",
    "bowling"
 };

  const char *countryArray[] = {
    "england",
    "ireland",
    "china",
    "wales",
    "bangladesh",
    "maldives",
    "slovenia",
    "uruguay",
    "colombia",
    "samoa",
    "jamaica",
    "malta",
    "bulgaria",
    "armenia",
    "gamnbia"
  };

  printf("---------------------------------\n");
  printf("| Welcome to the WordSearch Game |\n");
  printf("---------------------------------\n");
  printf("Choose a Category - \n");
  printf("Option 1 - Animals\n");
  printf("Option 2 - Colours\n");
  printf("Option 3 - Video Games\n");
  printf("Option 4 - Sports\n");
  printf("Option 5 - Countries\n");

  int i;

  if ((scanf("%d", &i) != 1) || (i < 1) || (i > 5))
    return -1;

  srand(time(NULL));

  switch (i) {
  case 1:
    fillWordsearch(animalArray, 
sizeof(animalArray)/sizeof(*animalArray));
    break;
  case 2:
    fillWordsearch(colourArray, 
sizeof(colourArray)/sizeof(*colourArray));
    break;
  case 3:
    fillWordsearch(videogameArray, 
sizeof(videogameArray)/sizeof(*videogameArray));
    break;
  case 4:
    fillWordsearch(sportsArray, 
sizeof(sportsArray)/sizeof(*sportsArray));
    break;
  default:
    fillWordsearch(countryArray, 
sizeof(countryArray)/sizeof(*countryArray));
    break;
  }

  /* print result */
  for (i = 0; i != HEIGHT; ++i) {
    for (int j = 0; j != WIDTH; ++j)
      putchar(wordsearch[i][j]);
    putchar('\n');
  }

  return 0;
}

1 个答案:

答案 0 :(得分:1)

有两个错误:     used中数组fillWordsearch的大小;     在同一功能中,}(第95行)中的symble for放在很远的地方。

这是固定代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define WIDTH 16
#define HEIGHT 16
#define NWORDS 6

char wordsearch[HEIGHT][WIDTH];

/* horizontaly */
int canPlaceH(const char * word, int i, int j) {
    if ((strlen(word) + j) > WIDTH)
        return 0;
    do {
        if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != * word))
            return 0;
        j += 1;
    } while ( * ++word);
    return 1;
}

void placeH(const char * word, int i, int j) {
    do {
        wordsearch[i][j++] = * word;
    } while ( * ++word);
}

/* verticaly */
int canPlaceV(const char * word, int i, int j) {
    if ((strlen(word) + i) > HEIGHT)
        return 0;
    do {
        if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != * word))
            return 0;
        i += 1;
    } while ( * ++word);
    return 1;
}

void placeV(const char * word, int i, int j) {
    do {
        wordsearch[i++][j] = * word;
    } while ( * ++word);
}

/* diagonal up */
int canPlaceDU(const char * word, int i, int j) {
    int ln = strlen(word);
    if (((ln + j) > WIDTH) || ((i - ln) < 0))
        return 0;
    do {
        if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != * word))
            return 0;
        i -= 1;
        j += 1;
    } while ( * ++word);
    return 1;
}

void placeDU(const char * word, int i, int j) {
    do {
        wordsearch[i--][j++] = * word;
    } while ( * ++word);
}

/* diagonal down */
int canPlaceDD(const char * word, int i, int j) {
    int ln = strlen(word);
    if (((ln + j) > WIDTH) || ((i + ln) > HEIGHT))
        return 0;
    do {
        if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != * word))
            return 0;
        i += 1;
        j += 1;
    } while ( * ++word);
    return 1;
}

void placeDD(const char * word, int i, int j) {
    do {
        wordsearch[i++][j++] = * word;
    } while ( * ++word);
}

void fillWordsearch(const char ** a, int sz) {
    /* first step add words from a */
    const char * used[NWORDS] = {
        NULL
    };
    /* to not get two times the same word */
    for (int w = 0; w != NWORDS; ++w) {
        /* random choice of a word not yet used */
        const char * word;
        for (;;) {
            word = a[rand() % sz];
            int i;
            /* check not yet used */
            for (i = 0; i != w; ++i)
                if (!strcmp(used[i], word))
                    break;
            if (i == w) {
                /* not yet used */
                used[w] = word;
                break;
            }
        }

        /* random placement */
        int i, j, d;
        typedef int( * canFunc)(const char * , int, int);
        typedef void( * placeFunc)(const char * , int, int);
        const canFunc canPlace[] = {
            canPlaceH,
            canPlaceV,
            canPlaceDU,
            canPlaceDD
        };
        const placeFunc place[] = {
            placeH,
            placeV,
            placeDU,
            placeDD
        };

        do {
            i = rand() % HEIGHT;
            j = rand() % WIDTH;
            d = rand() % 4;
        } while (!( * canPlace[d])(word, i, j));
        ( * place[d])(word, i, j);
    }

#ifdef DEBUG
  for (int i = 0; i != HEIGHT; ++i) {
    for (int j = 0; j != WIDTH; ++j)
      putchar((wordsearch[i][j] == 0) ? '.' : wordsearch[i][j]);
    putchar('\n');
  }
  putchar('\n');
#endif

  /* second step fill not yet set characters with random lowercase letters */
  int q,w;

  for (q = 0; q < HEIGHT; q++)
    for (w = 0; w != WIDTH; ++w)
      if (wordsearch[q][w] == 0)
        wordsearch[q][w] = 'a' + rand() % ('z' - 'a' + 1);
}

int main() {
  const char *animalArray[] = {
    "lynx",
    "kitten",
    "cheetah",
    "ape",
    "doe",
    "reindeer",
    "whale",
    "baboon",
    "skunk",
    "dugong",
    "elephant",
    "anteater",
    "chameleon",
    "lizard",
    "horse"
  };

  const char *colourArray[] = {
    "red",
    "green",
    "blue",
    "black",
    "pink",
    "yellow",
    "brown",
    "orange",
    "purple",
    "black",
    "white",
    "cyan",
    "maroon",
    "magenta",
    "gray"
  };

  const char *videogameArray[] = {
    "fortnite",
    "fifa",
    "hytale",
    "soma",
    "prey",
    "destiny",
    "titanfall",
    "woldenstein",
    "battlefield",
    "fallout",
    "tekken",
    "skyrim",
    "dishonored",
    "uncharted",
    "anthem"
  };

  const char *sportsArray[] = {
    "basketball",
    "football",
    "cricket",
    "wrestling",
    "fencing",
    "rowing",
    "volleyball",
    "baseball",
    "hockey",
    "racing",
    "golf",
    "bobsleigh",
    "curling",
    "snowboarding",
    "bowling"
 };

  const char *countryArray[] = {
    "england",
    "ireland",
    "china",
    "wales",
    "bangladesh",
    "maldives",
    "slovenia",
    "uruguay",
    "colombia",
    "samoa",
    "jamaica",
    "malta",
    "bulgaria",
    "armenia",
    "gamnbia"
  };

  printf("---------------------------------\n");
  printf("| Welcome to the WordSearch Game |\n");
  printf("---------------------------------\n");
  printf("Choose a Category - \n");
  printf("Option 1 - Animals\n");
  printf("Option 2 - Colours\n");
  printf("Option 3 - Video Games\n");
  printf("Option 4 - Sports\n");
  printf("Option 5 - Countries\n");

  int i;

  if ((scanf("%d", &i) != 1) || (i < 1) || (i > 5))
    return -1;

  srand(time(NULL));

  switch (i) {
  case 1:
    fillWordsearch(animalArray, 
sizeof(animalArray)/sizeof(*animalArray));
    break;
  case 2:
    fillWordsearch(colourArray, 
sizeof(colourArray)/sizeof(*colourArray));
    break;
  case 3:
    fillWordsearch(videogameArray, 
sizeof(videogameArray)/sizeof(*videogameArray));
    break;
  case 4:
    fillWordsearch(sportsArray, 
sizeof(sportsArray)/sizeof(*sportsArray));
    break;
  default:
    fillWordsearch(countryArray, 
sizeof(countryArray)/sizeof(*countryArray));
    break;
  }

  /* print result */
  for (i = 0; i != HEIGHT; ++i) {
    for (int j = 0; j != WIDTH; ++j)
      putchar(wordsearch[i][j]);
    putchar('\n');
  }

  return 0;
}

示例输出:

---------------------------------
| Welcome to the WordSearch Game |
---------------------------------
Choose a Category - 
Option 1 - Animals
Option 2 - Colours
Option 3 - Video Games
Option 4 - Sports
Option 5 - Countries
5
wqcelsjdbeqlqmkq
qsczholsedsavojs
emymaldivesrehuf
nksewcupvyoenbwt
hotceewiqucxdwer
jzvhbrwyqmdfozaa
bvqvxuzwfsshriwf
axnqwiyybuytrhdy
nxfasjamaicavvrr
gxnypwihiegnfrdc
lbdccaacwlhgwglx
ayiuplzrufainhqr
duwkaeablfcpfelo
ekkuxsjshdkjxrjh
seqyxnwjnwusylae
hzorenglandjjqsb

顺便说一句,这个游戏似乎很难玩;)