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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
string convert_int_to_string(int generated[5])
{
stringstream generated_num[5];
generated_num[0] << generated[0];
generated_num[1] << generated[1];
generated_num[2] << generated[2];
generated_num[3] << generated[3];
generated_num[4] << generated[4];
return generated_num[5].str();
};
string convert_guess_int_to_string(int guess[5])
{
stringstream players_guess[5];
players_guess[0] << guess[0];
players_guess[1] << guess[1];
players_guess[2] << guess[2];
players_guess[3] << guess[3];
players_guess[4] << guess[4];
return players_guess[5].str();
};
void welcome(); // Defining function
void instructions(); // Defining function
int generate(); // Defining function
int user_guess(); // Defining function
string convert_tostring(); // Defining function
void display(); // Defining function
void welcome()
{
cout << "Welcome to mastermind.\n " ;
}
void instructions ()
{
cout << "You will be prompted to enter a # 0-9 for 5 characters. \n ";
cout << "The computer will see if they match. \n ";
cout << "0 will imply that the number is not used. \n ";
cout << "1 will imply that the number is the correct and location. \n ";
cout << "2 will imply that the number is used but in a different location. \n ";
cout << "You will have 10 tries to guess the correct number. \n";
}
int main()
{
srand((unsigned)time(NULL)); // initializing random generator
int generated_num[5], guess[5], x;
int a = rand()%10; // Creating 1st random #
int b = rand()%10; // Creating 1st random #
int c = rand()%10; // Creating 1st random #
int d = rand()%10; // Creating 1st random #
int e = rand()%10; // Creating 1st random #
generated_num[0] = a;
generated_num[1] = b;
generated_num[2] = c;
generated_num[3] = d;
generated_num[4] = e;
int q, r, s, t, u;
int number_of_guesses = 10;
x = 0;
welcome(); // Calling the welcome function
instructions(); // Calling the instructions function
do
{
{
cout << "Please enter your 1st digit guess. ";
cin >> guess[0];
cout << "Please enter your 2nd digit guess. ";
cin >> guess[1];
cout << "Please enter your 3rd digit guess. ";
cin >> guess[2];
cout << "Please enter your 4th digit guess. ";
cin >> guess[3];
cout << "Please enter your 5th digit guess. ";
cin >> guess[4];
if (guess[0] == generated_num[0])
{
q = 1;
}
if (guess[0] == generated_num[1] || guess[0] == generated_num[2] || guess[0] == generated_num[3] || guess[0] == generated_num[4])
{
q = 2;
}
if (guess[0] != generated_num[0] && guess[0] != generated_num[1] && guess[0] != generated_num[2] && guess[0] != generated_num[3] && guess[0] != generated_num[4])
{
q = 0;
}
if (guess[1] == generated_num[1])
{
r = 1;
}
if (guess[1] == generated_num[0] || guess[1] == generated_num[2] || guess[1] == generated_num[3] || guess[1] == generated_num[4])
{
r = 2;
}
if (guess[1] != generated_num[0] && guess[1] != generated_num[1] && guess[1] != generated_num[2] && guess[1] != generated_num[3] && guess[1] != generated_num[4])
{
r = 0;
}
if (guess[2] == generated_num[2])
{
s = 1;
}
if (guess[2] == generated_num[0] || guess[2] == generated_num[1] || guess[2] == generated_num[3] || guess[2] == generated_num[4])
{
s = 2;
}
if (guess[2] != generated_num[0] && guess[2] != generated_num[1] && guess[2] != generated_num[2] && guess[2] != generated_num[3] && guess[2] != generated_num[4])
{
s = 0;
}
if (guess[3] == generated_num[3])
{
t = 1;
}
if (guess[3] == generated_num[0] || guess[3] == generated_num[1] || guess[3] == generated_num[2] || guess[3] == generated_num[4])
{
t = 2;
}
if (guess[3] != generated_num[0] && guess[3] != generated_num[1] && guess[3] != generated_num[2] && guess[3] != generated_num[3] && guess[3] != generated_num[4])
{
t = 0;
}
if (guess[4] == generated_num[4])
{
u = 1;
}
if (guess[4] == generated_num[0] || guess[4] == generated_num[1] || guess[4] == generated_num[2] || guess[4] == generated_num[3])
{
u = 2;
}
if (guess[4] != generated_num[0] && guess[4] != generated_num[1] && guess[4] != generated_num[2] && guess[4] != generated_num[3] && guess[4] != generated_num[4])
{
u = 0;
}
if (q == 1, r == 1, s == 1, t == 1, u == 1)
{
cout << "You are a mastermind and have won" << endl;
}
else
{
cout << "These are the current results " << q << r << s << t << u ;
}
}
}
while (x < 10, x++ || (q == 1, r == 1, s == 1, t == 1, u == 1) );
{
cout << "The numbers you needed were " <<a<<b<<c<<d<<e ;
}
return 0;
}
|