-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.java
203 lines (167 loc) · 8.06 KB
/
scheduler.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.io.*;
import java.nio.file.*;
import java.util.Scanner;
// import java.util.*;
public class scheduler {
private static final Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// Scanner input = new Scanner(System.in);
int playerCount = playerCount();
String[] names = createPlayerNames(playerCount);
schedule(names);
// input.close(); // The Scanner is used repeatedly, so it is finally closed at the very end of the program
}
public static int playerCount() {
int playerCount = 0;
boolean isValid = false;
while(!isValid){
System.out.print("Enter number of players: ");
if (input.hasNextInt()){
playerCount = input.nextInt();
isValid = true;
// input.close();
} else {
input.next();
System.out.println("That's not a number.");
}
}
return playerCount;
}
public static String[] createPlayerNames(int playerCount){
// Hashtable<Integer, String> playerIDs = new Hashtable<Integer,String>();
String[] names = new String[playerCount];
boolean isValid;
for(int counter = 1; counter <= names.length; counter++){
isValid = false;
while (!isValid){
System.out.print("Enter Player " + counter + " name:");
if (input.hasNext()){
names[counter - 1] = input.next();
isValid = true;
}
else {
input.next();
};
// }
// catch(IllegalStateException ex) {
//
// }
}
}
for(int counter = 1; counter <= names.length; counter++){
System.out.print("PLAYER " + counter + ": " + names[counter -1] + "\n");
}
return names;
}
public static String[] addByeWeek(String[] names) {
String[] byeWeek = new String[names.length + 1]; // So let's check for that and create a "BYE WEEK" that each player will have on their schedule.
for (int i = 0; i<names.length; i++){
byeWeek[i] = names[i]; // Java can't append to arrays, so this creates a new array and copies the old one over.
}
byeWeek[names.length] = "BYE";
return byeWeek;
}
public static int askWeeks(String[] names){
// If there's four teams, they can play in 3 weeks. If there's 3 teams, they can play in 3 weeks.
// But, since all odd numbers are rounded up by the addByeWeek() method, they can be handled in the same way.
int weeks = names.length -1;
boolean isValid = false;
while(!isValid){
System.out.print("How many rounds of games? " + weeks + " is default, for everyone to play each other once.");
if (input.hasNextInt()){
weeks = input.nextInt();
isValid = true;
} else {
input.next();
System.out.println("That's not a number.");
}
}
return weeks;
}
public static void schedule(String[] names) {
if (names.length % 2 !=0){ // If there's an odd number of players, then not everyone will be able to play each round. A player named "BYE" is created to represent the non-game.
names = addByeWeek(names);
}
int weeks = askWeeks(names); // Independent method to get user input about how many weeks should be played.
int rows = names.length +1; // Make an extra row for the header
int columns = weeks +1;
String[][] masterSchedule= new String[rows][columns]; // Schedule is [PLAYER][WEEK]
masterSchedule[0][0] = "-";
for ( int teamCounter = 0; teamCounter < names.length; teamCounter++){ //This loop cycles through each player
int opponentCounter = 9000; // placeholder integer that cannot be zero
masterSchedule[teamCounter+1][0] = names[teamCounter]; // Add team name to column 1
for ( int weekCounter = 0; weekCounter < weeks; weekCounter++){ //loop through each week
masterSchedule[0][weekCounter + 1] = "Week " + (weekCounter +1);
if (weekCounter == 0){
opponentCounter = teamCounter; //Each team's schedule begins at their own index
}
// To create schedules, players with an odd index increment through the list (player1 plays player 2, then player 3)
if (teamCounter%2 != 0){
opponentCounter++;
if (teamCounter == opponentCounter){ // Cannot play yourself, so skip over that index.
opponentCounter++;
}
// and players with an even index decrement through the list (player 2 will play player 1, then player0)
} else {
opponentCounter--;
if (teamCounter == opponentCounter){
opponentCounter--; // Cannot play yourself; skip that index.
}
}
//when the loop gets to the bottom of the list, it will jump to the top. (ex: play team 4, then team 0, then team 1, etc)
if (opponentCounter < 0){
opponentCounter += names.length;
}
//or when it gets to the top of the list, it should jump to the bottom.
if (opponentCounter >= names.length){
opponentCounter = 0;
}
// with the player, opponent, and week identified; add them together to the array.
masterSchedule[teamCounter+1][weekCounter+1] = names[teamCounter].toUpperCase() + " vs. " + names[opponentCounter].toUpperCase();
}
}
// When all loops are complete, print the completed schedule.
for (int i = 0; i < rows; i++) {
if (masterSchedule[i][0] != "BYE"){ // The bye week was a placeholder and doesn't need it's schedule printed.
for (int j = 0; j < columns; j++) {
System.out.format("%18s", masterSchedule[i][j]+ " ");
}
System.out.print("\n");
}
}
offerCSV(masterSchedule);
}
public static void offerCSV(String[][] masterSchedule) {
boolean isValid = false;
while (!isValid){
System.out.print("Schedule may be hard to read on terminal. Output schedule to spreadsheet? (Y/N):");
if (input.hasNext()){
String outputResponse = input.next();
if (outputResponse.toUpperCase().contains("Y")){
writeCSV(masterSchedule);
}
isValid = true;
}
}
}
public static void writeCSV(String[][] schedule) {
int rows = schedule.length;
int columns = schedule[0].length;
File output = new File("output.csv");
for (int i = 0; i < rows; i++) {
if (schedule[i][0] != "BYE"){
String teamRow = "";
for (int j = 0; j < columns; j++) {
teamRow = teamRow + schedule[i][j]+ ",";
}
teamRow = teamRow + "\n";
try{
Files.writeString(output.toPath(), teamRow, StandardOpenOption.APPEND);
}
catch(IOException ex) {
System.out.println("Error: " + ex.getMessage());
}
}
}
}
}