This is for Homework So I have to program a simple game of scrabble. I have comments throughout my whole program but I'll explain what I wanted to do towards the end of this post.
#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;
}
So my only issue in this program is how I'm going to get my 'check_word' function to work. This function has to check if the user entered the letters that are provided. In a game of scrabble you get 7 letters, and the array that has the 7 letters given to the user is stored in (arr[]) Then the in the 'read_word' function is the letters the user entered. The letters that are entered are stored in word[]. So my intuition to check if the user actually used the letters from arr[] was to make a conditional statement that compared the two arrays arr[] and word[]. However I realize that would check if the user actually used every single letter and I just have to check if the user used any letters that weren't provided. I'm lost on how to make this happen and any help would be appreciated! Also please let me know in the comments if any clarification is needed, also I apologize for the huge post.
So my only issue in this program is how I'm going to get my 'check_word' function to work. This function has to check if the user entered the letters that are provided. In a game of scrabble you get 7 letters, and the array that has the 7 letters given to the user is stored in (arr[]) Then the in the 'read_word' function is the letters the user entered.
There are many ways to solve this problem. Here is one way. Build a frequency table of the letters that are provided to the user. So if the user has been provided: R S T L N E E, then freq['R'] = 1, freq['S] = 1, freq['T'] = 1, freq['L'] = 1, freq['N'] = 1 and freq['E'] = 2. All the other values in freq are 0. Then when the user enters a word, you can loop over each letter and subtract 1 from the frequency table of that letter. If any value is less than 0, then the word is invalid.
However, you have another problem in your code regarding randomizing the input -- ie: dealing the letters to the user. @Weathervane has made a few comments there that might help. Consider putting all the letters into a set (or more properly a bag -- since letters can be repeated) and randomly drawing a letter from the bag. Repeat 7 times (or until the bag is empty).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With