From 30c5a823819263ad3617a87d27e217db421396ae Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 28 Mar 2021 16:37:13 +0200 Subject: [PATCH] Work on the Monopoly project. --- 03_monopoly.txt | 70 +++++--- 03_monopoly/.clang-format | 128 +++++++++++++ 03_monopoly/compile.sh | 23 +++ 03_monopoly/main.cpp | 27 +++ 03_monopoly/math.cpp | 72 ++++++++ 03_monopoly/math.h | 89 +++++++++ 03_monopoly/player.cpp | 136 ++++++++++++++ 03_monopoly/player.h | 94 ++++++++++ 03_monopoly/player_loader.cpp | 56 ++++++ 03_monopoly/player_loader.h | 13 ++ 03_monopoly/players.config | 6 + 03_monopoly/string.cpp | 327 ++++++++++++++++++++++++++++++++++ 03_monopoly/string.h | 72 ++++++++ 03_monopoly/tile.cpp | 140 +++++++++++++++ 03_monopoly/tile.h | 109 ++++++++++++ 03_monopoly/tile_loader.cpp | 53 ++++++ 03_monopoly/tile_loader.h | 13 ++ 03_monopoly/tiles.config | 6 + 03_monopoly/vector.h | 203 +++++++++++++++++++++ 19 files changed, 1613 insertions(+), 24 deletions(-) create mode 100755 03_monopoly/.clang-format create mode 100755 03_monopoly/compile.sh create mode 100755 03_monopoly/main.cpp create mode 100644 03_monopoly/math.cpp create mode 100644 03_monopoly/math.h create mode 100644 03_monopoly/player.cpp create mode 100644 03_monopoly/player.h create mode 100644 03_monopoly/player_loader.cpp create mode 100644 03_monopoly/player_loader.h create mode 100644 03_monopoly/players.config create mode 100644 03_monopoly/string.cpp create mode 100644 03_monopoly/string.h create mode 100644 03_monopoly/tile.cpp create mode 100644 03_monopoly/tile.h create mode 100644 03_monopoly/tile_loader.cpp create mode 100644 03_monopoly/tile_loader.h create mode 100644 03_monopoly/tiles.config create mode 100644 03_monopoly/vector.h diff --git a/03_monopoly.txt b/03_monopoly.txt index 39f1f02..cdb76ea 100644 --- a/03_monopoly.txt +++ b/03_monopoly.txt @@ -8,6 +8,52 @@ String name; virtual void player_arrived(Player *p); +---------- + +class Player + +String name; +int tile_index; +int money; +int jail_time; +bool _lost; + +virtual bool ask_buy(String name, int price); +virtual void pay_entry(int val, Player *to); +void pay_tax(int val); +void receive_payment(int val); +void lost(); +bool did_lose(); + +virtual void act(); +virtual int throw_dice(); +virtual void on_lose(); + + + +---------- + +class Playerloader + +vec pl laod_players(file); +save_players(file, Vec p); + + +---------- + +class TileLoader + +isame + +ezek ay oytalzok load savelnek +lehetne static fv playerbe is pl +meg lehetne a boardba +de igy egyszerubben ujra tudjuk majd hasznalni +javaban TileFactorynak lenne nevezve + + + + ---------- class TaxTile : Tile @@ -52,29 +98,6 @@ virtual void player_arrived(Player *p); ----------- - -class Player - -String name; -int tile_index; -int money; -int jail_time; -bool _lost; - -virtual bool ask_buy(String name, int price); -virtual void pay_entry(int val, Player *to); -void pay_tax(int val); -void receive_payment(int val); -void lost(); -bool did_lose(); - -virtual void act(); -virtual int throw_dice(); -virtual void on_lose(); - - - ---------- @@ -120,7 +143,6 @@ virtual void act(); virtual int throw_dice(); - ---------- class Board diff --git a/03_monopoly/.clang-format b/03_monopoly/.clang-format new file mode 100755 index 0000000..cb67d39 --- /dev/null +++ b/03_monopoly/.clang-format @@ -0,0 +1,128 @@ +# Commented out parameters are those with the same value as base LLVM style +# We can uncomment them if we want to change their value, or enforce the +# chosen value in case the base style changes (last sync: Clang 6.0.1). +--- +### General config, applies to all languages ### +BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: DontAlign +# AlignConsecutiveAssignments: false +# AlignConsecutiveDeclarations: false +# AlignEscapedNewlines: Right +# AlignOperands: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +# AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: true +# AllowShortLoopsOnASingleLine: false +# AlwaysBreakAfterDefinitionReturnType: None +# AlwaysBreakAfterReturnType: None +# AlwaysBreakBeforeMultilineStrings: false +# AlwaysBreakTemplateDeclarations: false +# BinPackArguments: true +# BinPackParameters: true +# BraceWrapping: +# AfterClass: false +# AfterControlStatement: false +# AfterEnum: false +# AfterFunction: false +# AfterNamespace: false +# AfterObjCDeclaration: false +# AfterStruct: false +# AfterUnion: false +# AfterExternBlock: false +# BeforeCatch: false +# BeforeElse: false +# IndentBraces: false +# SplitEmptyFunction: true +# SplitEmptyRecord: true +# SplitEmptyNamespace: true +# BreakBeforeBinaryOperators: None +# BreakBeforeBraces: Attach +# BreakBeforeInheritanceComma: false +BreakBeforeTernaryOperators: false +# BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: AfterColon +# BreakStringLiterals: true +ColumnLimit: 0 +# CommentPragmas: '^ IWYU pragma:' +# CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 8 +ContinuationIndentWidth: 8 +Cpp11BracedListStyle: false +# DerivePointerAlignment: false +# DisableFormat: false +# ExperimentalAutoDetectBinPacking: false +# FixNamespaceComments: true +# ForEachMacros: +# - foreach +# - Q_FOREACH +# - BOOST_FOREACH +# IncludeBlocks: Preserve +IncludeCategories: + - Regex: '".*"' + Priority: 1 + - Regex: '^<.*\.h>' + Priority: 2 + - Regex: '^<.*' + Priority: 3 +# IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: true +# IndentPPDirectives: None +IndentWidth: 4 +# IndentWrappedFunctionNames: false +# JavaScriptQuotes: Leave +# JavaScriptWrapImports: true +# KeepEmptyLinesAtTheStartOfBlocks: true +# MacroBlockBegin: '' +# MacroBlockEnd: '' +# MaxEmptyLinesToKeep: 1 +# NamespaceIndentation: None +# PenaltyBreakAssignment: 2 +# PenaltyBreakBeforeFirstCallParameter: 19 +# PenaltyBreakComment: 300 +# PenaltyBreakFirstLessLess: 120 +# PenaltyBreakString: 1000 +# PenaltyExcessCharacter: 1000000 +# PenaltyReturnTypeOnItsOwnLine: 60 +# PointerAlignment: Right +# RawStringFormats: +# - Delimiter: pb +# Language: TextProto +# BasedOnStyle: google +# ReflowComments: true +# SortIncludes: true +# SortUsingDeclarations: true +# SpaceAfterCStyleCast: false +# SpaceAfterTemplateKeyword: true +# SpaceBeforeAssignmentOperators: true +# SpaceBeforeParens: ControlStatements +# SpaceInEmptyParentheses: false +# SpacesBeforeTrailingComments: 1 +# SpacesInAngles: false +# SpacesInContainerLiterals: true +# SpacesInCStyleCastParentheses: false +# SpacesInParentheses: false +# SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: Always +--- +### C++ specific config ### +Language: Cpp +Standard: Cpp03 +--- +### ObjC specific config ### +Language: ObjC +Standard: Cpp03 +ObjCBlockIndentWidth: 4 +# ObjCSpaceAfterProperty: false +# ObjCSpaceBeforeProtocolList: true +--- +### Java specific config ### +Language: Java +# BreakAfterJavaFieldAnnotations: false +JavaImportGroups: ['org.godotengine', 'android', 'androidx', 'com.android', 'com.google', 'java', 'javax'] +... diff --git a/03_monopoly/compile.sh b/03_monopoly/compile.sh new file mode 100755 index 0000000..3cf2ebb --- /dev/null +++ b/03_monopoly/compile.sh @@ -0,0 +1,23 @@ + +if [ ! -d "obj" ]; then + mkdir obj +fi + +if [ ! -d "bin" ]; then + mkdir bin +fi + +#-Iinclude + +g++ -Wall -g -c string.cpp -o obj/string.o +g++ -Wall -g -c math.cpp -o obj/math.o + +g++ -Wall -g -c tile.cpp -o obj/tile.o +g++ -Wall -g -c player.cpp -o obj/player.o + +g++ -Wall -g -c player_loader.cpp -o obj/player_loader.o +g++ -Wall -g -c tile_loader.cpp -o obj/tile_loader.o + +g++ -Wall -g -c main.cpp -o obj/main.o +g++ -o bin/program obj/string.o obj/math.o obj/tile.o obj/tile_loader.o obj/player.o obj/player_loader.o obj/main.o + diff --git a/03_monopoly/main.cpp b/03_monopoly/main.cpp new file mode 100755 index 0000000..d26a3ba --- /dev/null +++ b/03_monopoly/main.cpp @@ -0,0 +1,27 @@ + +#include + +#include "string.h" +#include "vector.h" + +#include "player.h" +#include "tile.h" + +#include "tile_loader.h" +#include "player_loader.h" + +int main() { + + Vector tiles = TileLoader::load_tile_file("tiles.config"); + Vector players = PlayerLoader::load_player_file("players.config"); + + for (int i = 0; i < tiles.size(); ++i) { + tiles[i]->print(); + } + + for (int i = 0; i < players.size(); ++i) { + players[i]->print(); + } + + return 0; +} diff --git a/03_monopoly/math.cpp b/03_monopoly/math.cpp new file mode 100644 index 0000000..03106a3 --- /dev/null +++ b/03_monopoly/math.cpp @@ -0,0 +1,72 @@ +#include "math.h" + +float Math::inv_sqrt(const float x) { + return (float)(1.0 / ::sqrtf(x)); +} + +float Math::fast_inv_sqrt(const float number) { + long i; + float x2, y; + const float threehalfs = 1.5F; + + x2 = number * 0.5F; + y = number; + i = *(long *)&y; + i = 0x5f3759df - (i >> 1); + y = *(float *)&i; + y = y * (threehalfs - (x2 * y * y)); + + return y; +} + +float Math::is_equal_approx(const float a, const float b) { + if (a + EPSILON < b && a - EPSILON > b) { + return true; + } + + return false; +} + +float Math::is_zero_approx(const float a) { + if (a + EPSILON < 0 && a - EPSILON > 0) { + return true; + } + + return false; +} + +void Math::seed(const unsigned int s) { + srand(s); +} + +void Math::randomize() { + srand(time(NULL)); +} + +int Math::rand() { + return rand(); +} + +float Math::randf() { + return rand() / static_cast(RANDOM_32BIT_MAX); +} + +double Math::randd() { + return rand() / static_cast(RANDOM_32BIT_MAX); +} + +int rand(const int m) { + return rand() % m; +} + +int rand(const int from, const int to) { + return (rand() % (to - from)) + from; +} + +float Math::rand(const float from, const float to) { + return randf() * (to - from) + from; +} + +float Math::rand(const double from, const double to) { + return randd() * (to - from) + from; +} diff --git a/03_monopoly/math.h b/03_monopoly/math.h new file mode 100644 index 0000000..cd5fb8f --- /dev/null +++ b/03_monopoly/math.h @@ -0,0 +1,89 @@ +#ifndef MATH_H +#define MATH_H + +#include +#include +#include +#include + +#define MATH_PI 3.1415926535897932384626433833 +#define EPSILON 0.00001 + +class Math { +public: + + static const uint64_t RANDOM_32BIT_MAX = 0xFFFFFFFF; + + inline static float sin(const float x) { return ::sinf(x); } + inline static double sin(const double x) { return ::sin(x); } + + inline static float cos(const float x) { return ::cosf(x); } + inline static double cos(const double x) { return ::cos(x); } + + inline static float tan(const float x) { return ::tanf(x); } + inline static double tan(const double x) { return ::tan(x); } + + inline static float asin(const float x) { return ::asinf(x); } + inline static double asin(const double x) { return ::asin(x); } + + inline static float acos(const float x) { return ::acosf(x); } + inline static double acos(const double x) { return ::acos(x); } + + inline static float atan(const float x) { return ::atanf(x); } + inline static double atan(const double x) { return ::atan(x); } + + inline static float atan2(const float x, const float y) { return ::atan2f(x, y); } + inline static double atan2(const double x, const float y) { return ::atan2(x, y); } + + inline static float sqrt(const float x) { return ::sqrtf(x); } + inline static double sqrt(const double x) { return ::sqrt(x); } + + inline static float fmod(const float x, const float y) { return ::fmodf(x, y); } + inline static double fmod(const double x, const float y) { return ::fmod(x, y); } + + inline static float floor(const float x) { return ::floorf(x); } + inline static double floor(const double x) { return ::floor(x); } + + inline static float ceil(const float x) { return ::ceilf(x); } + inline static double ceil(const double x) { return ::ceil(x); } + + inline static float pow(const float x, const float y) { return ::powf(x, y); } + inline static double pow(const double x, const float y) { return ::pow(x, y); } + + inline static float log(const float x) { return ::logf(x); } + inline static double log(const double x) { return ::log(x); } + + static float inv_sqrt(const float x); + static float fast_inv_sqrt(const float x); + + inline static float abs(const float x) { return x > 0 ? x : -x; } + inline static double abs(const double x) { return x > 0 ? x : -x; } + inline static int abs(const int x) { return x > 0 ? x : -x; } + + inline static float deg2rad(const float x) { return x * MATH_PI / 180.0; } + inline static double deg2rad(const double x) { return x * MATH_PI / 180.0; } + inline static int deg2rad(const int x) { return x * MATH_PI / 180.0; } + + inline static float rad2deg(const float x) { return x * 180.0 / MATH_PI; } + inline static double rad2deg(const double x) { return x * 180.0 / MATH_PI; } + inline static int rad2deg(const int x) { return x * 180.0 / MATH_PI; } + + static float is_equal_approx(const float a, const float b); + static float is_zero_approx(const float a); + + static void seed(const unsigned int s); + static void randomize(); + + static int rand(); + static float randf(); + static double randd(); + + static int rand(const int m); + + static int rand(const int from, const int to); + static float rand(const float from, const float to); + static float rand(const double from, const double to); + +}; + +#endif diff --git a/03_monopoly/player.cpp b/03_monopoly/player.cpp new file mode 100644 index 0000000..05b915f --- /dev/null +++ b/03_monopoly/player.cpp @@ -0,0 +1,136 @@ +#include "player.h" + +#include + +String Player::get_name() { + return _name; +} + +void Player::set_name(const String &name) { + _name = name; +} + +int Player::get_tile_index() const { + return _tile_index; +} + +void Player::set_tile_index(const int val) { + _tile_index = val; +} + +int Player::get_money() const { + return _money; +} + +void Player::set_money(const int val) { + _money = val; +} + +int Player::get_jail_time() const { + return _jail_time; +} + +void Player::set_jail_time(const int val) { + _jail_time = val; +} + +bool Player::get_lost() const { + return _lost; +} + +void Player::set_lost(const bool val) { + _lost = val; +} + +bool Player::want_buy(const String &tile_name, int price) { + return false; +} + +void Player::pay_entry_fee(const int val, Player *to) { +} + +void Player::throw_dice() { +} + +void Player::on_lose() { +} + +void Player::print() { + printf("Player - %s - Name: %s Tile Index: %d Money: %d Jail time: %d Lost: %d\n", get_class_name().c_str(), _name.c_str(), _tile_index, _money, _jail_time, _lost); +} + +String Player::get_class_name() { + return "Player"; +} + +Player::Player() { + _tile_index = 0; + _money = 0; + _jail_time = 0; + _lost = 0; +} + +Player::~Player() { +} + +bool AgressivePlayer::want_buy(const String &tile_name, int price) { + return false; +} +void AgressivePlayer::on_lose() { +} +String AgressivePlayer::get_class_name() { + return "AgressivePlayer"; +} +AgressivePlayer::AgressivePlayer() : + Player() { +} + +bool ConservativePlayer::want_buy(const String &tile_name, int price) { + return false; +} +void ConservativePlayer::on_lose() { +} +String ConservativePlayer::get_class_name() { + return "ConservativePlayer"; +} +ConservativePlayer::ConservativePlayer() : + Player() { +} + +bool TrickyPlayer::want_buy(const String &tile_name, int price) { + return false; +} +void TrickyPlayer::on_lose() { +} +String TrickyPlayer::get_class_name() { + return "TrickyPlayer"; +} +TrickyPlayer::TrickyPlayer() : + Player() { +} + +bool HumanPlayer::want_buy(const String &tile_name, int price) { + return false; +} +void HumanPlayer::on_lose() { +} +String HumanPlayer::get_class_name() { + return "HumanPlayer"; +} +HumanPlayer::HumanPlayer() : + Player() { +} + +bool CheatingPlayer::want_buy(const String &tile_name, int price) { + return false; +} +void CheatingPlayer::throw_dice() { +} +void CheatingPlayer::on_lose() { +} +String CheatingPlayer::get_class_name() { + return "CheatingPlayer"; +} +CheatingPlayer::CheatingPlayer() : + Player() { +} diff --git a/03_monopoly/player.h b/03_monopoly/player.h new file mode 100644 index 0000000..1abd864 --- /dev/null +++ b/03_monopoly/player.h @@ -0,0 +1,94 @@ +#ifndef PLAYER_H +#define PLAYER_H + +#include "string.h" + +class Player { +public: + String get_name(); + void set_name(const String &name); + + int get_tile_index() const; + void set_tile_index(const int val); + + int get_money() const; + void set_money(const int val); + + int get_jail_time() const; + void set_jail_time(const int val); + + bool get_lost() const; + void set_lost(const bool val); + + virtual bool want_buy(const String &tile_name, int price); + virtual void pay_entry_fee(const int val, Player *to); + virtual void throw_dice(); + virtual void on_lose(); + + virtual void print(); + + virtual String get_class_name(); + + Player(); + virtual ~Player(); + +protected: + String _name; + int _tile_index; + int _money; + int _jail_time; + bool _lost; +}; + +class AgressivePlayer : public Player { +public: + bool want_buy(const String &tile_name, int price); + void on_lose(); + + String get_class_name(); + + AgressivePlayer(); +}; + +class ConservativePlayer : public Player { +public: + bool want_buy(const String &tile_name, int price); + void on_lose(); + + String get_class_name(); + + ConservativePlayer(); +}; + +class TrickyPlayer : public Player { +public: + bool want_buy(const String &tile_name, int price); + void on_lose(); + + String get_class_name(); + + TrickyPlayer(); +}; + +class HumanPlayer : public Player { +public: + bool want_buy(const String &tile_name, int price); + void on_lose(); + + String get_class_name(); + + HumanPlayer(); +}; + +class CheatingPlayer : public Player { +public: + bool want_buy(const String &tile_name, int price); + void throw_dice(); + void on_lose(); + + String get_class_name(); + + CheatingPlayer(); +}; + +#endif diff --git a/03_monopoly/player_loader.cpp b/03_monopoly/player_loader.cpp new file mode 100644 index 0000000..c9ede64 --- /dev/null +++ b/03_monopoly/player_loader.cpp @@ -0,0 +1,56 @@ +#include "player_loader.h" + +#include +#include +#include "player.h" + +Vector PlayerLoader::load_player_file(const String &file_name) { + Vector players; + + std::fstream f; + f.open(file_name.c_str()); + + if (!f.fail()) { + + while (!f.eof()) { + std::string class_name; + + f >> class_name; + + Player *p = nullptr; + + if (class_name == "Player") { + p = new Player(); + } else if (class_name == "AgressivePlayer") { + p = new AgressivePlayer(); + } else if (class_name == "ConservativePlayer") { + p = new ConservativePlayer(); + } else if (class_name == "TrickyPlayer") { + p = new TrickyPlayer(); + } else if (class_name == "HumanPlayer") { + p = new HumanPlayer(); + } else if (class_name == "CheatingPlayer") { + p = new CheatingPlayer(); + } + + if (p) { + int money; + std::string name; + + f >> money >> name; + + p->set_money(money); + + String n(name.c_str()); + + p->set_name(n); + + players.push_back(p); + } + } + + f.close(); + } + + return players; +} diff --git a/03_monopoly/player_loader.h b/03_monopoly/player_loader.h new file mode 100644 index 0000000..630bc26 --- /dev/null +++ b/03_monopoly/player_loader.h @@ -0,0 +1,13 @@ +#ifndef PLAYER_LOADER_H +#define PLAYER_LOADER_H + +#include "player.h" +#include "string.h" +#include "vector.h" + +class PlayerLoader { +public: + static Vector load_player_file(const String &file_name); +}; + +#endif diff --git a/03_monopoly/players.config b/03_monopoly/players.config new file mode 100644 index 0000000..551f62f --- /dev/null +++ b/03_monopoly/players.config @@ -0,0 +1,6 @@ +Player 10000 Jaj +AgressivePlayer 20000 FFF +ConservativePlayer 20000 Acq +TrickyPlayer 20000 dqd +HumanPlayer 20000 WFa +CheatingPlayer 20000 Fqqf \ No newline at end of file diff --git a/03_monopoly/string.cpp b/03_monopoly/string.cpp new file mode 100644 index 0000000..2c74d8c --- /dev/null +++ b/03_monopoly/string.cpp @@ -0,0 +1,327 @@ +#include "string.h" + +#include + +void String::push_back(const char element) { + ensure_capacity(_size + 1); + + _data[_size++] = element; + _data[_size] = '\0'; +} + +void String::pop_back() { + if (_size == 0) { + return; + } + + --_size; + + _data[_size] = '\0'; +} + +void String::remove(const int index) { + _data[index] = _data[_size - 1]; + + --_size; + + _data[_size] = '\0'; +} + +void String::erase(const char element) { + int index = find(element); + + if (index != -1) { + remove(index); + } +} + +void String::clear() { + _size = 0; +} + +bool String::empty() const { + return _size == 0; +} + +char String::get(const int index) { + return _data[index]; +} + +const char String::get(const int index) const { + return _data[index]; +} + +void String::set(const int index, const char value) { + _data[index] = value; +} + +int String::size() const { + return _size; +} + +int String::capacity() const { + return _actual_size; +} + +void String::ensure_capacity(const int capacity) { + if (capacity <= _actual_size) { + return; + } + + int tsize = capacity + _grow_by; + + char *nd = new char[tsize]; + + for (int i = 0; i < _size; ++i) { + nd[i] = _data[i]; + } + + delete[] _data; + + _data = nd; +} + +void String::resize(const int s) { + ensure_capacity(s + 1); // +1 for the null terminator + + _size = s; + + _data[_size] = '\0'; +} + +void String::append_array(const String &other) { + ensure_capacity(_size + other._size + 1); // +1 for the null terminator + + for (int i = 0; i < other._size; ++i) { + _data[_size++] = other._data[i]; + } + + _data[_size] = '\0'; +} + +int String::find(const char val) const { + for (int i = 0; i < _size; ++i) { + if (_data[i] == val) { + return i; + } + } + + return -1; +} + +float String::to_float() { + return atof(c_str()); +} + +double String::to_double() { + return atof(c_str()); +} + +int String::to_int() { + return atoi(c_str()); +} + +uint32_t String::to_uint() { + return static_cast(atoll(c_str())); +} + +char *String::c_str() { + return _data; +} + +const char *String::c_str() const { + return _data; +} + +char *String::dataw() { + return _data; +} + +const char *String::data() const { + return _data; +} + +const char String::operator[](const int index) const { + return _data[index]; +} + +char String::operator[](const int index) { + return _data[index]; +} + +String &String::operator+=(const String &b) { + ensure_capacity(_size + b._size + 1); // +1 for the null terminator + + for (int i = 0; i < b._size; ++i) { + _data[_size++] = b._data[i]; + } + + return *this; +} + +String &String::operator+=(const char chr) { + push_back(chr); + + return *this; +} + +String &String::operator+=(const char *p_c_str) { + int i = 0; + while (p_c_str[i] != '\0') { + push_back(p_c_str[i]); + ++i; + } + + return *this; +} + +String operator+(String lhs, const String &rhs) { + lhs += rhs; + + return lhs; +} + +String operator+(String lhs, const char *rhs) { + lhs += rhs; + + return lhs; +} + +String operator+(String lhs, const char rhs) { + lhs += rhs; + + return lhs; +} + +bool operator==(const String &a, const String &b) { + if (a._size != b._size) { + return false; + } + + for (int i = 0; i < a._size; ++i) { + if (a[i] != b[i]) { + return false; + } + } + + return true; +} + +bool operator!=(const String &a, const String &b) { + return !(a == b); +} + +bool operator==(const String &a, const char *b) { + int i = 0; + while (b[i] != '\0' && i < a._size) { + if (a[i] != b[i]) { + return false; + } + + ++i; + } + + if (i != a._size) { + return false; + } + + return true; +} + +bool operator!=(const String &a, const char *b) { + return !(a == b); +} + +bool operator==(const char *b, const String &a) { + int i = 0; + while (b[i] != '\0' && i < a._size) { + if (a[i] != b[i]) { + return false; + } + + ++i; + } + + if (i != a._size) { + return false; + } + + return true; +} + +bool operator!=(const char *b, const String &a) { + return !(a == b); +} + +String::String() { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; +} + +String::String(const String &other) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; + + //+1 for the null terminator in case its needed + ensure_capacity(other.size() + 1); + + for (int i = 0; i < other._size; ++i) { + _data[i] = other._data[i]; + } +} + +String::String(const String &other, int grow_by) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = grow_by; + + //+1 for the null terminator in case its needed + ensure_capacity(other.size() + 1); + + for (int i = 0; i < other._size; ++i) { + _data[i] = other._data[i]; + } + + _data[_size] = '\0'; +} + +String::String(const char* p_c_str) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; + + operator+=(p_c_str); +} + +String::String(const char* p_c_str, const int grow_by) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = grow_by; + + operator+=(p_c_str); +} + +String::String(int prealloc) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; + + ensure_capacity(prealloc); +} + +String::String(int prealloc, int grow_by) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = grow_by; + + ensure_capacity(prealloc); +} diff --git a/03_monopoly/string.h b/03_monopoly/string.h new file mode 100644 index 0000000..e161b7c --- /dev/null +++ b/03_monopoly/string.h @@ -0,0 +1,72 @@ +#ifndef STRING_H +#define STRING_H + +#include + +class String { + +public: + void push_back(const char element); + void pop_back(); + void remove(const int index); + void erase(const char element); + void clear(); + bool empty() const; + char get(const int index); + const char get(const int index) const; + void set(const int index, const char value); + + int size() const; + int capacity() const; + void ensure_capacity(const int capacity); + void resize(const int s); + void append_array(const String &other); + int find(const char val) const; + + float to_float(); + double to_double(); + int to_int(); + uint32_t to_uint(); + + char *c_str(); + const char *c_str() const; + + char *dataw(); + const char *data() const; + + const char operator[](const int index) const; + char operator[](const int index); + + String &operator+=(const String &b); + String &operator+=(const char chr); + String &operator+=(const char *p_c_str); + + friend String operator+(String lhs, const String &rhs); + friend String operator+(String lhs, const char *rhs); + friend String operator+(String lhs, const char rhs); + + friend bool operator==(const String &a, const String &b); + friend bool operator!=(const String &a, const String &b); + + friend bool operator==(const String &a, const char *b); + friend bool operator!=(const String &a, const char *b); + + friend bool operator==(const char *b, const String &a); + friend bool operator!=(const char *b, const String &a); + + String(); + String(const String &other); + String(const String &other, const int grow_by); + String(const char* p_c_str); + String(const char* p_c_str, const int grow_by); + String(const int prealloc); + String(const int prealloc, const int grow_by); + +private: + char *_data; + int _actual_size; + int _size; + int _grow_by; +}; + +#endif \ No newline at end of file diff --git a/03_monopoly/tile.cpp b/03_monopoly/tile.cpp new file mode 100644 index 0000000..010660a --- /dev/null +++ b/03_monopoly/tile.cpp @@ -0,0 +1,140 @@ +#include "tile.h" + +#include + +String Tile::get_name() { + return _name; +} + +void Tile::set_name(const String &name) { + _name = name; +} + +void Tile::on_player_arrived(Player *p) { +} + +void Tile::print() { + printf("Tile - %s - Name: %s\n", get_class_name().c_str(), _name.c_str()); +} + +String Tile::get_class_name() { + return "Tile"; +} + +Tile::Tile() { +} + +Tile::~Tile() { +} + +int TaxTile::get_tax() const { + return _tax; +} +void TaxTile::set_tax(const int tax) { +} + +void TaxTile::on_player_arrived(Player *p) { +} +String TaxTile::get_class_name() { + return "TaxTile"; +} + +TaxTile::TaxTile() : + Tile() { +} + +int OwnableTile::get_price() const { + return _price; +} +void OwnableTile::set_price(const int val) { + _price = val; +} + +int OwnableTile::get_enter_price() const { + return _enter_price; +} +void OwnableTile::set_enter_price(const int val) { + _enter_price = val; +} + +Player *OwnableTile::get_owner() const { + return _owner; +} +void OwnableTile::set_owner(Player *val) { + _owner = val; +} + +void OwnableTile::on_player_arrived(Player *p) { +} +String OwnableTile::get_class_name() { + return "OwnableTile"; +} + +OwnableTile::OwnableTile() : + Tile() { +} + +int GainTile::get_gain() const { + return _gain; +} +void GainTile::set_gain(const int val) { + _gain = val; +} + +void GainTile::on_player_arrived(Player *p) { +} +String GainTile::get_class_name() { + return "GainTile"; +} + +GainTile::GainTile() : + Tile() { +} + +int LuckTile::get_chance() const { + return _chance; +} +void LuckTile::set_chance(const int val) { + _chance = val; +} + +int LuckTile::get_gain_min() const { + return _gain_min; +} +void LuckTile::set_gain_min(const int val) { + _gain_min = val; +} + +int LuckTile::get_gain_max() const { + return _gain_max; +} +void LuckTile::set_gain_max(const int val) { + _gain_max = val; +} + +void LuckTile::on_player_arrived(Player *p) { +} +String LuckTile::get_class_name() { + return "LuckTile"; +} + +LuckTile::LuckTile() : + Tile() { +} + +int JailTile::get_jail_time() const { + return _jail_time; +} +void JailTile::set_jail_time(const int val) { + _jail_time = val; +} + +void JailTile::on_player_arrived(Player *p) { +} +String JailTile::get_class_name() { + return "JailTile"; +} + +JailTile::JailTile() : + Tile() { +} diff --git a/03_monopoly/tile.h b/03_monopoly/tile.h new file mode 100644 index 0000000..898b26f --- /dev/null +++ b/03_monopoly/tile.h @@ -0,0 +1,109 @@ +#ifndef TILE_H +#define TILE_H + +#include "player.h" + +class Tile { +public: + String get_name(); + void set_name(const String &name); + + virtual void on_player_arrived(Player *p); + + virtual void print(); + virtual String get_class_name(); + + Tile(); + virtual ~Tile(); + +private: + String _name; +}; + +class TaxTile : public Tile { +public: + int get_tax() const; + void set_tax(const int tax); + + void on_player_arrived(Player *p); + String get_class_name(); + + TaxTile(); + +private: + int _tax; +}; + +class OwnableTile : public Tile { +public: + int get_price() const; + void set_price(const int val); + + int get_enter_price() const; + void set_enter_price(const int val); + + Player *get_owner() const; + void set_owner(Player *val); + + void on_player_arrived(Player *p); + String get_class_name(); + + OwnableTile(); + +private: + int _price; + int _enter_price; + Player *_owner; +}; + +class GainTile : public Tile { +public: + int get_gain() const; + void set_gain(const int val); + + void on_player_arrived(Player *p); + String get_class_name(); + + GainTile(); + +private: + int _gain; +}; + +class LuckTile : public Tile { +public: + int get_chance() const; + void set_chance(const int val); + + int get_gain_min() const; + void set_gain_min(const int val); + + int get_gain_max() const; + void set_gain_max(const int val); + + void on_player_arrived(Player *p); + String get_class_name(); + + LuckTile(); + +private: + int _chance; + int _gain_min; + int _gain_max; +}; + +class JailTile : public Tile { +public: + int get_jail_time() const; + void set_jail_time(const int val); + + void on_player_arrived(Player *p); + String get_class_name(); + + JailTile(); + +private: + int _jail_time; +}; + +#endif diff --git a/03_monopoly/tile_loader.cpp b/03_monopoly/tile_loader.cpp new file mode 100644 index 0000000..ba234f2 --- /dev/null +++ b/03_monopoly/tile_loader.cpp @@ -0,0 +1,53 @@ +#include "tile_loader.h" + +#include +#include +#include "tile.h" + +Vector TileLoader::load_tile_file(const String &file_name) { + Vector tiles; + + std::fstream f; + f.open(file_name.c_str()); + + if (!f.fail()) { + + while (!f.eof()) { + std::string class_name; + + f >> class_name; + + Tile *t = nullptr; + + if (class_name == "Tile") { + t = new Tile(); + } else if (class_name == "TaxTile") { + t = new TaxTile(); + } else if (class_name == "OwnableTile") { + t = new OwnableTile(); + } else if (class_name == "GainTile") { + t = new GainTile(); + } else if (class_name == "LuckTile") { + t = new LuckTile(); + } else if (class_name == "JailTile") { + t = new JailTile(); + } + + if (t) { + std::string name; + + f >> name; + + String n(name.c_str()); + + t->set_name(n); + + tiles.push_back(t); + } + } + + f.close(); + } + + return tiles; +} diff --git a/03_monopoly/tile_loader.h b/03_monopoly/tile_loader.h new file mode 100644 index 0000000..c287c21 --- /dev/null +++ b/03_monopoly/tile_loader.h @@ -0,0 +1,13 @@ +#ifndef TILE_LOADER_H +#define TILE_LOADER_H + +#include "string.h" +#include "tile.h" +#include "vector.h" + +class TileLoader { +public: + static Vector load_tile_file(const String &file_name); +}; + +#endif diff --git a/03_monopoly/tiles.config b/03_monopoly/tiles.config new file mode 100644 index 0000000..fc54d86 --- /dev/null +++ b/03_monopoly/tiles.config @@ -0,0 +1,6 @@ +Tile Asf +TaxTile aAA +OwnableTile fqfqfdg +GainTile fffqq +LuckTile Qewew +JailTile Fqqfq diff --git a/03_monopoly/vector.h b/03_monopoly/vector.h new file mode 100644 index 0000000..d8371dc --- /dev/null +++ b/03_monopoly/vector.h @@ -0,0 +1,203 @@ +#ifndef VECTOR_H +#define VECTOR_H + +template +class Vector { + +public: + void push_back(const T &element); + void pop_back(); + void remove(const int index); + void erase(const T &element); + void clear(); + bool empty() const; + T get(const int index); + const T &get(const int index) const; + void set(const int index, const T &value); + + int size() const; + int capacity() const; + void ensure_capacity(const int capacity); + void resize(const int s); + void append_array(const Vector &other); + int find(const T &val) const; + + int *dataw(); + const int *data() const; + + const T &operator[](const int index) const; + T &operator[](const int index); + + Vector(); + Vector(int prealloc); + Vector(int prealloc, int grow_by); + +private: + T *_data; + int _actual_size; + int _size; + int _grow_by; +}; + +template +void Vector::push_back(const T &element) { + ensure_capacity(_size + 1); + + _data[_size++] = element; +} + +template +void Vector::pop_back() { + if (_size == 0) { + return; + } + + --_size; +} + +template +void Vector::remove(const int index) { + _data[index] = _data[_size - 1]; + + --_size; +} + +template +void Vector::erase(const T &element) { + int index = find(element); + + if (index != -1) { + remove(index); + } +} + +template +void Vector::clear() { + _size = 0; +} + +template +bool Vector::empty() const { + return _size == 0; +} + +template +T Vector::get(const int index) { + return _data[index]; +} + +template +const T &Vector::get(const int index) const { + return _data[index]; +} + +template +void Vector::set(const int index, const T &value) { + _data[index] = value; +} + +template +int Vector::size() const { + return _size; +} + +template +int Vector::capacity() const { + return _actual_size; +} + +template +void Vector::ensure_capacity(const int capacity) { + if (capacity <= _actual_size) { + return; + } + + int tsize = capacity + _grow_by; + + T *nd = new T[tsize]; + + for (int i = 0; i < _size; ++i) { + nd[i] = _data[i]; + } + + delete[] _data; + + _data = nd; +} + +template +void Vector::resize(const int s) { + ensure_capacity(s); + + _size = s; +} + +template +void Vector::append_array(const Vector &other) { + ensure_capacity(_size + other._size); + + for (int i = 0; i < other._size; ++i) { + _data[_size++] = other._data[i]; + } +} + +template +int Vector::find(const T &val) const { + for (int i = 0; i < _size; ++i) { + if (_data[i] == val) { + return i; + } + } + + return -1; +} + +template +int *Vector::dataw() { + return _data; +} + +template +const int *Vector::data() const { + return _data; +} + +template +const T &Vector::operator[](const int index) const { + return _data[index]; +} + +template +T &Vector::operator[](const int index) { + return _data[index]; +} + +template +Vector::Vector() { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; +} + +template +Vector::Vector(int prealloc) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; + + ensure_capacity(prealloc); +} + +template +Vector::Vector(int prealloc, int grow_by) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = grow_by; + + ensure_capacity(prealloc); +} + +#endif \ No newline at end of file