Create C# Logic for a Basketball Game App
April 6, 2022 ยท View on GitHub
Prerequisites
This activity requires you to create code in Visual Studio. You can use the C# Console Application template to complete the exercises in this activity. Enter your code within the Main method of the template and press Ctrl + F5 to run the program without debugging.
Instructions
In this activity, you'll use C# to create basic app logic for the basketball game. Each exercise reflects logic that could be used in a script for game play.
- Create a
playervariable for the app's player. - The
playermust have possession of the basketball to take a shot. If theplayerdoes not have the basketball, then they must wait to take another shot. When the game starts, theplayerhas possession of the basketball up until they shoot the basketball. Create a conditional statement that reflects what happens if theplayerdoes not have the basketball in their possession. - Create a
scorevariable for the total number of points received from a successful basketball shot. - When the game starts, the
scoreis set to0. Each time theplayermakes a successful shot, theirscoreincreases by1point. Create aTakeShotmethod that increments the player'sscoreby1point if their shot is successful. - The
playerhas5chances to make a shot. After their 5th shot, the game is over. Create a while loop that executes theTakeShotmethod until theplayerhas taken their 5th shot. - When the game is over, the total
scoreis output for theplayer. Use theConsole.WriteLog()command to create a string that outputs the player's name andscore.
Bonus
Without a game in play, it can be a challenge to verify whether the program you created is valid. However, there's a way to programmatically simulate game play! The ConsoleReadLine() command accepts input and stores the value to a variable within the program. Once the value is stored, the program can use the value as it continues to execute the script.
Using ConsoleReadLine(), modify the program you created to simulate game play. Use ConsoleReadLine() to do the following:
- Ask and assign the player's name to the
playervalue. Welcome the player and inform them that the game is about to start. - Confirm whether the player's shot was successful. If the player makes the shot, then increase their score by
1point. If the player doesn't make the shot, then tell the player to try again. - Once game is over, print the player's final score to the console.
Rubric
| Criteria | Exemplary | Adequate | Needs Improvement |
|---|---|---|---|
| Create game variables. | All variables are created with proper syntax. | Most variables are created with proper syntax. | Incorrect syntax used to create a variable(s). |
Create a method with a while loop. | Method and while loop created using proper syntax. | Method or while loop has a syntax error. | Method and while loop both have improper syntax. |
Use ConsoleReadLine() to simulate game play. | Game simulation outputs accurate strings. | Game simulation works but produces inaccurate string output. | Code does not run due to errors. |
Solution
To view a potential solution for this activity, check out the Solution.