-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssassinMain.java
92 lines (78 loc) · 3.21 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
84
85
86
87
88
89
90
91
92
import java.io.*;
import java.util.*;
/**
* Class AssassinMain is the main client program for assassin game management.
* It reads names from a file names.txt, shuffles them, and uses them to start
* the game. The user is asked for the name of the next victim until the game is
* over.
*/
public class AssassinMain {
/**
* input file name from which to read data
*/
public static final String INPUT_FILENAME = "names.txt";
/**
* true for different results every run; false for predictable results
*/
public static final boolean RANDOM = false;
/**
* If not random, use this value to guide the sequence of numbers that will
* be generated by the Random object.
*/
public static final int SEED = 42;
public static void main(String[] args) throws FileNotFoundException {
// read names into a Set to eliminate duplicates
File inputFile = new File(INPUT_FILENAME);
if (!inputFile.canRead()) {
System.out.println("Required input file not found; exiting.\n" + inputFile.getAbsolutePath());
System.exit(1);
}
Scanner input = new Scanner(inputFile);
Set<String> names = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
while (input.hasNextLine()) {
String name = input.nextLine().trim().intern();
if (name.length() > 0) {
names.add(name);
}
}
// transfer to an ArrayList, shuffle and build an AssassinManager
ArrayList<String> nameList = new ArrayList<>(names);
Random rand = (RANDOM) ? new Random() : new Random(SEED);
Collections.shuffle(nameList, rand);
AssassinManager manager = new AssassinManager(nameList);
// prompt the user for victims until the game is over
Scanner console = new Scanner(System.in);
while (!manager.isGameOver()) {
oneKill(console, manager);
}
// report who won
System.out.println("Game was won by " + manager.winner());
System.out.println("Final graveyard is as follows:");
System.out.println(manager.graveyard());
}
/**
* 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) {
// print both linked lists
System.out.println("Current kill ring:");
System.out.println(manager.killRing());
System.out.println("Current graveyard:");
System.out.println(manager.graveyard());
// prompt for next victim to kill
System.out.println();
System.out.print("next victim? ");
String name = console.nextLine().trim();
// kill the victim, if possible
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();
}
}