Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming
diff options
context:
space:
mode:
Commite9f62e28a4de3f9051da9b437bd6130328de2d90 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-05 21:32:06 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-05 21:32:06 +0300
Files4a78d585bf59f69f5070a0ddd7b8d5c731982012 /Year 1/Object-oriented Programming
uni-projects-e9f62e28a4de3f9051da9b437bd6130328de2d90.tar
uni-projects-e9f62e28a4de3f9051da9b437bd6130328de2d90.tar.gz
uni-projects-e9f62e28a4de3f9051da9b437bd6130328de2d90.zip
Parent51ff352a533ff14e9961deb642d110f2d5dc843c (diff)
Added todays oop practice work
diff --git a/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Game.cpp b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Game.cpp
new file mode 100644
index 0000000..ae5fcc0
--- /dev/null
+++ b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Game.cpp
@@ -0,0 +1,37 @@
+#include "Game.h"
+
+/* Private */
+
+void Game::turn(Player& player, double& bet) {
+ std::cout << "Enter command: ";
+ char buffer;
+ std::cin >> buffer;
+
+ bool result = false;
+ for (int i = 0; i < this->caseCount; i++) {
+ if (cases[i].get_comm() == buffer) {
+ result = cases[i].exec(player, bet);
+ break;
+ }
+ }
+
+ if (result) {
+ std::cout << "You win!";
+ player.addToBalance("", bet);
+ }
+ else {
+ std::cout << "You lost!";
+ player.deductFromBalance("", bet);
+ }
+ std::cout << " Current balance: " << player.get_balance() << std::endl;
+}
+
+/* Public */
+
+Game::Game(const GameCase* cases, unsigned casesCount) {
+ this->cases = new GameCase[casesCount];
+ for (int i = 0; i < casesCount; i++)
+ this->cases[i] = cases[i];
+}
+
+
diff --git a/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Game.h b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Game.h
new file mode 100644
index 0000000..96feeaf
--- /dev/null
+++ b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Game.h
@@ -0,0 +1,19 @@
+#include <iostream>
+#include "Player.h"
+#include "GameCase.h"
+
+class Game {
+ GameCase* cases;
+ unsigned caseCount;
+
+ void turn(Player&, double&);
+
+public:
+ Game(const GameCase*, unsigned);
+
+ Game(const Game&);
+ Game& operator=(const Game&);
+ ~Game();
+
+ void play(Player&);
+};
diff --git a/Year 1/Object-oriented Programming/06-Practice-05.04.2022/GameCase.cpp b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/GameCase.cpp
new file mode 100644
index 0000000..713c94c
--- /dev/null
+++ b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/GameCase.cpp
@@ -0,0 +1,13 @@
+#include "GameCase.h"
+
+GameCase::GameCase() : GameCase('\0', nullptr)
+{}
+
+GameCase::GameCase(char command, bool func(const Player&, double&)) {
+ this->command = command;
+ this->func = func;
+}
+
+bool GameCase::exec(const Player& player, double& bet) const {
+ return this->func(player, bet);
+}
diff --git a/Year 1/Object-oriented Programming/06-Practice-05.04.2022/GameCase.h b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/GameCase.h
new file mode 100644
index 0000000..bf5030f
--- /dev/null
+++ b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/GameCase.h
@@ -0,0 +1,13 @@
+#include "Player.h"
+
+class GameCase {
+ char command;
+ bool (*func)(const Player&, double&);
+
+public:
+ GameCase();
+ GameCase(char, bool f(const Player&, double&));
+
+ char get_comm() const;
+ bool exec(const Player&, double&) const;
+};
diff --git a/Year 1/Object-oriented Programming/06-Practice-05.04.2022/NumberHelpers.h b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/NumberHelpers.h
new file mode 100644
index 0000000..2e1050b
--- /dev/null
+++ b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/NumberHelpers.h
@@ -0,0 +1,3 @@
+bool isEven(int);
+bool isPrimary(int);
+bool isPowerOfTwo(int);
diff --git a/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Player.cpp b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Player.cpp
new file mode 100644
index 0000000..eaca839
--- /dev/null
+++ b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Player.cpp
@@ -0,0 +1,103 @@
+#include "Player.h"
+#include <cstring>
+
+/* Private */
+
+void Player::copyFrom(const Player& other) {
+ this->name = new char[strlen(other.name) + 1];
+ strcpy(this->name, other.name);
+ this->pass = new char[strlen(other.pass) + 1];
+ strcpy(this->pass, other.pass);
+ this->age = other.age;
+ this->balance = other.balance;
+}
+
+void Player::free() const {
+ delete[] this->name;
+ delete[] this->pass;
+}
+
+bool Player::isValidPassword(const char* passwordAttempt) {
+ return strcmp(this->pass, passwordAttempt) == 0;
+}
+
+/* Public */
+
+Player::Player(const char* name, const char* pass, unsigned age, double currentBalance) {
+ this->name = new char[strlen(name) + 1];
+ strcpy(this->name, name);
+ this->pass = new char[strlen(pass) + 1];
+ strcpy(this->pass, pass);
+ this->age = age;
+ this->balance = balance;
+}
+
+Player::Player(const Player& other) {
+ this->copyFrom(other);
+}
+
+Player& Player::operator=(const Player& other) {
+ if (this != &other) {
+ this->free();
+ this->copyFrom(other);
+ }
+ return *this;
+}
+
+Player::~Player() {
+ this->free();
+}
+
+const char* Player::get_name() const {
+ return this->name;
+}
+bool Player::set_name(const char* password, const char* name) {
+ if (!this->isValidPassword(password))
+ return false;
+
+ delete[] this->name;
+ this->name = new char[strlen(name) + 1];
+ strcpy(this->name, name);
+ return true;
+}
+
+bool Player::set_password(const char* password, const char* newPassword) {
+ if (!this->isValidPassword(password))
+ return false;
+
+ delete[] this->pass;
+ this->pass = new char[strlen(newPassword) + 1];
+ strcpy(this->pass, newPassword);
+ return true;
+}
+
+unsigned Player::get_age() const {
+ return this->age;
+}
+bool Player::set_age(const char* password, unsigned value) {
+ if (!this->isValidPassword(password))
+ return false;
+
+ this->age = value;
+ return true;
+}
+
+double Player::get_balance() const {
+ return this->balance;
+}
+
+bool Player::addToBalance(const char* password, double value) {
+ if (!this->isValidPassword(password))
+ return false;
+
+ this->balance += value;
+ return true;
+}
+
+bool Player::deductFromBalance(const char* password, double value) {
+ if (!this->isValidPassword(password))
+ return false;
+
+ this->balance -= value;
+ return true;
+}
diff --git a/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Player.h b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Player.h
new file mode 100644
index 0000000..2001c2b
--- /dev/null
+++ b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/Player.h
@@ -0,0 +1,34 @@
+#ifndef PLAYER_HEADER
+#define PLAYER_HEADER
+
+class Player {
+ char* name;
+ char* pass;
+ unsigned age;
+ double balance;
+
+ void copyFrom(const Player&);
+ void free() const;
+
+ bool isValidPassword(const char*);
+public:
+ Player(const char*, const char*, unsigned, double);
+
+ Player(const Player&);
+ Player& operator=(const Player&);
+ ~Player();
+
+ const char* get_name() const;
+ bool set_name(const char*, const char*);
+
+ bool set_password(const char*, const char*);
+
+ unsigned get_age() const;
+ bool set_age(const char*, unsigned value);
+
+ double get_balance() const;
+ bool addToBalance(const char*, double);
+ bool deductFromBalance(const char*, double);
+};
+
+#endif
diff --git a/Year 1/Object-oriented Programming/06-Practice-05.04.2022/source.cpp b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/source.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Year 1/Object-oriented Programming/06-Practice-05.04.2022/source.cpp