-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssassinMain.java
83 lines (75 loc) · 3.33 KB
/
AssassinMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Class AssassinMain is the driver program for the assassin management task.
// It reads names from a file, shuffles them if the user so desires, and uses
// them to start the game. The user is asked for the name of the next victim
// until the game is over.
import java.io.*;
import java.util.*;
public class AssassinMain {
public static void main(String[] args) throws FileNotFoundException {
// prompt for file name
Scanner console = new Scanner(System.in);
System.out.println("Welcome to the Assassin Manager");
System.out.println();
System.out.print("What name file do you want to use this time? ");
String fileName = console.nextLine();
// read names into a list, using a Set to avoid duplicates
Scanner input = new Scanner(new File(fileName));
Set<String> names = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
List<String> names2 = new ArrayList<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 the names shuffled?")) {
Collections.shuffle(names2);
}
// make an immutable version and use it to build an AssassinManager
List<String> names3 = Collections.unmodifiableList(names2);
AssassinManager manager = new AssassinManager(names3);
System.out.println();
// prompt the user for victims until the game is over
while (!manager.gameOver()) {
oneKill(console, manager);
}
// report who won
System.out.println("Game was won by " + manager.winner());
System.out.println("Final graveyard is as follows:");
manager.printGraveyard();
}
// Handles the details of recording one victim. Shows the current kill
// ring and graveyard to the user, prompts for a name and records the
// kill if the name is legal.
public static void oneKill(Scanner console, AssassinManager manager) {
System.out.println("Current kill ring:");
manager.printKillRing();
System.out.println("Current graveyard:");
manager.printGraveyard();
System.out.println();
System.out.print("next victim? ");
String name = console.nextLine().trim();
if (manager.graveyardContains(name)) {
System.out.println(name + " is already dead.");
} else if (!manager.killRingContains(name)) {
System.out.println("Unknown person.");
} else {
manager.kill(name);
}
System.out.println();
}
// post: asks the user a question, forcing an answer of "y" or "n";
// returns true if the answer was yes, returns false otherwise
public static boolean yesTo(Scanner console, String prompt) {
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");
}
}