skapat load_to_csv - Alma, Lukas, Malin
authormalak585 <malak585@tlvm-4-2-2.ad.liu.se>
Thu, 30 Nov 2023 13:05:10 +0000 (14:05 +0100)
committermalak585 <malak585@tlvm-4-2-2.ad.liu.se>
Thu, 30 Nov 2023 13:05:10 +0000 (14:05 +0100)
assets/highscore.csv
src/Menus.cc
src/States.cc
src/States.h

index ef57aeb62832754ceea05fd147bfa26e2ccdf2e6..2d4adabdf9a30c852dfa39bfd4ac6bf685ec695e 100644 (file)
@@ -1,5 +1,5 @@
-Gorilla,6
-Gorilla,4
-Gorilla,4
-Gorilla,4
-Gorilla,4
+gorilla, 5
+gorilla, 4
+gorilla, 3
+gorilla, 2
+gorilla, 1
index 41a1acc2aa3043ae7da88339d04baaec62a10994..1c0738b691b0edf5c2661732aeb429fef4d8d47d 100644 (file)
@@ -3,6 +3,7 @@
 #include <string>
 #include <fstream>
 #include <vector>
+#include <cmath>
 
 #include "States.h"
 #include "Context.h"
@@ -594,38 +595,6 @@ void GameOver_menu::update(Context& context)
     // mouse placement
     mouse_r.setPosition(((S_WIDTH / 2) + 60), ((S_HEIGHT / 2) - 45 + 50*(menu_index - 1)));
     mouse_l.setPosition(((S_WIDTH / 2) - 58), ((S_HEIGHT / 2) + 2 + 50*(menu_index - 1))); 
-
-    //spara namn till fil
-    std::string current_line{"Gorilla," + std::to_string(points)};
-
-    std::ifstream highscore_file_r{"assets/highscore.csv"};
-    std::vector<std::string> lines_read;
-    std::vector<std::string> lines_write;
-    for (std::string one_line; std::getline(highscore_file_r, one_line);lines_read.push_back(one_line));
-
-    bool added {false};
-    for (std::string::size_type line_count{0}; line_count < lines_read.size(); line_count++)
-    {   
-        if (!added && points > std::stoi(lines_read[line_count].substr(lines_read[line_count].find_first_of(',') + 1)))
-        {
-            std::cout << "i loop" << std::endl;
-            lines_write.push_back(current_line);
-            added = true;
-        }
-        lines_write.push_back(lines_read[line_count]);
-    }
-
-    if (!added) lines_write.push_back(current_line);
-    
-        
-
-    if (lines_write.size() > data["game_constants"]["scoreboard"]) lines_write.pop_back();
-
-    highscore_file_r.close();
-
-    std::ofstream highscore_file_w{"assets/highscore.csv"};
-    for (std::string::size_type line_count{0}; line_count < lines_write.size(); highscore_file_w << lines_write[line_count++] << std::endl);
-    highscore_file_w.close();
 }
 
 
@@ -660,6 +629,7 @@ void GameOver_menu::handle_input(sf::Event& event)
 
             if(menu_index == 1)
             {
+                load_to_csv();
                 menu = true;
                 menu_index = 1;
             }
@@ -711,4 +681,41 @@ void GameOver_menu::handle_input(sf::Event& event)
         playerInput += event.text.unicode;
         playerText.setString(playerInput);
     }
+
+
 }
+
+//spara namn till fil
+void GameOver_menu::load_to_csv() const
+{
+    std::string current_line{"Gorilla," + std::to_string(points)};
+
+    std::ifstream highscore_file_r{"assets/highscore.csv"};
+    std::vector<std::string> lines_read;
+    std::vector<std::string> lines_write;
+    for (std::string one_line; std::getline(highscore_file_r, one_line);lines_read.push_back(one_line));
+
+    bool added {false};
+    for (std::string::size_type line_count{0}; line_count < lines_read.size(); line_count++)
+    {   
+        if (!added && points > std::stoi(lines_read[line_count].substr(lines_read[line_count].find_first_of(',') + 1)))
+        {
+            std::cout << "i loop" << std::endl;
+            lines_write.push_back(current_line);
+            added = true;
+        }
+        lines_write.push_back(lines_read[line_count]);
+    }
+
+    if (!added) lines_write.push_back(current_line);
+    
+    
+
+    if (lines_write.size() > data["game_constants"]["scoreboard"]) lines_write.pop_back();
+
+    highscore_file_r.close();
+
+    std::ofstream highscore_file_w{"assets/highscore.csv"};
+    for (std::string::size_type line_count{0}; line_count < lines_write.size(); highscore_file_w << lines_write[line_count++] << std::endl);
+    highscore_file_w.close();
+}
\ No newline at end of file
index 77a0b50c93a2d0f569b8d5b9db71c453e52fca21..10c375d305b4d595758e3ba41ac70f71c4eacdf5 100644 (file)
@@ -59,13 +59,12 @@ void Game_state::update(Context &context)
         pause_game = false;
         return;
     }
+       
         
     game_time += context.time.asSeconds();
     int remaining_time {static_cast<int>(data["game_constants"]["game_time"]) - static_cast<int>(game_time)};
     if (remaining_time < 0)
     {
-        // LÄGG ALLT NEDAN I GAME OVER MENYN.
-
         context.next_state = std::make_unique<GameOver_menu>(points, data["game_constants"]);
         return;
     }
index d0cdefc67d9550da48642df3e091c290cba84698..d4e622d1adb432e744e28ee36f2a2edec78302d9 100644 (file)
@@ -154,6 +154,8 @@ public:
     void update(Context& context) override;
     void render(sf::RenderWindow& window) const override;
     void handle_input(sf::Event& event) override;
+    void load_to_csv() const;
+
 private:
     sf::Texture texture;
     sf::Texture texture2;