Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

code in c++ A game consists of a sequence of points played with the same player serving and is won by the first side to

code in c++

A game consists of a sequence of points played with the same player serving and is won by the first side to have won at least four points with a margin of two points or more over their opponent. By convention, the server's score is always called first and the receiver's score second. Score calling in tennis is unusual in that (except in tie-breaks) each point has a corresponding call that is different from its point value. The current point score is announced orally before each point by the umpire, or by the server if there is no umpire.

Table A: Point Values and Corresponding Umpires call.

Points Won Corresponding Call

0

"love"

1

"15"

2

"30"

3

"40"

4

"game"

otherwise

"ad'

Examples:

If the server has won three points so far in the game, and the non-server has won one, the score is 4015.

If the server has won zero points so far in the game, and the non-server has won one, the score is love15.

When both sides have won the same number of points within a given game i.e., when each side has won one or two pointsthe score is described as 15 all and 30 all, respectively. However, if each player has won three points, the score is called deuce, not 40 all. From that point on in the game, whenever the score is tied, it is described as deuce regardless of how many points have been played.

In standard play, scoring beyond a deuce score, in which the players have scored three points each, requires that one player must get two points ahead in order to win the game. This type of tennis scoring is known as advantage scoring (or ads). The side which wins the next point after deuce is said to have the advantage. If they lose the next point, the score is again deuce since the score is tied. If the side with the advantage wins the next point, that side has won the game, since they have a lead of two points.

When the server is the player with the advantage, the score is called advantage in". When the server's opponent has the advantage, the score is called advantage out". These phrases are sometimes shortened to ad in or van in (or my ad) and ad out (or your ad). Alternatively, the players' names are used: in professional tournaments, the umpire announces the score in this format (e.g., advantage Nadal or advantage Williams).

Tennis scoring rules

(adapted from Tennis scoring, explained by Joe Rivera at Sporting News)

There are multiple ways a point can be scored.

The server wins a point if:

the server hits an unreturnable ball (ball bounces twice on the opponents side of the net)

the server serves an Ace (unreturnable serve)

the receiver hits the ball out of bounds

the receiver hits the ball into the net

The receiver of the serve wins a point if:

the receiver hits an unreturnable ball (ball bounces twice on the opponents side of the net)

the server hits a Double fault (serves twice out of bounds)

the server hits the ball out of bounds (the lines count as in)

the server hits the ball into the net

PROGRAM ASSIGNMENT

Download and use the starter code, named TennisGame.cpp. Add the following functions to complete the program that simulates a tennis game including the umpires score calls. You are being asked to do things in a specific way in order for you to get good practice with static variables. Pay attention to the details in this program specification.

Write the following functions to complete the program.

getServer: This function will take no parameters. It will prompt the user to enter which player is the server: the user should enter 1 for Player 1 or 2 for Player 2. The function outputs an error message and prompts for a new value if the user does not enter 1 or 2. The function should return true if Player 1 is the server and false if Player 2 is the server.

Sample prompts of getServer

Sample of correct input:

Welcome to the tennis game simulator! Which player is serving? Enter 1 for player 1 or 2 for player 2: 2 Player 2 serves.

Sample of incorrect input:

Welcome to the tennis game simulator! Which player is serving? Enter 1 for player 1 or 2 for player 2: 8 Please enter either a 1 or 2: 1 Player 1 serves.

whatHappened: Each time this function is called, it will generate a random number that maps to one of the 8 possible outcomes in Table B. The function will display the event that happened based upon the random number generated. It will then return true if the server scored the point or false if the opponent scored the point.

Table B: Events, corresponding generated numbers, and point attribution (who gets the point).

Random Number Who wins the point? Event that occurred

0

server

The server hits an unreturnable ball (the ball bounces twice on the opponent's side of the net).

1

server

The server serves an Ace (unreturnable serve).

2

server

The receiver hits the ball out of bounds.

3

server

The receiver hits the ball into the net.

4

receiver

The receiver hits an unreturnable ball (the ball bounces twice on the opponent's side of the net).

5

receiver

The server hits a Double fault (serves twice out of bounds).

6

receiver

The server hits the ball out of bounds (the lines count as in).

7

receiver

The server hits the ball into the net.

other

server

Something strange happened! Give the server the point.

scoreToString: This function needs 1 parameter: pointValue. This function will convert the pointValue to the umpires call for that point. Based upon the integer value passed in, the function will return a string that contains the single word the umpire will use to call the score based upon Table A above.

Example 1: If the integer 0 is passed as a parameter, scoreToString will return, love.

Example 2: If the integer 3 is passed as a parameter, scoreToString will return, 40.

simulatePoint: This function will make use of the getServer, whatHappened, and umpireAnnouncement functions to simulate obtaining the server from the user, simulating a point being won, and converting the point values to the umpire's announcement respectively. The function should return true if this point ends the game; otherwise, return false.

First, initialize the static constant Boolean variable, PLAYER_1_SERVING, at declaration to the return value of getServer. This will ensure that the getServer function is only called the first time simulatePoint is called and the server will be remembered in subsequent calls.

This function will use two more static variables to keep track of the scores of the two players and increment the appropriate score based upon the return value of calling whatHappened. Return the boolean value received from umpireAnnouncement as the return value of the function.

Answer the questions in the Approach.txt file contained in the Starter Code zip file. Submit the Approach.txt file updated with your answers explaining your approach to programming this assignment. Did you code incrementally and test each function before proceeding to code the next function? Or did you code everything before testing? Explain how you tested your code. How did you make sure that every path of your code was tested?

Predefined Function(s) to Call

The following completed functions are included in the starter code. They do not need to be modified.

umpireAnnouncement: When this function is called, the numeric scores are converted into the umpire announcement based upon the results of calls to the scoreToString function and which player is serving. Remember that the servers score is always announced first.

Example 1: If player1s score (score1) is 1 and player2s score (score2) is 0, and player1 is serving, the score would be announced as, 15 love.

Example 2: If player1s score (score1) is 1 and player2s score (score2) is 0, and player2 is serving, the score would be announced as, love-15.

Note that the umpireAnnouncement function must also pay attention to the rules described above regarding when the score is tied. If the score is tied at 0, 1, or 2, umpireAnnouncement returns: "love - all", "15 - all", and "30 - all", respectively. If the score is tied at 40, umpireAnnouncement returns: "deuce".

After the score reaches deuce, umpireAnnouncement must determine whether to return either "ad - in" if the server takes the advantage or "ad - out" if the receiver takes the advantage.

logDebugInfo: This function creates a log file called tennis.log. When debug mode is turned on, debugging information is written to the tennis.log file.

main: The main function turns on debug mode if appropriate and calls simulatePoint in a loop until the game is won.

The sample output is below. User input is shown in yellow:

Example 1 where Player 1 is serving:

Sample Output (user input is in yellow)

Welcome to the tennis game simulator! Which player is serving? Enter 1 for player 1 or 2 for player 2: 1 Player 1 serves. The server hits a double fault (serves twice out of bounds). The score is love - 15 The server hits the ball into the net. The score is love - 30 The receiver hits the ball out of bounds. The score is 15 - 30 The receiver hits an unreturnable ball (the ball bounces twice on the opponent's side of the net). The score is 15 - 40 The receiver hits an unreturnable ball (the ball bounces twice on the opponent's side of the net). Game. Player 2 wins!

Example 2, where Player2 is serving:

Sample Output (user input is in yellow)

Welcome to the tennis game simulator! Which player is serving? Enter 1 for player 1 or 2 for player 2: 2 Player 2 serves. The server hits a double fault (serves twice out of bounds). The score is love - 15 The receiver hits the ball into the net. The score is 15 - all The receiver hits an unreturnable ball (the ball bounces twice on the opponent's side of the net). The score is 15 - 30 The receiver hits an unreturnable ball (the ball bounces twice on the opponent's side of the net). The score is 15 - 40 The server hits the ball out of bounds (outside of lines court lines). Game. Player 1 wins!

Example 3 (a longer game):

Sample Output (user input is in yellow)

Welcome to the tennis game simulator! Which player is serving? Enter 1 for player 1 or 2 for player 2: 2 Player 2 serves. The server hits a double fault (serves twice out of bounds). The score is love - 15 The server hits a double fault (serves twice out of bounds). The score is love - 30 The receiver hits the ball into the net. The score is 15 - 30 The server hits the ball into the net. The score is 15 - 40 The server serves an ace (unreturnable serve). The score is 30 - 40 The receiver hits the ball into the net. The score is deuce The server hits the ball into the net. The score is ad - out The server serves an ace (unreturnable serve). The score is deuce The receiver hits the ball into the net. The score is ad - in The receiver hits the ball out of bounds. Game. Player 2 wins!

Testing Your Code

Because we are dealing with random numbers, it may seem impossible to test every path of your code since you are at the mercy of whatever random number is generated. To overcome this, you should test your functions on your local computer before submitting them to Mimir. You could put temporary debugging code in the main function to call the other functions directly with the data you want the function to run against. and output the results.

For example, to test my umpireAnnouncement code from the main function, I put the following statements in main to verify the output of umpireAnnouncement. Once I had tested the function and was satisfied that it worked properly, I removed the DEBUG statements from main.

// DEBUG statements cout << "Testing (1,2,2): "; umpireAnnouncement(1, 2, 2); cout << "Testing (1,3,2): "; umpireAnnouncement(1, 3, 2); cout << "Testing (2,2,3): "; umpireAnnouncement(2, 2, 3); cout << "Testing (2,4,4): "; umpireAnnouncement(2, 4, 4); cout << "Testing (1,5,6): "; umpireAnnouncement(1, 5, 6);

Hint: Alternatively, since this program has a logging capability via logDebugInfo() function, one could add statements there to call functions directly and log the results in the tennis.log file. To turn on DEBUG mode, compile and run the program from the command line as below: If compiled with the following command:

g++ -Wextra -o tennis TennisGame.cpp

Run the program on the command line with the number 1 following the name of the executable, like this:

tennis 1

The function parameters of main() given in the starter code will check if there is a command link argument with the value 1. If so, it will enable debugging output to be logged to tennis.log.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

International Baccalaureate Computer Science HL And SL Option A Databases Part I Basic Concepts

Authors: H Sarah Shakibi PhD

1st Edition

1542457084, 978-1542457088

More Books

Students also viewed these Databases questions