Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

interface-based programming C++ cpp file Problem Set 4: Interface-based Programm

ID: 3839522 • Letter: I

Question

interface-based programming C++

cpp file

Problem Set 4: Interface-based Programming In problem set 3, you developed a console-based version of the paper and pencil game Bulls and Cows. The solution consisted of one class BullsAndcows and a main function defining the game loop. The approach works nicely, but did not follow the Model-View-Controller paradigm The task for this problem set is to develop an interface-based solution for Bulls and Cows. In particular, you are asked to implement a console view for the application that satisfies the following specification pragma once include

Explanation / Answer

I am writing down the code to implement the console view BullsAndCowsConsoleView:

#pragma once

#include "IBullsAndCowsView.h"

class BullsAndCowsConsoleView : public IBullsAndCowsView

{

public:

/**
* This method returns the user input. Error checking is omitted as assumption is of a reasonable player.
* as soon as four numbers are entered the string is returnd.
*/

   virtual std::string guess ()
   {
       string user_guess;
       int flag = 0;       //Basic size error check flag
  
       while (flag == 0)   //Until user gives string of length 4 or more the loop will not end
       {
           cout<<" Make a guess: ";
           cin>>user_guess;
           if ( strlen(user_guess) < 4)
               continue;
           else
           {
               flag = 1;
               user_guess = user_guess.substr(0,4); // To get only the first four characters of the string
           }
       }
       return user_guess;
   }

/**
* This method prints the number of bulls and cows guessed. This information is available through
* the corresponding parameter values.
*/
  
   virtual void showGuess( int aBulls, int a Cows)
   {
       cout<<"Number of bulls: "<<aBulls<<", number of cows: "<<aCows<<" "; //aBulls & aCows store the respective values.
   }


/**
* This method sends a greeting to the user.
*/

   virtual void welcome()
   {
       cout<<" Bulls and Cows, brought to you by StudentName (StudentID) ";   //Change StudentName & StudentID as needed within cout statement
   }

/**
* This method announces that a new game has started
*/

   virtual void startNewGame()
   {
       cout<<"New game ";   //Console Output
   }

/**
* This method interacts with the user and requires a Yes/No reponse.
*/

   virtual bool continueGame()
   {
       string lQuery;
       cout<<"New game, Y/N? ";   //Console Output
       cin>>lQuery;
       if ( lQuery.substr(0,1) == 'Y' || lQuery.substr(0,1) == 'y') //Check the first character of input
           return 0;   //if y or Y is entered then returns 0
       else
           return 1;   //if anything other than y or Y is entered then it returns 1
   }

/**
* This method announces that the game has ended
*/

   virtual void endGame()
   {
       cout<<"Game over. Good bye. ";   //Console Output
   }

}   //close class BullsAndCowsConsoleView
      

Hope the answer is satisfactory.

Kind Regards.