![]() |
|
Hangman, the command line game.[C++] - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: Hangman, the command line game.[C++] (/Thread-Hangman-the-command-line-game-C) |
Hangman, the command line game.[C++] - Slarek - 08-10-2013 Code: #include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
const int dc_lkm = 10;
// Let's create a table which includes all words
const string words[dc_lkm] = {"printer", "calculator", "television", "guitar", "speakers",
"shelf", "book", "cartridge", "clock", "paper"};
int main()
{
srand(time(0)); // Take a seed value of the time so the game is different every time
while(true)
{
int guesses = 5;
cout << "What you want to do?\n";
cout << "1.Play the game\n";
cout << "2.Turn the graphics mode on\n";
cout << "3.End the game\n";
int choice;
cin >> choice;
cout << string(3, '\n');
if(choice == 1)
{
// Raffle the word which will be asked
string word = words[rand() % dc_lkm];
//Format the variable, which will be show to the player.
int length = word.length();
string for_your_eyes_only(length, '-');
// This is for storing non-found characters
string bad_chars;
cout << "Your word: " << for_your_eyes_only << endl;
while(guesses > 0 && word != for_your_eyes_only)
{
// Show the word and ask for the letter
cout << "Guess a letter: ";
char letter;
cin >> letter;
cout << endl;
// Check if user has already guessed that letter
if (bad_chars.find(letter) != string::npos
|| for_your_eyes_only.find(letter) != string::npos)
{
cout << "You've already guessed that letter! Try again.\n";
continue;
}
// Initialize the variable which stores information about incrementing the letter (element) is the word.
int loc = word.find(letter);
// Check if the letter is in the word.
if (loc == string::npos)
{
cout << "Wrong!\n";
--guesses; // Reduce the number of guesses
bad_chars += letter; // Add to non-found letters
}
else // Letter was found
{
cout << "Good guess!\n";
// Add the letter to the right place
for_your_eyes_only[loc] = letter;
// We have to remember to check if the letter was in the word elsewhere
loc = word.find(letter, loc + 1);
while(loc != string::npos)
{
for_your_eyes_only[loc] = letter;
loc = word.find(letter, loc + 1);
}
}
cout << "Your word: " << for_your_eyes_only << endl;
if (for_your_eyes_only != word)
{
if (bad_chars.length() > 0)
// Show the wrong letters so the game is easier
cout << "Wrong guessed letters: " << bad_chars << endl;
cout << guesses << " wrong guesses left.\n";
}
}
// Check if user has guessed the word
if (guesses > 0)
cout << "It's correct!\n";
else
cout << "You lost\n";
cout << "Unfortunately the word is: " << word << ".\n";
cout << string(10, '\n');
}
if(choice == 2)
{
// Raffle the word which will be asked
string word = words[rand() % dc_lkm];
//Format the variable, which will be show to the player.
int length = word.length();
string for_your_eyes_only(length, '-');
// This is for storing non-found characters
string bad_chars;
cout << "Your word: " << for_your_eyes_only << endl;
while(guesses > 0 && word != for_your_eyes_only)
{
// Show the word and ask for the letter
cout << "Guess a letter: ";
char letter;
cin >> letter;
cout << endl;
// Check if user has already guessed that letter
if (bad_chars.find(letter) != string::npos
|| for_your_eyes_only.find(letter) != string::npos)
{
cout << "You've already guessed that letter! Try again.\n";
continue;
}
// Initialize the variable which stores information about incrementing the letter (element) is the word.
int loc = word.find(letter);
// Check if the letter is in the word.
if (loc == string::npos)
{
switch(guesses)
{
case 1:
cout << "Wrong!\n";
--guesses;
bad_chars += letter;
cout << " ------\n";
cout << " | |\n";
cout << " | |\n";
cout << " O |\n";
cout << "/( )\ |\n";
cout << " | | |\n";
cout << " |\n";
cout << "---------\n";
break;
case 2:
cout << "Wrong!\n";
--guesses;
bad_chars += letter;
cout << " ------\n";
cout << " | |\n";
cout << " | |\n";
cout << " O |\n";
cout << "/( )\ |\n";
cout << " |\n";
cout << " |\n";
cout << "---------\n";
break;
case 3:
cout << "Wrong!\n";
--guesses;
bad_chars += letter;
cout << " ------\n";
cout << " | |\n";
cout << " | |\n";
cout << " O |\n";
cout << " ( ) |\n";
cout << " |\n";
cout << " |\n";
cout << "---------\n";
break;
case 4:
cout << "Wrong!\n";
--guesses;
bad_chars += letter;
cout << " ------\n";
cout << " | |\n";
cout << " | |\n";
cout << " O |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << "---------\n";
break;
case 5:
cout << "Wrong!\n";
--guesses;
bad_chars += letter;
cout << " ------\n";
cout << " | |\n";
cout << " | |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << "---------\n";
break;
}
}
else // Letter was found
{
cout << "Good guess!\n";
// Add the letter to the right place
for_your_eyes_only[loc] = letter;
// We have to remember to check if the letter was in the word elsewhere
loc = word.find(letter, loc + 1);
while(loc != string::npos)
{
for_your_eyes_only[loc] = letter;
loc = word.find(letter, loc + 1);
}
}
cout << "Your word: " << for_your_eyes_only << endl;
if (for_your_eyes_only != word)
{
if (bad_chars.length() > 0)
// Show the wrong letters so the game is easier
cout << "Wrong guessed letters: " << bad_chars << endl;
cout << guesses << " wrong guesses left.\n";
}
}
// Check if user has guessed the word
if (guesses > 0)
cout << "It's correct!\n";
else
cout << "You lost\n";
cout << "Unfortunately the word is: " << word << ".\n";
cout << string(10, '\n'); //this is the ducktape way of solving this :/
}
if(choice == 3)
{
return false;
}
}
return 0;
}Enjoy this very entertaining game. Hangman, the command line game.[C++] - Slarek - 08-10-2013 Code: #include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
const int dc_lkm = 10;
// Let's create a table which includes all words
const string words[dc_lkm] = {"printer", "calculator", "television", "guitar", "speakers",
"shelf", "book", "cartridge", "clock", "paper"};
int main()
{
srand(time(0)); // Take a seed value of the time so the game is different every time
while(true)
{
int guesses = 5;
cout << "What you want to do?\n";
cout << "1.Play the game\n";
cout << "2.Turn the graphics mode on\n";
cout << "3.End the game\n";
int choice;
cin >> choice;
cout << string(3, '\n');
if(choice == 1)
{
// Raffle the word which will be asked
string word = words[rand() % dc_lkm];
//Format the variable, which will be show to the player.
int length = word.length();
string for_your_eyes_only(length, '-');
// This is for storing non-found characters
string bad_chars;
cout << "Your word: " << for_your_eyes_only << endl;
while(guesses > 0 && word != for_your_eyes_only)
{
// Show the word and ask for the letter
cout << "Guess a letter: ";
char letter;
cin >> letter;
cout << endl;
// Check if user has already guessed that letter
if (bad_chars.find(letter) != string::npos
|| for_your_eyes_only.find(letter) != string::npos)
{
cout << "You've already guessed that letter! Try again.\n";
continue;
}
// Initialize the variable which stores information about incrementing the letter (element) is the word.
int loc = word.find(letter);
// Check if the letter is in the word.
if (loc == string::npos)
{
cout << "Wrong!\n";
--guesses; // Reduce the number of guesses
bad_chars += letter; // Add to non-found letters
}
else // Letter was found
{
cout << "Good guess!\n";
// Add the letter to the right place
for_your_eyes_only[loc] = letter;
// We have to remember to check if the letter was in the word elsewhere
loc = word.find(letter, loc + 1);
while(loc != string::npos)
{
for_your_eyes_only[loc] = letter;
loc = word.find(letter, loc + 1);
}
}
cout << "Your word: " << for_your_eyes_only << endl;
if (for_your_eyes_only != word)
{
if (bad_chars.length() > 0)
// Show the wrong letters so the game is easier
cout << "Wrong guessed letters: " << bad_chars << endl;
cout << guesses << " wrong guesses left.\n";
}
}
// Check if user has guessed the word
if (guesses > 0)
cout << "It's correct!\n";
else
cout << "You lost\n";
cout << "Unfortunately the word is: " << word << ".\n";
cout << string(10, '\n');
}
if(choice == 2)
{
// Raffle the word which will be asked
string word = words[rand() % dc_lkm];
//Format the variable, which will be show to the player.
int length = word.length();
string for_your_eyes_only(length, '-');
// This is for storing non-found characters
string bad_chars;
cout << "Your word: " << for_your_eyes_only << endl;
while(guesses > 0 && word != for_your_eyes_only)
{
// Show the word and ask for the letter
cout << "Guess a letter: ";
char letter;
cin >> letter;
cout << endl;
// Check if user has already guessed that letter
if (bad_chars.find(letter) != string::npos
|| for_your_eyes_only.find(letter) != string::npos)
{
cout << "You've already guessed that letter! Try again.\n";
continue;
}
// Initialize the variable which stores information about incrementing the letter (element) is the word.
int loc = word.find(letter);
// Check if the letter is in the word.
if (loc == string::npos)
{
switch(guesses)
{
case 1:
cout << "Wrong!\n";
--guesses;
bad_chars += letter;
cout << " ------\n";
cout << " | |\n";
cout << " | |\n";
cout << " O |\n";
cout << "/( )\ |\n";
cout << " | | |\n";
cout << " |\n";
cout << "---------\n";
break;
case 2:
cout << "Wrong!\n";
--guesses;
bad_chars += letter;
cout << " ------\n";
cout << " | |\n";
cout << " | |\n";
cout << " O |\n";
cout << "/( )\ |\n";
cout << " |\n";
cout << " |\n";
cout << "---------\n";
break;
case 3:
cout << "Wrong!\n";
--guesses;
bad_chars += letter;
cout << " ------\n";
cout << " | |\n";
cout << " | |\n";
cout << " O |\n";
cout << " ( ) |\n";
cout << " |\n";
cout << " |\n";
cout << "---------\n";
break;
case 4:
cout << "Wrong!\n";
--guesses;
bad_chars += letter;
cout << " ------\n";
cout << " | |\n";
cout << " | |\n";
cout << " O |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << "---------\n";
break;
case 5:
cout << "Wrong!\n";
--guesses;
bad_chars += letter;
cout << " ------\n";
cout << " | |\n";
cout << " | |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << " |\n";
cout << "---------\n";
break;
}
}
else // Letter was found
{
cout << "Good guess!\n";
// Add the letter to the right place
for_your_eyes_only[loc] = letter;
// We have to remember to check if the letter was in the word elsewhere
loc = word.find(letter, loc + 1);
while(loc != string::npos)
{
for_your_eyes_only[loc] = letter;
loc = word.find(letter, loc + 1);
}
}
cout << "Your word: " << for_your_eyes_only << endl;
if (for_your_eyes_only != word)
{
if (bad_chars.length() > 0)
// Show the wrong letters so the game is easier
cout << "Wrong guessed letters: " << bad_chars << endl;
cout << guesses << " wrong guesses left.\n";
}
}
// Check if user has guessed the word
if (guesses > 0)
cout << "It's correct!\n";
else
cout << "You lost\n";
cout << "Unfortunately the word is: " << word << ".\n";
cout << string(10, '\n'); //this is the ducktape way of solving this :/
}
if(choice == 3)
{
return false;
}
}
return 0;
}Enjoy this very entertaining game. RE: Gallows, the command line game.[C++] - phantasm8 - 10-31-2013 Line 14 of the code seems to have an error: ||=== gallows, Debug ===| error: ‘srand’ was not declared in this scope Line 14: srand(time(0)); // Take a seed value of the time so the game is different every time RE: Gallows, the command line game.[C++] - phantasm8 - 10-31-2013 Line 14 of the code seems to have an error: ||=== gallows, Debug ===| error: ‘srand’ was not declared in this scope Line 14: srand(time(0)); // Take a seed value of the time so the game is different every time RE: Gallows, the command line game.[C++] - ArkPhaze - 10-31-2013 (10-31-2013, 01:13 AM)phantasm8 Wrote: Line 14 of the code seems to have an error: http://www.cplusplus.com/reference/cstdlib/srand/ - What header do you think you need to include then? With the headers that are included in the code tags in the first post though, it should already be defined. You're doing something wrong. RE: Gallows, the command line game.[C++] - ArkPhaze - 10-31-2013 (10-31-2013, 01:13 AM)phantasm8 Wrote: Line 14 of the code seems to have an error: http://www.cplusplus.com/reference/cstdlib/srand/ - What header do you think you need to include then? With the headers that are included in the code tags in the first post though, it should already be defined. You're doing something wrong. RE: Gallows, the command line game.[C++] - Psycho_Coder - 10-31-2013 Maybe @Deque can add this to the HC CLI game compilation after reviewing. RE: Gallows, the command line game.[C++] - Psycho_Coder - 10-31-2013 Maybe @Deque can add this to the HC CLI game compilation after reviewing. RE: Gallows, the command line game.[C++] - Slarek - 10-31-2013 (10-31-2013, 03:16 PM)Psycho_Coder Wrote: Maybe @Deque can add this to the HC CLI game compilation after reviewing. We have something like that? This is just pure awesomeness RE: Gallows, the command line game.[C++] - Slarek - 10-31-2013 (10-31-2013, 03:16 PM)Psycho_Coder Wrote: Maybe @Deque can add this to the HC CLI game compilation after reviewing. We have something like that? This is just pure awesomeness |