Input and file read
This commit is contained in:
		
							
								
								
									
										43
									
								
								src/life.cpp
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								src/life.cpp
									
									
									
									
									
								
							@@ -2,16 +2,51 @@
 | 
				
			|||||||
// Also remove these comments here and add your own.
 | 
					// Also remove these comments here and add your own.
 | 
				
			||||||
// TODO: remove this comment header
 | 
					// TODO: remove this comment header
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <iostream>
 | 
					 | 
				
			||||||
#include "grid.h"
 | 
					#include "grid.h"
 | 
				
			||||||
#include "lifeutil.h"
 | 
					#include "lifeutil.h"
 | 
				
			||||||
 | 
					#include <fstream>
 | 
				
			||||||
 | 
					#include <iostream>
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void printWelcome() {
 | 
				
			||||||
 | 
					    std::cout << "Welcome to the TDDD86 Game of Life,\n"
 | 
				
			||||||
 | 
					              << "a simulation of the lifecycle of a bacteria colony.\n"
 | 
				
			||||||
 | 
					              << "Cells (X) live and die by the following rules:\n"
 | 
				
			||||||
 | 
					              << "  - A cell with 1 or fewer neighbours dies.\n"
 | 
				
			||||||
 | 
					              << "  - Locations with 2 neighbours remain stable.\n"
 | 
				
			||||||
 | 
					              << "  - Locations with 3 neighbours will create life.\n"
 | 
				
			||||||
 | 
					              << "  - A cell with 4 or more neighbours dies." << std::endl;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::string input(const char *message) {
 | 
				
			||||||
 | 
					    if (message != nullptr) {
 | 
				
			||||||
 | 
					        std::cout << message;
 | 
				
			||||||
 | 
					        std::flush(std::cout);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    std::string ret;
 | 
				
			||||||
 | 
					    std::getline(std::cin, ret);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return ret;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::string readFile(const char *filePath) {
 | 
				
			||||||
 | 
					    std::ifstream file(filePath, std::ios::binary);
 | 
				
			||||||
 | 
					    if (!file.is_open())
 | 
				
			||||||
 | 
					        throw std::runtime_error("File couldn't be found");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    std::string content;
 | 
				
			||||||
 | 
					    const int BUF_LENGTH = 1024;
 | 
				
			||||||
 | 
					    char buf[BUF_LENGTH];
 | 
				
			||||||
 | 
					    while (file.read(buf, BUF_LENGTH))
 | 
				
			||||||
 | 
					        content.append(buf, file.gcount());
 | 
				
			||||||
 | 
					    content.append(buf, file.gcount());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    file.close();
 | 
				
			||||||
 | 
					    return content;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main() {
 | 
					int main() {
 | 
				
			||||||
 | 
					    printWelcome();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO: Finish the program!
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    std::cout << "Have a nice Life! "  << std::endl;
 | 
					 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user