This commit is contained in:
Love 2024-09-04 22:07:39 +02:00
parent faeead0cf0
commit 9e4790a0a0

View File

@ -54,17 +54,17 @@ std::string fileRead(const char *filePath) {
return content; return content;
} }
enum UserAction { ANIMATE, TICK, QUIT }; enum MenuAction { ANIMATE, TICK, QUIT };
UserAction askUserForANIMATE() { MenuAction askMenu() {
while (true) { while (true) {
std::string userInput = input("a)nimate, t)ick, q)uit? "); std::string userInput = input("a)nimate, t)ick, q)uit? ");
if (userInput == "a") if (userInput == "a")
return UserAction::ANIMATE; return MenuAction::ANIMATE;
else if (userInput == "t") else if (userInput == "t")
return UserAction::TICK; return MenuAction::TICK;
else if (userInput == "q") else if (userInput == "q")
return UserAction::QUIT; return MenuAction::QUIT;
std::cout << "The input '" << userInput << "' is an invalid option!\n"; std::cout << "The input '" << userInput << "' is an invalid option!\n";
} }
} }
@ -88,12 +88,12 @@ enum Cell { ALIVE, DEAD };
void gridPrint(Grid<Cell> &grid) { void gridPrint(Grid<Cell> &grid) {
std::stringstream ss; std::stringstream ss;
for (int idxRow = 0; idxRow < grid.numRows(); idxRow++) { for (int row = 0; row < grid.numRows(); row++) {
for (int idxCol = 0; idxCol < grid.numCols(); idxCol++) { for (int col = 0; col < grid.numCols(); col++) {
char sign = grid.get(idxRow, idxCol) == Cell::ALIVE ? 'X' : '-'; char sign = grid.get(row, col) == Cell::ALIVE ? 'X' : '-';
ss << sign; ss << sign;
} }
if (idxRow == grid.numRows() - 1) if (row == grid.numRows() - 1)
ss << std::endl; ss << std::endl;
else else
ss << '\n'; ss << '\n';
@ -176,10 +176,10 @@ int main() {
std::cerr << "No column, row header, in the file!" << std::endl; std::cerr << "No column, row header, in the file!" << std::endl;
return 1; return 1;
} }
int rows, columns; int headerRows, headerColumns;
try { try {
rows = std::stoi(lines[0]); headerRows = std::stoi(lines[0]);
columns = std::stoi(lines[1]); headerColumns = std::stoi(lines[1]);
} catch (const std::invalid_argument &e) { } catch (const std::invalid_argument &e) {
std::cerr << "Column or row wasn't a number" << std::endl; std::cerr << "Column or row wasn't a number" << std::endl;
return 1; return 1;
@ -189,24 +189,24 @@ int main() {
} }
int possibleRows = lines.size() - 2; int possibleRows = lines.size() - 2;
if (possibleRows < rows) { if (possibleRows < headerRows) {
std::cerr << "There's less rows (" << possibleRows std::cerr << "There's less rows (" << possibleRows
<< ") than described in the header! (" << rows << ')' << ") than described in the header! (" << headerRows << ')'
<< std::endl; << std::endl;
return 1; return 1;
} }
Grid<Cell> grid(columns, rows); Grid<Cell> grid(headerColumns, headerRows);
for (int x = 0; x < rows; x++) { for (int x = 0; x < headerRows; x++) {
std::string &row = lines[x + 2]; std::string &row = lines[x + 2];
if (columns > row.size()) { if (headerColumns > row.size()) {
std::cerr << "There's less columns (" << row.size() std::cerr << "There's less columns (" << row.size()
<< "), than described in the header! (" << rows << ')' << "), than described in the header! (" << headerRows << ')'
<< std::endl; << std::endl;
return 1; return 1;
} }
for (int y = 0; y < columns; y++) { for (int y = 0; y < headerColumns; y++) {
char sign = row[y]; char sign = row[y];
switch (sign) { switch (sign) {
@ -225,15 +225,15 @@ int main() {
} }
gridPrint(grid); gridPrint(grid);
UserAction ANIMATE; MenuAction action;
while ((ANIMATE = askUserForANIMATE()) != UserAction::QUIT) { while ((action = askMenu()) != MenuAction::QUIT) {
// We don't have to handle QUIT, since we do in the while check // We don't have to handle QUIT, since we do in the while check
switch (ANIMATE) { switch (action) {
case UserAction::TICK: case MenuAction::TICK:
grid = gridTick(grid); grid = gridTick(grid);
gridPrint(grid); gridPrint(grid);
break; break;
case UserAction::ANIMATE: case MenuAction::ANIMATE:
for (int i = 0; i < ANIMATION_N_GENERATIONS; i++) { for (int i = 0; i < ANIMATION_N_GENERATIONS; i++) {
grid = gridTick(grid); grid = gridTick(grid);
clearConsole(); clearConsole();