Reference no: EM132373724
Implementing the Tropical Golf Game v2.0
Task
You are to implement a golf game program as outlined in the following description. Use what you have learned during the entire trimester, as well as the feedback that you received from Assessment 1 and the class sessions on this assignment.
Please keep in mind that as this is a complex program, you will require substantial time to plan the program, break the game play into functions, and then code and test the result.
Program Features
Short description:
• You are required to create a Python 3 program for a game of Tropical Golf. The player will be attempting to get the ball in the hole using the least amount of swings.
• The rules and gameplay for the program are as follows:
o The game asks for the player's name then welcomes the player, asks the numerical value of par, the distance to the hole, and then displays the menu.
o The main menu displays 3 options "Instructions", "Play Game", and "Quit"
o If the player chooses "I" the instructions are displayed
o If the player chooses "P" the game is played:
– The ball starts at the distance to the hole specified by the player
– The player chooses one of three clubs, each of which hits a different average distance
– Every hit travels directly towards the hole in a straight line (this is a simple golf game, we're only playing in 2D)
– Distance of ball travel after a hit are only whole numbers (125, 250, etc)
– Once the ball is in the hole the player is told how many swings it took and whether they are over or under par
• ‘Par' is the estimate of how many swings a game should take
– The player may use as many swings as is necessary to get the ball in the hole
o After every menu choice OTHER than ‘Q' the menu is displayed again.
o Once the player enters "Q" the game thanks the player by name and says goodbye
Detailed instructions:
Ensure that your program has the following features:
1. Ask the player's name and welcome them to the game using their name.
2. Ask the player what is par for this game (number between 3-5 inclusive)
3. Ask the player what the distance to the hole for this game is (whole number between 195 and 250 inclusive)
4. Show the game menu: (I)nstructions (P)lay golf (Q)uit
The user will enter the first letter of each option to choose it.
5. If the user chooses (I) then the following message is to be displayed, given the par and distance amounts obtained above.
This is a simple golf game in which each hole is 230m game away with par 5. You are able to choose from 3 clubs, the Driver, Iron or Putter.
The Driver will hit around 100m, the Iron around 30m and the Putter around 10m. The putter is best used very close to the hole.
After the instructions are displayed the game menu will be presented again.
6. If the player chooses (Q) then the following message is to be displayed:
Farewell and thanks for playing <user name>.
7. After displaying the instructions, or playing a round of golf, the program is to return to the menu and loop until the user chooses to quit
8. If the user chooses (P) then a game of golf will begin as described below
Playing the game:
• The player is first notified of the distance to the hole, which was specified in step 3 above.
• For each swing, the player chooses a club and then the program generates the distance hit for each shot, updating the distance to the hole accordingly. After each swing the distance the ball travelled, and the distance remaining is displayed along with the current score using a message similar to the one below:
Your shot went 103m.
You are 127m from the hole, after 1 shot/s.
• The player has three clubs to choose from for each shot. Each club hits the ball a set average distance, but the actual distance randomly varies between 80% and 120% of the average. For the Driver you will need to generate a random number between 80 and 120 to do this.
• The clubs and their average distances are:
o Driver: 100m (actual distance will be a random number between 80 and 120)
o Iron: 30m (actual distance will be a random number between 24 and 36)
o Putter: 10m (see below for actual distance)
• When the ball is inside 10m and the putter is used, the shot will be between 80% and 120% of the distance to the hole, not the club's average distance. The minimum distance the putter can hit is 1m (no 0m hits). All distances are whole numbers.
• The user will enter the first letter of a club to choose it (i.e. ‘I' or ‘i' for the Iron)
• If an invalid club is chosen, the player is asked to make a valid choice.
• The ball cannot be a negative distance to the hole. Whether it is in front of or behind the hole, it is still a positive distance to the hole. Python has an absolute value function that you can use to help you with this.
• Play proceeds until the ball is in the hole (distance to the hole is zero), and then the program informs the user of their score.
• The players score is the number of shots taken to get the ball in the hole. The final output will display the number of shots taken and how this relates to par. If their score is less than par for this hole, output is "under par", equal to par is called "par", and more than par is "over par". See sample output for exact outputs.
After 10 hits, the ball is in the hole! Disappointing. You are 5 over par.
After 5 hits, the ball is in the hole! And that's par.
After 3 hits, the ball is in the hole! Congratulations, you are 2 under par.
• If the player has done a previous round (i.e. this is the second time the game is played), the game should then display the total score with an appropriate message, and then show the game menu again.
Implementation:
Python file:
You are to divide your solution into functions, following the principles shown in class. These may involve one for each menu option (other than Quit) as well as functions for parts of the program (e.g. calculating the current player score could be implemented as a function).
You may show your assignment to your tutor during practical time to get comments or suggestions. It is important to note that you can only get help from staff in practical time after your prac work is finished.
General Principles:
In this assignment, you will be focusing on implementation using selections, repetition and functions. Lists and string formatting should be used where they are feasible.
Use the techniques and patterns that you have learned and seen demonstrated in class.
• Coding standards:
o Variables:
- Local constants should be set at the top of the function
- Although Python does not use constants you may set variables to values that will remain the same for the life of the program. These should be named in all caps
• You should be able to modify these variables to adjust the various values of items such as PAR, STARTING_DISTANCE, etc. This is an important aspect of this assignment, so consider carefully how to use constants. Remember to use these variables everywhere you can.
• You will need to decide the best position to place these variables.
- You should AVOID the use of global variables.
• In other words, you should pass the values from a function to another as parameters; variables and constants should be local only when needed by a single function.
o Identifier Names
- Variables should be named appropriately using either camelCase or snake_case
- No variables should be a single character such as "x" or "y" unless they are used in a loop as an index. Variable names should be meaningful, and describe their use and/or purpose
- Any list variable should be named with plurals to show multiple data is held (e.g. prices versus price).
- Function names should begin with a verb (they are active), for example getValue() is better than value(). Good verbs include - ‘get', ‘display', ‘calculate', ‘check' etc (not all these are good for this problem...)
o Comments:
- You will need to add at LEAST one comment for each function that you define, including main.
- The comment should either be placed directly above or below the function header
- The comment should describe: (a) the purpose of the function, (b) any inputs and (c) any returned values
- Additional comments should be placed at any position where the functionality of the program may be unclear.
- All code MUST be properly indented
o Error handling:
- Note that menu choice and other character selection should handle upper and lower case letters.
• Look into the upper() or lower() functions in Python
- You also must make sure that any functions that get input from the user do error checking to ensure that the input is within the expected range.
- Use isdigit() and isalpha() functions to validate correct type of user input.
o Output
- Make your output display as close as possible to the given examples, especially formatting and layout.
- Use format() to display tabs and strings at a set width
o Python Functions
• ‘random.randint' to see how to generate a random number in Python
• ‘abs' to see how to use Pythons absolute value function
• ‘upper' or ‘lower' to see case conversion functions
• ‘format' to display output
• ‘isdigit' and ‘isalpha' for validating user input
Attachment:- Implementing the Tropical Golf Game.rar