Swe20004 semester 1 2017 dr markus lumpe 2 - create and


Problem Set: Object-Oriented Solution Design

Bulls and Cows is an old code-breaking round-based paper and pencil game for two players, predating the commercially marketed board game Mastermind.

The game works as follows. Both players write down a 4-digit secret number. The digits must be all different. Then, in turn, the players try to guess their opponent's number who gives the number of matches. If the matching digits are on their right positions, then they are "bulls." If the numbers are on different positions, then they are "cows."

Example:
- Secret number: 4286
- Opponent's try: 1234
- Answer: 1 bull and 1 cow. (The bull is "2", the cow is "4.")

The first player to reveal the other player's secret number wins the game.

The goal of this problem set is to develop an object-oriented solution for this game in which a human player tries to guess the secret number chosen by the computer. The aim of the game becomes: use as few guesses as possible to reveal the computer's secret number.

The program consists of two parts: the class BullsAndCows and a main function. The main function defines a "game loop" that allows a player to repeatedly guess the computer's secret number. The game loop, defined in the main function, keeps the game alive as long as the flag lPlayAgain is true. In each round, the game asks the human player to guess the secret number. We use a do-while loop to allow for multiple guesses. Once the players guessed correctly, the round is over and the player has the option to continue or terminate the game.

If the player presses any character other than ‘Y' and ‘y', then the game ends. BullsAndCows lGame; bool lPlayAgain = true; while ( lPlayAgain )
{
cout << "New game" << endl; lGame.start ();
string lInput;
do
{
cout << "Make a guess: "; cin >> lInput; lGame.guess( lInput );
cout << "Number of bulls: " << lGame.getBulls()
<< ", number of cows: " << lGame.getCows() << endl;
} while ( lGame.getBulls() != 4 ); cout << "New game, Y/N? ";
cin >> lInput;
if ( lInput[0] != 'Y' )
{
lPlayAgain = lInput[0] == 'y';
}
}
cout << "Game over. Good bye." << endl;

The specification of the class BullsAndCows is given below:
SWE20004 Semester 1, 2017 Dr. Markus Lumpe 3
#include
class BullsAndCows
{
private:
int fSecretNumbers[9];
int fBulls; int fCows; public: BullsAndCows(); void start();
void guess( std::string aNumberString ); int getBulls() const; // getter for bulls int getCows() const; // getter for cows
};

The class BullsAndCows defines a private array of nine integers 1 to 9. The constructor BullsAndCows() initializes this array. In particular, it uses a for-loop to set each element in the array fSecretNumbers to a unique value between 1 and 9.

The method start() shuffles the numbers in the array fSecretNumbers. We use the shuffling process developed by Fisher&Yates, shown in lecture 7, to rearrange the numbers. The value n has to be set to 9.

The method guess( std::string aNumberString ) implements the game logic. The parameter aNumberString contains the user input. It must be a 4-digit number in which all digits are different. For the purpose of this problem set, we assume that the player always provides a correct number (i.e., all digits are different). Each character in aNumberString can be easily converted into an integer value by subtracting ‘0' - the character for zero. The checking process uses a simple for-loop over the first four entries in the fSecretNumbers array. The following pseudo-code captures the required algorithm:

fBulls = 0
fCows = 0
for i = 0 to 3 do
declare int variable lTest, initialized to aNumberString[i] - ‘0'
if fSecretNumbers[i] == lTest then
lBulls = lBulls + 1;
else
for j = 0 to 3 do
if fSecretNumbers[j] == lTest then
lCows = lCows + 1; break; // terminate for loop end;
end; end; end;
In order to implement the class BullsAndCows, you need to include the cstdlib and the iostream C++ libraries. Also, you need to define two .cpp files, one for the implementation
of class BullsAndCows and one containing the main function. The program is a simple Win32 console application that does not require any command line arguments.
Test run:
Bulls and Cows, brought to you by StudentName (StudentID)

New game
Make a guess: 1234
Number of bulls: 0, number of cows: 3 Make a guess: 5678
Number of bulls: 0, number of cows: 1 Make a guess: 2813
Number of bulls: 1, number of cows: 3 Make a guess: 2381
Number of bulls: 4, number of cows: 0 New game, Y/N? n
Game over. Good bye.

Tasks

You have already been given the details regarding the requirements analysis and the design specification. To complete this problem set, you need to

1. Create and study a test scenario (on paper). What is a test scenario for Bulls and Cows? Answer: pick a 4-digit number in which all numbers are different. This number serves as the computer's secret choice. Testing the game now revolves around guessing this number. Proceed normally. That is, assume the human player's role and guess a number. Since you know the secret number, you can generate the answer of the computer (number of cows and bulls). Naturally, the answer needs to be correct. Then follow a logical inference process. That is, the test continues with drawing a logical conclusion about the closeness of the player's number to the computer's number and which steps to take to improve the guess, if the player has not yet guessed correctly. The test ends, when the player has found the correct number.

2. Implement the solution. (print program code and result from test run)

3. Annotate your solution with documentation comment tags

Your application also needs to produce a "production maker". This information must be written to the console (and be part of the program):

Bulls and Cows, brought to you by StudentName (StudentID) where StudentName is you name and StudentID is your student id.

The implementation requires approx. 80 lines of code plus the main function.

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: Swe20004 semester 1 2017 dr markus lumpe 2 - create and
Reference No:- TGS02282704

Expected delivery within 24 Hours