• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hangman game. Long arrays for words, not sure the best way to handle it.  RSS feed

 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I'm just finishing up making a hangman game and now i'm adding a good variety of words and i was hoping to get an opinion on the best way of doing this.

In my hangman game i have a class called WordList. I'm just in the middle of altering the class to hold all the words i need, as before i only had about 10 to test the program. There are three difficulty levels in my game so i was going to have 3 arrays each containing the words for each difficulty level. I also orginally had these arrays as instance variables. Now i intend to have each difficulty level have about 8 different words per letter which works out at about 624 words in total. And one day i may add more.

So i was wondering which, if any of these ideas is the best way to handle these long arrays. I mean for both code readability and for memory usage and just generally the most appropriate way to go about it.


Idea 1: I could just have three arrays (one for each difficulty level) as instance variables, and method generateWord() returns one of these words from the appropriate difficulty level at random . But with this idea i thought it would waste memory, especially if i add more and more words. Am i correct by saying that?


Idea 2: Method generateWord() could check which difficulty level the user has selected and then call an appropriate method (generateEasyWord(), generateMediumWord()). And then in these methods it could create the array of words and return a random word.

Idea 3: I could break it down even further then Idea 2 by using the generateEasyWord() or generateMediumWord() methods to randomly pick a number between 1 and 26 and then use that to choose which array to create. I could divide the arrays up so each of the 26 arrays contain words beginning with a different letter of the alphabet. Would i be right in saying that this would save memory because the only arrays that would be created are the ones it needs and then they would quickly go out of scope? This is the idea i was currently doing then i suddenly realised my code looked insane and i better ask for help. I had many long long long switch statments; one for each difficulty level, each with 26 cases containing arrays with words beginning with each letter of the alphabet.

So thanks for reading and any advice would be greatly appreciated. Forgive me if i don't post the code for the class as i wanted to hold off finishing it until i got an opinion.

 
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I would like to know is how you store your words, because an answer would depend on how much it costs to read / sort / search the words.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:What I would like to know is how you store your words, because an answer would depend on how much it costs to read / sort / search the words.


In arrays like this....



Although that is much shorter. There will be many more words. So i need to decide how to group them together and how much to split them up. Like to create different arrays for each letter of the alphabet or not ect.,
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put those words into a file.

Format:
difficulty;word

Write a reader that takes a level, open the file, look for all words with the requested level and put them into a list. Generate a random number with a max value (size of list!) and return that word. Reading a file with several hundred lines is inexpensive. Or hold the complete file in memory, split up into a list for each level.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why do you need to store all 600+ words in memory since you need only one word per game? what if you create a text or xml file and store everything you need there. Then, as soon as the new game starts pick one random word and present it to the player.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garik Ustinov wrote:But why do you need to store all 600+ words in memory since you need only one word per game? what if you create a text or xml file and store everything you need there. Then, as soon as the new game starts pick one random word and present it to the player.


Oh i see. Well to be honest i've not learned how to do that yet. I'm working my way through a book and it's not covered that so far, but i'm sure it will soon. I guess that will be in the next chapter about I/O.

So i guess there is no professional way to do it like i'm trying to do as no one would store all the words in arrays.

Thanks for the input, i appreciate you taking the time.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Dillinger wrote:Put those words into a file.

Format:
difficulty;word

Write a reader that takes a level, open the file, look for all words with the requested level and put them into a list. Generate a random number with a max value (size of list!) and return that word. Reading a file with several hundred lines is inexpensive. Or hold the complete file in memory, split up into a list for each level.


OK thanks for the reply. I didn't see your post when replying the last person. I think that kind of thing will be covered in the next chapter of the book i'm working my way through. I'll come back to my program then. For now i'll maybe just use fewer words.
 
Garik Ustinov
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neil Cartmell wrote: For now i'll maybe just use fewer words.


Yup, it's not the best practice to hard-code a huge amount of data, presuming that the largest part of it won't be needed. It's not about memory (however, of course at some point it'll start to matter), but about style. Create a WordFactory class or interface, hard-code some words for testing and then as soon as you're comfortable with file IO (which isn't too hard) replace the implementation. Goodluck
 
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you added another person into the picture, like with a real hangman game, where one person picks a word then the other tries to guess it; you wouldn't need to store any data at all. I realize that probably isn't your goal for this game, but just an idea.


Hunter
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garik Ustinov wrote:
Neil Cartmell wrote: For now i'll maybe just use fewer words.


Yup, it's not the best practice to hard-code a huge amount of data, presuming that the largest part of it won't be needed. It's not about memory (however, of course at some point it'll start to matter), but about style. Create a WordFactory class or interface, hard-code some words for testing and then as soon as you're comfortable with file IO (which isn't too hard) replace the implementation. Goodluck


Thanks!
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:If you added another person into the picture, like with a real hangman game, where one person picks a word then the other tries to guess it; you wouldn't need to store any data at all. I realize that probably isn't your goal for this game, but just an idea.


Hunter


Yes good idea. I might add a two player opttion later. At the moment i can't stop playing it. haha The death scene is brutal, it might really offend stickmen though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!