-
March 21st, 2010, 08:25 PM
#1
Hangman Game Help
Hey guys, i need some help with a hangman project for school.
The outline is:
make program that plays the game of hangman, read the word to be guessed into a string variable. ( The programmer must make about 10-20 words to have one randomly picked each time a game is started. the player must guess the letters belonging to the word. the words letters must be * before they are guessed correctly.
when the program is started you pick a difficulty level. easy,medium,hard. easy has 15 lives, medium has 10 lives and hard has only 5. each inccorect guess means you lose a life, if all are lost it says game over and then you terminate. maybe use an array for the list of difficultys?
i have started but need some help on the main parts.
try to help me with basic ways of doing it a we havent gone too far yet
we were told to have a variable named solution to keep track of it at any time during the program. intialize solution to a string of *. each time a letter is in the word is guessed replaces that * with the letter in that place
my main questions are how to store the words, and how to do the part with the **** and replacing them with a letter if its right and in the right position.?
for my last project i did this, this might help you know how much I know so far:
//Olympic Games program
//Created by Wayne Daly
//2009/2010
//Header files
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
#include<windows.h>
int main()
{
//Variables
float judge1score;
float judge2score;
float judge3score;
float judge4score;
float judge5score;
float min;
float max;
int times;
float avg;
char response;
start:
//set colour to green
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
//Title page, Graphics
cout<<"\n\n\n";
cout<<" ++++++ ++ ++ ++ +++ +++ +++++++ ++ ++++++ \n";
cout<<" ++++++++ ++ ++ ++ ++++++++++ ++ +++ ++ ++++++++\n";
cout<<" ++ ++ ++ ++ ++ ++++++++++ ++ +++ ++ +++ ++\n";
cout<<" ++ ++ ++ ++ ++ ++ ++ ++ ++ +++ ++ ++ \n";
cout<<" ++ ++ ++ ++++ ++ ++ ++ +++++++ ++ ++ \n";
cout<<" ++ ++ ++ ++ ++ ++ ++ ++ ++ +++ ++\n";
cout<<" ++++++++ ++++++ ++ ++ ++ ++ ++ ++ ++++++++\n";
cout<<" ++++++ ++++++ ++ ++ ++ ++ ++ ++ ++++++ \n\n";
cout<<"\t\t ++++++ +++++ ++ ++ ++++++++ ++++++++\n";
cout<<"\t\t ++++++++ +++++++ ++++++++ ++++++++ +++++++ \n";
cout<<"\t\t ++ ++ ++ ++++++++++ ++ ++ \n";
cout<<"\t\t ++ ++++ ++ ++ ++ ++ ++ +++++++ ++++++++ \n";
cout<<"\t\t ++ ++ +++++++ ++ ++ ++ +++++++ ++++++++ \n";
cout<<"\t\t ++ ++ +++++++ ++ ++ ++ ++ ++ \n";
cout<<"\t\t +++++++ ++ ++ ++ ++ ++ ++++++++ +++++++ \n";
cout<<"\t\t ++++ ++ ++ ++ ++ ++ ++++++++ +++++++ \n";
cout<<"\n\n\t\t\t ---> Press any key <--- ";
cout<<"\n\t\t\t\t\t\t\t\t -Wayne Daly-";
getch();
system("CLS");
//start of main program
cout<<"\n\n\t\t\t\t======================\n I I\n I OLYMPIC GAMES 2012 I\n I I";
cout<<"\n\t\t\t\t======================";
//Asks how many competitors
cout<<"\n\nEnter how many competitors? ";
cin>>times;
//Error validation
while (times < 1 || times > 50)
{
cout<<"\nInvalid entry!";
cout<<"\nEnter amount of competitors: ";
cin>>times;
}
//loop for how many competitors
for(int i = 1; i <= times; i++)
{
//set colour to green
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
//Judge 1
cout<<"\n\n\n\n\t\t\tJudge 1 enter score: ";
cin>>judge1score;
while (judge1score < 0.0 || judge1score > 6.0)
{
cout<<"\n\t\t\tInvalid score";
cout<<"\n\t\t\tMust be between 0.0 and 6.0!!";
cout<<"\n\n\t\t\tJudge 1 enter score: ";
cin>>judge1score;
}
//judge 2
cout<<"\n\n\n\n\t\t\tJudge 2 enter score: ";
cin>>judge2score;
while (judge2score < 0.0 || judge2score >6.0)
{
cout<<"\n\t\t\tInvalid score";
cout<<"\n\t\t\tMust be between 0.0 and 6.0!!";
cout<<"\n\n\t\t\tJudge 2 enter score: ";
cin>>judge2score;
}
//judge 3
cout<<"\n\n\n\n\t\t\tJudge 3 enter score: ";
cin>>judge3score;
while (judge3score < 0.0 || judge3score > 6.0)
{
cout<<"\n\t\t\tInvalid score";
cout<<"\n\t\t\tMust be between 0.0 and 6.0!!";
cout<<"\n\n\t\t\tJudge 3 enter score: ";
cin>>judge3score;
}
//Judge 4
cout<<"\n\n\n\n\t\t\tJudge 4 enter score: ";
cin>>judge4score;
while (judge4score < 0.0 ||judge4score > 6.0)
{
cout<<"\n\t\t\tInvalid score";
cout<<"\n\t\t\tMust be between 0.0 and 6.0!!";
cout<<"\n\n\t\t\tJudge 4 enter score: ";
cin>>judge4score;
}
//judge 5
cout<<"\n\n\n\n\t\t\tJudge 5 enter score: ";
cin>>judge5score;
while (judge5score < 0.0 || judge5score > 6.0)
{
cout<<"\n\t\t\tInvalid score";
cout<<"\n\t\t\tMust be between 0.0 and 6.0!!";
cout<<"\n\n\t\t\tJudge 5 enter score: ";
cin>>judge5score;
}
//start of finding lowest number of the 5 inputs
min=judge1score;
if (judge2score<min)
{min=judge2score;}
if (judge3score<min)
{min=judge3score;}
if (judge4score<min)
{min=judge4score;}
if (judge5score<min)
{min=judge5score;}
//start of finding highest number of 5 inputs
max=judge1score;
if (judge2score>max)
{max=judge2score;}
if (judge3score>max)
{max=judge3score;}
if (judge4score>max)
{max=judge4score;}
if (judge5score>max)
{max=judge5score;}
//start of getting average without knowing what the highest and lowest numbers were.
avg=(judge1score+judge2score+judge3score+judge4score+judge5score)-(min+max);
avg=(avg/3);
//Set colour to red
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
//score table
cout<<"\n\n\n\n\n\t\t\t --SCORE TABLE-- ";
//set colour to blue
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout<<"\n\n\t\t\tJudge 1 score is: "<<judge1score;
cout<<"\n\n\t\t\tJudge 2 score is: "<<judge2score;
cout<<"\n\n\t\t\tJudge 3 score is: "<<judge3score;
cout<<"\n\n\t\t\tJudge 4 score is: "<<judge4score;
cout<<"\n\n\t\t\tJudge 5 score is: "<<judge5score;
cout<<"\n\n\n\t\tThe Average score for this competitor is "<<avg;}
//Start of program exit loop
//If answer = y, or Y
exit:
cout<<"\n\n\n\t\tDo you want to exit program?";
cin>>response;
if (response == 'Y' || response == 'y' )
{
//Goodbye graphics
cout<<"\n\n\t\t\t +\n";
cout<<"\t\t\t + +\n";
cout<<"\t\t\t + +\n";
cout<<"\t\t\t + +\n";
cout<<"\t\t\t + +\n";
cout<<"\t\t\t+ GOODBYE +\n";
cout<<"\t\t\t + +\n";
cout<<"\t\t\t + +\n";
cout<<"\t\t\t + +\n";
cout<<"\t\t\t + +\n";
cout<<"\t\t\t +\n";
getch();
return 0;
}
//if answer = N or n
else if (response == 'N' || response == 'n' )
{
system("CLS");
goto start;
}
// If anything else is input
else
{
cout<<"\n\n\n\t\tInvalid input try again: ";
goto exit;
}
//End of program
}
-
March 21st, 2010, 09:11 PM
#2
Re: Hangman Game Help
1) Please use code tags when posting code. The code you posted is unreadable and not formatted.
2)
Code:
#include<iostream.h>
#include<iomanip.h>
These are non-standard header files. The correct header files are these:
Code:
#include<iostream>
#include<iomanip>
Any code showing you <iostream.h> and <iomanip.h> is either wrong or outdated. The correct standard headers are <iostream> and <iomanip>.
3) Once "goto" shows up in code, the amount of help you get will be reduced. Why are you using "goto" in your code?
4)
Code:
float judge1score;
float judge2score;
float judge3score;
float judge4score;
float judge5score;
Learn to use arrays. As a result of not using arrays, you have code repeated over and over again, and the only difference is the judge score number:
Code:
float judgescore[5]; // array of 5 judge scores
Regards,
Paul McKenzie
-
March 22nd, 2010, 03:23 PM
#3
Re: Hangman Game Help
Thank you for your help, how would i go about naming the variables i put in the array/
like
float judgescore[5];
how would i have separte scores put in there before the program even starts.?
-
March 22nd, 2010, 07:09 PM
#4
Re: Hangman Game Help
Originally Posted by zendet
Thank you for your help, how would i go about naming the variables i put in the array/
like
float judgescore[5];
how would i have separte scores put in there before the program even starts.?
First, clean up the code:
Code:
float judgescore[5];
//...
for (int i = 0; i < 5; ++i )
{
bool inputOK = false;
while ( !inputOK )
{
cout<<"\n\n\n\n\t\t\tJudge " << i + 1 << " enter score: ";
cin>>judgescore[i];
if (judgescore[i] < 0 || judgescore[i] > 6.0)
{
cout<<"\n\t\t\tInvalid score";
cout<<"\n\t\t\tMust be between 0.0 and 6.0!!";
}
else
inputOK = true;
}
}
This one loop replaces that entire block of code you had. What if there were 10 judges? Or 50 judges? Would you write that code 10 or 50 times?
Secondly, to get the maximum score:
Code:
#include <algorithm>
//...
float *maxscore = max_element(judgescore, judgescore + 5);
//...
cout << "Maximum score is " << *maxscore;
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 23rd, 2010 at 04:26 AM.
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
|