Commit 15a50b62 authored by Srb František's avatar Srb František
Browse files

Added Highscore system

Fix #3 by adding new HighscoreModule class that interacts with file highscore.txt and can store up to 10 top players by level completion (or time if they reached same level)
parent f87ab806
......@@ -155,12 +155,14 @@
<ClCompile Include="Characters.cpp" />
<ClCompile Include="Display.cpp" />
<ClCompile Include="glad.c" />
<ClCompile Include="HighscoreModule.cpp" />
<ClCompile Include="Line.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Scene.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Display.h" />
<ClInclude Include="HighscoreModule.h" />
<ClInclude Include="Line.h" />
<ClInclude Include="Characters.h" />
<ClInclude Include="Scene.h" />
......
......@@ -33,6 +33,9 @@
<ClCompile Include="Characters.cpp">
<Filter>Zdrojové soubory</Filter>
</ClCompile>
<ClCompile Include="HighscoreModule.cpp">
<Filter>Zdrojové soubory</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Line.h">
......@@ -47,6 +50,9 @@
<ClInclude Include="Characters.h">
<Filter>Hlavičkové soubory</Filter>
</ClInclude>
<ClInclude Include="HighscoreModule.h">
<Filter>Hlavičkové soubory</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="map0.txt">
......
#include "HighscoreModule.h"
#include <filesystem>
#include <iostream>
#include <fstream>
std::tuple<long,int,std::string> parse_entry(std::string&& str)
{
size_t pos = 0;
std::vector<std::string> tokens;
while ((pos = str.find(' ')) != std::string::npos) {
tokens.push_back(str.substr(0, pos));
str.erase(0, pos + 1);
}
if (str != "") tokens.push_back(str);
return std::make_tuple(std::stol(tokens[0]), std::stoi(tokens[1]),std::move(tokens[2]));
}
HighscoreModule::HighscoreModule(std::string path_to_highscore_file)
: path(path_to_highscore_file), entries_count(0)
{
if (std::filesystem::exists(path)) {
std::ifstream file(path);
std::string line;
while (!file.eof()) {
std::getline(file, line);
if (line == "") break;
auto&& [time, level_count, name] = parse_entry(std::move(line));
entries[entries_count++] = entry_st(time,level_count,name);
}
file.close();
}
else {
std::ofstream file(path);
file.close();
}
}
void HighscoreModule::Show()
{
const std::string divider = "+-------+--------+----------------+\n";
std::cout << "| HIGHSCORE TABLE |\n";
std::cout << "+-------+--------+----------------+\n";
std::cout << "+ LEVEL + TIME + NAME +\n";
std::cout << "+-------+--------+----------------+\n";
for (int i = 0; i < entries_count; i++)
{
std::cout << '|' << std::setw(7) << entries[i].level_count <<
'|' << std::setw(8) << entries[i].time <<
'|' << std::setw(16) << entries[i].name <<
'|' << std::endl;
std::cout << divider;
}
}
void HighscoreModule::Store(long time, int level_count, std::string name)
{
int i = 0; // upcoming new index
for (i = 0; i < entries_count; i++)
{
int entry_index = entries_count - i - 1;
if (level_count < entries[entry_index].level_count)
break;
else if (level_count == entries[entry_index].level_count) {
if (time > entries[entry_index].time)
break;
}
//push entry one spot down
if (entry_index != 9)
entries[entry_index + 1] = entries[entry_index];
}
// i=0 && entries==10 -> no spot on ladder for this entry
if (i != 0 || entries_count != 10)
entries[entries_count - i] = entry_st(time, level_count, name);
if (entries_count != 10)
entries_count++;
}
HighscoreModule::~HighscoreModule()
{
//fill in the file
std::ofstream file(path);
for (int i = 0; i < entries_count; i++)
{
file << entries[i].time << ' ' << entries[i].level_count << ' ' << entries[i].name;
if (i != entries_count - 1)
file << std::endl;
}
file.close();
}
HighscoreModule::entry_st::entry_st() : name(""),level_count(0),time(0)
{
}
HighscoreModule::entry_st::entry_st(long time, int level_count, std::string name)
: name(name), level_count(level_count), time(time)
{
}
#pragma once
#include<string>
class HighscoreModule
{
public:
HighscoreModule(std::string path_to_highscore_file);
void Show();
void Store(long time,int level_count, std::string name);
~HighscoreModule();
private:
std::string path;
int entries_count;
typedef struct entry_st {
std::string name;
long time;
int level_count;
struct entry_st(long time, int level_count, std::string name);
struct entry_st();
};
struct entry_st entries[10];
};
86 3 Any%Speedruner
18 2 BetterBen
21 2 AvarageJohny
2 0 quickEnder123
\ No newline at end of file
#include <glad/glad.h>
#include <glfw3.h>
#include <iostream>
#include <time.h>
#include "Display.h"
#include "Scene.h"
#include <vector>
#include <string>
#include "HighscoreModule.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 800;
const unsigned int map_count = 3;
const char* vertexShaderSource = "#version 330 core\n"
......@@ -121,6 +124,9 @@ int main()
Scene level(Player(PI/2.0f,0.65f,std::make_pair(0.0f,0.0f),0.3f,1.5f,120,tuple_color(1.0f,1.0f,1.0f)));
level.load_scene_from_file("map"+std::to_string(level_counter)+".txt");
bool game_over=false;
time_t start_time= time(NULL); //sets start clock in
long runtime_s = 0;
auto highscore_module = HighscoreModule("highscore.txt");
//main game loop
while (!glfwWindowShouldClose(window))
{
......@@ -130,8 +136,11 @@ int main()
level.player_win = false;
game_over = false;
level_counter++;
if (level_counter == 3)
if (level_counter == map_count) {
level.draw_walls = true;
time_t end_time = time(NULL);
runtime_s = (long)difftime(end_time, start_time);
}
}
// game over screen
else game_over = !level.end_screen(display);
......@@ -159,6 +168,15 @@ int main()
}
glfwTerminate();
//highscore
if(runtime_s==0)
runtime_s = (long)difftime(time(NULL), start_time);
highscore_module.Show();
std::cout << "You finished at level " << level_counter << " in " << runtime_s << " seconds! Enter your name: ";
std::string name;
std::cin >> name;
highscore_module.Store(runtime_s, level_counter, name);
return 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment