Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this project, you will construct a Java program to play a slightly simplified version of Deal or No Deal. In the two-player game a

For this project, you will construct a Java program to play a slightly simplified version of Deal or No Deal.

In the two-player game a contestant (the player) plays (gambles) against a banker. For the program simulating the game the user will be the player and the computer will be the banker.

The structure and rules of the game

There are 11 numbered briefcases (26 in the real life game), each contains a cash value between $1 and $1,000,000. Different cases contain different values. The individual amounts are public, but nobody knows how the dollar values get distributed in the briefcases.

At the start of the game the player selects one out of the 11 briefcases. The selected initial briefcase is set aside, the cash value in the initial briefcase is not revealed until the end of the game. This initial briefcase is not eliminated from the game.

The game continues for up to 10 rounds. In each round played in the game (from 1st round up to the 9th) three actions are performed in the order as listed.

The player must choose one of the remaining briefcases. The selected briefcase together with its cash content is eliminated from the game. When a case has been eliminated, it is opened and the money contained in it is revealed.

The banker offers the contestant a deal of quitting the game for a certain amount of cash.

The contestant may accept the deal or may reject it. If the deal is accepted, the offered money is the winning of the player and the game is finished. If the deal is rejected, the player enters the next round and the game is continued.

The rounds continue until either the player makes a deal, that is accepts the bankers offer, or rejecting all offers the game reaches the last (10th) round when there is only one unopened briefcase left that has never been picked for elimination. If this happens, the banker offers to the player a trade: the initial briefcase for the last remaining unopened one. Should the player refuse the trade, the winning is the money in the initial briefcase, otherwise the winning is the money in the last briefcase.

As a result of the rules above, the player always wins some money and the goal for the player to maximize the winning which is either the content of the initial briefcase, or the content of the last briefcase, or the cash offer from the banker in one of the rounds.

At the end of the game all remaining briefcases including the initial case are opened and their values revealed.

Analysis and Requirements

Input to set up the game

The dollar values contained in the briefcases:

1; 10; 100; 1,000; 4,000; 10,000; 40,000; 100,000; 300,000; 700,000;

1,000,000

Note that additional data used in the code, the number of briefcases (11) and the number of rounds (10) follow from the number of dollar values given above

Input to play the game (each solicited from the user)

The serial number of the initial briefcase

In each round, the serial number of the briefcase the user eliminates

deal or no deal answers to the bankers offers

Output

A complete log book (description of every event) of the game

The dollar value won by the contestant at the end of the game

Formula

The bankers cash offer depends on the serial number of the current round and on the average of the contents of the non-eliminated briefcases. The formula used in a real life game is not public. In this model you have the following formula to apply:

N is the serial number of the round currently finished in the game (0 avg is the average of the dollar values in the remaining briefcases

temp = 0.01 *(75 + 5 * N 3 * max(0, N -5)) * avg*(1+0.15*(avg-55000)/Math.abs(avg 55000)) offer = Math.max(100, 1000*Math.round(temp/1000))

Design

Input for setting up the game (see in the Analysis) shall be directly assigned to the relevant variable

Briefcase serial numbers selected by the player are solicited on the console

Deal or No Deal shall be answered on a JOptionPane confirm dialog

The output describing the game events shall be displayed on the console

The dollar value won by the contestant shall also be displayed on the JOptionPane dialog

Class design is described below in three steps:

A chart showing the logic of class co-operations

A UML diagram for each class

A detailed specification of classes and methods, these should be used together with the UML diagrams

The program will consist of five user-defined classes:

Note that an arrow pointing from class A to B indicates that the code in A utilizes resources (like calling some methods) of B:

The next chart shows the UML diagrams of the classes. The arrows indicate the same aggregation/access logic as above. The classes are further described in the tables following the UML diagrams. Instance methods that called in other classes must be declared public. All instance fields and all helper methods not called in other classes are to be declared private.

Briefcase

This class represents a single briefcase.

Fields

$value

The dollar value contained in the briefcase, double

chosen

true if the player has chosen this briefcase; false otherwise.

Methods

Briefcase( )

Constructor. Takes a parameter to initialize the data field $value

getValue( )

Accessor. Returns $value.

setValue( )

Mutator. Takes a parameter. Assigns $value the parameter

isChosen( )

Returns the value of the instance field, chosen (accessor)

setChosen( )

Mutator. Takes a parameter. Assigns field chosen the parameter

BriefcaseCollection

This class represents as an array the collection of 11 briefcases used in the game.

Fields

cases An array of type Briefcase. The array stores the collection of briefcases of the game

Methods

BriefcaseCollection( )

Constructor. Takes an array of double values for parameter (this parameter is intended to initialize the dollar values in the briefcases of the collection, see below). Instantiates and initializes the array field cases to the length of the parameter array. Runs a for loop and instantiates the Briefcase array entries, such that it initializes $value of Briefcase of index k to the parameter array entry of the same index. Calls the permute( ) method to create a random re-arrangement of the briefcases in the array.

getLastCase( )

Returns the last remaining case after all others have been chosen.

showRemainingCases()

Displays as a list the serial numbers of those array entries from cases which have not been chosen. The serial number is 1 greater than the array index of the same briefcase.

For instance, if only the briefcases of index 0, 1, 4, 8 have not yet been chosen, the output would be: 1 2 5 9

chooseThisCase( )

Takes a parameter say, num of type int, to be used as the serial number of a briefcase. The method sets the chosen field of the Briefcase of index num 1 to true and returns the corresponding Briefcase

invalidChoice( )

Takes a parameter say, num of type int, to be used as the serial number of a briefcase. The method returns true, if num is an invalid choice that is, if it is less than 1 or greater than cases.length or the case of array index num -1 has been chosen (use the isChosen method of the Briefcase class)

computeAverage()

Computes and returns the average of the dollar values currently in the collection (briefcases not chosen considered only)

permute( )

Private helper method, void. Creates a random permutation of the elements of cases; the method runs a for loop to the length of the array, in the loop for every loop index k another array index q>0 is randomly selected thencases[k] is swapped with cases[m], where m = (k + q)%cases.length

Banker

This class represents the banker. The banker knows the set of dollar values used in the game. The bankers only responsibility is to make offers to encourage the player to quit the game.

Fields

MONEY_VALUES

Array of the dollar values; public static and constant; holds all the dollar values used in the game. Initialized at the declaration by an initializer list of values

Methods

makeOffer( )

static method. Takes two parameters: an int type for the serial number of the current round and a double value for the average of the dollar values in the remaining cases. The method computes and returns the offer to be made by the banker. The calculation is described above in the Formula section if Analysis.

GameControl

This class controls game play. All methods except play( ) are helper methods as such those must be declared private.

Fields

collection

to hold a BriefcaseCollection object; instantiate it at declaration using the MONEY_VALUES array from the Banker class for parameter

kb

Scanner object applied for obtaining input from the user to direct game play; instantiate at declaration

Methods

chooseACase( )

Takes a BriefcaseCollection object collection for parameter and returns one of the briefcases from collection; the method

calls showRemainingCases( ) w.r.t collection to display the serial numbers of the remaining briefcases

solicits a choice by the player from displayed serial numbers

saves the selected input in a local variable

runs a while loop to validate the input from the console (control the loop with the return value of the invalidChoice() method)

if input accepted, the chooseThisCase( ) method with the solicited number as parameter is called and the value is returned

makeDeal( )

The double type parameter will be used for the bankers offer; the method

opens a confirm dialog (see Figure 1 below) to display the offer

The user clicks Yes to accept and No to reject the deal

Checks if the answer is equal to JOptionPane.YES_OPTION and the boolean result is returned

playOneRound()

void; takes an int for parameter (which round to play);

Prints the round number and a solicitation Choose a case please to the console, see the sample output

calls chooseACase( ) with collection for parameter to get the next selected briefcase, that calls the getValue( ) accessor to get the dollar value which is printed to the console, see the sample output

play()

Controls the overall play of the game; void, no parameter

The method prints the message of Figure 1 to the console, the second and third line of the message is done by the chooseACase() method called here with collection for parameter; the return value of chooseACase is saved in a local variable initialCase

Additional local variables recommended:

double offer = 0; int round = 1; boolean deal = false;

Runs a while loop as long as deal is false and round

In the loop

call playOneRound( ) round for parameter

b.call makeOffer( ) from the Banker class, save the return value in offer; parameters are round and the return value ofcomputeAverage( )

call makeDeal( ) with offer for parameter, store the answer in deal d. updateround

After the loop a selection logic determines the output:

if (deal) display messages like the templates Figure 2 and Figure 3 terminate the process

else call getLastCase( ) and save the return in a local variable lastCase call showRemainingCases( ) (this call just prints the serial number of lastCase to the console)

offer the trade by opening a confirm dialog as shown on Figure 4

if Yes display the message dialog as shown on Figure 5 display message like Figure 6 terminate the process

else display the message dialog as shown on Figure 7 display message like Figure 8 terminate the process

PlayTheGame

This class contains the main method.

Methods

static void main(String[] args)

calls play( ) to run the game; (you may use an anonymous GameControl object for prefix)

Output templates

Make your initial choice!

Remaining cases: 1 2 3 4 5 6 7 8 9 10 11

Which case?

Figure 1

You made a deal for $191,000.00.

Your initial case contains $100.00

Figure 2

Figure 3

Figure 4

Figure 5

Your winning from the last cases is $1000.00. Your case contained $100.00.

Figure 6

Figure 7

Your winning from your initial case is $40,000.00. The last case contained $1,000,000.00.

Figure 8

As it is specified in the above method descriptions, the player answers input questions

on the console for selecting a case

on a confirm dialog window for accepting/refusing a deal offered

Console input values must be validated, since wrong input may totally throw off the game. If the player selects a non-existing case number (too large, or has already been eliminated), then the wrong input value must be ignored and another value is to be solicited from the player. Process repeats until the input is correct

The dollar values used in the game and to be stored in the MONEY_VALUES array of the Banker class are as follows:

1, 10, 100, 1000, 4000, 10000, 40000, 100000, 300000, 700000, 1000000

Use an initializer list to instantiate the array. This array is a public static field since it is independent of any objects.

Note that eliminating a briefcase from the collection simply means to change the chosen field to the boolean true value.

There are several options to create a random permutation of an array.

For every index k choose randomly two other indices and swap the elements of those indices; this process may not affect the element of index k

For every index k choose randomly another index (including k) and swap the two entries

For every index k apply a cyclic rotation by a random shift and swap the two entries. For our array used in the game this means to select q randomly between 1 and 10, define m = (k+q)%11 and swap the two entries at index k and m

The last procedure is recommended, it completes the specification of the permute method in the design section. You may also implement the second item

The only static method in the program, other than main is makeOffer ( ) in the Banker class. This is a utility method working only on its parameters. There is no need to create a Banker object. To call the method or access the array, the class name Banker is the prefix to use.

There are no explicit constructors in the Banker, GameControl and PlatTheGame classes. Banker and the main are not instantiated and only the default constructor of GameControl is used. The other two classes all have explicit constructors with various functionalities.

The play( ) method of the GameControl class must be public. The other three methods are helpers, that is, they are called internally only in the class, as such make them private.

The same holds for the permute() method of the BriefcaseCollection class. All other methods in any of the classes must be public.

Numeric figures in the output messages should be formatted as shown on the samples and Figures

Dollar values and case numbers displayed in output are generated by random events, therefore they are not suitable for testing purposes

When working on implementation, carefully and simultaneously study the UML diagrams and the class/method specifications.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Documen Word File Home Insert Design Layout References Mailings Review View Foxit PDF Tell me what you want to do Brief caseCollection Briefcase Banker Play TheGame Game Control Page 1 of 1 5 words OK Ask me anything Derrick White Jr X Share 5.37 PM 4/15/2017 R2

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago