Spin Game Version 4 In this version, start by making a copy of SpinGame3 and nam
ID: 3546472 • Letter: S
Question
Spin Game Version 4
In this version, start by making a copy of SpinGame3 and name it SpinGame4. This version has just a few small changes that will handle multiple players using most of the functionality and components you have already built.
Make the following changes:
1. Change thePlayer attribute in SpinGame4 to an array of Player instances.
2. In the SpinGame4 constructor, create thePlayers array of size 2, then create two Player instances with unique names, and add them to the array.
3. In Player, add getter methods for the name and score attributes.
4. In SpinGame4
In this version, start by making a copy of SpinGame3 and name it SpinGame4. This version has just a few small changes that will handle multiple players using most of the functionality and components you have already built. Make the following changes: Change thePlayer attribute in SpinGame4 to an array of Player instances. In the SpinGame4 constructor, create thePlayers array of size 2, then create two Player instances with unique names, and add them to the array.In Player, add getter methods for the name and score attributes. In SpinGame4's playGame method, change the logic to send the takeTurn message to each player within each round. Add print statements at the end of SpinGame4's playGame method that compares the final scores of each player and displays the name of the the winner (or declares a tie.) You will need to use the Player object's getName and getScore methods to do this from within the SpingGame4 class. Finally, create a copy of GameTester3 and name it GameTester4 with the following change:Explanation / Answer
// Player.java
public class Player
{
private String name;
private int score;
public Player(String n)
{
name = n;
score = 0;
}
public void takeTurn(Spinner spin)
{
System.out.println("Starting " + name + "'s turn with score = " + score);
System.out.print("Spin values: ");
int sc = 0;
spin.spin();
while (spin.getSpinValue() != 5)
{
sc++;
System.out.print(spin.getSpinValue() + " ");
spin.spin();
}
score += sc;
System.out.println("5");
System.out.println(name + "'s score is now " + score);
}
public String getName()
{
return name;
}
public int getScore()
{
return score;
}
}
// Spinner.java
import java.util.Random;
public class Spinner
{
private Random randomMaker;
private int spinValue;
public Spinner()
{
randomMaker = new Random();
spinValue = 0;
}
public void spin()
{
spinValue = 1 + randomMaker.nextInt(10);
}
public int getSpinValue()
{
return spinValue;
}
}
// SpinGame4.java
public class SpinGame4
{
private int numberOfRounds;
private Spinner theSpinner;
private Player[] thePlayers;
public SpinGame4(int x)
{
numberOfRounds = x;
theSpinner = new Spinner();
thePlayers = new Player[2];
thePlayers[0] = new Player("Tom");
thePlayers[1] = new Player("Jerry");
}
public void playGame()
{
for (int x = 1; x <= numberOfRounds; x++)
{
System.out.println("-------------------------------------------");
System.out.println("ROUND " + x);
thePlayers[0].takeTurn(theSpinner);
thePlayers[1].takeTurn(theSpinner);
}
int winner = -1;
int score0 = thePlayers[0].getScore();
int score1 = thePlayers[1].getScore();
if (score0 > score1)
winner = 0;
else if (score1 > score0)
winner = 1;
if (winner >= 0)
System.out.println(thePlayers[winner].getName() + " WON!!!");
else
System.out.println("TIE GAME");
}
}
// GameTester4.java
import java.util.Scanner;
public class GameTester4
{
// test game4
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
//prompt for number of rounds to play:
System.out.print("Enter number of rounds to play: ");
int x = s.nextInt();
//start SpinGame4 passing in the number of rounds to play
SpinGame4 sg = new SpinGame4(x);
sg.playGame();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.