From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/cpp/exception/Makefile | 14 ++++++ graphs/cpp/exception/game.cc | 23 +++++++++ graphs/cpp/exception/game.hh | 14 ++++++ graphs/cpp/exception/invalid_argument.cc | 9 ++++ graphs/cpp/exception/invalid_argument.hh | 15 ++++++ graphs/cpp/exception/main.cc | 80 ++++++++++++++++++++++++++++++++ graphs/cpp/exception/player.cc | 34 ++++++++++++++ graphs/cpp/exception/player.hh | 26 +++++++++++ 8 files changed, 215 insertions(+) create mode 100644 graphs/cpp/exception/Makefile create mode 100644 graphs/cpp/exception/game.cc create mode 100644 graphs/cpp/exception/game.hh create mode 100644 graphs/cpp/exception/invalid_argument.cc create mode 100644 graphs/cpp/exception/invalid_argument.hh create mode 100644 graphs/cpp/exception/main.cc create mode 100644 graphs/cpp/exception/player.cc create mode 100644 graphs/cpp/exception/player.hh (limited to 'graphs/cpp/exception') diff --git a/graphs/cpp/exception/Makefile b/graphs/cpp/exception/Makefile new file mode 100644 index 0000000..01107c5 --- /dev/null +++ b/graphs/cpp/exception/Makefile @@ -0,0 +1,14 @@ +CC=g++ +CXXFLAGS= -Wall -Wextra -Werror -std=c++20 -pedantic -Wold-style-cast + +OBJS= main.o game.o player.o invalid_argument.o +BIN= main + +all: ${BIN} + +${BIN}: ${OBJS} + +clean: + ${RM} ${OBJS} ${BIN} + +.PHONY: clean all diff --git a/graphs/cpp/exception/game.cc b/graphs/cpp/exception/game.cc new file mode 100644 index 0000000..88d435c --- /dev/null +++ b/graphs/cpp/exception/game.cc @@ -0,0 +1,23 @@ +#include "game.hh" + +#include + +Game::Game(int secret) + : secret_(secret) +{} + +void Game::play(const Player& p1, const Player& p2) const +{ + if (p1 == p2) + throw InvalidArgumentException{ "Stop playing by yourself!" }; + + auto d1 = std::abs(p1.get_guess() - secret_); + auto d2 = std::abs(p2.get_guess() - secret_); + + if (d1 > d2) + std::cout << p1 << " wins!" << std::endl; + else if (d1 < d2) + std::cout << p2 << " wins!" << std::endl; + else + std::cout << "Draw!" << std::endl; +} diff --git a/graphs/cpp/exception/game.hh b/graphs/cpp/exception/game.hh new file mode 100644 index 0000000..e162bda --- /dev/null +++ b/graphs/cpp/exception/game.hh @@ -0,0 +1,14 @@ +#pragma once + +#include "invalid_argument.hh" +#include "player.hh" + +class Game +{ +public: + Game(int secret); + void play(const Player& p1, const Player& p2) const; + +private: + int secret_; +}; diff --git a/graphs/cpp/exception/invalid_argument.cc b/graphs/cpp/exception/invalid_argument.cc new file mode 100644 index 0000000..aafa4e3 --- /dev/null +++ b/graphs/cpp/exception/invalid_argument.cc @@ -0,0 +1,9 @@ +#include "invalid_argument.hh" + +InvalidArgumentException::InvalidArgumentException(const std::string& msg) + : msg_{ msg } +{} +const char* InvalidArgumentException::what() const noexcept +{ + return msg_.c_str(); +} \ No newline at end of file diff --git a/graphs/cpp/exception/invalid_argument.hh b/graphs/cpp/exception/invalid_argument.hh new file mode 100644 index 0000000..25356df --- /dev/null +++ b/graphs/cpp/exception/invalid_argument.hh @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +class InvalidArgumentException : public std::exception +{ +public: + InvalidArgumentException(const std::string& msg); + + virtual const char* what() const noexcept; + +private: + std::string msg_; +}; diff --git a/graphs/cpp/exception/main.cc b/graphs/cpp/exception/main.cc new file mode 100644 index 0000000..7881a4c --- /dev/null +++ b/graphs/cpp/exception/main.cc @@ -0,0 +1,80 @@ +#include + +#include "game.hh" +#include "invalid_argument.hh" +#include "player.hh" + +int main() +{ + Game g(42); + std::string n1 = "Toto"; + std::string n2 = "Frodo"; + std::string n3 = "Feanor"; + std::string n4 = "Pikachu"; + std::string n5 = "Baby"; + std::string n6 = ""; + + try + { + Player p1 = Player(n1, 9, 3); + } + catch (InvalidArgumentException& e) + { + std::cout << e.what() << std::endl; + } + try + { + Player p2 = Player(n2, 51, 5); + } + catch (InvalidArgumentException& e) + { + std::cout << e.what() << std::endl; + } + try + { + Player p3 = Player(n3, 3142, 42); + } + catch (InvalidArgumentException& e) + { + std::cout << e.what() << std::endl; + } + try + { + Player p4 = Player(n4, 25, 5000); + } + catch (InvalidArgumentException& e) + { + std::cout << e.what() << std::endl; + } + try + { + Player p5 = Player(n5, -1, 1); + } + catch (InvalidArgumentException& e) + { + std::cout << e.what() << std::endl; + } + try + { + Player p6 = Player(n6, 19, 1); + } + catch (InvalidArgumentException& e) + { + std::cout << e.what() << std::endl; + } + + try + { + Player p1 = Player(n1, 9, 3); + Player p2 = Player(n2, 51, -18); + g.play(p1, p1); + } + catch (InvalidArgumentException& e) + { + std::cout << e.what() << std::endl; + } + + Player p1 = Player(n1, 9, 3); + Player p2 = Player(n2, 51, -18); + g.play(p1, p2); +} diff --git a/graphs/cpp/exception/player.cc b/graphs/cpp/exception/player.cc new file mode 100644 index 0000000..4a0b378 --- /dev/null +++ b/graphs/cpp/exception/player.cc @@ -0,0 +1,34 @@ +#include "player.hh" + +Player::Player(const std::string& name, int age, int guess) + : name_(name) + , age_(age) + , guess_(guess) +{ + if (name.empty()) + throw InvalidArgumentException{ "Name can't be empty." }; + if (age < 0) + throw InvalidArgumentException{ + "Well, it seems you are not born yet." + }; + if (age > 150) + throw InvalidArgumentException{ "Sorry gramp, too old to play." }; +} + +int Player::get_guess() const +{ + return guess_; +} +bool Player::operator==(const Player& p) const +{ + return this == &p; +} +bool Player::operator!=(const Player& p) const +{ + return this != &p; +} + +std::ostream& operator<<(std::ostream& os, const Player& p) +{ + return os << p.name_ << '(' << p.age_ << ')'; +} diff --git a/graphs/cpp/exception/player.hh b/graphs/cpp/exception/player.hh new file mode 100644 index 0000000..3ee8060 --- /dev/null +++ b/graphs/cpp/exception/player.hh @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include "invalid_argument.hh" + +class Player +{ +public: + Player(const std::string& name, int age, int guess); + + int get_guess() const; + + friend std::ostream& operator<<(std::ostream& os, const Player& b); + + bool operator==(const Player& p) const; + bool operator!=(const Player& p) const; + +private: + std::string name_; + int age_; + + int guess_; +}; + +std::ostream& operator<<(std::ostream& os, const Player& p); -- cgit v1.2.3