-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayers.java
More file actions
134 lines (114 loc) · 4.04 KB
/
Copy pathPlayers.java
File metadata and controls
134 lines (114 loc) · 4.04 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Players {
private final Map<String, Player> players;
private String currentPlayer;
public Players() {
players = new HashMap<>();
currentPlayer = "GUEST"; // placeholder value that will be changed at launch
}
public Player getPlayer(String name) {
return players.get(name);
}
public void addPlayer(Player p) {
players.put(p.getUsername(), p);
}
public Player getCurrentPlayer() {
return getPlayer(currentPlayer);
}
public void setCurrentPlayer(String currentPlayer) {
if (!players.containsKey(currentPlayer)) {
addPlayer(new Player(currentPlayer));
}
this.currentPlayer = currentPlayer;
}
public void loadPlayers() {
try {
File file = new File(Game.PLAYER_FILE);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
if (parts.length != 5) {
System.out.println("Invalid data read from player file!");
scanner.close();
return;
}
players.put(parts[0], new Player(parts[0], Integer.parseInt(parts[1]), Integer.parseInt(parts[2]),
Integer.parseInt(parts[3]), Integer.parseInt(parts[4])));
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void savePlayers() {
try {
FileWriter fw = new FileWriter(Game.PLAYER_FILE);
Player p;
for (Map.Entry<String, Player> i : players.entrySet()) {
p = i.getValue();
fw.write(p.getUsername() + "," + p.getTotalGuesses() + "," + p.getTotalCorrectGuesses() + "," + p.getCryptogramsPlayed() + ","
+ p.getCryptogramsCompleted());
fw.write("\n");
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void displayPlayer() {
System.out.println(getCurrentPlayer().toString());
System.out.println("Guess Accuracy: " + getCurrentPlayer().getAccuracy() + "\n");
}
public void getTopScores() throws IOException {
BufferedReader bf = new BufferedReader(new FileReader(Game.PLAYER_FILE));
int countLines = readLines();
String line;
int[] IntTokens = new int[countLines];
System.out.println("Initial Array: ");
while (bf.readLine() != null) {
line = bf.readLine();
String[] tokens = line.split(",");
IntTokens = new int[countLines];
for (int i = 0; i < countLines; i++) {
if (i == 4) {
IntTokens[i] = Integer.parseInt(tokens[i]);
System.out.println("Score: " + IntTokens[i]);
}
}
}
bf.close();
for (int k : IntTokens) {
System.out.println(k);
}
Arrays.sort(IntTokens);
System.out.println("Sorted Array (Ascending Order): ");
for (int k : IntTokens) {
System.out.println(k);
}
int[] FinalScores = new int[10];
for (int i = IntTokens.length - 1; i >= 0 ; i--) {
for (int j = 0; j < 10; j++) {
FinalScores[j] = IntTokens[i];
}
}
System.out.println("Top 10 Scores:");
for (int i = 0; i < FinalScores.length; i++) {
System.out.println((i + 1) + ". " + FinalScores[i]);
}
}
public int readLines(){
Scanner scanner = new Scanner(Game.PLAYER_FILE);
int countLines = 0;
while (scanner.hasNextLine()) {
countLines++;
}
scanner.close();
System.out.println("Num File Lines: " + countLines);
return countLines;
}
}