-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster.cpp
115 lines (98 loc) · 2.65 KB
/
master.cpp
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
/*******************
* COMP11
* Homework 2
* Problem 2
* Eberechukwu Justin
*********************/
#include <iostream>
#include <string>
using namespace std;
//variable declarations
const string ALLOWED_LETTERS = "roygbv";
const string SECRET_CODE = "grob";
char user_answer[4];//stores the users answers
char play_again;
bool allowed = false;
bool answer_used[4] = {false, false, false, false};//keeps track of which elements in the secret code have been used
int black_pegs, white_pegs;
bool is_allowed(char x);
int main()
{
cout << "Welcome to THE MASTERMIND!\n";
cout << "Enter your guesses\nr for red\no for orange\ny for yellow\n";
cout << "g for green\nb for blue and\nv for violet\n\n";
do
{
for (int i = 0; i < 4; ++i)
{
while (allowed == false)
{
cout << "Enter a guess: ";
cin >> user_answer[i];
allowed = is_allowed(user_answer[i]);
if (allowed == false)
cout << "Please enter a valid letter\n";
}
allowed = false;
}
for (int i = 0; i < 4; ++i)
{
if(user_answer[i] == SECRET_CODE[i])
{
black_pegs++ ;
answer_used[i] = true;//makes each used letter true so it wont be used again
}
}
for (int i = 0; i < 4; ++i)//i represents the users answers
{
for (int j = 0; j < 4; ++j)//j represents the secret code, each i is checked with every j
{
if((user_answer[i] == SECRET_CODE[j]) && (answer_used[j]== false))//increases white_pegs only if answer_used is still false
{
white_pegs++;
answer_used[j] = true;
}
}
}
if (black_pegs == 4)
{
cout << "You win! Your guess was right\n";
return 0;
}
else
{
cout << "Result: " << black_pegs << " black pegs and " << white_pegs << " white pegs." ;
}
cout << "Do you want to play again y/n? ";
do
{
cin >> play_again;
if (!(play_again == 'y' || play_again == 'n'))
cout << "Please enter y or n\n";
}
while (!(play_again == 'y' || play_again == 'n'));
//resets the variables for the next iteration
black_pegs = 0, white_pegs = 0;
for (int i = 0; i < 4; ++i)
{
answer_used[i] = false;
}
}
while (play_again == 'y');
//all the code is enclosed in a do-while loop to continue the game if the user chooses to play again
return 0;
}
//function to check if entered color is among the allowed colors
bool is_allowed(char x)
{
int count = 0;
for (string::size_type k = 0; k < ALLOWED_LETTERS.size(); ++k)
{
if (x == ALLOWED_LETTERS[k])
count++;
}
if (count == 0) //count == 0 means no allowed letter matched the entered letter, thus false
return false;
else
return true;
}