Skip to content

Commit

Permalink
Fixed typos
Browse files Browse the repository at this point in the history
  • Loading branch information
ediardi committed Apr 20, 2024
1 parent b942b5d commit 24008dd
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 155 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ message("Compiler: ${CMAKE_CXX_COMPILER_ID} version ${CMAKE_CXX_COMPILER_VERSION
if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /permissive- /wd4244 /wd4267 /wd4996 /external:anglebrackets /external:W0)
else()
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -pedantic)
target_compile_options(${PROJECT_NAME} PRIVATE -Werror -Wall -Wextra -pedantic)
endif()

###############################################################################
Expand Down
38 changes: 20 additions & 18 deletions Drawables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,47 @@ void Drawables::clear_all() {
points.clear();
triangles.clear();
circles.clear();
trianglepoints=0;
}

void Drawables::draw(sf::RenderTarget &target, sf::RenderStates states) const {
// to do: delete (placed here to avoid unused warning)
states.texture=states.texture;
//
target.draw(points);
for(auto& circle:circles)
{
target.draw(circle);
}
sf::VertexArray linepoints(sf::Lines,0);
sf::VertexArray line_points(sf::Lines, 0);
for(const auto& triangle_struct:triangles)
{
sf::Vertex p;
Point a=triangle_struct.triangle->geta();
Point b=triangle_struct.triangle->getb();
Point c=triangle_struct.triangle->getc();
Point a= triangle_struct.triangle->get_a();
Point b= triangle_struct.triangle->get_b();
Point c= triangle_struct.triangle->get_c();

p=a.tovertex();
p= a.to_vertex();
p.color=triangle_struct.color;
linepoints.append(p);
p=b.tovertex();
line_points.append(p);
p= b.to_vertex();
p.color=triangle_struct.color;
linepoints.append(p);
line_points.append(p);

p=b.tovertex();
p= b.to_vertex();
p.color=triangle_struct.color;
linepoints.append(p);
p=c.tovertex();
line_points.append(p);
p= c.to_vertex();
p.color=triangle_struct.color;
linepoints.append(p);
line_points.append(p);

p=c.tovertex();
p= c.to_vertex();
p.color=triangle_struct.color;
linepoints.append(p);
p=a.tovertex();
line_points.append(p);
p= a.to_vertex();
p.color=triangle_struct.color;
linepoints.append(p);
line_points.append(p);
}
target.draw(linepoints);
target.draw(line_points);
}

ColoredTriangle * Drawables::add_triangle(const Triangle* triangle) {
Expand Down
3 changes: 1 addition & 2 deletions Drawables.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ class Drawables : public sf::Drawable{
static sf::VertexArray points;
static std::vector<ColoredTriangle> triangles;
static std::vector<sf::CircleShape> circles;
static inline int trianglepoints=0;
public:
static void add(sf::Vertex x);
static sf::CircleShape* add_circle(Point origin,float rad);
static void change_circle_color(sf::CircleShape& reference,sf::Color color);
//static void change_circle_color(sf::CircleShape& reference,sf::Color color);
static void clear_all();
static ColoredTriangle * add_triangle(const Triangle* triangle);
private:
Expand Down
88 changes: 57 additions & 31 deletions GameStateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
#include "Drawables.h"
#include "Point.h"

GameStateManager::GameStateManager():fin("nivele.txt") {
GameStateManager::GameStateManager():fin("levels.txt") {
fin>>n;
index=0;
next_level();
std::cout << "This game has " << n << " levels in which you have to make a triangle of largest area possible that is within the circumscribed circle of the given triangle and does not intersect it" << std::endl;
std::cout << "To pass each level you have to have an area of at least 75% of the theoretical maximum area" << std::endl;
}

void GameStateManager::handle_click(float x, float y) {
Expand All @@ -22,20 +24,20 @@ void GameStateManager::handle_click(float x, float y) {
case 1:
{
candidate_triangle = new Triangle();
candidate_triangle->seta(Point(x,y));
candidate_triangle->set_a(Point(x, y));
Drawables::add(sf::Vertex(sf::Vector2f (x,y)));
break;
}
case 2:
{
candidate_triangle->setb(Point(x,y));
candidate_triangle->set_b(Point(x, y));
Drawables::add(sf::Vertex(sf::Vector2f (x,y)));
break;
}
case 3:
{
points = 0;
candidate_triangle->setc(Point(x,y));
candidate_triangle->set_c(Point(x, y));
Drawables::add(sf::Vertex(sf::Vector2f (x,y)));
evaluate();
break;
Expand All @@ -58,18 +60,32 @@ void GameStateManager::handle_click(float x, float y) {

GameStateManager::~GameStateManager() {
fin.close();
Drawables::clear_all();
delete challenge_triangle;
delete candidate_triangle;
draw_circle_reference = nullptr;
draw_challenge_triangle_reference = nullptr;
draw_candidate_triangle_reference = nullptr;
}

void GameStateManager::next_level() {
Drawables::clear_all();
draw_circle_reference = nullptr;
draw_challenge_triangle_reference = nullptr;
draw_candidate_triangle_reference = nullptr;
delete candidate_triangle;
if(replaylevel)
if(replay_level)
{
draw_challenge_triangle_reference = challange_triangle->add_on_screen();
//for the sake of using constructors:
Triangle* old=challenge_triangle;
challenge_triangle = new Triangle(*old);
delete old;
//
draw_challenge_triangle_reference = challenge_triangle->add_on_screen();
state = awaiting_point;
}
else {
delete challange_triangle;
delete challenge_triangle;
if (index < n) {
float x, y;
fin >> x >> y;
Expand All @@ -78,8 +94,8 @@ void GameStateManager::next_level() {
Point b(x, y);
fin >> x >> y;
Point c(x, y);
challange_triangle = new Triangle(a, b, c);
draw_challenge_triangle_reference = challange_triangle->add_on_screen();
challenge_triangle = new Triangle(a, b, c);
draw_challenge_triangle_reference = challenge_triangle->add_on_screen();
index++;
state = awaiting_point;
} else {
Expand All @@ -94,56 +110,60 @@ bool GameStateManager::has_ended() {
return false;
}

bool inline GameStateManager::is_in_circle(){
return candidate_triangle->is_inside_circle(challange_triangle->getcenter(),challange_triangle->getradius());
bool inline GameStateManager::is_in_circle() const{
return candidate_triangle->is_inside_circle(challenge_triangle->get_center(), challenge_triangle->get_radius());
}

bool inline GameStateManager::triangles_do_not_intersect(){
return candidate_triangle->does_not_intersect_triangle(*challange_triangle);
bool inline GameStateManager::triangles_do_not_intersect() const{
return candidate_triangle->does_not_intersect_triangle(*challenge_triangle);
}

void GameStateManager::evaluate() {
state=showing_result;
frames_to_show_results=0;

draw_candidate_triangle_reference = candidate_triangle->add_on_screen();
draw_circle_reference= challange_triangle->add_circumcircle_on_screen();
draw_circle_reference= challenge_triangle->add_circumscribed_circle_on_screen();

// check for intersections
if(is_in_circle() && triangles_do_not_intersect())
{
float score=100*candidate_triangle->get_area()/challange_triangle->best_area();
float score= 100 * candidate_triangle->get_area() / challenge_triangle->best_area();
std::cout<<"The 3 points you submitted created a valid triangle"<<std::endl;
std::cout<<"The triangle has an are equal to "<<candidate_triangle->get_area()<<std::endl;
std::cout<<"The maximum possible area is "<<challange_triangle->best_area()<<std::endl;
std::cout << *candidate_triangle;
std::cout<<"The triangle has an area equal to "<<candidate_triangle->get_area()<<std::endl<<std::endl;

std::cout<<"The challenge has the following characteristics:" << std::endl;
std::cout<< *challenge_triangle;
std::cout << "The maximum possible area is " << challenge_triangle->best_area() << std::endl <<std::endl;
std::cout<<"On level "<<index<<" you achieved a score of "<<score<<" %"<<std::endl;
if(score>90)
{
std::cout<<"Excellent result"<<std::endl;
replaylevel=false;
replay_level=false;
}
else
{
if(score>75)
{
std::cout<<"You passed the level but you can do better"<<std::endl;
replaylevel=false;
replay_level=false;
}
else
{
std::cout<<"That score looks pretty low, try and get a better one"<<std::endl;
replaylevel=true;
replay_level=true;
}
}
std::cout<<"Actions disabled until animation finishes to prevent accidental skip"<<std::endl;
highligh_color=sf::Color::Green;
highlight_color=sf::Color::Green;
}
else
{
std::cout<<"The 3 points you submitted formed a triangle that intersected some geometry"<<std::endl;
std::cout<<"Actions disabled until animation finishes to prevent accidental skip"<<std::endl;
replaylevel=true;
highligh_color=sf::Color::Red;
replay_level=true;
highlight_color=sf::Color::Red;
}
}

Expand All @@ -153,7 +173,7 @@ void GameStateManager::frame_update() {
if(frames_to_show_results%10==0)
{
draw_candidate_triangle_reference->color= opposite(draw_candidate_triangle_reference->color);
if(replaylevel)
if(replay_level)
{
if(!is_in_circle())
draw_circle_reference->setOutlineColor(opposite(draw_circle_reference->getOutlineColor()));
Expand All @@ -162,20 +182,26 @@ void GameStateManager::frame_update() {
}
}
frames_to_show_results++;
if (frames_to_show_results > results_max_frames)
{
state=click_to_continue;
if(!replaylevel)
std::cout<<"Click anywhere on the game screen to continue to next level"<<std::endl;
if (frames_to_show_results > results_max_frames) {
state = click_to_continue;
std::cout << "Animation finished!" << std::endl;
if (!replay_level) {
if(index!=n)
std::cout << "Click anywhere on the game screen to continue to next level" << std::endl;
else {
std::cout << "CONGRATULATIONS! YOU FINISHED THE PUZZLE!" << std::endl;
std::cout << "Click anywhere in the interface to close the application" << std::endl;
}
}
else
std::cout<<"Click anywhere on the game screen to restart level"<<std::endl;
}
}
}

sf::Color GameStateManager::opposite(sf::Color color) {
sf::Color GameStateManager::opposite(const sf::Color color) const{
if(color==sf::Color::White)
return highligh_color;
return highlight_color;
return sf::Color::White;
}

Expand Down
23 changes: 11 additions & 12 deletions GameStateManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@

class GameStateManager {
std::ifstream fin;
int n,index,points=0, frames_to_show_results=0;
const int results_max_frames = 90;
int n{},index,points=0, frames_to_show_results=0;
const int results_max_frames = 45;
void next_level();
void evaluate();
bool replaylevel=false;
Triangle *challange_triangle= nullptr;
bool replay_level=false;
Triangle *challenge_triangle= nullptr;
Triangle *candidate_triangle= nullptr;
sf::Color highligh_color;
sf::Color opposite(sf::Color color);
sf::CircleShape *draw_circle_reference;
ColoredTriangle *draw_challenge_triangle_reference;
ColoredTriangle *draw_candidate_triangle_reference;
sf::Color highlight_color;
sf::Color opposite(sf::Color color) const;
sf::CircleShape *draw_circle_reference = nullptr;
ColoredTriangle *draw_challenge_triangle_reference = nullptr;
ColoredTriangle *draw_candidate_triangle_reference = nullptr;
enum State{
awaiting_point,
showing_result,
Expand All @@ -35,9 +35,8 @@ class GameStateManager {
void handle_click(float x, float y);
bool has_ended();
void frame_update();
bool is_in_circle();

bool triangles_do_not_intersect();
bool is_in_circle() const;
bool triangles_do_not_intersect() const;
};


Expand Down
19 changes: 12 additions & 7 deletions Line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include "Line.h"
#include "cmath"

Point Line::startpoint() { return a;}
//Point Line::startpoint() { return a;}

Point Line::endpoint() { return b;}
//Point Line::endpoint() { return b;}

Line::Line(Point a1, Point b1) : a(a1),b(b1) {}

bool Line::intersects(Line line) {
bool Line::intersects(Line line) const{
// credit goes to https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
// Returns 1 if the lines intersect, otherwise 0. In addition, if the lines
// p0= a
Expand All @@ -37,8 +37,13 @@ bool Line::intersects(Line line) {
}
}

float Line::length() {
float lengthx=b.getx()-a.getx();
float lengthy=b.gety()-a.gety();
return sqrtf( lengthx * lengthx + lengthy * lengthy);
float Line::get_length() const{
float length_x= b.getx() - a.getx();
float length_y= b.gety() - a.gety();
return sqrtf(length_x * length_x + length_y * length_y);
}

std::ostream &operator<<(std::ostream &os, const Line &line) {
os << "Line has length " << line.get_length() << std::endl;
return os;
}
9 changes: 5 additions & 4 deletions Line.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
class Line {
Point a,b;
public:
Point startpoint();
Point endpoint();
//Point startpoint();
//Point endpoint();
Line(Point a1,Point b1);
bool intersects(Line line);
float length();
[[nodiscard]] bool intersects(Line line) const;
[[nodiscard]] float get_length() const;
friend std::ostream& operator<<(std::ostream& os, const Line& line);
};


Expand Down
Loading

0 comments on commit 24008dd

Please sign in to comment.