Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "pch.h"
#include <iostream>
#include <fstream>//read the file
#include <ctime> //random number generator
#include <string>//using words instead of charactors
#include <cstdlib>
using namespace std;
int main()
{
char word_start{};
system("CLS");
cout << " Hangman " << endl;
cout << " " << endl;
cout << " ------------------" << endl;
cout << " | } " << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | / | \\ " << endl;
cout << " | / | \\ " << endl;
cout << " | / \\ " << endl;
cout << " | / \\ " << endl;
cout << " | " << endl;
cout << " |_________________" << endl;
cout << "\n PRESS ANY KEY (and enter)TO word_start \n" << endl;
cin >> word_start;
system("CLS");//cleaning the screen
//actual code
//set variables throughout the game
int numberOfAttempt{ 6 };//this is the number of times the player can attempt for correct answer
char Guess;
bool rightattempt = false;
char pre_Guess[7];//array of guesses so the lenght of the words its not more than 7char
pre_Guess[0] = '\0';
for (int i = 1;i < 6;i++)
{
pre_Guess[i] = '-';
}
pre_Guess[0] = '\0';
string word;
string WList[99];//array for 100 words"strings"
//chosing the word randomly
srand(time(NULL)); //using this to get the random words from array
ifstream Rand_word;//this gets the random
Rand_word.open("words.txt"); //reads this file which contains the list of words
for (int i = 0; i < 99; i++)
{
Rand_word>> WList[i]; //this takes the words from the list and each time the i increases as it starts from 0 and goes up to 99
}
int RandomNum = rand() % 100;
word = WList[RandomNum];
Rand_word.close();
string MysteryWord(word.length(), '*');
//for printing the stars
while (numberOfAttempt >= 0)
{
rightattempt = false;
//display information related to the word
cout << " the word you have to guess is: \n\n ";
cout << MysteryWord << endl;
cout << "\nThere are " << MysteryWord.length() << " letters in the word\n";
cout << " you have " << numberOfAttempt << " guesses left\n";
if (pre_Guess[0] == '\0')
{
cout << endl;
}
else
{
cout << "you have guessed: " << pre_Guess << endl;
}
cout << "\n Guess a letter: ";
cin >> Guess;
//checking is guess is correct
for (int i = 0;i < MysteryWord.length();i++)//lenght of word
{
if (word[i] == Guess)//check is the guess same as the correct answer
{
MysteryWord[i] = Guess;
rightattempt = true;
}
}
if (word == MysteryWord)//end game text
{
cout << "\n\n Congratulations! you've got the word correct: " << MysteryWord << endl;
break;
}
if (rightattempt == false)
{
numberOfAttempt--;
cout << " sorry, " << Guess << " is not part of the word" << endl;
}
else
{
cout << " Congratulation !!! " << Guess << " is one of the letters!" << endl;
}
switch (numberOfAttempt)
{
case 6:
{
cout << " " << endl;
cout << " ------------------" << endl;
cout << " | } " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |_________________" << endl;
break;
}
case 5:
{
cout << " ------------------" << endl;
cout << " | } " << endl;
cout << " | O " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |_________________" << endl;
if (rightattempt == false)
{
pre_Guess[0] = Guess;
}
break;
}
case 4:
{
cout << " " << endl;
cout << " ------------------" << endl;
cout << " | } " << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |_________________" << endl;
if (rightattempt == false)
{
pre_Guess[1] = Guess;
}
break;
}
case 3:
{
cout << " " << endl;
cout << " ------------------" << endl;
cout << " | } " << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | / | " << endl;
cout << " | / | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |_________________" << endl;
if (rightattempt == false)
{
pre_Guess[2] = Guess;
}
break;
}
case 2:
{
cout << " " << endl;
cout << " ------------------" << endl;
cout << " | } " << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | / | \\ " << endl;
cout << " | / | \\ " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |_________________" << endl;
if (rightattempt == false)
{
pre_Guess[3] = Guess;
}
break;
}
case 1:
{
cout << " " << endl;
cout << " ------------------" << endl;
cout << " | } " << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | / | \\ " << endl;
cout << " | / | \\ " << endl;
cout << " | / " << endl;
cout << " | / " << endl;
cout << " | " << endl;
cout << " |_________________" << endl;
if (rightattempt == false)
{
pre_Guess[4] = Guess;
}
break;
}
case 0:
{
cout << " " << endl;
cout << " ------------------" << endl;
cout << " | } " << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | / | \\ " << endl;
cout << " | / | \\ " << endl;
cout << " | / \\ " << endl;
cout << " | / \\ " << endl;
cout << " | " << endl;
cout << " |_________________" << endl;
cout << " \n GAME OVVVEEEERRRRR dude!!!!" << endl;
cout << " the correct word is: "<< word << endl;
numberOfAttempt = -1;
break;
}
default:
cout << "if i see this line something is wrong" << endl;
}
}
return 0;
}