Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We will will upgrade our game to make it multiplayer. To do this, we will need to create an array of the Players subclass objects

We will will upgrade our game to make it multiplayer. To do this, we will need to create an array of the Players subclass objects within the GamePlay class.

Create a instance variable array called currentPlayers. The array should hold 3 elements and those elements should be of type Players.

Modify GamePlay.java so that the player name inputs are within a loop that creates 3 players and adds them to the currentPlayers array.

Modify the loop that asks for guesses, so that it asks for a guess from each player in turn, starting over when it reaches the last player.

Next, we will modify the winning so that you can also win physical prizes instead of money. Create an interface called Award.

Create the abstract method headers for a method called displayWinnings(). It should accept a Player object and a boolean as parameters. This boolean represents whether the guess was correct. It should return an integer.

Create a class called Money that implements Award.

Move the fields for winning and losing money amounts into this class.

Create the displayWinnings() method.

If the parameter is true, display a message that calls the player by name and says they won. Return the winning amount as a positive integer.

If the parameter is false, display a message that calls the player by name and says they lost. Return the losing amount as a negative integer.

Create a class called Physical that implements Award.

Create a instance field that is an array of at least 5 String elements that are physical prizes someone could win (THINK: television game shows)

Create a getRandomPrize() method that gets a random number and uses that for the index of the prize array to determine which prize is available. It should return an integer that is the index of the physical prize array.

Create the displayWinnings() method.

If the parameter is true, display a message that calls the player by name and says they won. Use the getRandomPrize() method to determine which prize they won and display that prize. Return 0 for the integer.

If the parameter is false, display a message that calls the player by name and says they lost. Use the getRandomPrize() method to determine which prize they won and display that prize telling the player this is what they could have won. Return 0 for the integer.

In the takeTurn() method of Turn.java make the following changes

Remove the winning and losing money variables from this class. This is handled in the Money class, now.

When comparing the number, generate a random number to decide if the player should be trying to win money or a physical prize. You can make up your own way to determine this with the random number.

If the random number determines a money prize, instantiate the Money class and call the displayWinnings method. Add the integer returned to the player's money before displaying the player.toString(). (Remember that it returns a negative integer if the player lost, so you can add in both cases.)

If the random number determines a physical prize, instantiate the Physical class and call the displayWinnings method. Add the integer returned to the player's money before displaying the player.toString(). (Remember that it returns a 0 integer, so you can still add in this case too, if that makes your code easier.) HERE is my code so far.

import java.util.Scanner;

public class GamePlay {

private Players player;

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

GamePlay game = new GamePlay();

Hosts host = new Hosts("Iron Man","");

host.randomizeNum();

Turn turn = new Turn();

System.out.print("Enter your name: ");

String name = input.nextLine();

System.out.print("Would you like to enter a last name? (yes/no): ");

String answer = input.nextLine();

if (answer.equals("yes")) {

System.out.print("Enter your last name: ");

String lastName = input.nextLine();

game.player = new Players(name, lastName);

} else {

game.player = new Players(name);

}

game.player.setMoney(1000);

boolean playAgain = true;

while (playAgain) {

while (!turn.takeTurn(game.player, host)) {

}

System.out.println("Would you like to play again? (yes/no): ");

answer = input.nextLine();

if (answer.equals("no")) {

playAgain = false;

} else {

host.randomizeNum();

}

}

}

}

public class Hosts extends Person {

private Numbers number;

public Hosts(String firstName, String lastName) {

super(firstName, lastName);

}

public void randomizeNum() {

this.number = new Numbers();

this.number.generateNumber();

}

}

import java.util.Random;

public class Numbers {

private static int randomNum;

public static int getRandomNum() {

return randomNum;

}

public static void setRandomNum(int randomNum) {

Numbers.randomNum = randomNum;

}

public void generateNumber() {

Random rand = new Random();

randomNum = rand.nextInt(101);

}

public boolean compareNumber(int guess) {

if (guess == randomNum) {

System.out.println("Congratulations, you guessed the number!");

return true;

} else if (guess > randomNum) {

System.out.println("I'm sorry. That guess was too high.");

return false;

} else {

System.out.println("I'm sorry, That guess was too low.");

return false;

}

}

}

public class Person {

private String firstName;

private String lastName;

public Person(String firstName) {

this.firstName = firstName;

this.lastName = "";

}

public Person(String firstName, String lastName) {

this.firstName = firstName;

this.lastName = lastName;

}

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

}

public class Players extends Person {

private int money;

public int getMoney() {

return money;

}

public void setMoney(int money) {

this.money = money;

}

public Players(String firstName, String lastName) {

super(firstName, lastName);

this.money = 1000;

}

public Players(String firstName) {

super(firstName);

this.money = 1000;

}

@Override

public String toString() {

return "Player: " + getFirstName() + " " + getLastName() + " - Current of money: $" + money;

}

}

import java.util.Scanner;

public class Turn {

private final int INCREMENT_AMOUNT = 500;

private final int DECREMENT_AMOUNT = 200;

public boolean takeTurn(Players player, Hosts host) {

host.randomizeNum();

Numbers numbers = new Numbers();

System.out.println(host.getFirstName() + ": " + player.getFirstName() + ", please enter your guess between 0 and 100: ");

Scanner input = new Scanner(System.in);

int guess = input.nextInt();

boolean result = numbers.compareNumber(guess);

if (result) {

player.setMoney(player.getMoney() + INCREMENT_AMOUNT);

System.out.println("Congratulations! " + player.getFirstName() + " won $" + INCREMENT_AMOUNT);

System.out.println(player.toString());

return true;

} else {

player.setMoney(player.getMoney() - DECREMENT_AMOUNT);

System.out.println(player.getFirstName() + " lost $" + DECREMENT_AMOUNT);

System.out.println(player.toString());

return false;

}

}

}

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_2

Step: 3

blur-text-image_3

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

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions

Question

Briefly describe the steps in the mutual fund selection process.

Answered: 1 week ago

Question

=+6. What five driving forces make CSR more relevant today?

Answered: 1 week ago