From dc8e7816d5893f041fa2109a2b59d9dd1c655d74 Mon Sep 17 00:00:00 2001 From: IlyaV Date: Sat, 22 Oct 2022 08:18:20 +0000 Subject: [PATCH 1/9] first commit --- homework_01/task_01/src/utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework_01/task_01/src/utils.cpp b/homework_01/task_01/src/utils.cpp index a6989bd..75a6ca9 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -3,5 +3,5 @@ #include std::vector SplitString(const std::string& data) { - return {}; + return {data}; } From 22da209a76dd2a56824dcbf41dbe19444018dfa6 Mon Sep 17 00:00:00 2001 From: IlyaV Date: Sat, 22 Oct 2022 08:55:10 +0000 Subject: [PATCH 2/9] task 1 done --- homework_01/task_01/src/main.cpp | 3 ++- homework_01/task_01/src/utils.cpp | 36 ++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/homework_01/task_01/src/main.cpp b/homework_01/task_01/src/main.cpp index 0b3dcb2..eab7b15 100644 --- a/homework_01/task_01/src/main.cpp +++ b/homework_01/task_01/src/main.cpp @@ -3,8 +3,9 @@ #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/utils.cpp b/homework_01/task_01/src/utils.cpp index 75a6ca9..4b0193d 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -2,6 +2,36 @@ #include -std::vector SplitString(const std::string& data) { - return {data}; -} +/*std::vector SplitString(const std::string& data) { + return {}; +}*/ + +std::vector SplitString(const std::string &str){ + std::vector braces; + std::vector res; + std::string temp; + for (auto c : str){ + if ((c!=' ') and (c!='\t')){ + 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; +} \ No newline at end of file From 16bb07089c2813b5bf6cf150deacc7cfb5f48f1c Mon Sep 17 00:00:00 2001 From: IlyaV Date: Sat, 22 Oct 2022 09:13:49 +0000 Subject: [PATCH 3/9] second task --- homework_01/task_02/src/utils.cpp | 143 +++++++++++++++++++++++++++++- homework_01/task_02/src/utils.hpp | 2 +- 2 files changed, 142 insertions(+), 3 deletions(-) diff --git a/homework_01/task_02/src/utils.cpp b/homework_01/task_02/src/utils.cpp index ebc7ffc..2cf6019 100644 --- a/homework_01/task_02/src/utils.cpp +++ b/homework_01/task_02/src/utils.cpp @@ -2,6 +2,145 @@ #include -int Calculate(const std::string& data) { - return 0; +auto split_string(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 prep_string(std::string str){ + auto vect = split_string(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, '~'); + + 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, '~'); + + final_vals.push_back(temp_value); + num_of_operations -= 1; + } + } + } + return values.at(0); +} + + + +std::string brace_clipper(std::string str){ + std::string f_str; + for (int i = 1; i < str.size()-1; ++i){ + f_str+=str[i]; + } + return f_str; +} + + +//calculator +float Calculate(const std::string &str){ + auto vect = split_string(str); + 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); + + //braces + if ((el[0] == '(') || (el[0] == '{') || (el[0] == '[')){ + values.push_back(Calculate(brace_clipper(el))); + flags.push_back('~'); + } + //other cases + 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); +} \ No newline at end of file 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); From 48aed4b2c5f55eaa8035700f8e7c7462578c60fb Mon Sep 17 00:00:00 2001 From: IlyaV Date: Mon, 24 Oct 2022 15:44:52 +0000 Subject: [PATCH 4/9] test --- homework_01/task_02/src/utils.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homework_01/task_02/src/utils.cpp b/homework_01/task_02/src/utils.cpp index 2cf6019..43cbf42 100644 --- a/homework_01/task_02/src/utils.cpp +++ b/homework_01/task_02/src/utils.cpp @@ -143,4 +143,5 @@ float Calculate(const std::string &str){ } } return conductor(values, flags, num_of_operations); -} \ No newline at end of file +} + From fcd4fcabae2e8ecc1db5a478fecd7dfc728d0c19 Mon Sep 17 00:00:00 2001 From: IlyaV Date: Sat, 12 Nov 2022 08:02:25 +0000 Subject: [PATCH 5/9] homework 1 and 2 --- homework_01/task_01/src/main.cpp | 1 - homework_01/task_01/src/utils.cpp | 49 +++--- homework_01/task_02/src/utils.cpp | 242 +++++++++++++++--------------- homeworks_cpp | 1 + 4 files changed, 141 insertions(+), 152 deletions(-) create mode 160000 homeworks_cpp diff --git a/homework_01/task_01/src/main.cpp b/homework_01/task_01/src/main.cpp index eab7b15..58006e0 100644 --- a/homework_01/task_01/src/main.cpp +++ b/homework_01/task_01/src/main.cpp @@ -1,5 +1,4 @@ #include - #include int main() { diff --git a/homework_01/task_01/src/utils.cpp b/homework_01/task_01/src/utils.cpp index 4b0193d..7fff12f 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -6,32 +6,29 @@ return {}; }*/ -std::vector SplitString(const std::string &str){ - std::vector braces; - std::vector res; - std::string temp; - for (auto c : str){ - if ((c!=' ') and (c!='\t')){ - 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(); - } - } +std::vector SplitString(const std::string &str) { + std::vector braces; + std::vector res; + std::string temp; + for (auto c : str) { + if ((c != ' ') and (c != '\t')) { + temp += c; + } else if (braces.size() != 0) { + temp += c; + } else if (!temp.empty()) { + res.push_back(temp); + temp.clear(); } - if (!temp.empty()){ - res.push_back(temp); + if (c == '(') { + braces.push_back(c); + } else if ((braces.size() > 0) && (c == ')')) { + if (braces.back() == '(') { + braces.pop_back(); + } } - return res; + } + if (!temp.empty()) { + res.push_back(temp); + } + return res; } \ 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 43cbf42..4b04d2a 100644 --- a/homework_01/task_02/src/utils.cpp +++ b/homework_01/task_02/src/utils.cpp @@ -2,146 +2,138 @@ #include -auto split_string(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(); - } - } +auto split_string(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 (!temp.empty()){ - res.push_back(temp); + if (c == '(') { + braces.push_back(c); + } else if ((braces.size() > 0) && (c == ')')) { + if (braces.back() == '(') { + braces.pop_back(); + } } - return res; + } + if (!temp.empty()) { + res.push_back(temp); + } + return res; } -std::string prep_string(std::string str){ - auto vect = split_string(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; - } +std::string prep_string(std::string str) { + auto vect = split_string(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; + } + 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, '~'); - - 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, '~'); - - final_vals.push_back(temp_value); - num_of_operations -= 1; - } +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); } - } - return values.at(0); -} + 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, '~'); -std::string brace_clipper(std::string str){ - std::string f_str; - for (int i = 1; i < str.size()-1; ++i){ - f_str+=str[i]; + final_vals.push_back(temp_value); + num_of_operations -= 1; + } } - return f_str; -} - + // + - + 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); + } -//calculator -float Calculate(const std::string &str){ - auto vect = split_string(str); - float result = 0; - std::vector values; - std::vector flags; - int num_of_operations = 0; + 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, '~'); - for (int i = 0; i < vect.size(); ++i){ - std::string el = vect.at(i); - - //braces - if ((el[0] == '(') || (el[0] == '{') || (el[0] == '[')){ - values.push_back(Calculate(brace_clipper(el))); - flags.push_back('~'); - } - //other cases - 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('~'); - } + final_vals.push_back(temp_value); + num_of_operations -= 1; + } } - return conductor(values, flags, num_of_operations); + } + return values.at(0); +} + +std::string brace_clipper(std::string str) { + std::string f_str; + for (int i = 1; i < str.size() - 1; ++i) { + f_str += str[i]; + } + return f_str; } +// calculator +float Calculate(const std::string &str) { + auto vect = split_string(str); + 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); + + // braces + if ((el[0] == '(') || (el[0] == '{') || (el[0] == '[')) { + values.push_back(Calculate(brace_clipper(el))); + flags.push_back('~'); + } + // other cases + 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/homeworks_cpp b/homeworks_cpp new file mode 160000 index 0000000..8989e68 --- /dev/null +++ b/homeworks_cpp @@ -0,0 +1 @@ +Subproject commit 8989e68a04e4c21c40bcc95d5208b6591debfb41 From e5953371a7e35c634ff748c3df6ab3316e67aaf3 Mon Sep 17 00:00:00 2001 From: IlyaV Date: Wed, 23 Nov 2022 14:28:37 +0000 Subject: [PATCH 6/9] 23_11_2022 --- homework_01/task_01/src/utils.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/homework_01/task_01/src/utils.cpp b/homework_01/task_01/src/utils.cpp index 7fff12f..2d7dfc3 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -8,15 +8,15 @@ std::vector SplitString(const std::string &str) { std::vector braces; - std::vector res; + std::vector result; std::string temp; for (auto c : str) { - if ((c != ' ') and (c != '\t')) { + if (c != ' ' && c != '\t') { temp += c; } else if (braces.size() != 0) { temp += c; } else if (!temp.empty()) { - res.push_back(temp); + result.push_back(temp); temp.clear(); } if (c == '(') { @@ -28,7 +28,7 @@ std::vector SplitString(const std::string &str) { } } if (!temp.empty()) { - res.push_back(temp); + result.push_back(temp); } - return res; + return result; } \ No newline at end of file From f202cf63167e739262039b5982b82aa47effe2f2 Mon Sep 17 00:00:00 2001 From: IlyaV Date: Sat, 10 Dec 2022 09:26:39 +0000 Subject: [PATCH 7/9] for pr review --- homework_01/task_01/src/split_string_test.cpp | 3 ++- homework_01/task_01/src/utils.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) 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 2d7dfc3..f27bb9c 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -21,7 +21,7 @@ std::vector SplitString(const std::string &str) { } if (c == '(') { braces.push_back(c); - } else if ((braces.size() > 0) && (c == ')')) { + } else if (braces.size() > 0 && c == ')') { if (braces.back() == '(') { braces.pop_back(); } From 366022b006853b551eec150781d7d4bec23f6eea Mon Sep 17 00:00:00 2001 From: IlyaV Date: Wed, 14 Dec 2022 15:38:43 +0000 Subject: [PATCH 8/9] f r a final evaluationn --- homework_01/task_02/src/utils.cpp | 37 ++++++++++++------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/homework_01/task_02/src/utils.cpp b/homework_01/task_02/src/utils.cpp index 4b04d2a..f213b0b 100644 --- a/homework_01/task_02/src/utils.cpp +++ b/homework_01/task_02/src/utils.cpp @@ -2,7 +2,7 @@ #include -auto split_string(const std::string &str) { +auto SplitString(const std::string &str) { std::vector braces; std::vector res; std::string temp; @@ -29,8 +29,8 @@ auto split_string(const std::string &str) { return res; } -std::string prep_string(std::string str) { - auto vect = split_string(str); +std::string PrepString(std::string str) { + auto vect = SplitString(str); std::string temp_str; for (auto i : vect) { temp_str += i; @@ -49,15 +49,13 @@ std::string prep_string(std::string str) { } return final_str; } - -float conductor(std::vector values, std::vector flags, +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) == '*') { @@ -71,12 +69,12 @@ float conductor(std::vector values, std::vector flags, 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) == '+') { @@ -91,6 +89,7 @@ float conductor(std::vector values, std::vector flags, 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; @@ -100,17 +99,13 @@ float conductor(std::vector values, std::vector flags, return values.at(0); } -std::string brace_clipper(std::string str) { - std::string f_str; - for (int i = 1; i < str.size() - 1; ++i) { - f_str += str[i]; - } - return f_str; +std::string BraceClipper(const std::string &str) { + return str.substr(1, str.size() - 2); } -// calculator float Calculate(const std::string &str) { - auto vect = split_string(str); + std::string str1 = PrepString(str); + auto vect = SplitString(str1); float result = 0; std::vector values; std::vector flags; @@ -118,15 +113,11 @@ float Calculate(const std::string &str) { for (int i = 0; i < vect.size(); ++i) { std::string el = vect.at(i); - - // braces if ((el[0] == '(') || (el[0] == '{') || (el[0] == '[')) { - values.push_back(Calculate(brace_clipper(el))); + values.push_back(Calculate(BraceClipper(el))); flags.push_back('~'); - } - // other cases - else if ((el[0] == '+') || (el[0] == '-') || (el[0] == '*') || - (el[0] == '/')) { + } else if ((el[0] == '+') || (el[0] == '-') || (el[0] == '*') || + (el[0] == '/')) { flags.push_back(el[0]); values.push_back(0); num_of_operations += 1; @@ -135,5 +126,5 @@ float Calculate(const std::string &str) { flags.push_back('~'); } } - return conductor(values, flags, num_of_operations); + return Conductor(values, flags, num_of_operations); } From 295a9bd66b199fafb34f121c50252f9b48437769 Mon Sep 17 00:00:00 2001 From: IlyaV Date: Wed, 14 Dec 2022 15:57:07 +0000 Subject: [PATCH 9/9] f added Noughts and Crosses --- .../Noughts_and_Crosses.cpp/NandCGame.cpp | 237 ++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 homework_01/Noughts_and_Crosses.cpp/NandCGame.cpp 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