Skip to content

Commit

Permalink
added singleton timer class and more error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ediardi committed Jun 10, 2024
1 parent 4b0366e commit 3f83ca7
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 39 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ add_executable(${PROJECT_NAME}
TriangleWithCenter.h
PlayerTriangle.cpp
PlayerTriangle.h
DegenerateTriangleError.cpp
DegenerateTriangleError.h
GeometricError.cpp
GeometricError.h
Stopwatch.h
Stopwatch.cpp
)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/levels.txt
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
Expand Down
9 changes: 0 additions & 9 deletions DegenerateTriangleError.cpp

This file was deleted.

20 changes: 0 additions & 20 deletions DegenerateTriangleError.h

This file was deleted.

8 changes: 7 additions & 1 deletion GameStateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

#include <iostream>
#include "Stopwatch.h"
#include "GameStateManager.h"
#include "Drawables.h"
#include "Point.h"
Expand All @@ -16,6 +17,7 @@ GameStateManager::GameStateManager():fin("levels.txt") {
}

void GameStateManager::handle_click(float x, float y) {
Stopwatch::discard_double_action();
switch (state) {
case awaiting_point:
{
Expand Down Expand Up @@ -51,6 +53,10 @@ void GameStateManager::handle_click(float x, float y) {
break;
}
case showing_result:
{
throw CannotProcessClick("The game is currently processing results, click ignored");
break;
}
case game_end:
{
break;
Expand Down Expand Up @@ -127,7 +133,7 @@ void GameStateManager::evaluate() {
std::cout << candidate_triangle;
std::cout<<"The triangle has an area equal to "<<candidate_triangle.get_area()<<std::endl<<std::endl;
}
catch (DegenerateTriangleError& err){
catch (GeometricError& err){
std::cout << "The triangle you entered is degenerate and has no area"<< std::endl;
std::cout << err.what() << std::endl;
}
Expand Down
15 changes: 15 additions & 0 deletions GeometricError.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Created by eciuc on 6/3/2024.
//

#include "GeometricError.h"
#include <string>

DegenerateTriangle::DegenerateTriangle(const std::string &message) :
GeometricError(std::string ("Triunghi degenerat:") + message) {}

DoubleClick::DoubleClick(const std::string &message) :
GeometricError(message) {}

CannotProcessClick::CannotProcessClick(const std::string &message) :
GeometricError(message) {}
35 changes: 35 additions & 0 deletions GeometricError.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Created by eciuc on 6/3/2024.
//

#ifndef OOP_GEOMETRICERROR_H
#define OOP_GEOMETRICERROR_H


#include <stdexcept>

class GeometricError : public std::runtime_error {
using std::runtime_error::runtime_error;
};

class DegenerateTriangle : public GeometricError{

This comment has been minimized.

Copy link
@mcmarius

mcmarius Jun 14, 2024

Contributor

E mai ok să aibă sufixul Error, sare mai ușor în ochi că e vorba de clasă de excepții; acum poți să lași așa, dar ca idee

public:
explicit DegenerateTriangle(const std::string &message) ;
};

class DoubleClick : public GeometricError{
public:
explicit DoubleClick(const std::string &message);
};

class CannotProcessClick : public GeometricError{
public:
explicit CannotProcessClick(const std::string &message);
};






#endif //OOP_GEOMETRICERROR_H
6 changes: 3 additions & 3 deletions PlayerTriangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ PlayerTriangle::PlayerTriangle() = default;
float PlayerTriangle::get_area() const
{
if(a==b)
throw DegenerateTriangleError("matching points a and b");
throw DegenerateTriangle("matching points a and b");
if(b==c)
throw DegenerateTriangleError("matching points b and c");
throw DegenerateTriangle("matching points b and c");
if(a==c)
throw DegenerateTriangleError("matching points a and c");
throw DegenerateTriangle("matching points a and c");
return fabsf(get_signed_area());
}
4 changes: 2 additions & 2 deletions PlayerTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#define OOP_PLAYERTRIANGLE_H

#include "Triangle.h"
#include "DegenerateTriangleError.h"
#include "GeometricError.h"

class PlayerTriangle : public Triangle {
public:
[[nodiscard]] bool is_inside_circle(Point origin,float other_radius) const;
PlayerTriangle();

float get_area() const;
[[nodiscard]] float get_area() const override;
};


Expand Down
13 changes: 13 additions & 0 deletions Stopwatch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Created by eciuc on 6/10/2024.
//

#include "Stopwatch.h"

void Stopwatch::start_timing() {
start_moment = high_resolution_clock::now();
}

high_resolution_clock::duration Stopwatch::elapsed_time() {
return high_resolution_clock::now()-start_moment;
}
43 changes: 43 additions & 0 deletions Stopwatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Created by eciuc on 6/10/2024.
//

#ifndef OOP_STOPWATCH_H
#define OOP_STOPWATCH_H

#include <chrono>
#include "GeometricError.h"

using namespace std::chrono;
class Stopwatch {
private:
static inline high_resolution_clock::time_point start_moment;
static inline bool isrunning=false;
Stopwatch() = default;
static void start_timing();
static high_resolution_clock::duration elapsed_time();
public:
Stopwatch(const Stopwatch&) = delete;
Stopwatch& operator=(const Stopwatch&) = delete;
static Stopwatch& get_app() {
static Stopwatch stopwatch;
return stopwatch;
}
static void discard_double_action(){
if(isrunning)
{
auto interval=elapsed_time();
if(interval < 0.2s)
{
start_timing();
double dur= static_cast<double>(interval.count())/1e9;
throw DoubleClick("Action detected " + std::to_string (dur) + "s after previous action, discarding input");
}
}
start_timing();
isrunning = true;
}
};


#endif //OOP_STOPWATCH_H
3 changes: 2 additions & 1 deletion Triangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class Triangle{
void set_b(const Point& point_b);
void set_c(const Point& point_c);
[[nodiscard]] bool does_not_intersect_triangle(const Triangle& other) const;
[[nodiscard]] float get_area() const;

[[nodiscard]] virtual float get_area() const;
friend std::ostream& operator<<(std::ostream& os, const Triangle& triangle);

[[nodiscard]] virtual float best_area() const {
Expand Down
10 changes: 9 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ int main() {
sf::Vector2 mouse_position = sf::Mouse::getPosition(window);
sf::Vector2f coordinates(static_cast<float>(mouse_position.x), static_cast<float>(mouse_position.y));
std::cout << "Got click " << mouse_position.x << ' ' << mouse_position.y << "\n";
game.handle_click(coordinates.x,coordinates.y);
try {
game.handle_click(coordinates.x, coordinates.y);
}
catch (CannotProcessClick& err){
std::cout << err.what() << std::endl;
}
catch (DoubleClick& err){
std::cout << err.what() << std::endl;
}
break;
}
default:
Expand Down

0 comments on commit 3f83ca7

Please sign in to comment.