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
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
//Repeat a character a number of times, for decoration
void rept(char c, int repnum){
for (int a = 0; a < repnum; a++){
cout.put(c);
}
}
//Generate a new random code. Level = number of unique characters (eg. 3=abc)
string genrand(int level){
string ret;
for (int a = 0; a < level; a++){
ret += ('a'+(rand()%level));
}
return ret;
}
//Return a string indicating results of a guess
//rcrp = right character right place (default !)
//rcwp = right character wrong place (default ?)
string check(const string& user, const string& key, char rcrp = '!', char rcwp = '?'){
//If the guess string is the wrong size, somebody is doing something wrong
if (user.size()!=key.size()){
return "FAILURE";
}
string ret;
string tmp=user;
string tmp2=key;
//Run through the key backwards, check for equality based on that index,
//if the code's character matches the user's character, add a rcrp and
//remove both characters so we don't duplicate a check.
for (int a = key.size()-1; a >= 0; a--){
if (user[a]==key[a]){
ret+=rcrp;
tmp.erase(tmp.begin()+a);
tmp2.erase(tmp2.begin()+a);
}
}
//Same as previous, only this time for every character of the key, run through
//the entire user's input
for (int a = 0; a < tmp2.size(); a++){
for (int b = 0; b < tmp.size(); b++){
if (tmp[b]==tmp2[a]){
ret+=rcwp;
tmp.erase(tmp.begin()+b);
tmp2.erase(tmp2.begin()+a);
a--; //This is necessary because we're deleting chars at a
break;
}
}
}
return ret;
}
//Make a box around a string, make stuff look pretty :D
void outtitle(string title, char decorate, int borderwidth, int borderheight){
for (int a=0;a<borderheight;a++){
rept(decorate,title.size()+borderwidth*2);
cout << '\n';
}
rept(decorate,borderwidth);
cout << title;
rept(decorate,borderwidth);
cout << '\n';
for (int a=0;a<borderheight;a++){
rept(decorate,title.size()+borderwidth*2);
cout << '\n';
}
}
int main(){
//So we generate different keys with genrand(blah)
srand(time(0));
bool quit = false;
while(!quit){
string winstr="HOLY COW YOU WINNNNNNN!!!";
outtitle(" Welcome To Mastermind ",32+rand()%32,28,2);
int level=5; //number of colors
int rows=12; //number of guesses allowed
string code = genrand(level);
string input;
cout << "\n\n";
outtitle("Gameplay Specifics",'-',4,1);
cout << "\n\nGuess by inputting a string " << level << " characters long.";
cout << "\n(example " << genrand(level) << ")";
cout << "\n\nAvailable characters : ";
//output all available characters for guessing
for (int b = 0; b < level; b++){
cout << " [" << char('a'+b) << "]";
}
cout << "\n\nX = right letter right spot\n- = right letter wrong spot";
cout << "\n\n";
//loop through user guesses
for (int a = 0; a < rows && input!=code; a++){
cout << "\n";
getline(cin,input);
//don't allow incorrect input VERY remote chance FAILURE could be the key
//but the likelyhood is astronomical
if (input.size() != level || input == "FAILURE"){
a--;
cout << "\t\tTry : miss";
}
else{
cout << check(input,code,'X','-') << "\t\tTry : " << a << "/" << rows;
}
}
cout << "\n\n";
if (input==code){
outtitle(" You WWWWIIIIINNNNNNNNN!!! ",'%',10,2);
}
else{
outtitle("Sorry, the code was " + code,'-',10,2);
}
cout << "\nPush Enter To Play Again!";
cin.get();
cin.clear();
}
}
|