Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Piet Souris

scrabble game problem

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Here is the function that generates the letters:

And here's my Main function:

And here is the scores from the file:

Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Catherine

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[26] 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)).
Posts: 67976
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!