Please excuse the messy code I'm currently making a scrabble game that can be run from the terminal. The game requires to read the letter scores from a text file and to generate the letters randomly. While I have the letters generated and the scores reading from the file, I am having trouble assigning the scores to the letters that are generated. Since the scores are in an array, I don't know how to get the score that should match the letter generated. Here's the function to read the text from the file:
First of all, I will excuse the messy code but I really do suggest spending a bit of time tidying it up. It will make everyone's job easier.
So, the code...
In main() you have array to hold the letter scores read from the file. Calling it (say) letter_scores would be a lot better.
But then you have array_length = 7, which is the number of lines the code will read from the file. I can see the file does contain 26 lines, one for each letter, so presumably array_length should be 26?
Relatively minor point - in generate_letters() the comment says 'half of the letters should be vowels', and the first for() loop does do that. But the second for() loop doesn't avoid vowels, so it's likely you'll end up with more than 50% vowels. The second loop should only store the randomly selected letter if it isn't a vowel - use strchr() to check if the letter is in vowels (although you'll have to make vowels a string to do that ie. vowels = "aeiou" to get the string terminator on the end which strchr() needs).
So assuming you fix the above and end up with a generated word in destination (although word would be a much better name, and why size 26?), the score for letter number i in the word is:
Note the - 'a' to convert the letter ('a'..'z') into an array index (0..25). And you should use a for() loop over the letters in destination rather than lots of separate printfs.
By the way, instead of using separate variables to hold array sizes, you can use code to give you the size of an array:
Then you could get rid of number (=ARRAY_SZ(destination)) and array_length (=ARRAY_SZ(array)).
John Matthews wrote:. . . messy code . . . tidying it up. It will make everyone's job easier. . . .
It will specifically make your job easier. If the code is badly formatted, it is you who will have the most trouble with it. If you aren't using an IDE, get a decent text editor (e.g. on Windows® use NotePad++) which can help to do the formatting as you go. You will find that much easier than tidying the code afterwards.
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad: