diff --git a/homework_01/Noughts_and_Crosses.cpp/NandCGame.cpp b/homework_01/Noughts_and_Crosses.cpp/NandCGame.cpp new file mode 100644 index 0000000..7bcc5f1 --- /dev/null +++ b/homework_01/Noughts_and_Crosses.cpp/NandCGame.cpp @@ -0,0 +1,237 @@ +#include +#include +#include + +#include +#include +#include + +// enumerations +enum class Field { Free, Nought, Cross }; + +enum class GameState { Running, Draw, N_wins, C_wins }; + +// functions +int Randint(int min, int max) { return rand() % (max - min + 1) + min; } + +std::vector SplitString(const std::string& str) { + std::vector braces; + std::vector result; + std::string temp; + for (auto c : str) { + if (c != ' ' && c != '\t') { + temp += c; + } else if (braces.size() != 0) { + temp += c; + } else if (!temp.empty()) { + result.push_back(temp); + temp.clear(); + } + if (c == '(') { + braces.push_back(c); + } else if (braces.size() > 0 && c == ')') { + if (braces.back() == '(') { + braces.pop_back(); + } + } + } + if (!temp.empty()) { + result.push_back(temp); + } + return result; +} + +std::string GStoS(GameState f) { + if (f == GameState::Running) { + return "Running"; + } else if (f == GameState::Draw) { + return "Draw"; + } else if (f == GameState::N_wins) { + return "Noughts win"; + } else if (f == GameState::C_wins) { + return "Crosses win"; + } +} + +class NandCGrid { + public: + Field Get(int x, int y) const { return m_grid[x][y]; } + void Set(int x, int y, Field f) { m_grid[x][y] = f; } + GameState GetState() const { + if ((m_grid[0][0] == m_grid[1][1]) && (m_grid[1][1] == m_grid[2][2])) { + if (m_grid[0][0] == Field::Nought) { + return GameState::N_wins; + } else if (m_grid[0][0] == Field::Cross) { + return GameState::C_wins; + } else { + return GameState::Running; + } + } + if ((m_grid[0][2] == m_grid[1][1]) && (m_grid[1][1] == m_grid[2][0])) { + if (m_grid[0][2] == Field::Nought) { + return GameState::N_wins; + } else if (m_grid[0][2] == Field::Cross) { + return GameState::C_wins; + } else { + return GameState::Running; + } + } + for (int i = 0; i < 3; ++i) { + if ((m_grid[i][0] == m_grid[i][1]) && (m_grid[i][1] == m_grid[i][2])) { + if (m_grid[i][0] == Field::Nought) { + return GameState::N_wins; + } else if (m_grid[i][0] == Field::Cross) { + return GameState::C_wins; + } else { + return GameState::Running; + } + } else if ((m_grid[0][i] == m_grid[1][i]) && + (m_grid[1][i] == m_grid[2][i])) { + if (m_grid[0][i] == Field::Nought) { + return GameState::N_wins; + } else if (m_grid[0][i] == Field::Cross) { + return GameState::C_wins; + } else { + return GameState::Running; + } + } + } + for (int i = 0; i < 3; ++i) { + int f_cnt = 0; + int n_cnt = 0; + int c_cnt = 0; + for (int j = 0; j < 3; ++j) { + if (m_grid[i][j] == Field::Free) { + f_cnt += 1; + } else if (m_grid[i][j] == Field::Nought) { + n_cnt += 1; + } else { + n_cnt += 1; + } + } + if (f_cnt >= 2) { + return GameState::Running; + } else if ((n_cnt == 2) || (c_cnt == 2)) { + return GameState::Running; + } + + f_cnt = 0; + n_cnt = 0; + c_cnt = 0; + for (int j = 0; j < 3; ++j) { + if (m_grid[j][i] == Field::Free) { + f_cnt += 1; + } else if (m_grid[j][i] == Field::Nought) { + n_cnt += 1; + } else { + n_cnt += 1; + } + } + if (f_cnt >= 2) { + return GameState::Running; + } else if ((n_cnt == 2) || (c_cnt == 2)) { + return GameState::Running; + } + } + return GameState::Draw; + } + std::string ToString() const { + std::string field; + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + if (m_grid[i][j] == Field::Free) { + field += " * "; + } else if (m_grid[i][j] == Field::Cross) { + field += " x "; + } else { + field += " o "; + } + } + field += "\n"; + } + return field; + } + + friend std::ostream& operator<<(std::ostream& stream, NandCGrid& grid); + + private: + Field m_grid[3][3]; +}; + +std::ostream& operator<<(std::ostream& stream, NandCGrid& grid) { + stream << grid.ToString(); + return stream; +} + +class Player { + public: + Player(std::string name, Field f) { + m_type = f; + m_name = name; + } + std::pair p; + + bool MakeMove(NandCGrid& grid) { + std::cout << grid << "\n"; + std::cout << m_name << " is making a move: "; + while (true) { + std::string responce; + std::getline(std::cin, responce); + std::vector vect = SplitString(responce); + std::pair p; + p.first = std::stoi(vect.at(0)) - 1; + p.second = std::stoi(vect.at(1)) - 1; + if (grid.Get(p.first, p.second) == Field::Free) { + grid.Set(p.first, p.second, m_type); + return false; + } else { + std::cout << "This cell is already assigned\n"; + std::cout << grid << "\n"; + } + } + } + + private: + Field m_type; + std::string m_name; +}; + +class NandCGame { + public: + NandCGame() { + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + m_grid.Set(i, j, Field::Free); + } + } + } + + void Run() { + std::cout << "Enter participants (robot or human):\n"; + std::string responce; + std::getline(std::cin, responce); + std::vector vect = SplitString(responce); + Player pl1("pl1", Field::Cross); + Player pl2("pl2", Field::Nought); + while (m_grid.GetState() == GameState::Running) { + pl1.MakeMove(m_grid); + if (m_grid.GetState() == GameState::Running) { + pl2.MakeMove(m_grid); + std::cout << GStoS(m_grid.GetState()) << "\n"; + continue; + } + std::cout << GStoS(m_grid.GetState()) << "\n"; + } + std::cout << m_grid; + } + + private: + NandCGrid m_grid; +}; + +int main() { + std::cout << std::endl; + NandCGame a; + a.Run(); + return 0; +} \ No newline at end of file diff --git a/homework_01/task_01/src/main.cpp b/homework_01/task_01/src/main.cpp index 0b3dcb2..58006e0 100644 --- a/homework_01/task_01/src/main.cpp +++ b/homework_01/task_01/src/main.cpp @@ -1,10 +1,10 @@ #include - #include int main() { - for (const auto& word : SplitString("asdas das das fgag (adasd 1fas)")) { + /*for (const auto& word : SplitString("asdas das das fgag (adasd 1fas)")) { std::cout << word << "\n"; } + */ return 0; } diff --git a/homework_01/task_01/src/split_string_test.cpp b/homework_01/task_01/src/split_string_test.cpp index 82d4994..c501970 100644 --- a/homework_01/task_01/src/split_string_test.cpp +++ b/homework_01/task_01/src/split_string_test.cpp @@ -11,5 +11,6 @@ TEST_CASE("SplitString", "[simple]") { CHECK(SplitString("??? ??") == std::vector{"???", "??"}); CHECK(SplitString("a\na\ta a") == std::vector{"a\na", "a", "a"}); CHECK(SplitString("a (a a)") == std::vector{"a", "(a a)"}); - CHECK(SplitString("a (a a) b (asd as) ") == std::vector{"a", "(a a)", "b", "(asd as)"}); + CHECK(SplitString("a (a a) b (asd as) ") == + std::vector{"a", "(a a)", "b", "(asd as)"}); } diff --git a/homework_01/task_01/src/utils.cpp b/homework_01/task_01/src/utils.cpp index a6989bd..f27bb9c 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -2,6 +2,33 @@ #include -std::vector SplitString(const std::string& data) { +/*std::vector SplitString(const std::string& data) { return {}; -} +}*/ + +std::vector SplitString(const std::string &str) { + std::vector braces; + std::vector result; + std::string temp; + for (auto c : str) { + if (c != ' ' && c != '\t') { + temp += c; + } else if (braces.size() != 0) { + temp += c; + } else if (!temp.empty()) { + result.push_back(temp); + temp.clear(); + } + if (c == '(') { + braces.push_back(c); + } else if (braces.size() > 0 && c == ')') { + if (braces.back() == '(') { + braces.pop_back(); + } + } + } + if (!temp.empty()) { + result.push_back(temp); + } + return result; +} \ No newline at end of file diff --git a/homework_01/task_02/src/utils.cpp b/homework_01/task_02/src/utils.cpp index ebc7ffc..f213b0b 100644 --- a/homework_01/task_02/src/utils.cpp +++ b/homework_01/task_02/src/utils.cpp @@ -2,6 +2,129 @@ #include -int Calculate(const std::string& data) { - return 0; +auto SplitString(const std::string &str) { + std::vector braces; + std::vector res; + std::string temp; + for (auto c : str) { + if (c != ' ') { + temp += c; + } else if (braces.size() != 0) { + temp += c; + } else if (!temp.empty()) { + res.push_back(temp); + temp.clear(); + } + if (c == '(') { + braces.push_back(c); + } else if ((braces.size() > 0) && (c == ')')) { + if (braces.back() == '(') { + braces.pop_back(); + } + } + } + if (!temp.empty()) { + res.push_back(temp); + } + return res; +} + +std::string PrepString(std::string str) { + auto vect = SplitString(str); + std::string temp_str; + for (auto i : vect) { + temp_str += i; + } + + std::string final_str; + + for (char i : temp_str) { + if ((i == '*') || (i == '/') || (i == '-') || (i == '+')) { + final_str += ' '; + final_str += i; + final_str += ' '; + } else { + final_str += i; + } + } + return final_str; +} +float Conductor(std::vector values, std::vector flags, + int num_of_operations) { + int i = 0; + std::vector final_vals; + int index = 0; + float temp_value = 0; + while (num_of_operations > 0) { + for (int i = 0; i < flags.size(); ++i) { + if ((flags.at(i) == '*') || (flags.at(i) == '/')) { + if (flags.at(i) == '*') { + temp_value = values.at(i - 1) * values.at(i + 1); + } else { + temp_value = values.at(i - 1) / values.at(i + 1); + } + + values.erase(values.begin() + i - 1, values.begin() + i + 2); + values.insert(values.begin() + i - 1, temp_value); + + flags.erase(flags.begin() + i - 1, flags.begin() + i + 2); + flags.insert(flags.begin() + i - 1, '~'); + i -= 2; + + final_vals.push_back(temp_value); + num_of_operations -= 1; + } + } + for (int i = 0; i < flags.size(); ++i) { + if ((flags.at(i) == '+') || (flags.at(i) == '-')) { + if (flags.at(i) == '+') { + temp_value = values.at(i - 1) + values.at(i + 1); + // std::cout << "this: " << temp_value << std::endl; + } else { + temp_value = values.at(i - 1) - values.at(i + 1); + } + + values.erase(values.begin() + i - 1, values.begin() + i + 2); + values.insert(values.begin() + i - 1, temp_value); + + flags.erase(flags.begin() + i - 1, flags.begin() + i + 2); + flags.insert(flags.begin() + i - 1, '~'); + i -= 2; + + final_vals.push_back(temp_value); + num_of_operations -= 1; + } + } + } + return values.at(0); +} + +std::string BraceClipper(const std::string &str) { + return str.substr(1, str.size() - 2); +} + +float Calculate(const std::string &str) { + std::string str1 = PrepString(str); + auto vect = SplitString(str1); + float result = 0; + std::vector values; + std::vector flags; + int num_of_operations = 0; + + for (int i = 0; i < vect.size(); ++i) { + std::string el = vect.at(i); + if ((el[0] == '(') || (el[0] == '{') || (el[0] == '[')) { + values.push_back(Calculate(BraceClipper(el))); + flags.push_back('~'); + } else if ((el[0] == '+') || (el[0] == '-') || (el[0] == '*') || + (el[0] == '/')) { + flags.push_back(el[0]); + values.push_back(0); + num_of_operations += 1; + } else { + values.push_back(std::stof(el)); + flags.push_back('~'); + } + } + return Conductor(values, flags, num_of_operations); } diff --git a/homework_01/task_02/src/utils.hpp b/homework_01/task_02/src/utils.hpp index f1bceeb..48e58e9 100644 --- a/homework_01/task_02/src/utils.hpp +++ b/homework_01/task_02/src/utils.hpp @@ -4,4 +4,4 @@ #include #include -int Calculate(const std::string& data); +float Calculate(const std::string& data); diff --git a/homeworks_cpp b/homeworks_cpp new file mode 160000 index 0000000..8989e68 --- /dev/null +++ b/homeworks_cpp @@ -0,0 +1 @@ +Subproject commit 8989e68a04e4c21c40bcc95d5208b6591debfb41