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
/
TicTacToeEnumerations.java
136 lines (120 loc) · 3.8 KB
/
TicTacToeEnumerations.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
import java.util.LinkedList;
public class TicTacToeEnumerations
{
/**
* The template game to analyze
*/
TicTacToe template;
/**
* The list of lists of all generated games
*/
LinkedList<LinkedList<TicTacToe>> allGames;
/**
* A constructor where you can specify the dimensions
* of your game as rows x coluns grid, and a sizeToWin
* to analyze.
*
* @param aNumRows the number of lines in the game
* @param aNumColumns the number of columns in the game
* @param aSizeToWin the number of cells that must be aligned to win.
*/
public TicTacToeEnumerations(int aNumRows, int aNumColumns, int aSizeToWin)
{
template = new TicTacToe(aNumRows, aNumColumns, aSizeToWin);
}
public LinkedList<LinkedList<TicTacToe>> generateAllGames()
{
allGames = new LinkedList<LinkedList<TicTacToe>>();
addToGames(template);
LinkedList<TicTacToe> workingGames = new LinkedList<TicTacToe>();
workingGames.add(template);
while (!workingGames.isEmpty())
{
TicTacToe game = workingGames.pop();
for (int nextMove : game.emptyPositions())
{
TicTacToe nextGame = game.cloneNextPlay(nextMove);
if (addToGames(nextGame) && nextGame.gameState == GameState.PLAYING)
{
workingGames.add(nextGame);
}
}
}
return allGames;
}
public String toString()
{
return toString(false);
}
public String toString(boolean showAllGames)
{
if (allGames == null)
{
return "No games generated.";
}
StringBuilder s = new StringBuilder();
int numGames = 0;
int numXWin = 0;
int numOWin = 0;
int numDraw = 0;
for (int i = 0; i < allGames.size(); i++)
{
LinkedList<TicTacToe> games = allGames.get(i);
int numStillPlaying = 0;
StringBuilder sGames = new StringBuilder();
for (TicTacToe g : games)
{
numGames += 1;
switch (g.gameState)
{
case PLAYING:
numStillPlaying += 1;
break;
case XWIN:
numXWin += 1;
break;
case OWIN:
numOWin += 1;
break;
case DRAW:
numDraw += 1;
break;
}
if (showAllGames)
{
sGames.append(g);
sGames.append("\n\n");
}
}
s.append("======= level " + i + " =======: ");
s.append(games.size() + " element(s) (");
s.append(numStillPlaying + " still playing)\n");
s.append(sGames.toString());
}
s.append("that's " + numGames + " games\n");
s.append(numXWin + " won by X\n");
s.append(numOWin + " won by O\n");
s.append(numDraw + " draw");
return s.toString();
}
/**
* Add the game to our all games list
* make sure the position exists, and then add it
*/
private boolean addToGames(TicTacToe game)
{
int index = game.numRounds;
int size = game.numRounds + 1;
while (allGames.size() < size)
{
allGames.add(new LinkedList<TicTacToe>());
}
LinkedList<TicTacToe> l = allGames.get(index);
boolean isNewGame = !l.contains(game);
if (isNewGame)
{
l.add(game);
}
return isNewGame;
}
}