CS 162 Assignment #1 Go Hooping
Goals:
● Practice good software engineering design principles: o Design your solution before writing the program o Develop test cases before writing the program
-
● Review conditionals, loops, functions, and basic contiguous buffers (arrays)
-
● Use functions to modularize code to increase readability and reduce repeated code
Problem Statement:
You are tasked with implementing a C++ program that simulates a basketball shooting game for a local high school. The game supports 2 players, with each player attempting to score as many points as possible by making shots from five fixed shooting positions.
At each shooting position, there is a rack of five basketballs. Out of the five balls, the first four are regular balls, worth one point each. The fifth ball, often nicknamed the "money ball", is worth two points. The “money ball” can only be shot after the four regular balls are shot.
Of those five positions, the player can choose a position to have a rack consisting only of "money balls", which are all worth two points. This position is called the “money-ball rack”.
Additionally, two ball pedestals are positioned at “Starry Range” (two deep shot locations between racks 2 and 3 and the other between racks 3 and 4. Each ball pedestal holds one special white “Starry” ball. Shots made with the white “Starry” ball are worth 3 points.
The goal of this game is to score as many points as possible. Player 1 needs to finish shooting from all five positions before the next player shoots. The player with the highest score at the end of the game is the winner.
After the game is completed, the players will have the option to play again or quit. The program should keep restarting from the beginning until the players eventually decide to quit.
Here is a video demonstration of the shooting game concept (ignore the shot clock restriction)
1
Implementation Requirements:
-
Before each player’s shooting, prompt them to choose the “money-ball rack” position. The program should re-prompt until a valid input is given.
-
For both players, assume there is always a 50% chance of successfully making the shot, no matter if it is for the regular balls, the money balls, or the Starry ball.
-
For each player, you must use at least one array to keep track of the results of each shot (e.g., which shots missed vs which shots made it). The results of the starry ball shots do not have to be stored in an array (though they can be), but the results of the other 25 shots do.
1. You may not use the STL for this assignment (e.g., std::array, std::vector, etc). We’ll be covering the STL later; for this assignment, it’s imperative that you demonstrate knowledge of basic buffers. That’s to say, your array must be a regular C/C++ array (statically allocated or dynamically allocated—there’s extra credit involving a dynamically allocated array)
-
The shooting result of each player should be displayed using the following notation:
-
● _ (underscore) for missed shots
-
● O for made shots worth 1 point
-
● M for made shots worth 2 points (the "money ball")
-
● S for made shots worth 3 points (the “starry ball”)
-
-
For each player, the program needs to display the scores for each rack, and the total score.
-
After both players have completed their shooting, declare the winner (or a tie game).
-
Prompt the players whether they want to play again or quit the game. If they want to play again,
restart from the beginning. Otherwise, the program should terminate gracefully (you should not use the exit() function for this; using the exit() function is generally considered bad practice—your program should end “naturally” by reaching the end of the main() function).
-
Your program must scold the user and reprompt any time they supply a bad value, but not a bad data type. For example, when your program asks the user for an integer between 1 and 5 (e.g., for the money ball rack position), you may assume that the user will enter some sort of integer. However, you may not assume that they will specifically enter an integer between 1 and 5. Your program must check whether the user’s entered integer is between 1 and 5 and, if not, scold them for their invalid input and reprompt (in a loop) until they eventually supply a valid value.
-
Your user interface must provide clear instructions for the user and information about the data being presented
-
Your code must adhere to the course’s C++ style guidelines (including the SRP and DRY principles—you should have many modular functions in your implementation)
-
Your code most compile and run (without runtime errors, like segmentation faults). If your code does not compile or run correctly, large portions of your assignment may go ungraded, potentially resulting in a zero in the gradebook (this policy is outlined in the syllabus).
Example Output: (User inputs are highlighted)
Note: You don’t have to follow the example output exactly, but you need to display all required fields and
they must be readable.
(Example output starts on the next page)
2
Welcome to the basketball shooting contest!
Player 1:
Where do you want to put your money-ball rack? Enter 1-5: 8
Invalid input, try again.
Where do you want
to put your money-ball rack? Enter 1-5: 4
| 4 pts | 3 pts |0pts | 5 pts |3pts | 8 pts | 2 pts
to put your money-ball rack? Enter 1-5: -8 That’s not a valid input.
Rack 1: _ Rack 2: O Starry: _ Rack 3: O Starry: S Rack 4: M Rack 5: O
Total: 25
_ O O O
_ O
M _ _ O
pts
O M _ _
O M
M M _ _
Player 2:
Where do you want
Where do you want
to put your money-ball rack? Enter 1-5: 5
| 0 pts | 1 pts |0pts | 6 pts |0pts | 2 pts | 10 pts
Rack 1: _ Rack 2: O Starry: _ Rack 3: O Starry: _ Rack 4: O Rack 5: M
_ _ _ _
O O
_ O M M
_ _ _ _
O M
_ _ M M
Total: 19
Player 1 is the winner!!
Do you want to play again? (1-yes, 0-no): 3
Sorry, that’s not a valid input.
Do you want to play again? (1-yes, 0-no): 0
pts
3
(15 pts) Extra Credit: N players using Dynamic Arrays
Instead of supporting only two players, modify the program so it can support N players, where N is a
positive integer supplied by the user.
Additional implementation requirements:
-
● The value of N is determined by a user input during runtime at the start of the program.
-
● Use dynamic arrays allocated on the heap to keep track of the total scores of each player (you do
not have to store all players’ shooting results in a gigantic multidimensional dynamic array; however, you do need to keep track of every player’s total score in one dynamic array [containing a total of N integers], and you do still need to use arrays to store players’ shooting results in some way or another, as per the regular assignment requirements).
-
● Your program must not have memory leaks. Make sure you use valgrind to check this!
4