【发布时间】:2018-04-07 04:13:06
【问题描述】:
这是家庭作业 所以我必须编写一个简单的拼字游戏。我在整个程序中都有 cmets,但我会在这篇文章的结尾解释我想要做什么。
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#define N 96
int main() {
srand((unsigned) time(NULL));
int letter_set = N , size_let = 7 , num_let = 7 , max_size_word = 7 , size_letter_set = 7, size_word, arr[N];
char word [7];
printf("This program plays a game of scrabble.\n");
generate_letter_set(letter_set , size_let , num_let, arr);
read_word(word, max_size_word);
check_word(word, size_word, letter_set, size_letter_set, arr);
return 0;
}
void generate_letter_set(int letter_set[] , int size_let , int num_let, int arr[])
{
const char let[26] =
{'K','J','X','Q','Z','B','C','M','P','F','H','V','W','Y','G','L','S','U','D','N','R','T','O','A','I','E'};
int freq[26] =
{ 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 6, 6, 6, 8, 9, 9, 12 };
const int score[26] =
{ 5, 8, 8, 10, 10, 3, 3, 3, 3, 4, 4, 4, 4, 4, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1};
int index = 0;
for(int i = 0 ; i < 26 ; i++) {
for(int f = 0 ; f < freq[i]; f++) {
arr[index++] = let[i]; //All the 96 letters are stored in let[i]
//printf("%c " , let[i]); // Created the letter bank for all the letters
}
}
int letter;
printf("Your letters are: ");
for(int l = 0; l < 7; l++){
letter = rand() % 97; //Gives the user their letters all the letters are from arr[letter]
printf("%c ", arr[letter]);
}
}
int read_word(char word[], int max_size_word) {
{
int c = 0, let_count = 0;
printf("\nPlease enter your word: ");
char input = toupper(getchar());
for(c = 0; c < max_size_word; c++) {
if(input != '\n')
{ word[c] = input;
let_count++;
}
else if(input == '\n')
input = toupper(getchar()); //The word the user entered is in word[c]
}
return let_count;
}
}
int check_word(char word[], int size_word, int letter_set[], int
size_letter_set, int arr[]) {
//Figure out how to pass two arrays through the functions
//Pass word[c] into this function
//Pass arr[letter] into this function then compare the two arrays
//Make it so the user has to enter less than 7 chars
for (int a; a < 7; a++) {
if (word[a] != arr[a]) {
printf("Use your letters");
}
}
return 1;
}
所以我在这个程序中唯一的问题是我将如何让我的“check_word”函数工作。此功能必须检查用户是否输入了提供的字母。在拼字游戏中,你得到 7 个字母,给用户的 7 个字母的数组存储在 (arr[]) 然后'read_word'函数中的字母是用户进入。输入的字母存储在 word[] 中。所以我检查用户是否真的使用了 arr[] 中的字母的直觉是做一个比较两个数组 arr[] 和 word[ ]。但是我意识到这将检查用户是否真的使用了每一个字母,我只需要检查用户是否使用了任何未提供的字母。我不知道如何做到这一点,任何帮助将不胜感激!如果需要任何澄清,也请在 cmets 中告诉我,我也为这个巨大的帖子道歉。
【问题讨论】:
-
您不是已经在这里发布了这个问题吗:stackoverflow.com/questions/46901918/… 您可以编辑您的帖子以进一步澄清而不是重新发布。就像我在那里发布的那样,为用户提供的字母建立一个频率表,并将它们与建议单词中的字母进行核对。
-
@MFisherKDX ,对不起,我对这个网站有点陌生,我没有意识到有一个编辑选项,但是频率表不会在拼字游戏中包含每个可能的字母吗?
-
letter = rand() % 97;是在'A'到'Z'范围内选择一个字母的糟糕方法。另外,字母袋应该有每个字母瓦片的特定数量,所以我建议你找到一种算法,随机挑选一个瓦片并将其从袋子中取出。磁贴必须是带有字符和分数的struct。根据剩余的数量从袋子(一个数组)中选择一个瓦片struct,并调整数组。 -
@Weather Vane 我明白你在说什么,但对于这个项目来说,这不是必需的,因为用户只会玩一次拼字游戏,所以从数组中递减字母不会产生任何影响。跨度>
-
我可以看到您尝试使用
int freq[26]对可用的瓷砖数量进行建模,但仍然可以在信包中使用struct数组。