Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <ctime>
- #include <conio.h>
- using namespace std;
- int Gameslost; int Gameswon;
- void PlaceCursor(const int x, const int y) {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- COORD PlaceCursorHere;
- PlaceCursorHere.X = x;
- PlaceCursorHere.Y = y;
- SetConsoleCursorPosition(hConsole, PlaceCursorHere);
- return;
- }
- void MissedLetter(int miss) {
- switch (miss) {
- case 1: PlaceCursor(55, 4); cout << ("0"); break;
- case 2: PlaceCursor(55, 5); cout << ("|"); break;
- case 3: PlaceCursor(54, 5); cout << ("/"); PlaceCursor(56, 5); cout << ("\\"); break;
- case 4: PlaceCursor(54, 6); cout << ("/"); PlaceCursor(56, 6); cout << ("\\"); break;
- case 5: Gameslost++; PlaceCursor(50, 8); cout << ("Game over!!!"); break;
- }
- return;
- }
- void ClearConsole() {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- COORD coordScreen = { 0, 0 };
- DWORD cCharsWritten;
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- DWORD dwConSize;
- if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) { return; }
- dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
- if (!FillConsoleOutputCharacter(hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten)) { return; }
- if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) { return; }
- if (!FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten)) { return; }
- return;
- }
- void Help() {
- PlaceCursor(0, 10);
- cout << ("The computer stores a word and the player tries to guess it by suggesting letters.\n") << ("If the suggested letter does not occur in the word, the computer draws one element of the hangman.\n");
- cout << ("The game is over when: \n") << (" The guessing player completes the word.\n") << (" The computer completes the hangman diagram.\n") << ("\n") << ("Press any key to continue!");
- getch();
- ClearConsole();
- return;
- }
- void Score(int Gameswon, int Gameslost) {
- PlaceCursor(0, 15);
- cout << ("Games won: ") << Gameswon << "\n" << ("Games lost: ") << Gameslost << "\n" << "\n" << ("Press any key to continue!");
- getch();
- ClearConsole();
- return;
- }
- int main() {
- Gameslost = Gameswon = 0;
- bool Game = true;
- char *WordLibrary[] = {
- "automobile",
- "baker",
- "carrot",
- "darkness",
- "ellipse",
- "forest",
- "garrison",
- "house",
- "integral",
- "jumper",
- "kangaroo",
- "lamp",
- "monocle",
- "night",
- "owl",
- "peanut",
- "quintuple",
- "rebus",
- "soft",
- "teapot",
- "umbrella",
- "velocity",
- "water",
- "xerox",
- "yacht",
- "zebra"
- };
- while(Game == true) {
- ClearConsole();
- srand((unsigned)time(0));
- int Selected = rand() % 26;
- int miss = 0;
- char *Word = WordLibrary[Selected];
- int WordLength = strlen(WordLibrary[Selected]);
- char guess;
- int Completion = WordLength;
- bool GotOne = false;
- bool Uncomplete;
- int mov = 0;
- PlaceCursor(0, 0);
- cout << ("|| || /\\ |\\\\ || /--| /\\ /\\ /\\ |\\\\ ||") << "\n";
- cout << ("||--|| //\\\\ ||\\\\||| --| //\\\\//\\\\ //\\\\ ||\\\\||") << "\n";
- cout << ("||--|| //==\\\\ || \\ || |__/| // \\/ \\\\ //==\\\\ || \\ |") << "\n";
- cout << ("|| ||// \\\\|| \\| \\____/// \\\\// \\\\|| \\|") << "\n";
- PlaceCursor(0, 5);
- cout << ("[1] Go!\n") << ("[2] Help.\n") << ("[3] Scoreboard.\n") << ("[4] Exit.\n");
- cout << ("Menu: ");
- int Menu_choice;
- cin >> Menu_choice;
- switch (Menu_choice) {
- case 1: ClearConsole(); break;
- case 2: Help(); break;
- case 3: Score(Gameswon, Gameslost); break;
- case 4: Game = false; break;
- }
- if(Menu_choice == 1) {
- PlaceCursor(0, 2);
- cout << ("Word: ");
- PlaceCursor(0, 3);
- cout << ("False letters used: ");
- PlaceCursor(45, 2); cout << (" _______");
- PlaceCursor(45, 3); cout << (" | |");
- PlaceCursor(45, 4); cout << (" |");
- PlaceCursor(45, 5); cout << (" |");
- PlaceCursor(45, 6); cout << (" |");
- PlaceCursor(45, 7); cout << ("__|__");
- for (int x = 0; x < WordLength; x++) {
- PlaceCursor(x + 6, 2); cout << ("_"); }
- Uncomplete = true;
- while (Uncomplete == true) {
- PlaceCursor(0, 1);
- cout << ("Your guess: ");
- cin >> guess;
- for (int x = 0; x <= WordLength; x++) {
- if (guess == Word[x]) {
- PlaceCursor(x + 6, 2);
- cout << Word[x];
- --Completion;
- GotOne = true;
- }
- }
- if (GotOne == false) {
- PlaceCursor(20+mov, 3);
- cout << guess << (",");
- mov += 3;
- miss++;
- if (miss == 5) {
- Uncomplete = false;
- }
- MissedLetter(miss);
- }
- else {
- GotOne = false;
- }
- if (Completion == 0) { mov = 0; Uncomplete = false; PlaceCursor(0, 8); Gameswon++; }
- }
- miss = 0;
- ClearConsole();
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment