-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
186 lines (147 loc) · 4.7 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <chrono>
#include <sstream>
#include "Board.h"
#include "Game.h"
#include "Player.h"
#include "HumanPlayer.h"
#include "BasicAI.h"
#include "MyAI.h"
using std::cout;
using std::cin;
using std::string;
void Menu();
void Menu_GenerateSeedset();
void GenerateSeedset(std::string _path, int _seedCount = 1000, int _width = 20, int _height = 20, int _colorCount = 6, int _maxMoves = 100);
void Menu_RunAI();
void RunTests(string _seedsetFile, string _resultsFile);
int main()
{
Menu();
return 0;
}
void Menu()
{
cout << "========= Flood-It =========\n"
<< "Base code by Mitchell Merry, Edits by Broderick Westrope\n"
<< "Choose an option below:\n"
<< "[1] Run AI test using a seed set\n"
<< "[2] Generate a new seed set\n";
char choice;
cin >> choice;
while (choice < '1' || choice > '2')
{
cout << "Please enter valid input.\n";
cin >> choice;
}
if (choice == '1')
{ Menu_RunAI(); }
else if (choice == '2')
{ Menu_GenerateSeedset(); }
else
{ cout << "ERROR: Invalid input passed through.\n"; }
}
void Menu_RunAI()
{
string seedset, results;
cout << "Enter the filepath of the seedset to run (e.g. seedsets/test_set.txt):\n";
cin >> seedset;
cin.ignore();
cout << "Enter the filepath for the results to results to (e.g. results/test_set_r.txt):\n";
cin >> results;
cin.ignore();
RunTests(seedset, results);
}
void RunTests(string _seedsetFile, string _resultsFile)
{
std::ifstream seedsetInput(_seedsetFile);
string line;
getline(seedsetInput, line);
std::istringstream iss(line);
int seedCount, width, height, colourCount, maxMoves;
getline(iss, line, ',');
seedCount = stoi(line);
getline(iss, line, ',');
width = stoi(line);
getline(iss, line, ',');
height = stoi(line);
getline(iss, line, ',');
colourCount = stoi(line);
getline(iss, line, ',');
maxMoves = stoi(line);
int sum = 0;
int errorCount = 0;
string str = "";
for (int i = 0; i < seedCount; i++)
{
getline(seedsetInput, line);
if (line.empty())
{
cout << "ERROR: The test found an empty line in the given seedset.\n";
return;
}
int s = stoi(line);
Player *p = new MyAI(); // intended for people to write their AIs to MyAI, BasicAI is just an example one
Game game(p, width, height, colourCount, maxMoves, s);
int moves = game.play();
if (moves == -1)
{
errorCount++;
cout << "GAME " + std::to_string(i + 1) + ": **TOO MANY MOVES**\n";
// Results parser can detect this and generate appropriate message, but to avoid conflicts
// with loading code will just put -1 for DNF and print to console the error
str += "-1\n";
}
else
{
sum += moves;
// str += "GAME " + std::to_string(i + 1) + ": " + std::to_string(moves) + "\n";
str += std::to_string(moves) + "\n"; // for simplicity
}
}
std::ofstream out(_resultsFile);
// out << "TOTAL MOVES: " << std::to_string(sum) << "\n";
// out << "AVG. MOVES: " << std::to_string(sum / seedCount) << "\n";
out << str;
out.close();
seedsetInput.close();
}
void Menu_GenerateSeedset()
{
string filepath;
int seeds, width, height, colourCount, totalMoves;
cout << "(use '1000 20 20 6 300 seedsets/[filename].txt' for quick setup)\n";
cout << "Enter a number of seeds to generate:\n";
cin >> seeds;
cin.ignore();
cout << "Enter the width of the boards:\n";
cin >> width;
cin.ignore();
cout << "Enter the height of the boards:\n";
cin >> height;
cin.ignore();
cout << "Enter the number of colours on the boards:\n";
cin >> colourCount;
cin.ignore();
cout << "Enter the maximum number of moves per board allowed:\n";
cin >> totalMoves;
cin.ignore();
cout << "Enter the filepath for the generated seed set (e.g. seedsets/[filename].txt):\n";
cin >> filepath;
cin.ignore();
GenerateSeedset(filepath, seeds, width, height, colourCount, totalMoves);
}
void GenerateSeedset(std::string _path, int _seedCount, int _width, int _height, int _colorCount, int _maxMoves)
{
std::ofstream seedsetOutput(_path);
// Write the settings of the set to the top of the file
seedsetOutput << _seedCount << "," << _width << "," << _height << "," << _colorCount << "," << _maxMoves;
// Write each generated seed to the file
srand(time(NULL));
for (int i = 0; i < _seedCount; i++)
{
seedsetOutput << "\n" << rand() * rand();
}
seedsetOutput.close();
}