[Solved]Java Help Use Nested Loops Assignment Write Class Gamemanager Keeps Track Targeting Histor Q37178914
JAVA Help! Do NOT use nested loops.
What to do
In this assignment, you will write a class GameManager thatkeeps track of who is targeting whom and the history of who stolefrom whom. You will maintain two linked lists:
- a list of people currently playing (the “thief ring”) and
- a list of those who are done (the “stolen list”).
As people are stolen from, you will move them from the thiefring to the stolen list by rearranging links between nodes. Thegame ends when only one node remains in the thief ring,representing the winner.
A client program called RobinHood has been written for you. Itreads a file of names, shuffles the names, and constructs an objectof the class GameManager. You are writing the GameManagerclass.
This main program then asks the user for the names of eachperson to steal from until there is just one player left. RobinHoodcalls methods of the GameManager class to carry out the tasksinvolved in administering the game.
GameManager
Use the node class which is called PlayerNode. The PlayerNodeclass has three data fields: one for the name of the person, onefor the name of the thief and a “next” field to keep track of thenext value in the list. This class is defined within theGameManager class (it is a “nested class”). You can use it withinyour GameManager class just like you would use any other class. Onevalue of inner classes is that if you do not return a PlayerNodefrom any public method, then users of your class cannot see oralter your internal structures. You must not change PlayerNodeclass; in particular, name is final, so once a node is created, itis the node for that player for the duration of the game.
GameManager must have two fields, and no more (not even privateones):
- a reference to the front node of the thief ring
- a reference to the front node of the stolen list
It must not define any other fields.
The constructor for the GameManager class will be passed anobject of type List<String>. You can manipulate this objectthe same way you would manipulate an ArrayList<String>. Soyou can call methods like size() and get() and you can write afor-each loop using the List object, but you are not allowed tomodify the list instance you are handed, or remember it beyond theconstructor call. You will need to include the following line atthe beginning of your class to have access to List:
import java.util.*;
Your class should have the following public methods.
Method
Description
GameManager(List<String> names)
This is your constructor. It should add the names from the listinto the thief ring in the same order in which they appear in thelist. For example, if the list contains {“Sam”, “Zach”, “Sarah”},in that order, then in the initial thief ring we should see thatSam is targeting Zach who is targeting Sarah who is targeting Sam(reported in that order). You may assume that the names arenon-empty strings and that there are no duplicate names (ignoringcase). Your method should throw an IllegalArgumentException if thelist is null or empty.
void printThiefRing()
This method should print the names of the people in the thiefring, one per line, with each line indented four spaces(not a tab!), with output of the form “<name> willsteal from <name>”, where each <name> is replaced by aplayer’s name with no <>’s. If the game is over, it shouldinstead print (with the indentation still): “<name> is thePrince of Thieves!”. (Note, RobinHood never calls printThiefRingonce the game is won, so you have to test for this behavioryourself.)
void printStolenList()
This method should print the names of the people in the stolenlist, one per line, indented four spaces, with output of the form“<name> was stolen from by <name>”. It should print thenames in reverse order (most recently stolen from first, then nextmore recently stolen from, and so on). It should produce no outputif the stolen list is empty (no-one has been stolen from yet).
boolean thiefRingContains(String name)
This should return true if the given name is in the currentthief ring and should return false otherwise. It should ignore casein comparing names.
boolean stolenListContains(String name)
This should return true if the given name is in the currentstolen list and should return false otherwise. It should ignorecase in comparing names.
boolean isGameOver()
This should return true if the game is over (i.e., if the thiefring has just one person in it) and should return falseotherwise.
String winner()
This should return the name of the winner of the game. It shouldreturn null if the game is not over.
void steal(String name)
This method records the stealing from the person with the givenname, transferring that person from the thief ring to the stolenfrom list. This operation should not change the thief ring order ofprintThiefRing (i.e., whoever used to be printed first should stillbe printed first unless that’s the person who was stolen from, inwhich case the person who used to be printed second should now beprinted first). It should throw an IllegalArgumentException if thegiven name is not part of the current thief ring and it shouldthrow an IllegalStateException if the game is over (it doesn’tmatter which it throws if both are true). It should ignore case incomparing names.
Note: Exceptions should be thrown as soon as possible in thismethod.
public class GameManager {
// YOUR CODE GOES ABOVE THIS LINE ONLY
//////// DO NOT MODIFY PlayerNode. ////////
/**
* Each PlayerNode object represents a single node in alinked list for a game
* of Robin Hood.
*/
private static class PlayerNode {
public final String name; // thisperson’s name
public String thief; // name of whostole from this person (null if still playing)
public PlayerNode next; // nextnode in the list (null if none)
/**
* Constructs a new node to storethe given name and no next node.
*/
public PlayerNode(String name){
this(name,null);
}
/**
* Constructs a new node to storethe given name and a reference to the given
* next node.
*/
public PlayerNode(String name,PlayerNode next) {
this.name =name;
this.thief =null;
this.next =next;
}
}
}
import java.io.*;
import java.util.*;
public class RobinHood {
public static void main(String[] args) throwsFileNotFoundException {
// prompt for file name
Scanner console = newScanner(System.in);
System.out.println(“Welcome toSherwood Forest!”);
System.out.println();
System.out.print(“What player filedo you want to use? “);
String fileName =console.nextLine();
// read names into a list, usinga Set to avoid duplicates
Scanner input = new Scanner(newFile(fileName));
Set<String> names = newTreeSet<String>(String.CASE_INSENSITIVE_ORDER);
List<String> names2 = newArrayList<String>();
while (input.hasNextLine()) {
String name =input.nextLine().trim();
if(name.length() > 0 && !names.contains(name)) {
names.add(name);
names2.add(name);
}
}
// Shuffle if desired
if (yesTo(console, “Do you want theplayers shuffled?”)) {
Collections.shuffle(names2);
}
// Make an immutable version anduse it to build a GameManager
List<String> names3 =Collections.unmodifiableList(names2);
GameManager manager = newGameManager(names3);
System.out.println();
// Prompt the user for victimsuntil the game is over
while (!manager.isGameOver())
oneRound(console, manager);
// Report who won
System.out.println(manager.winner()+” is Robin Hood!”);
System.out.println(“Final stolenlist is as follows:”);
manager.printStolenList();
}
// Handles the details of recording one victim.Shows the current kill
// ring and graveyard to the user, prompts for a nameand records the
// kill if the name is legal.
public static void oneRound(Scanner console,GameManager manager) {
System.out.println(“Current thiefring:”);
manager.printThiefRing();
System.out.println(“Current stolenlist:”);
manager.printStolenList();
System.out.println();
System.out.print(“Who’s next?”);
String name =console.nextLine().trim();
if(manager.stolenListContains(name)) {
System.out.println(name + ” was already stolen from.”);
} else if(!manager.thiefRingContains(name)) {
System.out.println(“Unknown person.”);
} else {
manager.steal(name);
}
System.out.println();
}
// post: asks the user a question, forcing ananswer of “y” or “n”;
// returns true if the answer was yes, returns falseotherwise
public static boolean yesTo(Scanner console, Stringprompt) {
System.out.print(prompt + ” (y/n)?”);
String response =console.nextLine().trim().toLowerCase();
while (!response.equals(“y”)&& !response.equals(“n”)) {
System.out.println(“Please answer y or n.”);
System.out.print(prompt + ” (y/n)? “);
response =console.nextLine().trim().toLowerCase();
}
return response.equals(“y”);
}
}
Expert Answer
Answer to JAVA Help! Do NOT use nested loops. What to do In this assignment, you will write a class GameManager that keeps track o… . . .
OR

