Hi Guys!!
I need to put a variable so that two players are able to play the following game, but I am unaware how to do this. Is someone able to assit me?
PHP Code:
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <fstream>
using namespace std;
void info ()
{
;
}
void get_word (string& word)
{
ifstream read ("words.txt");
vector <string> word_list;
read >> word;
while (word != "$") //at the end of the file there is a '$' to stop reading when it gets there!! :~)
{
word_list.push_back (word);
read >> word;
}
srand (time (0) );
int i = rand() % word_list.size();
word = word_list [i];
}
void check_for_win (vector <char> secret_word, vector <char> myword, bool& win)
{
//check if the played has found the word
for (int i = 0; i < myword.size(); i++)
{
if (secret_word [i] == myword [i])
{
win = true;
}
else
{
win = false;
break;
}
}
}
void process (vector <char>& secret_word, vector <char> myword, int& tries_left)
{
typedef vector <char> ::size_type vec_siz;
vec_siz word_size = myword.size();
cout << "\n" << "->Enter a letter: ";
char letter;
cin >> letter;
bool exists = false;
bool already_exists = false;
for (int i = 0; i < word_size; i++) //is the letter in the word?
{
if (letter == myword [i])
{
if (secret_word [i] == letter)
{
already_exists = true;
}
exists = true; //the letter exists in the word
secret_word [i] = letter;
}
}
if (already_exists == true)
{
cout << " The letter '" << letter << "' already exists! ";
}
else
{
if (exists == true)
{
cout << " Nice! '" << letter << "' is in the word! ";
}
else
{
cout << " Sorry. '" << letter << "' is not in the word.. ";
}
tries_left --;
}
}
void print (const vector <char> secret_word) /*const before 'vector' increases velocity*/
{ // prints secret_word on the screen
typedef vector <char> ::size_type vec_siz;
vec_siz size = secret_word.size();
for (int i = 0; i != size; i++)
{
cout << secret_word [i];
}
}
int main()
{
string word;
info ();
get_word (word);
typedef string::size_type str_siz;
str_siz word_size = word.size(); //how many letters the word has
vector <char> myword; //storage for the letters
for (int i = 0; i != word_size; i++) //add the word's letters to the vector
{
myword.push_back (word[i]);
}
vector <char> secret_word; //create a vector to store the secret word, e.g. s***p
//set secret_word's data
for (int i = 0; i < word_size; i++)
{
secret_word.push_back ('*');
}
secret_word [0] = myword [0];
secret_word [word_size - 1] = myword [word_size - 1]; //send the first and the second letter of 'myword' to 'secret word'
//replace words same as the first and the last, e.g. d*dd* [daddy]
for (int i = 0; i < word_size; i++)
{
if (myword [i] == myword [0]) // same as the first letter
{
secret_word [i] = myword [0];
}
if (word [i] == myword [word_size - 1]) // same as the last letter
{
secret_word [i] = myword [word_size - 1];
}
}
// THE 'MAIN LOOP'
int tries_left;
if (word_size == 3)
{
tries_left = 2;
}
else if ( (word_size > 3) && (word_size <= 5) )
{
tries_left = 6;
}
else if ( (word_size >=5) && (word_size < 10) )
{
tries_left = int (word_size * 1.4);
}
cout << "..::HangMan::.. [by Minas Mina]"
<< "\n\n[";
print (secret_word);
cout << "]" << "\n";
bool win = false;
while ( (win == false) && (tries_left > 0) )
{
process (secret_word, myword, tries_left); //get input and check if exists in the word
check_for_win (secret_word, myword, win); //has the player won? here we check it
cout << "Tries left:" << tries_left << ", [";
print (secret_word);
cout << "]\n";
}
if (win == true)
{
cout << "\n" << "Congratulations, you win!! ";
}
else
{
cout << "\n" << "You lose..Try harder next time.. ";
}
system ("PAUSE");
return 0;
}