![]() |
Mastermind
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! |
Re: Mastermind
"foolsmart2005@gmail.com" <foolsmart2005@gmail.com> writes:
> 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(unsigne d 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/Gam...deBreaker.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. -- __Pascal Bourguignon__ |
Re: Mastermind
On Jun 13, 8:15 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote: > "foolsmart2...@gmail.com" <foolsmart2...@gmail.com> writes: > > 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(unsigne d 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/Gam...deBreaker.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. > > -- > __Pascal Bourguignon__ 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. |
| All times are GMT. The time now is 09:59 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.