diff --git a/game.cpp b/game.cpp index 46b27e8..45dc595 100644 --- a/game.cpp +++ b/game.cpp @@ -27,7 +27,7 @@ Game::Game() racecar.setRotation(180); // Initialize the times and quadrants data structure - timesAndQuadrants.push_back(std::make_pair("Quadrant 1", "00:00:00.000")); + timesAndQuadrants.push_back(std::make_pair(1, "00:00:00.000")); } void Game::run() { @@ -59,10 +59,12 @@ void Game::run() { timerText.setString(timerString); // Detect when the racecar changes quadrants and save the time - std::string currentQuadrant = determineQuadrant(racecar.getX(), racecar.getY()); + int currentQuadrant = determineQuadrant(racecar.getX(), racecar.getY()); if (currentQuadrant != timesAndQuadrants.back().first) { timesAndQuadrants.push_back(std::make_pair(currentQuadrant, timerString)); } + + determineLap(timerString); sf::Event event; while (window.pollEvent(event)) { @@ -163,20 +165,37 @@ bool Game::isTrackCollision(float x, float y) { return false; // No collision with track or out of bounds } -std::string Game::determineQuadrant(float x, float y) { - for (int i = 0; i < timesAndQuadrants.size(); i++) { - if (timesAndQuadrants[i] != requiredOrder[i]) - +void Game::determineLap(std::string timerString) { + lapFinished = true; + int requiredOrder[] = {1,2,1,2,3,4,1}; + if(timesAndQuadrants.size() >= 7) { + for (int i = 0; i < 7; i++) { + if (std::get<0>(timesAndQuadrants[i]) != requiredOrder[i]) + lapFinished = false; + } } - + else { + lapFinished = false; + } + + if (lapFinished) { + lapTimes.push_back(timerString); + timesAndQuadrants.clear(); + timesAndQuadrants.push_back(std::make_pair(1, timerString)); + } +} + +int Game::determineQuadrant(float x, float y) { if (y <= 460 && x <= 1150) - return "Quadrant 1"; + return 1; if (y > 460 && x <= 1150) - return "Quadrant 2"; + return 2; if (y > 460 && x > 1150) - return "Quadrant 3"; + return 3; if (y <= 460 && x > 1150) - return "Quadrant 4"; + return 4; + + return 0; } void Game::displaySavedTimes() { @@ -194,7 +213,7 @@ void Game::displaySavedTimes() { textY += 30; // Adjust the spacing as needed } for (const auto& timeAndQuadrant : timesAndQuadrants) { - std::string timeString = timeAndQuadrant.first + ": " + timeAndQuadrant.second; + std::string timeString = "Quadrant " + std::to_string(timeAndQuadrant.first) + ": " + timeAndQuadrant.second; sf::Text text; text.setFont(font); text.setCharacterSize(18); diff --git a/game.h b/game.h index e67745d..8459893 100644 --- a/game.h +++ b/game.h @@ -11,8 +11,9 @@ public: Game(); void run(); bool isTrackCollision(float x, float y); - std::string determineQuadrant(float x, float y); + int determineQuadrant(float x, float y); void displaySavedTimes(); + void determineLap(std::string timerString); private: int max_x; @@ -26,8 +27,8 @@ private: sf::Clock gameTimeClock; sf::View view; Racecar racecar; - std::vector> timesAndQuadrants; // Store times and quadrants - bool lapStarted; // Indicates if a lap has started + std::vector> timesAndQuadrants; // Store times and quadrants + bool lapFinished; std::vector lapTimes; // Store lap times std::string asciiMap = " /------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ \n" " / \\ \n" diff --git a/terminal_racer b/terminal_racer index c64a95a..df4a6a4 100755 Binary files a/terminal_racer and b/terminal_racer differ