c/c++代写 Project 2 – Subtract a Square

Project 2 – Subtract a Square

Overview:  Your project is to create a game called Subtract a Square.

Goal:  The goal of the project is to use indefinite repetition and perform user input validation.

Description:  In Subtract a Square, you have 2 heaps of marbles.  Your program will randomly select how many marbles are in each heap by generating a number between 10 and 100.  The user and the computer will then take turns drawing marbles from the heaps.  A player must take at least 1 marble each turn and the number of marbles that they draw must be a square number (1, 4, 9, 16, 25, 36, 49, 64, 81, 100 …).  A player can only draw from one pile in a single turn.  The player who gets stuck picking up the last marble in the game (not a particular heap) loses.

The user will always be given the first turn.  You should not allow the user to make an illegal move. Neither the user nor the computer can draw ZERO marbles from a heap, that’s cheating!  You will also need to verify that the number that the user enters is a square number.

The computer’s strategy for taking a turn should be very simple.

  • Select a random heap that still contains marbles.
  • Select a random integer between 1 and the square root of the heap size, then square that integer to get the number of marbles that the computer will remove.
  • Take its move and print out a message indicating what it did.

Extra Credit (5 points) : Give the computer more intelligence than the basic strategy. Make sure to comment your code that this logic is for extra credit.

 

At the end of a game, you should print out a message indicating who won.

A sample set of output is shown here (with the user’s input in red).  Your output does not have to match this exactly.

Welcome to Subtract a Square

Heap 1:  19
Heap 2:  90

Which heap would you like to draw from?  2
How many marbles would you like to draw?  81

Heap 1:  19
Heap 2:  9

I drew 16 marbles from heap 1.

Heap 1:  3
Heap 2:  9

Which heap would you like to draw from?  2
How many marbles would you like to draw?  16
Too many!  You must choose a square number between 1 and 9.  Please enter again:  0
Too few!  You must choose a square number between 1 and 9.  Please enter again:  6
Not a square number!  You must choose a square number between 1 and 9.  Please enter again:  9

Heap 1:  3
Heap 2:  0

I drew 1 marbles from heap 1.

Heap 1:  2
Heap 2:  0

Which heap would you like to draw from?  2
Sorry, that pile is already empty, please choose again:  17
Sorry, that is not a valid pile, please choose again:  1
How many marbles would you like to draw?  1

Heap 1:  1
Heap 2:  0

I drew 1 marbles from heap 1.

Heap 1:  0
Heap 2:  0

Congratulations, you win!!

Helpful Code:  Calculating the square of a number is straightforward, you simply multiply the number by itself, e.g.

int x = 5;
int squareX = x * x;

 

If you want to go in the other direction and calculate the square root of a number, you will need to #include the cmath library.  In the cmath library, there is a function called sqrt:

double x = 89;
double rootX = sqrt(x);

int y = 87;
double rootY = sqrt((double)y);
// sqrt expects its input to be type double, so if the variable is an integer,
// you will need to type cast it.

Project Requirements:  Your code should be thoroughly commented, and you must include your name in a comment at the top of the file. You should also follow all of the good programming practices we have discussed in class so far, such as meaningful variable names, indentation, and input validation.

Submission:  Using the Canvas assignment feature, you should submit the source code (.cpp file). Make sure you submit your assignment after uploading the file attachment!

 

Getting Started:  If you sit down and try to implement the entire project in one-shot, it will seem over-whelming.  It is important to break down the implementation into small manageable pieces and get each piece working correctly before starting on the next one.  As you get more comfortable with programming, you’ll naturally start to see how to break apart the projects into small pieces.  Below, I’ve suggested a possible sequence of steps you could take to implement your project.  Write the code to:

  1. Generate the two heaps and display them to the user.
  2. Prompt the user for which heap they want to draw from, validating that it is a reasonable input and that the heap selected is non-empty.
  3. Prompt the user for the number of marbles that they wish to draw, validating that the number is greater than 0, less than or equal to the total number of marbles in the heap they chose, and a square number.
  4. Subtract the chosen number of marbles from the correct heap and display the new heaps.
  5. Have the computer randomly select one of the piles (making sure it is a non-empty pile).
  6. Have the computer pick the square root of the number of marbles it will withdraw (making sure the number is between 1 and the square root of the pile size).
  7. Calculate the square of the number that the computer has chosen and deduct that number of marbles from the corresponding pile. Display the new piles.
  8. Add a loop to keep repeating the alternation of turns between human and computer until there are no marbles left.
  9. Add in the code to detect who won.

You should run and thoroughly test your code after each step.  It is MUCH easier to find 1 error in 5 new lines of code than it is to find 10 errors in 50 lines of code.