Mastermind

Discussion in 'C++' started by foolsmart2005@gmail.com, Jun 13, 2008.

  1. Guest

    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!
    , Jun 13, 2008
    #1
    1. Advertising

  2. "" <> 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(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.


    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Jun 13, 2008
    #2
    1. Advertising

  3. Guest

    On Jun 13, 8:15 pm, (Pascal J. Bourguignon)
    wrote:
    > "" <> 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(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.
    >
    > --
    > __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.
    , Jun 13, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kraig
    Replies:
    6
    Views:
    1,409
    Will Twentyman
    Dec 10, 2004
  2. ImmortalFire

    c++ mastermind game console app

    ImmortalFire, Jun 22, 2008, in forum: C++
    Replies:
    0
    Views:
    652
    ImmortalFire
    Jun 22, 2008
  3. Steve Jones

    Ruby Mastermind Game

    Steve Jones, Oct 30, 2008, in forum: Ruby
    Replies:
    4
    Views:
    94
    Todd Benson
    Nov 1, 2008
Loading...

Share This Page