How to resolve the algorithm Mastermind step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Mastermind step by step in the C++ programming language

Table of Contents

Problem Statement

Create a simple version of the board game:   Mastermind. It must be possible to:

The (computer program) game should display all the player guesses and the results of that guess. Display (just an idea):

A text version example:       1.   ADEF   -   XXO- Translates to: the first guess; the four colors (ADEF); result: Happy coding!

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Mastermind step by step in the C++ programming language

The code snippet you provided is a game of Mastermind written in C++. The game is played by two players, one of whom is the "master" and the other is the "guesser". The master chooses a secret code of four colors, and the guesser tries to guess the code by making up to 12 guesses. After each guess, the master gives the guesser feedback in the form of black and white pegs. A black peg indicates that the guesser has guessed a correct color in the correct position, while a white peg indicates that the guesser has guessed a correct color but in the wrong position.

The code snippet you provided defines a class called master that represents the master player. The master class has a constructor that takes four arguments: the length of the secret code, the number of colors that the secret code can use, the number of guesses that the guesser has, and a boolean value that indicates whether or not the secret code can contain repeated colors.

The master class also has several methods:

  • play(): This method starts the game. It generates a secret code and then calls the getInput() and checkInput() methods to get the guesser's guesses and check whether or not they have guessed the correct code.
  • showBoard(): This method prints the game board to the console. The game board shows the guesser's guesses and the feedback that the master has given.
  • getInput(): This method gets the guesser's guess. It prompts the guesser to enter a guess of four colors, and then it checks to make sure that the guess is valid.
  • checkInput(): This method checks whether or not the guesser's guess is correct. It compares the guess to the secret code and returns true if the guess is correct and false if it is not.
  • getCombo(): This method generates the secret code. It randomly selects four colors from the list of available colors.

The main() function creates an instance of the master class and calls the play() method to start the game.

Source code in the cpp programming language

#include <iostream>
#include <algorithm>
#include <ctime>
#include <string>
#include <vector>

typedef std::vector<char> vecChar;

class master {
public:
    master( size_t code_len, size_t clr_count, size_t guess_count, bool rpt ) {
        std::string color = "ABCDEFGHIJKLMNOPQRST";

        if( code_len < 4 ) code_len = 4; else if( code_len > 10 ) code_len = 10;
        if( !rpt && clr_count < code_len ) clr_count = code_len; 
        if( clr_count < 2 ) clr_count = 2; else if( clr_count > 20 ) clr_count = 20;
        if( guess_count < 7 ) guess_count = 7; else if( guess_count > 20 ) guess_count = 20;
        
        codeLen = code_len; colorsCnt = clr_count; guessCnt = guess_count; repeatClr = rpt;

        for( size_t s = 0; s < colorsCnt; s++ ) {
            colors.append( 1, color.at( s ) );
        }
    }
    void play() {
        bool win = false;
        combo = getCombo();

        while( guessCnt ) {
            showBoard();
            if( checkInput( getInput() ) ) {
                win = true;
                break;
            }
            guessCnt--;
        }
        if( win ) {
            std::cout << "\n\n--------------------------------\n" <<
                "Very well done!\nYou found the code: " << combo <<
                "\n--------------------------------\n\n";
        } else {
            std::cout << "\n\n--------------------------------\n" <<
                "I am sorry, you couldn't make it!\nThe code was: " << combo <<
                "\n--------------------------------\n\n";
        }
    }
private:
    void showBoard() {
        vecChar::iterator y;
        for( int x = 0; x < guesses.size(); x++ ) {
            std::cout << "\n--------------------------------\n";
            std::cout << x + 1 << ": ";
            for( y = guesses[x].begin(); y != guesses[x].end(); y++ ) {
                std::cout << *y << " ";
            }

            std::cout << " :  ";
            for( y = results[x].begin(); y != results[x].end(); y++ ) {
                std::cout << *y << " ";
            }

            int z = codeLen - results[x].size();
            if( z > 0 ) {
                for( int x = 0; x < z; x++ ) std::cout << "- ";
            }
        }
        std::cout << "\n\n";
    }
    std::string getInput() {
        std::string a;
        while( true ) {
            std::cout << "Enter your guess (" << colors << "): ";
            a = ""; std::cin >> a;
            std::transform( a.begin(), a.end(), a.begin(), ::toupper );
            if( a.length() > codeLen ) a.erase( codeLen );
            bool r = true;
            for( std::string::iterator x = a.begin(); x != a.end(); x++ ) {
                if( colors.find( *x ) == std::string::npos ) {
                    r = false;
                    break;
                }
            }
            if( r ) break;
        }
        return a;
    }
    bool checkInput( std::string a ) {
        vecChar g;
        for( std::string::iterator x = a.begin(); x != a.end(); x++ ) {
            g.push_back( *x );
        }
        guesses.push_back( g );
        
        int black = 0, white = 0;
        std::vector<bool> gmatch( codeLen, false );
        std::vector<bool> cmatch( codeLen, false );
 
        for( int i = 0; i < codeLen; i++ ) {
            if( a.at( i ) == combo.at( i ) ) {
                gmatch[i] = true;
                cmatch[i] = true;
                black++;
            }
        }
 
        for( int i = 0; i < codeLen; i++ ) {
            if (gmatch[i]) continue;
            for( int j = 0; j < codeLen; j++ ) {
                if (i == j || cmatch[j]) continue;
                if( a.at( i ) == combo.at( j ) ) {
                    cmatch[j] = true;
                    white++;
                    break;
                }
            }
        }
       
        vecChar r;
        for( int b = 0; b < black; b++ ) r.push_back( 'X' );
        for( int w = 0; w < white; w++ ) r.push_back( 'O' );
        results.push_back( r );

        return ( black == codeLen );
    }
    std::string getCombo() {
        std::string c, clr = colors;
        int l, z;

        for( size_t s = 0; s < codeLen; s++ ) {
            z = rand() % ( int )clr.length();
            c.append( 1, clr[z] );
            if( !repeatClr ) clr.erase( z, 1 );
        }
        return c;
    }

    size_t codeLen, colorsCnt, guessCnt;
    bool repeatClr;
    std::vector<vecChar> guesses, results;
    std::string colors, combo;
};

int main( int argc, char* argv[] ) {
    srand( unsigned( time( 0 ) ) );
    master m( 4, 8, 12, false );
    m.play();
    return 0;
}


  

You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the xEec programming language
You may also check:How to resolve the algorithm Nautical bell step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Almost prime step by step in the BCPL programming language
You may also check:How to resolve the algorithm Bernstein basis polynomials step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Permutation test step by step in the Elixir programming language