-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cs
210 lines (167 loc) · 4.93 KB
/
main.cs
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
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
public class MainClass
{
private static bool running;
private static bool auto = false;
public static void Main(string[] args)
{
if (running) throw new Exception("Main() is already running.");
running = true;
while (true)
{
int columns, rows, connect, teamCount;
bool noMiddleStart, load, ascii, colorBlind;
string loadString = null;
if (BoolQuery("Quickplay? (y/n)"))
{
columns = 7;
rows = 6;
connect = 4;
teamCount = 2;
noMiddleStart = false;
load = false;
ascii = false;
colorBlind = false;
}
else
{
columns = IntQuery("How many columns should the board have?");
while (columns < 3 || columns > 16) columns = IntQuery("Columns must be between 3 and 16.");
rows = IntQuery("How many rows should the board have?");
while (rows < 3 || rows > 16) rows = IntQuery("Rows must be between 3 and 16.");
connect = IntQuery("How many dots should the player have to connect?");
while (connect < 3 || (connect > columns && connect > rows)) connect = IntQuery("Connect must be greater than 2 and less than or equal to either columns or rows.");
teamCount = IntQuery("How many teams are playing?");
while (teamCount < 2 || teamCount > 8) teamCount = IntQuery("Teams must be between 2 and 8.");
if (columns % 2 != 0)
{
noMiddleStart = BoolQuery("Should the first player be forbidden from starting in the center? (y/n)");
}
else noMiddleStart = false;
colorBlind = BoolQuery("Enable colorblind mode? (y/n)");
ascii = BoolQuery("Enable ASCII mode? (y/n)");
load = BoolQuery("Load Game? (y/n)");
if (load)
{
Console.WriteLine("Enter Load String. (Round History of desired game)");
loadString = Console.ReadLine();
}
}
var @assembly = typeof(AI).Assembly;
var AIs = @assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(AI)));
Dictionary<State, AI> teams = new Dictionary<State, AI>();
Dictionary<State, int> wins = new Dictionary<State, int>();
int draws = 0;
int totalGames = 0;
Console.WriteLine("Available AIs:");
foreach (Type ai in AIs)
{
Console.WriteLine(ai.Name);
}
string input = "";
for (int i = 0; i < teamCount; i++)
{
Console.WriteLine($"Which AI should be Player {i + 1}?");
Type foundAI = null;
while (foundAI == null)
{
input = Console.ReadLine().ToLower();
foreach (Type AI in AIs)
{
if (AI.Name.ToLower() == input)
{
foundAI = AI;
break;
}
}
}
AI createAI;
createAI = Activator.CreateInstance(foundAI) as AI;
createAI.Team = (State)(i + 1);
teams.Add((State)(i + 1), createAI);
wins.Add((State)(i + 1), 0);
}
bool humanPresent = false;
foreach(AI ai in teams.Values)
{
if (ai is Human)
{
humanPresent = true;
break;
}
}
if (!humanPresent)
{
if (BoolQuery("Should the game repeat without player input? (y/n)")) auto = true;
else auto = false;
}
Console.WriteLine("Press enter to begin.");
Console.ReadLine();
State currentTeam = State.Red;
while (true)
{
Board board = new Board(columns, rows, connect, teamCount, noMiddleStart, currentTeam, ascii, colorBlind);
Game game = new Game(board, teams, board.Auth, load, loadString);
if (load) load = false;
game.RunGame();
totalGames++;
if (board.Victor != State.Empty)
{
wins[board.Victor]++;
}
else draws++;
if (!auto)
{
input = null;
while (input != "next" && input != "end" && input != string.Empty)
{
Console.WriteLine("Type \"next\" to continue or \"end\" to stop playing.");
input = Console.ReadLine();
}
if (input == "end") break;
}
else if (Console.KeyAvailable) break;
currentTeam = Next(currentTeam, teamCount);
}
Console.WriteLine();
foreach (AI ai in teams.Values)
{
Console.WriteLine($"Team {ai.Team}, under {ai.Name}, had {wins[ai.Team]} wins, for a win rate of {(wins[ai.Team] / (double)totalGames).ToString("P2")}");
try
{
ai.GameEnd();
}
catch {}
}
Console.WriteLine($"{draws} games ended in a draw.");
}
running = false;
}
private static int IntQuery(string text)
{
string input = "";
int intInput;
Console.WriteLine(text);
while (!Int32.TryParse(input, out intInput)) input = Console.ReadLine();
return intInput;
}
private static bool BoolQuery(string text)
{
Console.WriteLine(text);
string input = "";
while (input != "y" && input != "n") input = Console.ReadLine().Trim().ToLower();
if (input == "y") return true;
else return false;
}
public static State Next(State state, int teamCount)
{
if (state == State.Empty) throw new Exception("Invalid Argument: state cannot be State.Empty!");
int id = (int)state;
id++;
if (id > teamCount) id = 1;
return (State)id;
}
}