|
-
May 14th, 1999, 10:30 AM
#1
hangman again
there is a minor problem with hangman; whenever a word is randomly chosen, if it is less then six letters, the program attaches junk to the end until it equals six characters (junk is random characters ' X . )what shall i do?
//program Hangman
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
//------------------definition of class hangman----------------------
class hangman {
public:
hangman();
//constructor
char* getWord(int&);
//gets word to be guessed
int getLength();
//gets length of word
void initTemp();
//creates temporary holder for unguessed characters
void checkGuess(char&, int&);
//checks user guess against inputted word
char* SecWord;
char* temp;
protected:
int letterOfSec;
};
//-------------------------------------------------------------------
//__________________-Hangman Member Functions-_____________________
/*****************************************************************/
hangman::hangman()
{
SecWord = temp = new char[13];
letterOfSec = 0;
}
/******************************************************************/
char* hangman::getWord(int& diff)
{
char *easy[5], *med[5], *hard[5];
for(int y = 0; y < 5; y++)
{
easy[y] = med[y] = hard[y] = new char[13];
}
easy[0] = "house";
easy[1] = "door";
easy[2] = "garage";
easy[3] = "bolo";
easy[4] = "john";
med[0] = "caucasian";
med[1] = "catastrophic";
med[2] = "dionysian";
med[3] = "accolade";
med[4] = "preprocessing";
hard[0] = "patrilinial";
hard[1] = "tendentious";
hard[2] = "portentious";
hard[3] = "euclydian";
hard[4] = "superannuated";
int randN;
randN = rand() % 5;
switch(diff)
{
case 1 :SecWord = easy[randN];
break;
case 2 :SecWord = med[randN];
break;
case 3 :SecWord = hard[randN];
break;
}
return SecWord;
}
/*******************************************************************/
int hangman::getLength()
//pre: SecWord has a value
//post: length of word is stored and returned
{
int x = strlen(SecWord);
return x;
}
/*******************************************************************/
void hangman::initTemp()
//pre: SecWord has a value
//post: a string of equal length, composed of '-' is created
{
letterOfSec = getLength();
for(int x = 0; x < letterOfSec; x++)
{
temp[x] = '-';
}
}
/******************************************************************/
void hangman::checkGuess(char& guess, int& check)
//pre: user enters a guess
//post: if guess is in SecWord, it is put into temp and check is
// incremented
{
if(islower(guess)) { guess = toupper(guess); }
for(int o = 0; o < letterOfSec; o++)
{
if(guess == SecWord[o]) {
temp[o] = guess;
check++;
}
}
if(isupper(guess)) { guess = tolower(guess); }
for(o = 0; o < letterOfSec; o++)
{
if(guess == SecWord[o]) {
temp[o] = guess;
check++;
}
}
}
/*******************************************************************/
//-------------------definition of class alpha----------------------
class alpha {
public:
initAlpha();
//constructs alphabet
void printAlpha();
//prints alphabet
void checkAlpha(char&);
//checks if guess is in alphabet, and replaces it with '*'
protected:
char letter[26];
};
//-----------------------------------------------------------------
//___________________-alpha Member Functions-_______________________
/******************************************************************/
alpha::initAlpha()
//post: alpha is initialized
{
char ch = 'A';
for(int y = 0; y < 26; y++)
{
letter[y] = ch;
ch++;
}
}
/******************************************************************/
void alpha: rintAlpha()
//pre: array letter has values
//post: remaining alphabet is printed
{
for(int y = 0; y < 26; y++)
{
cout << letter[y] << " ";
}
}
/*******************************************************************/
void alpha::checkAlpha(char& guess)
//pre: array letter has been initialized
//post: guessed letter is replaced with an asterix
{
if(islower(guess)) { guess = toupper(guess); }
for(int t = 0; t < 26; t++)
{
if(guess == letter[t]) { letter[t] = '*'; }
}
}
/*******************************************************************/
//___________________________-MAIN-________________________________
void main()
{
int diff;
hangman h;
alpha a;
randomize();
cout << "------------------HANGMAN-------------------\n"
"Choose difficulty:\n"
"<1> Easy\n"
"<2> Medium\n"
"<3> Hard\n";
cin >> diff;
h.SecWord = h.getWord(diff);
h.initTemp();
a.initAlpha();
int check, turn;
char guess;
turn = check = 0;
do
{
cout << "\n" << h.temp << "\n";
a.printAlpha();
cout << "\nEnter your guess: ";
cin >> guess;
a.checkAlpha(guess);
h.checkGuess(guess, check);
turn++;
} while(check != h.getLength());
cout << "\n" << h.SecWord << "\n";
cout << "you win.\n"
"you took " << turn << " tries.\n";
}
-
May 14th, 1999, 09:54 PM
#2
Re: hangman again
Take a look at this: (note: it gets the words from words.dat located in same directory)
//////////////////////////Begin Program HangMan///////////////////////
// main4a.cpp
#include <iostream.h> // For input/output
#include <fstream.h> // For file input/output
#include <string.h> // For strcpy
#include <time.h> // For time
#include <stdlib.h> // For toupper and tolower
#define MAX_WORD_SIZE 15 // The max size for words
#define MAX_WORDS 255 // The max number of words
void LoadFile(); // Prototypes for our functions
void RunGame();
void DrawGallows(int State);
typedef char String[MAX_WORD_SIZE]; // Make a char type with 15 chars
String Words[MAX_WORDS - 1]; // This array will hold our words
int Count; //word count
// the main function of our hangman game
void main()
{
char Continue = 'Y'; // end game if = to 'N'
cout<<"Welcome to Hangman . . . Don't lose your head!"<<endl;
LoadFile(); // Load the data file
// Continue the game as longas the player wants
while(Continue == 'Y')
{
RunGame(); // Run the game
cout<<endl<<"Do you want to play again (Yes or No)? ";
cin>>Continue;
Continue = toupper(Continue);
}
// say good bye
cout<<endl<<"Thanks for playing."<<endl;
}
// This will load our file
void LoadFile()
{
char C; //Used to find EOF
ifstream Datfile; //The in file
Count=0; //Set count to 0
// Open the data file
Datfile.open("words.dat");
//Loop till the end of file
while((C=Datfile.peek()) != EOF)
{
// Get the next word and then increment Count
Datfile>>Words[Count++];
// If we surpass the max exit and tell the user
if(Count > MAX_WORDS - 1)
{
cout<<endl<<"Too many words in the file, stopping with "
<<MAX_WORDS<<" Words."<<endl;
Count = MAX_WORDS;
break;
}}
// We need to subtract one to get the correct number of words
Count--;
// Close the data file
Datfile.close();
}
// This function will run the game
void RunGame()
{
int Word; // This will hold the subscript of our word
int Size; // This will hold the length of our word
int State=1; // This holds the game state
int Subscript=0; // This will hold subscripts
char Guess[MAX_WORD_SIZE]; // this will hold their current word
char Copy[MAX_WORD_SIZE]; // This will hold a copy of the word
char Letter; //This will be their letter guess
int Correct=0; // This is a True/False value deciding if they got a good answer
// Seed and create a random number
srand((unsigned)time( NULL )); // use time as a seed
Word = rand() % Count;
// Make a local copy of the word
strcpy(Copy,Words[Word]);
Size = strlen(Words[Word]); // Get the word's size
// Create a null terminated string to represent the word as the
// player knows it.
for(; Subscript < Size; Subscript++)
{
Guess[Subscript] = '-';
}
// insert the null charachter
Guess[Subscript] = '\0';
// Go till the player is hung
while(State!=6)
{
DrawGallows(State); //Draw the gallows
cout<<Guess<<endl; // Draw Guess
cout<<"Guess a letter :";
cin>>Letter;
// We will use only lower case letters
Letter = tolower(Letter);
// Loop through the word
for(Subscript = 0; Subscript < Size; Subscript++)
{
//if the guess is good tell the user and update Guess
if(Copy[Subscript] == Letter)
{
Guess[Subscript] = Letter;
Correct = 1;
cout<<endl<<"Good Guess!";
// If guess equals the word they won so exit
if(strcmp(Words[Word],Guess) == 0)
{
cout<<endl<<"Yea, You survived and won!";
return;
}}}
// If they didn't get aletter correct chide the user
if(Correct == 0)
{
cout<<endl<<"Sorry, bad guess!";
State++;
}
Correct = 0; // reset Correct
}
DrawGallows(State); //Draw the gallows at end of game
//They lost if they are here so tell them the answer.
cout<<"The word was : "<<Words[Word]<<endl<<endl;
}
// This will Draw the gallows according to the state
void DrawGallows(int State)
{
if(State==6)
{
// The \\ will translate as '\' because it is a special char
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | / \\ "<<endl
<<" |Your Dead "<<endl
<<" ============"<<endl<<endl;
}
else if(State==5)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | \\ "<<endl
<<" | "<<endl
<<" ============"<<endl<<endl;
}
else if(State==4)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(State==3)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /| "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(State==2)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(State==1)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|