Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

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.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M92295875
  • Price:- $160

Guranteed 48 Hours Delivery, In Price:- $160

Have any Question?


Related Questions in C/C++

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As