Guess word

Hello! It is necessary to create a game, I will show the conditions below. Currently, if the name is guessed, then the next word is no longer displayed - ....


When starting a game with the help of a random generator, one of the words is selected.
The number of letters must be indicated by the corresponding dot "." number and, after successful guessing at the appropriate place, the point must be replaced by a letter (in several places if there are several letters).
Letters must be entered without pressing "Enter".
1 penalty point is awarded for an incorrect letter.
The game continues until the player collects 10 penalty points or until a "0" is entered instead of a letter.
After each guessed word, the player burns 5 penalty points, if any (there must be no negative penalty points!). And the next word is suggested (words must not be repeated!).
At the end of the game, the number of words must be entered. Or congratulations if all the words were guessed

My code -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "conio.h"

using namespace std;
const int Max_Points=5;
int letterFill (char, string, string&);
string words_rand ();

int main (){
  
char letter;
int Points=0;
string word;
string words[] ={"test","tes","set","pet","dog","cat","lion","forgot","let","sets","king","market","business","park","street","drink","eat","pizza","sprite","fruit"};

word = words_rand ();
string unknown(word.length(),'.');

do 
{
   cout << "\n" << unknown;
   cout << "\nGuess word: ";

   letter = getch();
   system("cls");

   if (letterFill(letter, word, unknown)==0)
   {
   cout << endl << "there is no such letter:" << endl;
   Points++;
   }
   else
   {
   cout << endl << "Correct letter: " << endl;
   }
   cout << "You have: " <<  Points;
   cout << " points." << endl;
   if (word==unknown)
   {
   cout << word << endl;
   cout << "Whoop, whoop! You guessed a word!";
   }

   if(Points == Max_Points || letter == '0' )
   {   
   cout << "\nGAME OVER" << endl;
   cout << "Word : " << word << endl;
   system("pause");
   return 0;
   }
  
}
   while (Points < Max_Points);
 
   cin.ignore();
   cin.get();
   system("pause");
   return 0;
}

string words_rand ()
{
string word;
string words[] ={"test","tes","set","pet","dog","cat","lion","forgot","let","sets","king","market","business","park","street","drink","eat","pizza","sprite","fruit"};
srand(time(0));
int i=rand()% 20;
word = words[i];
return word;    
}

int letterFill (char guess, string secretword, string &guessword)
{
    int i;
    int matches=0;
    int len=secretword.length();
    for (i = 0; i< len; i++)
    {
   if (guess == guessword[i])
   return 0;
   if (guess == secretword[i])
   {
   guessword[i] = guess;
   matches++;
   }
   }
return matches;
}
So what is your problem or question?
As i said - when i guess first word, then he needs generate the next but it's not generating it..
Currently, if the name is guessed, then the next word is no longer displayed - ....
The reason is that you need line 19 and line 20 (similar) after line 44. Plus you need to subtract 5.

Also line 68 needs to be at the top of main().
Do you mean something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <iterator>
#include <cstdlib>
#include <ctime>

using namespace std;

const unsigned Max_Points {5};

int letterFill(char, const string&, string&);
string words_rand();

int main() {
	bool again {true};

	srand(static_cast<unsigned>(time(nullptr)));

	while (again) {
		unsigned Points {};
		const auto word {words_rand()};
		string unknown(word.length(), '.');

		do {
			char letter {};

			cout << "\n" << unknown;
			cout << "\nGuess letter: ";
			cin >> letter;

			switch (letterFill(letter, word, unknown)) {
				case -1:
					cout << "\nletter has already been used\n";
					break;

				case 0:
					cout << "\nthere is no such letter\n";
					++Points;
					break;

				default:
					cout << "\nCorrect letter\n";
					break;
			}

			cout << "You have: " << Points;
			cout << " points.\n";

			if (word == unknown) {
				cout << word << '\n';
				cout << "Whoop, whoop! You guessed a word!";
			}

		} while (Points < Max_Points && word != unknown);

		char ans {};

		cout << "\nGAME OVER\n";
		cout << "Word : " << word << '\n';
		cout << "Do you want to play again (y/n): ";
		cin >> ans;

		again = (ans == 'y' || ans == 'Y');
	}
}

string words_rand() {
	const static string words[] {"test","tes","set","pet","dog","cat","lion","forgot","let","sets","king","market","business","park","street","drink","eat","pizza","sprite","fruit"};

	return words[rand() % std::size(words)];
}

int letterFill(char guess, const string& secretword, string& guessword) {
	int matches {};

	for (size_t i = 0; i < secretword.size(); ++i) {
		if (guess == guessword[i])
			return -1;

		if (guess == secretword[i]) {
			guessword[i] = guess;
			++matches;
		}
	}

	return matches;
}

Last edited on
seeplus, your code doesnt work...
coder777, something like this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "conio.h"

using namespace std;
const int Max_Points=5;
int letterFill (char, string, string&);
string words_rand ();

string words_rand ()
{
string word;
string words[] ={"test","tes","set","pet","dog","cat","lion","forgot","let","sets","king","market","business","park","street","drink","eat","pizza","sprite","fruit"};
srand(time(0));
int i=rand()% 20;
word = words[i];
return word;    
}

int main (){
  
char letter;
int Points=0;
string word;
string words[] ={"test","tes","set","pet","dog","cat","lion","forgot","let","sets","king","market","business","park","street","drink","eat","pizza","sprite","fruit"};

word = words_rand ();
string unknown(word.length(),'.');

do 
{
   cout << "\n" << unknown;
   cout << "\nGuess word: ";

   letter = getch();
   system("cls");

   if (letterFill(letter, word, unknown)==0)
   {
   cout << endl << "there is no such letter:" << endl;
   Points++;
   }
   else
   {
   cout << endl << "Correct letter: " << endl;
   }
   cout << "You have: " <<  Points;
   cout << " points." << endl;
   if (word==unknown)
   {
   cout << word << endl;
   cout << "Whoop, whoop! You guessed a word!";
   word = words_rand ();
string unknown(word.length(),'.');
   }

   if(Points == Max_Points || letter == '0' )
   {   
   cout << "\nGAME OVER" << endl;
   cout << "Word : " << word << endl;
   system("pause");
   return 0;
   }
  
}
   while (Points < Max_Points);
 
   cin.ignore();
   cin.get();
   system("pause");
   return 0;
}



int letterFill (char guess, string secretword, string &guessword)
{
    int i;
    int matches=0;
    int len=secretword.length();
    for (i = 0; i< len; i++)
    {
   if (guess == guessword[i])
   return 0;
   if (guess == secretword[i])
   {
   guessword[i] = guess;
   matches++;
   }
   }
return matches;
}
You did not take the advice to move srand() to the beginning of main.
Do not call srand() multiple times. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

L10: Is not needed since words_rand() is at the top of your code.

L15: Why do you repeat this at line 26? words[] is not used in main().
L15: Should be const

L40,85-86: If the letter has already been guessed, you return 0, which causes "there is no such letter:" to be displayed. That's misleading.

L30,56: You're declaring unknown twice. They're in different scopes. The second unknown goes out of scope at L57.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "conio.h"

using namespace std;
const int Max_Points = 5;
int letterFill(char, string, string&);

string words_rand()
{
    string word;
    const string words[] = { "test","tes","set","pet","dog","cat","lion","forgot","let","sets",
                             "king","market","business","park","street","drink","eat","pizza","sprite","fruit" };    
    int i = rand() % 20;
    word = words[i];
    return word;
}

int main() {

    char letter;
    int Points = 0;
    string word;    

    srand((unsigned int)time(0));
    word = words_rand();
    string unknown(word.length(), '.');

    do
    {
        cout << "\n" << unknown;
        cout << "\nGuess word: ";

        letter = _getch();
        system("cls");

        if (letterFill(letter, word, unknown) == 0)
        {
            cout << endl << "there is no such letter:" << endl;
            Points++;
        }
        else
        {
            cout << endl << "Correct letter: " << endl;
        }
        cout << "You have: " << Points;
        cout << " points." << endl;
        if (word == unknown)
        {
            cout << word << endl;
            cout << "Whoop, whoop! You guessed a word!";
            word = words_rand();
            unknown.assign (word.length(), '.');
        }

        if (Points == Max_Points || letter == '0')
        {
            cout << "\nGAME OVER" << endl;
            cout << "Word : " << word << endl;
            system("pause");
            return 0;
        }

    } while (Points < Max_Points);

    cin.ignore();
    cin.get();
    system("pause");
    return 0;
}

int letterFill(char guess, string secretword, string& guessword)
{
    int i;
    int matches = 0;
    size_t len = secretword.length();
    for (i = 0; i < len; i++)
    {
        if (guess == guessword[i])
            return 0;
        if (guess == secretword[i])
        {
            guessword[i] = guess;
            matches++;
        }
    }
    return matches;
}





Big thanks, AbstractionAnon, everything works great.
The last question is about points. Example - i trying to guess a word, then i guess, and i have 4 penalty points, but when i guess the correct word, then the penalty point one goes off. How can i do that ? And the penalty points can't be under 0 or negative.
seeplus, your code doesnt work...


In what way?
when i guess the correct word, then the penalty point one goes off.

That's easy enough.
After line 55:
1
2
    if (points > 0) 
        points--;

yeah, then substract only -1 but i need -5..
So change it is -= 5? (and have a "if less than 0 set to 0" additional check)
@dobbijs - if a program doesn't work as required, then firstly use the debugger to trace through the code to find out why it doesn't work as expected. Then once you're found that, then either your design is wrong or you're coded the design wrong (you have designed the program first before coding, haven't you?). If it's a simple coding issue then fix the code. If it's a design issue then you go back to the design, fix that and then re-code the part(s) of the program required from the design change. Then repeat this until the program works as required. Using the debugger, finding issues in code and knowing how to fix them is a very important part of learning how to program. Programming isn't just about coding, but also about design, debugging, testing etc.
Can somebody explain what this function does ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int letterFill(char guess, string secretword, string& guessword)
{
    int i;
    int matches = 0;
    size_t len = secretword.length();
    for (i = 0; i < len; i++)
    {
        if (guess == guessword[i])
            return 0;
        if (guess == secretword[i])
        {
            guessword[i] = guess;
            matches++;
        }
    }
    return matches;
}
From OP first post:

My code -


So if this is the OP's code - how come an explanation is asked for???

It returns the number of matches of guess in secretword and if the guess char is in the secretword then sets the appropriate chars in guessword.

If guess already exists or not present, then 0 is returned.

See my code above for an alternative implementation which returns -1 if guess already exists.
Last edited on
Topic archived. No new replies allowed.