This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.java
133 lines (119 loc) · 3.63 KB
/
Player.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
public abstract class Player
{
private static final int DEFAULT_WINDOW_SIZE = 50;
public String name;
public boolean isDebug;
protected CellValue myMove;
private int numberOfWins;
private int numberOfLosses;
private int numberOfDraws;
private int numberOfGames;
private final int windowSize;
private final GameOutcome[] history;
private int historyIndex;
public Player()
{
this(DEFAULT_WINDOW_SIZE);
}
public Player(int aWindowSize)
{
windowSize = aWindowSize;
name = "This player";
isDebug = false;
numberOfWins = 0;
numberOfLosses = 0;
numberOfDraws = 0;
numberOfGames = 0;
myMove = CellValue.EMPTY;
history = new GameOutcome[windowSize];
historyIndex = -1;
}
abstract boolean play(TicTacToe game);
/**
* Set my letter to X if I am playing first,
* or O if I am not playing first (i.e. playing second)
*
* @param isFirst Am I the first to play?
*/
public void startGame(boolean isFirst)
{
myMove = isFirst ? CellValue.X : CellValue.O;
}
/**
* The game is over. Record the WIN, LOSE or DRAW.
* <p>
* Compare my move (X or O) to the state of the game (XWIN, YWIN, DRAW, other)
* and return the GameOutcome (did I WIN, did I LOSE, did I DRAW, or UNKNOWN)
*
* @param game The outcome of the game (XWIN, YWIN, DRAW, etc)
*
* @return a GameOutcome showing if I WIN, LOSE or DRAW (or UNKNOWN for we cannot tell)
*/
public GameOutcome endGame(GameState game)
{
GameOutcome outcome = calculateOutcome(game);
numberOfGames += 1;
switch (outcome)
{
case WIN:
numberOfWins += 1;
break;
case LOSE:
numberOfLosses += 1;
break;
case DRAW:
numberOfDraws += 1;
break;
}
historyIndex = (historyIndex + 1) % windowSize;
history[historyIndex] = outcome;
return outcome;
}
private GameOutcome calculateOutcome(GameState game)
{
switch (game)
{
case XWIN:
return myMove == CellValue.X ? GameOutcome.WIN : GameOutcome.LOSE;
case OWIN:
return myMove == CellValue.O ? GameOutcome.WIN : GameOutcome.LOSE;
case DRAW:
return GameOutcome.DRAW;
default:
return GameOutcome.UNKNOWN;
}
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(name + " has won " + numberOfWins + " games, ");
sb.append("lost " + numberOfLosses + " games, ");
sb.append("and " + numberOfDraws + " were draws.");
if (numberOfGames > windowSize)
{
int w = 0;
int l = 0;
int d = 0;
for (GameOutcome h : history)
{
switch (h)
{
case WIN:
w++;
break;
case LOSE:
l++;
break;
case DRAW:
d++;
break;
}
}
sb.append(" Over the last " + windowSize + " games, ");
sb.append("this player has won " + w + ", ");
sb.append("lost " + l + ", ");
sb.append("and tied " + d + ".");
}
return sb.toString();
}
}