![]() |
|
#1
|
|||
|
|||
Help on Basic Design C++My problems lie in remembering how to generate a random number. I have the frame set down for this program, but honestly, it's barely worth putting here.
Hangman is an old guessing game, good for improving vocabulary. Your assignment is to write a program to play the game Hangman. The game keeps a list of words to be guessed in a file, called words.dat. The very first piece of data in the words.dat file is an integer number that represents how many words are in the file. The format of the rest of the words.dat file is one word per line in lower case letters, in no particular order, with no spaces. The idea of Hangman is that the computer picks a word at random and lets the player guess a letter to see if it is in the word or not. The program gives feedback about whether the letter is a good guess or not. The player keeps guessing until they guess all the letters in the word and they win, or until they lose. If the player misses 7 times, gives a letter not in the word, they are "hung" and lose the game. Each time the player misses, another part of a "body" is drawn. Here are two examples of the interaction between the player and your program. Capture of one game Capture of another game So you can see that if the player guesses some wrong letters, then a correct one, the count of errors does NOT go back to zero, but stays where it is, and the parts of the body are redrawn. (The program does NOT have to handle uppercase input.) Part I Write a design on paper of how you think you will do the game. Bring this to the scheduled design lab. You and your TA will discuss designs. Your TA will be expecting more participation this time than last design lab. Think about what kind of loops you will need. Think about each function - what are its preconditions and postconditions? what does each parameter mean? Part II * Write void functions that draw different parts of the body, one for each of the 7 parts above. Some will have to draw two or three parts, such as the left arm, body and right arm, and the right and left legs. Feel free to improve on the graphic art. * Write a void function that will call the drawing functions above, depending on one parameter which could be 1, 2, ... up to 7. A one would mean draw the head, 2 would mean draw the head and neck, 3 would mean draw the head, neck and left arm, and so on. * Write a string function that gets a word at random from a file and returns it as the value of the function. It should use the random number generator to get a random number in the range 1 to number of words in the file, then open the file and read in that many words. Return the last word that was read in. Random number generation will be discussed in class shortly. * Write a string function that will create and return a string of all stars that is as long as the parameter sent in. For example, if it were called as "stars = makestars (5);" it would return "*****" and "stars = makestars ( You should test these functions independently, to see that they are working correctly before you move on. In summary, you need the functions that draw the body parts, the function that calls those functions, a "getword" function, and a "makestars" function. * body parts (7 of them) - void and have no parameters * draw body parts - void and has one parameter * get a word - returns a string and has no parameters * make stars - returns a string and has one parameter Part III Implement your main function by using these functions to play the game. The traditional rule for Hangman is that if a player guesses a letter in the word, that EVERY occurrence of the letter is displayed (e.g., if there are three e's, all three are displayed). We are going to simplify this a bit by saying that only ONE occurrence of the letter (the first one from the left end of the word) is displayed. So a player could guess an e three times if they wanted. You will be using some libraries for this program. Document what you are using from each library. This program will use string functions extensively. Make sure you include the string library and that you document which functions you are going to use from it. Testing: You should think carefully about testing your program. One game will produce a good bit of output. Please capture two test runs. During a game you should be able to test many different things - for example, correct guesses followed by wrong guesses and vice versa, a game with all correct guesses, etc. Label these captures so that it is clear what you are testing. CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
Re: Help on Basic Design C++You can generate the random number by using rand() and srand() functions in the cstdlib library.
First, to generate a random number, we should seed the random function. For this, we should do: CPP / C++ / C Code:
After we seed the rand function we are ready to use it. Here is a sample: CPP / C++ / C Code:
But see the output: Code:
We should limit the random numbers within a certain range! For example, if the number of words in the file is n, then we can do like this: CPP / C++ / C Code:
Now, rand() / 32767 will give a value between 0 and 1. If we multiply it with n, then the values will be between 0 and 27!!! Now, the sample output for n = 27 is: Code:
Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: Help on Basic Design C++Thanks for the help, you seem to be very active here.
|
|
#4
|
|||
|
|||
Re: Help on Basic Design C++Oh, but I need to make a function that picks a random word out of a file. I know I need to make a string function for this. How would I do this?
I have to show how many letters the word is using "*'s". Then I need to know how to allow users to guess a letter and determine if it is in the word that was picked at random. If the letter is correct, it would swap that letter in for a "*". If not, I will use a counter to display at what point the "man" is in hanging. |
|
#5
|
||||
|
||||
Re: Help on Basic Design C++maliquen,
You can use the random number generated to get a particular word from a file. For example, consider that you have a file: Code:
So, we should get the number of lines in the file first, say n. Then we should create a random number within the limits of n; After that, we should skip n number of lines in the file. Use getline to skip lines. Then we should read one string, which represents the randomly selected string. Regarding this: Quote:
Calculate the number of letters using a loop, say num. Then, show num '*' s in the console using a loop, by storing in a separate string say s1. Allow the user to enter his letter. Search for the particular letter in the string s. If the letter is present, replace the * in the s1 with the corresponding letter. Use a counter to count whether the user entered a valid value or not.(as you said) Best Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
Recent GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [ANN] New script engine: Open Basic (Basic syntax) | MKTMK | Computer Programming Advertisements & Offers | 0 | 01-Sep-2005 06:13 |
| Hello from cali, questions about web design | jonnydangerous | New Member Introductions | 3 | 20-Aug-2004 13:59 |
| The resurrection for the "designers adrenalin kick" | Michael Christe | Web Design Forum | 0 | 19-Jan-2003 04:36 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The