-
Notifications
You must be signed in to change notification settings - Fork 0
/
WarLogger.java
114 lines (99 loc) · 3.28 KB
/
WarLogger.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
//DO NOT EDIT THIS CODE FOR ANY REASON
import java.io.*;
import java.util.*;
/**
The WarLogger class logs results of battles, wars, and games to a file.
*/
public class WarLogger{
private static WarLogger m_instance;
private PrintWriter m_pw;
/** A string representation for Player 1 for use in log methods*/
public static final String P1 = "Player 1";
/** A string representation for Player 2 for use in log methods*/
public static final String P2 = "Player 2";
/** A string representation for War for use in log methods*/
public static final String WAR = "WAR!!!";
private WarLogger(){
try{
m_pw = new PrintWriter(new FileWriter("WarLogger.txt"));
} catch(Exception e){
System.err.println("WarLogger is unable to create log file!");
m_pw = null;
}
}
/**This method returns the instance of the WarLogger to be used for logging
*@return the instance of the WarLogger to call methods on
*/
public static synchronized WarLogger getInstance(){
if(m_instance == null){
m_instance = new WarLogger();
}
return m_instance;
}
/** Logs the details of a single battle
*@param battleNum The number of the battle
*@param player The player (WarLogger.P1 or WarLogger.P2)
*@param hand an array of cards played by the player for the battle
*/
public void logBattle(int battleNum, String player, Card[] hand){
try{
m_pw.println("Battle Number: " + battleNum);
m_pw.println("Player Number: " + player);
m_pw.print("Player Hand: ");
if(hand == null || hand.length < 1){
m_pw.println("Null or Empty Hand");
} else{
for(Card c: hand){
m_pw.print(c);
m_pw.print(" ");
}
m_pw.print("\n");
}
}catch(Exception e){
System.err.println("WarLogger error while printing");
}
}
/** Logs the outcome of a single battle
*@param battleNum The number of the battle
*@param result The player who won (WarLogger.P1 or WarLogger.P2 or WarLogger.WAR if tie)
*/
public void logBattleOutcome(int battleNum, String result){
try{
m_pw.println("The outcome of battle " + battleNum + " is " + result);
} catch(Exception e){
System.err.println("WarLogger error while printing");
}
}
/** Logs the outcome of a single war
*@param warNum The number of the war
*@param result The player who won (WarLogger.P1 or WarLogger.P2 or WarLogger.WAR if tie)
*/
public void logWarOutcome(int warNum, String result){
try{
m_pw.println("The outcome of war " + warNum + " is " + result);
} catch(Exception e){
System.err.println("WarLogger error while printing");
}
}
/** Logs the outcome of a single game
*@param gameNum The number of the game
*@param playerNum The player who won (WarLogger.P1 or WarLogger.P2)
*/
public void logGameOutcome(int gameNum, String playerNum){
try{
m_pw.println("The winner of game " + gameNum + " is " + playerNum);
} catch(Exception e){
System.err.println("WarLogger error while printing");
}
}
/**Closes the logger and cleans up. Should be the last thing called in main
*method of the simulation class.
*/
public void release(){
try{
m_pw.close();
} catch(Throwable t){
System.err.println("WarLogger unable to close correctly");
}
}
}