检查用户输入是否与拼字游戏迷你游戏中的给定数组匹配

时间:2017-04-11 08:25:58

标签: c

下面是一个名为scrabble.c的程序,用于播放游戏的简化版本。向用户发出7个随机字符,然后告诉用这些字符输入单词。程序应该检查用户是否使用给出的字母来创建有效的单词。我遇到了检查用户是否输入有效单词的功能问题。

#include <stdio.h>
#include <time.h>
#include <stdbool.h>

#define SIZE_LETTER_SET 100
#define SIZE_CHOOSEN_LETTER 7

void generate_letter_set(int letter_set[7], int size_letter_set, int 
num_letters)
{

//num_letters should be 7
//size_letter_set should be 100

bool choosen[SIZE_LETTER_SET] = {false};
int group[SIZE_LETTER_SET] =
{'A','A','A','A','A','A','A','A','A','B','B','C','C','D','D','D','D','E','E','E','E','E','E','E','E','E','E','E','E','F','F','G','G','G','H','H',
'I','I','I','I','I','I','I','I','I','J','K','L','L','L','L','M','M','N','N','N','N','N','N','O','O','O','O','O','O','O','O','P','P','Q','R','R','R','R','R','R','S','S',
'S','S','T','T','T','T','T','T','U','U','U','U','V','V','W','W','X','Y','Y','Z',' ',' '};

     int c, letter, letterfound;
     srand((unsigned) time(NULL));
     for(c = 0; c < num_letters; c++)
     {  
         letterfound = 1;
    while(letterfound % 2 != 0) 
    {       
        letter = rand() % 100;

        if(choosen[letter] == false)
        {           
                letter_set[c] = group[letter];          
             choosen[letter] = true;
             letterfound++;
        }
    }

}

printf("Your letters are: ");
for(c = 0; c < num_letters; c++)
{
printf("%c ", letter_set[c]);
}
printf("\n");
return;
}


int read_word(char word[], int max_size_word)
{
int c = 0, input_count = 0;
printf("Please enter your word : ");

char user_input = getchar();
for(c = 0; c < max_size_word; c++)
{
    if(user_input != '\n')
    {
    word[c] = user_input;   
    input_count++;
    //printf("input_count = %d, letter entered = %c\n", input_count, 
user_input);
    }
    else if(user_input == '\n')
    {
    return input_count;
    }
    user_input = getchar();
}

return input_count;
}

此函数是发生错误的地方,该函数应检查用户是否使用7个字母创建有效字。

 _Bool check_word (char word[], int size_word, int letter_set[], int 
size_letter_set)
{
printf("beginning word check : \n");
int c = 0;
int b = 0; 
int dup_count = 0;
int pair_found = 0;
for(c = 0; c < size_word; c++);
pair_found = 1;
    for(b = 0; b < size_letter_set; b++)
    {
        printf("Checking if %c is equal to %c", word[c], letter_set[b]);
        if((toupper(word[c])) == letter_set[b])
        {
            letter_set[b] = 0;
            dup_count++;
            pair_found++;

        }
    }
printf("Total duplicates = %d : check word is ", dup_count);
if((dup_count >= size_word))
{
printf("true");
return true;
}
else
{
printf("false");
return false;
}
   }


int main(void)
{
int letter_set[7] = {0};
char word[7] = {0};
int size_letter_set = 100;
int num_letters = 7;

generate_letter_set(letter_set, size_letter_set, num_letters);
int size_word = read_word(word, num_letters);
check_word(word, num_letters, letter_set, size_letter_set);



return 0;
}

0 个答案:

没有答案