/*
* Mastermind
*
* Author: Gerald Cruz
*
* Date: June 21, 2008
*
*/
#include<iostream>
#include<string>
#include<ctime>
#include<algorithm>
#define CODELEN 4 // length of code
#define NUMSYM 6 // number of symbols
#define MAXTRIES 10
using namespace std;
void clrscr()
{
system("cls");
}
int main()
{
srand(time(NULL));
bool playagain=false;
do
{
//create code
string code = "";
for(int i=0; i<CODELEN; i++)
{
code.append(1,rand() % NUMSYM + 'A');
}
clrscr();
cout << "Mastermind" << endl << endl;
cout << "Guess the " << CODELEN << " digit code using letters A to "
<< (char)('A'+NUMSYM-1) << "." << endl << endl;
cout << "The computer will give a clue:" << endl;
cout << "\t0 for a correct letter at a wrong position." << endl;
cout << "\t1 for a correct letter at a correct position." << endl <<
endl;
cout << "Enter a period (.) to exit the program." << endl << endl;
int numtries = 0;
bool gameover = false;
do
{
string guess("");
cout << "Enter you guess? ";
getline(cin,guess);
// capitalize guess
transform(guess.begin(),guess.end(),guess.begin(), ::toupper);
// check for invalid characters
bool invalid = false;
for(unsigned int i=0; i<guess.length(); i++)
{
if(guess.at(i)<'A' || guess.at(i)>(char)('A'+NUMSYM-1))
{
invalid=true;
break;
}
}
if(guess.compare(".")==0)
{
goto exitprogram;
}
else if(invalid)
{
cout << "Your input has invalid characters." << endl;
}
else if(guess.length()!=CODELEN)
{
cout << "Your guess must be " << CODELEN << " characters long."
<< endl;
}
else
{
string tmpcode=code;
string tmpguess=guess;
string clue="";
int correct=0;
int misplaced=0;
numtries++;
// check for a correct letter at a correct position
for(int i=0; i<CODELEN; i++)
{
if(tmpcode.at(i)==tmpguess.at(i))
{
correct++;
clue.append("1");
tmpcode[i]='*';
tmpguess[i]='*';
}
}
// check for a correct letter at a wrong position
for(int i1=0; i1<CODELEN; i1++)
{
if(tmpguess.at(i1)=='*') continue;
for(int i2=0; i2<CODELEN; i2++)
{
if(tmpcode.at(i2)!='*' && tmpcode.at(i2)==tmpguess.at(i1))
{
misplaced++;
clue.append("0");
tmpcode[i2]='*';
tmpguess[i1]='*';
}
}
}
// shuffle clue
random_shuffle(clue.begin(),clue.end());
cout << "Clue: " << clue << endl;
if(correct==CODELEN)
{
cout << "Correct! The code is " << code << "." << endl;
if(numtries<2)
cout << "You got it in your first try." << endl;
else
cout << "You got it in " << numtries << " tries." << endl;
gameover=true;
}
else if(numtries>=MAXTRIES)
{
cout << "Wrong! The code is " << code << "." << endl;
gameover=true;
}
}
}
while(!gameover);
cout << endl << "Game Over" << endl << endl;
// ask to play again
bool askagain = true;
do
{
string ans = "";
cout << "Play again? ";
getline(cin,ans);
transform(ans.begin(),ans.end(),ans.begin(),::toup per);
if(ans.compare("Y")==0 || ans.compare("YES")==0)
{
askagain=false;
playagain=true;
}
else if(ans.compare("N")==0 || ans.compare("NO")==0)
{
askagain=false;
playagain=false;
}
}
while(askagain);
}
while(playagain);
exitprogram:
cout << endl << "Bye." << endl;
return 0;
} 2 7189
ImmortalFire <Fi****@gmail.comwrote:
/*
* Mastermind
*
* Author: Gerald Cruz
*
* Date: June 21, 2008
*
*/
[code snipped]
Good job! Let's start making some minor edits and see if we can make the
program more concise...
First, you have a variable defined as "int correct". Modify the program
so you don't need that variable. (Hint, if (code == guess)...)
"ImmortalFire" <Fi****@gmail.comwrote in message
news:2b**********************************@w34g2000 prm.googlegroups.com...
/*
* Mastermind
*
* Author: Gerald Cruz
*
* Date: June 21, 2008
*
*/
#include<iostream>
#include<string>
#include<ctime>
#include<algorithm>
#define CODELEN 4 // length of code
#define NUMSYM 6 // number of symbols
#define MAXTRIES 10
using namespace std;
void clrscr()
{
system("cls");
}
[...]
What if there is a system in which the command `cls' stands for `Clear Local
Storage'?
:^o This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Moosebumps |
last post by:
When I have time, I am planning to evaluate Python for console game
development (on Playstation 2, GameCube, and Xbox). Does anyone have any
experience with this?
Pretty much the only resource...
|
by: theodp |
last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml
Microsoft Patents Saving The Name Of A Game
Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM
from the...
|
by: enki |
last post by:
I am writing a game and I am having trouble with moving the character
on the map.
Here is what I have right now. It involves win32 programming but that
not my problem. I would like some...
|
by: NeedJavaHelp |
last post by:
Hello everyone, first time poster here, bear with me.
RuneScape is an online multiplayer game run by Java www.runescape.com. The game itself is run on the website and when you play the game for...
|
by: mira88 |
last post by:
Hi,
I have to program the Mastermind game( dont' know if anyone knows it). Anyway I've got my GUI looking quite pretty :), but I want to know something on working with Colors. I' ve got 6 colors to...
|
by: Butterflybis |
last post by:
Hi all,
I’m newbie in java code development, I have written a mastermind play, and I would like to include a time limitation (for example 3 minutes) which appears like a coloured and vertical...
|
by: foolsmart2005 |
last post by:
I have written a snake game. There are 2 levels in the game(I
finished 1st level). It can run in VC++ without problem but, when I
run it on the dev C++ 4.9.9.2, it cannot run. I want to...
|
by: foolsmart2005 |
last post by:
The idea of the game is for one player (the code-breaker) to guess the
secret code chosen by the
other player (the code-maker). The code is a sequence of n colored
pegs chosen from m available...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |