这是一个代码,用户有一个“单词搜索游戏”,用户有一个菜单,他可以选择做一个新的拼图,然后显示它并尝试解决它。用户需要输入4个单词,输入的单词需要以矩阵显示,供其他用户查找和播放。我的代码问题是当用户输入4个单词时,系统崩溃并存在程序。 另外,我如何将用户写入的单词放在矩阵中?
以下代码:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
int main(void)
{
srand((unsigned)time(NULL));
char Matrix[10][10];
char Location[4][4];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
Matrix[i][j] = (rand() % (91 - 65)) + 65;
}
}
int menuChoice;
do
{
system("cls");
puts("WORD SEARCH GAME PUZZLE");
puts("-----------------------");
puts("1. New Puzzle");
puts("2. Show Puzzle");
puts("3. Exit");
puts("-----------------------");
puts("Select your choice: ");
scanf_s("%d", &menuChoice);
switch (menuChoice)
{
case 1: newPuzzle(); break;
case 2: showPuzzle(Matrix); break;
}
} while (menuChoice != 3);
return 0;
}
int newPuzzle()
{
int i;
char word[7];
system("cls");
printf("Enter 4 words of your choice to be entered in the puzzle: \n");
for (i = 0; i < 4; i++)
{
printf("Enter word %d: ", i + 1);
scanf_s("%s", &word);
}
return 0;
}
int showPuzzle(char m[10][10])
{
system("cls");
printf(" ");
char c;
for (c = 'A'; c <= 'J'; ++c)
{
printf("%c ", c);
}
printf("\n\n");
for (int i = 0; i < 10; i++) {
printf("%d ", i);
for (int j = 0; j < 10; j++) {
printf("%c ", m[i][j]);
}
printf("\n");
}
getch();
return 0;
}