Mastermind

F

foolsmart2005

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
colors. The code-breaker makes a series of pattern guesses. After each
guess, the code-maker
gives feedback in the form of two numbers: the number of pegs that are
of the right color and in
the correct position (represented by small black pegs), and the number
of pegs that are of the
correct color but in a wrong position (represented by small white
pegs).
Complete the class CodeMaker, and construct a simple driver to test
your logic in checkBlack
and checkWhite functions.

class CodeMaker
{
public:
// add in relevant Big 3 and supporting functions
unsigned int checkBlack (char guess[]) const; // same position same
color
unsigned int checkWhite (char guess[]) const; // same color different
position

private:
unsigned int color; // total number of colors (m)
unsigned int length; // length of secret code (n)
char *secret; // secret code, supports duplicate colors
};

I don't quite understand what the code for
unsigned int checkBlack (char guess[]) const; // same position same
color
unsigned int checkWhite (char guess[]) const; // same color different
position
want me to do. Could anyone give details to me?
I have written the Big3 and some supporting functions. Thanks!
 
P

Pascal J. Bourguignon

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
colors. The code-breaker makes a series of pattern guesses. After each
guess, the code-maker
gives feedback in the form of two numbers: the number of pegs that are
of the right color and in
the correct position (represented by small black pegs), and the number
of pegs that are of the
correct color but in a wrong position (represented by small white
pegs).
Complete the class CodeMaker, and construct a simple driver to test
your logic in checkBlack
and checkWhite functions.

class CodeMaker
{
public:
// add in relevant Big 3 and supporting functions
unsigned int checkBlack (char guess[]) const; // same position same
color
unsigned int checkWhite (char guess[]) const; // same color different
position

private:
unsigned int color; // total number of colors (m)
unsigned int length; // length of secret code (n)
char *secret; // secret code, supports duplicate colors

I'm sorry, but in the problem statement above, I don't see the word
"character" or "char", much less "pointer". How do you justify this
type for secret?


It is said that there's a secret code, and that "The code is a
sequence of n colored pegs chosen from m available".


So you should have:

Sequence<N,ColoredPegAmongst<M> > secretCode;

or something like that...

Or, if you don't want to deal with template (which may be overkill),
you could define types such as;

class ColoredPeg ... { ... };
class SequenceOfColoredPeg ... { ... };

with functional abstractions such as:

SequenceOfColoredPeg makeSetOfColoredPegs(unsigned int M);
SequenceOfColoredPeg makeSequenceOfRandomColoredPeg(unsigned int N,SequenceOfColoredPeg setOfColoredPegsToChooseFrom);

ColoredPeg SequenceOfColoredPeg::chooseRandomColoredPeg();
ColoredPeg SequenceOfColoredPeg::coloredPegAtPosition(unsigned int position);
unsigned int SequenceOfColoredPeg::length();

bool sameColor(ColoredPeg a,ColoredPeg b);

unsigned int checkBlack(SequenceOfColoredPeg guess) const; // same color same position
unsigned int checkWhite(SequenceOfColoredPeg guess) const; // same color different position

etc... (or perhaps define classes for ColoredPeg and SequenceOfColoredPeg, and methods).

But I don't understand how you can came with such an horror from the
deep bowels of hell such as « char* ».
};

I don't quite understand what the code for
unsigned int checkBlack (char guess[]) const; // same position same
color
unsigned int checkWhite (char guess[]) const; // same color different
position
want me to do. Could anyone give details to me?
I have written the Big3 and some supporting functions. Thanks!

Let's take an example: Assume we have 4 colors, A, B, C, and D, (M=4)
and we have codes sequences of length 5 (N=5).

Assume the secret is: AABCD
and the guess is: ABCAD
B B

then checkBlack will return 2 because there are only two exact
matches, position per position:

AABCD
ABCAD
B B

and checkWhite will return 3, because there are three positions, that
are filled with a permutation of the remaining position that matches.

.ABC.
.BCA.
.WWW.

You can try the game here:
http://www.squiglysplayhouse.com/Games/Special/CodeBreaker.html


So to count the black, it's rather easy, you just have to scan the
code and the guess at the same time, and count and mark the matches.

Then to count the whites, you can scan for example the guess, and find
if there's a matching color in the code that is not marked yet. If
yes, then you mark it and count one.
 
F

foolsmart2005

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
colors. The code-breaker makes a series of pattern guesses. After each
guess, the code-maker
gives feedback in the form of two numbers: the number of pegs that are
of the right color and in
the correct position (represented by small black pegs), and the number
of pegs that are of the
correct color but in a wrong position (represented by small white
pegs).
Complete the class CodeMaker, and construct a simple driver to test
your logic in checkBlack
and checkWhite functions.
class CodeMaker
{
public:
// add in relevant Big 3 and supporting functions
unsigned int checkBlack (char guess[]) const; // same position same
color
unsigned int checkWhite (char guess[]) const; // same color different
position
private:
unsigned int color; // total number of colors (m)
unsigned int length; // length of secret code (n)
char *secret; // secret code, supports duplicate colors

I'm sorry, but in the problem statement above, I don't see the word
"character" or "char", much less "pointer". How do you justify this
type for secret?

It is said that there's a secret code, and that "The code is a
sequence of n colored pegs chosen from m available".

So you should have:

Sequence<N,ColoredPegAmongst<M> > secretCode;

or something like that...

Or, if you don't want to deal with template (which may be overkill),
you could define types such as;

class ColoredPeg ... { ... };
class SequenceOfColoredPeg ... { ... };

with functional abstractions such as:

SequenceOfColoredPeg makeSetOfColoredPegs(unsigned int M);
SequenceOfColoredPeg makeSequenceOfRandomColoredPeg(unsigned int N,SequenceOfColoredPeg setOfColoredPegsToChooseFrom);

ColoredPeg SequenceOfColoredPeg::chooseRandomColoredPeg();
ColoredPeg SequenceOfColoredPeg::coloredPegAtPosition(unsigned int position);
unsigned int SequenceOfColoredPeg::length();

bool sameColor(ColoredPeg a,ColoredPeg b);

unsigned int checkBlack(SequenceOfColoredPeg guess) const; // same color same position
unsigned int checkWhite(SequenceOfColoredPeg guess) const; // same color different position

etc... (or perhaps define classes for ColoredPeg and SequenceOfColoredPeg, and methods).

But I don't understand how you can came with such an horror from the
deep bowels of hell such as « char* ».
I don't quite understand what the code for
unsigned int checkBlack (char guess[]) const; // same position same
color
unsigned int checkWhite (char guess[]) const; // same color different
position
want me to do. Could anyone give details to me?
I have written the Big3 and some supporting functions. Thanks!

Let's take an example: Assume we have 4 colors, A, B, C, and D, (M=4)
and we have codes sequences of length 5 (N=5).

Assume the secret is: AABCD
and the guess is: ABCAD
B B

then checkBlack will return 2 because there are only two exact
matches, position per position:

AABCD
ABCAD
B B

and checkWhite will return 3, because there are three positions, that
are filled with a permutation of the remaining position that matches.

.ABC.
.BCA.
.WWW.

You can try the game here:http://www.squiglysplayhouse.com/Games/Special/CodeBreaker.html

So to count the black, it's rather easy, you just have to scan the
code and the guess at the same time, and count and mark the matches.

Then to count the whites, you can scan for example the guess, and find
if there's a matching color in the code that is not marked yet. If
yes, then you mark it and count one.

Actually,char *secret is a dynamic array storing the secret code.
Length of dynamic array is determined by the member variable length.
Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,244
Members
46,839
Latest member
MartinaBur

Latest Threads

Top