mirror of
https://github.com/Relintai/programming_tutorials.git
synced 2025-04-21 21:51:22 +02:00
Work on the Monopoly project.
This commit is contained in:
parent
8f3651b3f0
commit
30c5a82381
@ -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
|
||||
|
128
03_monopoly/.clang-format
Executable file
128
03_monopoly/.clang-format
Executable file
@ -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']
|
||||
...
|
23
03_monopoly/compile.sh
Executable file
23
03_monopoly/compile.sh
Executable file
@ -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
|
||||
|
27
03_monopoly/main.cpp
Executable file
27
03_monopoly/main.cpp
Executable file
@ -0,0 +1,27 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "string.h"
|
||||
#include "vector.h"
|
||||
|
||||
#include "player.h"
|
||||
#include "tile.h"
|
||||
|
||||
#include "tile_loader.h"
|
||||
#include "player_loader.h"
|
||||
|
||||
int main() {
|
||||
|
||||
Vector<Tile *> tiles = TileLoader::load_tile_file("tiles.config");
|
||||
Vector<Player *> 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;
|
||||
}
|
72
03_monopoly/math.cpp
Normal file
72
03_monopoly/math.cpp
Normal file
@ -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<float>(RANDOM_32BIT_MAX);
|
||||
}
|
||||
|
||||
double Math::randd() {
|
||||
return rand() / static_cast<double>(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;
|
||||
}
|
89
03_monopoly/math.h
Normal file
89
03_monopoly/math.h
Normal file
@ -0,0 +1,89 @@
|
||||
#ifndef MATH_H
|
||||
#define MATH_H
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <cstdint>
|
||||
|
||||
#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
|
136
03_monopoly/player.cpp
Normal file
136
03_monopoly/player.cpp
Normal file
@ -0,0 +1,136 @@
|
||||
#include "player.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
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() {
|
||||
}
|
94
03_monopoly/player.h
Normal file
94
03_monopoly/player.h
Normal file
@ -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
|
56
03_monopoly/player_loader.cpp
Normal file
56
03_monopoly/player_loader.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
#include "player_loader.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include "player.h"
|
||||
|
||||
Vector<Player *> PlayerLoader::load_player_file(const String &file_name) {
|
||||
Vector<Player *> 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;
|
||||
}
|
13
03_monopoly/player_loader.h
Normal file
13
03_monopoly/player_loader.h
Normal file
@ -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<Player *> load_player_file(const String &file_name);
|
||||
};
|
||||
|
||||
#endif
|
6
03_monopoly/players.config
Normal file
6
03_monopoly/players.config
Normal file
@ -0,0 +1,6 @@
|
||||
Player 10000 Jaj
|
||||
AgressivePlayer 20000 FFF
|
||||
ConservativePlayer 20000 Acq
|
||||
TrickyPlayer 20000 dqd
|
||||
HumanPlayer 20000 WFa
|
||||
CheatingPlayer 20000 Fqqf
|
327
03_monopoly/string.cpp
Normal file
327
03_monopoly/string.cpp
Normal file
@ -0,0 +1,327 @@
|
||||
#include "string.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
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<uint32_t>(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);
|
||||
}
|
72
03_monopoly/string.h
Normal file
72
03_monopoly/string.h
Normal file
@ -0,0 +1,72 @@
|
||||
#ifndef STRING_H
|
||||
#define STRING_H
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
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
|
140
03_monopoly/tile.cpp
Normal file
140
03_monopoly/tile.cpp
Normal file
@ -0,0 +1,140 @@
|
||||
#include "tile.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
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() {
|
||||
}
|
109
03_monopoly/tile.h
Normal file
109
03_monopoly/tile.h
Normal file
@ -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
|
53
03_monopoly/tile_loader.cpp
Normal file
53
03_monopoly/tile_loader.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include "tile_loader.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include "tile.h"
|
||||
|
||||
Vector<Tile *> TileLoader::load_tile_file(const String &file_name) {
|
||||
Vector<Tile *> 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;
|
||||
}
|
13
03_monopoly/tile_loader.h
Normal file
13
03_monopoly/tile_loader.h
Normal file
@ -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<Tile *> load_tile_file(const String &file_name);
|
||||
};
|
||||
|
||||
#endif
|
6
03_monopoly/tiles.config
Normal file
6
03_monopoly/tiles.config
Normal file
@ -0,0 +1,6 @@
|
||||
Tile Asf
|
||||
TaxTile aAA
|
||||
OwnableTile fqfqfdg
|
||||
GainTile fffqq
|
||||
LuckTile Qewew
|
||||
JailTile Fqqfq
|
203
03_monopoly/vector.h
Normal file
203
03_monopoly/vector.h
Normal file
@ -0,0 +1,203 @@
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H
|
||||
|
||||
template <class T>
|
||||
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<T> &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 <class T>
|
||||
void Vector<T>::push_back(const T &element) {
|
||||
ensure_capacity(_size + 1);
|
||||
|
||||
_data[_size++] = element;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::pop_back() {
|
||||
if (_size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
--_size;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::remove(const int index) {
|
||||
_data[index] = _data[_size - 1];
|
||||
|
||||
--_size;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::erase(const T &element) {
|
||||
int index = find(element);
|
||||
|
||||
if (index != -1) {
|
||||
remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::clear() {
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool Vector<T>::empty() const {
|
||||
return _size == 0;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T Vector<T>::get(const int index) {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
const T &Vector<T>::get(const int index) const {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::set(const int index, const T &value) {
|
||||
_data[index] = value;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int Vector<T>::size() const {
|
||||
return _size;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int Vector<T>::capacity() const {
|
||||
return _actual_size;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::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 <class T>
|
||||
void Vector<T>::resize(const int s) {
|
||||
ensure_capacity(s);
|
||||
|
||||
_size = s;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::append_array(const Vector<T> &other) {
|
||||
ensure_capacity(_size + other._size);
|
||||
|
||||
for (int i = 0; i < other._size; ++i) {
|
||||
_data[_size++] = other._data[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int Vector<T>::find(const T &val) const {
|
||||
for (int i = 0; i < _size; ++i) {
|
||||
if (_data[i] == val) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int *Vector<T>::dataw() {
|
||||
return _data;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
const int *Vector<T>::data() const {
|
||||
return _data;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
const T &Vector<T>::operator[](const int index) const {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T &Vector<T>::operator[](const int index) {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Vector<T>::Vector() {
|
||||
_data = nullptr;
|
||||
_actual_size = 0;
|
||||
_size = 0;
|
||||
_grow_by = 100;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Vector<T>::Vector(int prealloc) {
|
||||
_data = nullptr;
|
||||
_actual_size = 0;
|
||||
_size = 0;
|
||||
_grow_by = 100;
|
||||
|
||||
ensure_capacity(prealloc);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Vector<T>::Vector(int prealloc, int grow_by) {
|
||||
_data = nullptr;
|
||||
_actual_size = 0;
|
||||
_size = 0;
|
||||
_grow_by = grow_by;
|
||||
|
||||
ensure_capacity(prealloc);
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user