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