GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Nov-2005, 17:31
maliquenavidad maliquenavidad is offline
New Member
 
Join Date: Oct 2005
Posts: 8
maliquenavidad is on a distinguished road

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 (;" would return "********".

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:
 #include <iostream>
#include <string>
#include <fstream>

using namespace std;

swap (int a, int b); //swaps word and stars

int main ()

{

	string word, partial, x; //full word and stars and string to close loop
	int cont, a, b; //counter, a and b swaped characters between word and partial [ ]


	while (x != 'no')

        word.find()
		if 

		else 
		 cont = cont + 1;
		 // insert void funtion here, arm

		cout<< "Play again?";
	    cin >> x;




	return 0;
}

   swap (int a, int b)
{
	
		int tmp = a;
		    a   = b;
			b   = tmp;

}
  #2  
Old 27-Nov-2005, 17:46
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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:
    srand((unsigned)time( NULL ));    
This will generate different seed depending upon the local time.

After we seed the rand function we are ready to use it.
Here is a sample:
CPP / C++ / C Code:
    for(i = 0; i < 10; i++)
    {
        cout<< rand() <<endl;
    }
This will print 10 random numbers.

But see the output:
Code:
27286 19949 19368 1956 28372 30665 12680 5605 17842 10889
It is generating random values from 0 to 32767.

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:
    n = 27;           //consider, for example that the number of lines is 27

    for(i = 0; i < 10; i++)
    {
        cout<< n * rand() / 32767 <<endl; 
    }


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:
23 2 1 9 11 11 10 11 24 23


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  
Old 28-Nov-2005, 17:56
maliquenavidad maliquenavidad is offline
New Member
 
Join Date: Oct 2005
Posts: 8
maliquenavidad is on a distinguished road

Re: Help on Basic Design C++


Thanks for the help, you seem to be very active here.
  #4  
Old 28-Nov-2005, 19:19
maliquenavidad maliquenavidad is offline
New Member
 
Join Date: Oct 2005
Posts: 8
maliquenavidad is on a distinguished road

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  
Old 28-Nov-2005, 21:24
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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:
10 windows linux microsoft redhat oracle playstation winamp mozilla firefox opera
The first line gives the number of words in the file.

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:
Originally Posted by maliquen
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.
The string s is the randomly selected string.
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 GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 18:20.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.