Delete

Delete
Last edited on

What have you even tries ? Start here:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main( int argc, char **argv ) {

   showInstructions( ...); // and then write this function

   displayWord( ... ); // and then write this function

   char c = playerSelection( ...); // and then write this function

   if ( isComplete( ... ) ) {
      // do something here
   }
   
}


The requirement to clear the display screen is system dependent. C++ as a language has no knowledge of screens, monitors, etc. On many platforms this might even be impossible to do.
To give you an idea:

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <random>
#include <chrono>

void guessArray(char guess[][4], int& rows);
void answerArray(char answer[][4], int rows);
void playerTurn(char guess[][4], char answer[][4], int rows);
void completedWord(char guess[][4], char answer[][4], int rows);
int randomChoice(int min, int max);
int countAsterisks(char guess[][4], int rows);
void displayInstructions();

int main()
{
    int rowNumber{ 0 };
    bool tryAgain{ true };
    char again{ 'Y' };
    char guessWord[5][4] = { { 'C', '*', 'R', '*' },
				    { '*', 'R', '*', '*' },
				    { '*', 'O', '*', 'T' },
				    { '*', 'R', '*', 'E' },
				    { '*', '*', 'N', '*' } };

    char answerWord[5][4] = { { 'C', 'A', 'R', 'S' },
				    { 'A', 'R', 'M', 'S' },
				    { 'B', 'O', 'A', 'T' },
				    { 'T', 'R', 'E', 'E' },
				    { 'L', 'I', 'N', 'K' } };


    displayInstructions();

    while (tryAgain)
    {

	   guessArray(guessWord, rowNumber);
	   playerTurn(guessWord, answerWord, rowNumber);
	   answerArray(answerWord, rowNumber);

	   std::cout << "Try Again?(Y/N)";
	   std::cin >> again;

	   if (again == 'Y')
		  tryAgain = true;

	   else if (again == 'N')
		  tryAgain = false;

	   else
	   {
		  std::cout << "Your input was not Y/N.\n";
		  std::cout << "I will assume is N";
		  tryAgain = false;
	   }
    }

    return 0;
}

void guessArray(char word[][4], int& rows)
{

    rows = randomChoice(0, 4);
    for (int cols = 0; cols < 4; cols++)
    {
	   std::cout << word[rows][cols];
    }

    std::cout << std::endl;
}

void answerArray(char word[][4], int rows)
{
    std::cout << "The answer was : ";
    for (int cols = 0; cols < 4; cols++)
    {
	   std::cout << word[rows][cols];
    }

    std::cout << std::endl;
}

void playerTurn(char guess[][4], char answer[][4], int rows)
{

    int numberOfGuesses{ 8 };
    int countAsterisk = countAsterisks(guess, rows);
    char letter{ ' ' };

    while (countAsterisk !=0 && numberOfGuesses !=0)
    {
	   std::cout << "Enter a letter to guess the word.\n";
	   std::cin >> letter;

	   for (int cols = 0; cols < 4; cols++)
	   {

		  if (answer[rows][cols] == letter && guess[rows][cols] == '*')
		  {
			 std::cout << "Letter was found.\n";
			 guess[rows][cols] = letter;
			 countAsterisk--;

			 if (numberOfGuesses < 9)
				numberOfGuesses++;
		  }

	   }

	   if (countAsterisk == 0)
		  completedWord(guess, answer, rows);

	   else
	   {
 		numberOfGuesses--;
		std::cout << "You have " << numberOfGuesses << " tries left.\n";
	   }

    }

    std::cout << std::endl;

}

void completedWord(char guess[][4], char answer[][4], int rows)
{
    bool completeWord{ false };

    for (int cols = 0; cols < 4; cols++)
    {
	   if (guess[rows][cols] == '*')
		  completeWord = false;

	   else if (guess[rows][cols] != '*')
		  completeWord = true;
    }

    if (completeWord == false)
    {
	   std::cout << "You did not complete the word.\n";
	   answerArray(answer, rows);
	}

    else if (completeWord == true)
    {
	   std::cout << "You got it correct.";
	   answerArray(answer, rows);
    }

    std::cout << std::endl;

}

int randomChoice(int min, int max)
{
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::default_random_engine generator(seed);
    std::uniform_int_distribution<int> distributionToss(min, max); // Set the numbers for int.

    return distributionToss(generator);

}

int countAsterisks(char guess[][4], int rows)
{
    int countAsterisk{ 0 };

    for (int cols = 0; cols < 4; cols++)
    {
	   if (guess[rows][cols] == '*')
		  countAsterisk++;
    }

    return countAsterisk;
}

void displayInstructions()
{
    std::cout << "The guessing game.\n";
    std::cout << "The user will try and guess a word.\n";
    std::cout << "You will try and guess the missing letters in the word.\n";
    std::cout << "You will only be allowed 8 guesses and then you will lose the game.\n";
    std::cout << "Good luck!!!\n";
}
Topic archived. No new replies allowed.