From 7f348095cecd9e4895caf69d6c52311681bbf8ee Mon Sep 17 00:00:00 2001 From: Vycheslav Zhdanovsky Date: Mon, 16 Mar 2020 21:44:46 +0300 Subject: [PATCH 1/5] Add refactored C++ version --- CMakeLists.txt | 6 +++++ src/cpp/game.cpp | 49 ++++++++++++++++++++++++++++++++++++ src/cpp/game.hpp | 23 +++++++++++++++++ src/cpp/guessed_word.cpp | 40 +++++++++++++++++++++++++++++ src/cpp/guessed_word.hpp | 18 +++++++++++++ src/cpp/main.cpp | 10 ++++++++ src/cpp/mistakes.cpp | 22 ++++++++++++++++ src/cpp/mistakes.hpp | 16 ++++++++++++ src/cpp/progress_display.cpp | 27 ++++++++++++++++++++ src/cpp/progress_display.hpp | 15 +++++++++++ src/cpp/user_input.cpp | 9 +++++++ src/cpp/user_input.hpp | 6 +++++ src/cpp/word_display.cpp | 18 +++++++++++++ src/cpp/word_display.hpp | 16 ++++++++++++ 14 files changed, 275 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 src/cpp/game.cpp create mode 100644 src/cpp/game.hpp create mode 100644 src/cpp/guessed_word.cpp create mode 100644 src/cpp/guessed_word.hpp create mode 100644 src/cpp/main.cpp create mode 100644 src/cpp/mistakes.cpp create mode 100644 src/cpp/mistakes.hpp create mode 100644 src/cpp/progress_display.cpp create mode 100644 src/cpp/progress_display.hpp create mode 100644 src/cpp/user_input.cpp create mode 100644 src/cpp/user_input.hpp create mode 100644 src/cpp/word_display.cpp create mode 100644 src/cpp/word_display.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..cfbb1de --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.15) +project(hangman) + +set(CMAKE_CXX_STANDARD 17) +file(GLOB SOURCES "src/cpp/*.cpp" "src/cpp/*.hpp") +add_executable(hangman ${SOURCES}) \ No newline at end of file diff --git a/src/cpp/game.cpp b/src/cpp/game.cpp new file mode 100644 index 0000000..29bf2fe --- /dev/null +++ b/src/cpp/game.cpp @@ -0,0 +1,49 @@ +// +// Created by zeronsix on 16.03.2020. +// + +#include "game.hpp" + +constexpr size_t MAX_MISTAKES = 5; + +Game::Game() + : m_guessed_word() + , m_mistakes(MAX_MISTAKES) + , m_word_display(m_guessed_word.GetSize()) + , m_user_input() + , m_progress_display(m_mistakes) { +} + +void Game::Play() { + while (!IsOver()) { + char letter = m_user_input.InputLetter(); + auto guessed = m_guessed_word.Guess(letter); + if (guessed.empty()) { + m_mistakes.MakeOne(); + m_progress_display.ShowMiss(); + } else { + m_progress_display.ShowHit(); + } + + m_word_display.Reveal(letter, guessed); + m_word_display.Show(); + } + if (IsWon()) { + m_progress_display.ShowWin(); + } else { + m_progress_display.ShowLoss(); + } +} + +bool Game::IsOver() const { + return IsWon() || IsLost(); +} + +bool Game::IsWon() const { + return m_guessed_word.IsGuessed(); +} + +bool Game::IsLost() const { + return m_mistakes.IsMaxAmountReached(); +} + diff --git a/src/cpp/game.hpp b/src/cpp/game.hpp new file mode 100644 index 0000000..af77d0e --- /dev/null +++ b/src/cpp/game.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "guessed_word.hpp" +#include "mistakes.hpp" +#include "word_display.hpp" +#include "user_input.hpp" +#include "progress_display.hpp" + +class Game { +public: + Game(); + void Play(); +private: + GuessedWord m_guessed_word; + Mistakes m_mistakes; + WordDisplay m_word_display; + UserInput m_user_input; + ProgressDisplay m_progress_display; + + bool IsOver() const; + bool IsWon() const; + bool IsLost() const; +}; diff --git a/src/cpp/guessed_word.cpp b/src/cpp/guessed_word.cpp new file mode 100644 index 0000000..e068cc3 --- /dev/null +++ b/src/cpp/guessed_word.cpp @@ -0,0 +1,40 @@ +#include "guessed_word.hpp" +#include +#include + +static const std::vector WORDS = { + "simplicity", "equality", "grandmother", + "neighborhood", "relationship", "mathematics", + "university", "explanation" +}; + +GuessedWord::GuessedWord() + : m_word(WORDS[std::rand() % WORDS.size()]) + , m_guessed_amount(0) + , m_already_guessed() { +} + +std::vector GuessedWord::Guess(char letter) { + std::vector result; + + for (size_t pos = 0; pos < m_word.size(); ++pos) { + if (m_word[pos] == letter) { + result.push_back(pos); + } + } + + if (m_already_guessed.count(letter) == 0) { + m_already_guessed.insert(letter); + m_guessed_amount += result.size(); + } + + return result; +} + +size_t GuessedWord::GetSize() const { + return m_word.size(); +} + +bool GuessedWord::IsGuessed() const { + return m_guessed_amount == m_word.size(); +} diff --git a/src/cpp/guessed_word.hpp b/src/cpp/guessed_word.hpp new file mode 100644 index 0000000..113a2af --- /dev/null +++ b/src/cpp/guessed_word.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include + +class GuessedWord { +public: + GuessedWord(); + + std::vector Guess(char letter); + size_t GetSize() const; + bool IsGuessed() const; +private: + std::string m_word; + std::size_t m_guessed_amount; + std::set m_already_guessed; +}; \ No newline at end of file diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp new file mode 100644 index 0000000..8c82267 --- /dev/null +++ b/src/cpp/main.cpp @@ -0,0 +1,10 @@ +#include "game.hpp" +#include +#include + +int main() { + std::srand(std::time(nullptr)); + Game().Play(); + + return 0; +} diff --git a/src/cpp/mistakes.cpp b/src/cpp/mistakes.cpp new file mode 100644 index 0000000..ec5fa84 --- /dev/null +++ b/src/cpp/mistakes.cpp @@ -0,0 +1,22 @@ +#include "mistakes.hpp" + +Mistakes::Mistakes(int max_amount) + : m_max_amount(max_amount) + , m_amount(0) { +} + +void Mistakes::MakeOne() { + ++m_amount; +} + +int Mistakes::GetAmount() const { + return m_amount; +} + +int Mistakes::GetMaxAmount() const { + return m_max_amount; +} + +int Mistakes::IsMaxAmountReached() const { + return m_amount >= m_max_amount; +} diff --git a/src/cpp/mistakes.hpp b/src/cpp/mistakes.hpp new file mode 100644 index 0000000..c376e49 --- /dev/null +++ b/src/cpp/mistakes.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include + +class Mistakes { +public: + explicit Mistakes(int max_amount); + + void MakeOne(); + int GetAmount() const; + int GetMaxAmount() const; + int IsMaxAmountReached() const; +private: + int m_max_amount; + int m_amount; +}; diff --git a/src/cpp/progress_display.cpp b/src/cpp/progress_display.cpp new file mode 100644 index 0000000..1d10e59 --- /dev/null +++ b/src/cpp/progress_display.cpp @@ -0,0 +1,27 @@ +// +// Created by zeronsix on 16.03.2020. +// + +#include "progress_display.hpp" +#include + +ProgressDisplay::ProgressDisplay(Mistakes &mistakes) + : m_mistakes(mistakes) { +} + +void ProgressDisplay::ShowHit() const { + std::cout << "Hit!\n"; +} + +void ProgressDisplay::ShowMiss() const { + std::cout << "Missed, mistake " << m_mistakes.GetAmount() << " out of " + << m_mistakes.GetMaxAmount() << "\n"; +} + +void ProgressDisplay::ShowWin() const { + std::cout << "You won!\n"; +} + +void ProgressDisplay::ShowLoss() const { + std::cout << "You lost.\n"; +} diff --git a/src/cpp/progress_display.hpp b/src/cpp/progress_display.hpp new file mode 100644 index 0000000..2070542 --- /dev/null +++ b/src/cpp/progress_display.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "mistakes.hpp" + +class ProgressDisplay { +public: + explicit ProgressDisplay(Mistakes& mistakes); + + void ShowHit() const; + void ShowMiss() const; + void ShowWin() const; + void ShowLoss() const; +private: + Mistakes& m_mistakes; +}; diff --git a/src/cpp/user_input.cpp b/src/cpp/user_input.cpp new file mode 100644 index 0000000..0fef8d8 --- /dev/null +++ b/src/cpp/user_input.cpp @@ -0,0 +1,9 @@ +#include "user_input.hpp" +#include + +char UserInput::InputLetter() const { + char c = 0; + std::cout << "Guess a letter: "; + std::cin >> c; + return c; +} diff --git a/src/cpp/user_input.hpp b/src/cpp/user_input.hpp new file mode 100644 index 0000000..9eb3273 --- /dev/null +++ b/src/cpp/user_input.hpp @@ -0,0 +1,6 @@ +#pragma once + +class UserInput { +public: + char InputLetter() const; +}; diff --git a/src/cpp/word_display.cpp b/src/cpp/word_display.cpp new file mode 100644 index 0000000..f1e1f3b --- /dev/null +++ b/src/cpp/word_display.cpp @@ -0,0 +1,18 @@ +#include "word_display.hpp" +#include + +WordDisplay::WordDisplay(size_t word_size) + : m_buffer(word_size, HIDDEN_LETTER) { + // initialize the buffer with hidden letters +} + +void WordDisplay::Reveal(char letter, const std::vector& positions) { + for (auto pos : positions) { + m_buffer[pos] = letter; + } +} + +void WordDisplay::Show() const { + std::cout << "The word: "; + std::cout << m_buffer << "\n\n"; +} diff --git a/src/cpp/word_display.hpp b/src/cpp/word_display.hpp new file mode 100644 index 0000000..65d004a --- /dev/null +++ b/src/cpp/word_display.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +class WordDisplay { +public: + static constexpr char HIDDEN_LETTER = '?'; + + explicit WordDisplay(size_t word_size); + + void Reveal(char letter, const std::vector& positions); + void Show() const; +private: + std::string m_buffer; +}; From 2adb21c023d533f5f74d9962e20f7736e739a665 Mon Sep 17 00:00:00 2001 From: Vycheslav Zhdanovsky Date: Mon, 16 Mar 2020 21:48:23 +0300 Subject: [PATCH 2/5] Update README.md --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index ffd2d35..56f14c1 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,18 @@ new methods, anything. Just make sure it still builds. When ready, please submit a pull request. +## Refactored object-oriented C++ version +A refactored version written in C++ is located at ``src/cpp/``. +Instructions to build and run (CMake is required): +``` +cmake . +make +./hangman +``` + ## License + The MIT License (MIT) Copyright (c) 2017 Yegor Bugayenko From a04f13b03e820f3ca628db1e0084b5d0a1ffaad4 Mon Sep 17 00:00:00 2001 From: Vycheslav Zhdanovsky Date: Mon, 16 Mar 2020 22:04:32 +0300 Subject: [PATCH 3/5] Fix codestyle --- CMakeLists.txt | 2 +- src/cpp/game.cpp | 53 ++++++++++++++++++------------------ src/cpp/game.hpp | 20 +++++++------- src/cpp/guessed_word.cpp | 38 +++++++++++++------------- src/cpp/guessed_word.hpp | 16 +++++------ src/cpp/main.cpp | 6 ++-- src/cpp/mistakes.cpp | 12 ++++---- src/cpp/mistakes.hpp | 14 +++++----- src/cpp/progress_display.cpp | 14 +++++----- src/cpp/progress_display.hpp | 12 ++++---- src/cpp/user_input.cpp | 8 +++--- src/cpp/user_input.hpp | 2 +- src/cpp/word_display.cpp | 14 +++++----- src/cpp/word_display.hpp | 10 +++---- 14 files changed, 111 insertions(+), 110 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cfbb1de..077cf8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(hangman) set(CMAKE_CXX_STANDARD 17) file(GLOB SOURCES "src/cpp/*.cpp" "src/cpp/*.hpp") -add_executable(hangman ${SOURCES}) \ No newline at end of file +add_executable(hangman ${SOURCES}) diff --git a/src/cpp/game.cpp b/src/cpp/game.cpp index 29bf2fe..6d8a6c9 100644 --- a/src/cpp/game.cpp +++ b/src/cpp/game.cpp @@ -7,43 +7,44 @@ constexpr size_t MAX_MISTAKES = 5; Game::Game() - : m_guessed_word() - , m_mistakes(MAX_MISTAKES) - , m_word_display(m_guessed_word.GetSize()) - , m_user_input() - , m_progress_display(m_mistakes) { + : m_guessed_word() + , m_mistakes(MAX_MISTAKES) + , m_word_display(m_guessed_word.GetSize()) + , m_user_input() + , m_progress_display(m_mistakes) { } void Game::Play() { - while (!IsOver()) { - char letter = m_user_input.InputLetter(); - auto guessed = m_guessed_word.Guess(letter); - if (guessed.empty()) { - m_mistakes.MakeOne(); - m_progress_display.ShowMiss(); - } else { - m_progress_display.ShowHit(); - } - - m_word_display.Reveal(letter, guessed); - m_word_display.Show(); - } - if (IsWon()) { - m_progress_display.ShowWin(); - } else { - m_progress_display.ShowLoss(); - } + while (!IsOver()) { + char letter = m_user_input.InputLetter(); + auto guessed = m_guessed_word.Guess(letter); + if (guessed.empty()) { + m_mistakes.MakeOne(); + m_progress_display.ShowMiss(); + } else { + m_progress_display.ShowHit(); + } + + m_word_display.Reveal(letter, guessed); + m_word_display.Show(); + } + + if (IsWon()) { + m_progress_display.ShowWin(); + } else { + m_progress_display.ShowLoss(); + } } bool Game::IsOver() const { - return IsWon() || IsLost(); + return IsWon() || IsLost(); } bool Game::IsWon() const { - return m_guessed_word.IsGuessed(); + return m_guessed_word.IsGuessed(); } bool Game::IsLost() const { - return m_mistakes.IsMaxAmountReached(); + return m_mistakes.IsMaxAmountReached(); } diff --git a/src/cpp/game.hpp b/src/cpp/game.hpp index af77d0e..cace8ef 100644 --- a/src/cpp/game.hpp +++ b/src/cpp/game.hpp @@ -8,16 +8,16 @@ class Game { public: - Game(); - void Play(); + Game(); + void Play(); private: - GuessedWord m_guessed_word; - Mistakes m_mistakes; - WordDisplay m_word_display; - UserInput m_user_input; - ProgressDisplay m_progress_display; + GuessedWord m_guessed_word; + Mistakes m_mistakes; + WordDisplay m_word_display; + UserInput m_user_input; + ProgressDisplay m_progress_display; - bool IsOver() const; - bool IsWon() const; - bool IsLost() const; + bool IsOver() const; + bool IsWon() const; + bool IsLost() const; }; diff --git a/src/cpp/guessed_word.cpp b/src/cpp/guessed_word.cpp index e068cc3..5d447e5 100644 --- a/src/cpp/guessed_word.cpp +++ b/src/cpp/guessed_word.cpp @@ -3,38 +3,38 @@ #include static const std::vector WORDS = { - "simplicity", "equality", "grandmother", - "neighborhood", "relationship", "mathematics", - "university", "explanation" + "simplicity", "equality", "grandmother", + "neighborhood", "relationship", "mathematics", + "university", "explanation" }; GuessedWord::GuessedWord() - : m_word(WORDS[std::rand() % WORDS.size()]) - , m_guessed_amount(0) - , m_already_guessed() { + : m_word(WORDS[std::rand() % WORDS.size()]) + , m_guessed_amount(0) + , m_already_guessed() { } std::vector GuessedWord::Guess(char letter) { - std::vector result; + std::vector result; - for (size_t pos = 0; pos < m_word.size(); ++pos) { - if (m_word[pos] == letter) { - result.push_back(pos); - } - } + for (size_t pos = 0; pos < m_word.size(); ++pos) { + if (m_word[pos] == letter) { + result.push_back(pos); + } + } - if (m_already_guessed.count(letter) == 0) { - m_already_guessed.insert(letter); - m_guessed_amount += result.size(); - } + if (m_already_guessed.count(letter) == 0) { + m_already_guessed.insert(letter); + m_guessed_amount += result.size(); + } - return result; + return result; } size_t GuessedWord::GetSize() const { - return m_word.size(); + return m_word.size(); } bool GuessedWord::IsGuessed() const { - return m_guessed_amount == m_word.size(); + return m_guessed_amount == m_word.size(); } diff --git a/src/cpp/guessed_word.hpp b/src/cpp/guessed_word.hpp index 113a2af..5c38d65 100644 --- a/src/cpp/guessed_word.hpp +++ b/src/cpp/guessed_word.hpp @@ -6,13 +6,13 @@ class GuessedWord { public: - GuessedWord(); + GuessedWord(); - std::vector Guess(char letter); - size_t GetSize() const; - bool IsGuessed() const; + std::vector Guess(char letter); + size_t GetSize() const; + bool IsGuessed() const; private: - std::string m_word; - std::size_t m_guessed_amount; - std::set m_already_guessed; -}; \ No newline at end of file + std::string m_word; + std::size_t m_guessed_amount; + std::set m_already_guessed; +}; diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 8c82267..ca62bac 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -3,8 +3,8 @@ #include int main() { - std::srand(std::time(nullptr)); - Game().Play(); + std::srand(std::time(nullptr)); + Game().Play(); - return 0; + return 0; } diff --git a/src/cpp/mistakes.cpp b/src/cpp/mistakes.cpp index ec5fa84..7c1ace5 100644 --- a/src/cpp/mistakes.cpp +++ b/src/cpp/mistakes.cpp @@ -1,22 +1,22 @@ #include "mistakes.hpp" Mistakes::Mistakes(int max_amount) - : m_max_amount(max_amount) - , m_amount(0) { + : m_max_amount(max_amount) + , m_amount(0) { } void Mistakes::MakeOne() { - ++m_amount; + ++m_amount; } int Mistakes::GetAmount() const { - return m_amount; + return m_amount; } int Mistakes::GetMaxAmount() const { - return m_max_amount; + return m_max_amount; } int Mistakes::IsMaxAmountReached() const { - return m_amount >= m_max_amount; + return m_amount >= m_max_amount; } diff --git a/src/cpp/mistakes.hpp b/src/cpp/mistakes.hpp index c376e49..79b3796 100644 --- a/src/cpp/mistakes.hpp +++ b/src/cpp/mistakes.hpp @@ -4,13 +4,13 @@ class Mistakes { public: - explicit Mistakes(int max_amount); + explicit Mistakes(int max_amount); - void MakeOne(); - int GetAmount() const; - int GetMaxAmount() const; - int IsMaxAmountReached() const; + void MakeOne(); + int GetAmount() const; + int GetMaxAmount() const; + int IsMaxAmountReached() const; private: - int m_max_amount; - int m_amount; + int m_max_amount; + int m_amount; }; diff --git a/src/cpp/progress_display.cpp b/src/cpp/progress_display.cpp index 1d10e59..755024f 100644 --- a/src/cpp/progress_display.cpp +++ b/src/cpp/progress_display.cpp @@ -5,23 +5,23 @@ #include "progress_display.hpp" #include -ProgressDisplay::ProgressDisplay(Mistakes &mistakes) - : m_mistakes(mistakes) { +ProgressDisplay::ProgressDisplay(Mistakes& mistakes) + : m_mistakes(mistakes) { } void ProgressDisplay::ShowHit() const { - std::cout << "Hit!\n"; + std::cout << "Hit!\n"; } void ProgressDisplay::ShowMiss() const { - std::cout << "Missed, mistake " << m_mistakes.GetAmount() << " out of " - << m_mistakes.GetMaxAmount() << "\n"; + std::cout << "Missed, mistake " << m_mistakes.GetAmount() << " out of " + << m_mistakes.GetMaxAmount() << "\n"; } void ProgressDisplay::ShowWin() const { - std::cout << "You won!\n"; + std::cout << "You won!\n"; } void ProgressDisplay::ShowLoss() const { - std::cout << "You lost.\n"; + std::cout << "You lost.\n"; } diff --git a/src/cpp/progress_display.hpp b/src/cpp/progress_display.hpp index 2070542..9f6549b 100644 --- a/src/cpp/progress_display.hpp +++ b/src/cpp/progress_display.hpp @@ -4,12 +4,12 @@ class ProgressDisplay { public: - explicit ProgressDisplay(Mistakes& mistakes); + explicit ProgressDisplay(Mistakes& mistakes); - void ShowHit() const; - void ShowMiss() const; - void ShowWin() const; - void ShowLoss() const; + void ShowHit() const; + void ShowMiss() const; + void ShowWin() const; + void ShowLoss() const; private: - Mistakes& m_mistakes; + Mistakes& m_mistakes; }; diff --git a/src/cpp/user_input.cpp b/src/cpp/user_input.cpp index 0fef8d8..08f38f2 100644 --- a/src/cpp/user_input.cpp +++ b/src/cpp/user_input.cpp @@ -2,8 +2,8 @@ #include char UserInput::InputLetter() const { - char c = 0; - std::cout << "Guess a letter: "; - std::cin >> c; - return c; + char c = 0; + std::cout << "Guess a letter: "; + std::cin >> c; + return c; } diff --git a/src/cpp/user_input.hpp b/src/cpp/user_input.hpp index 9eb3273..c063508 100644 --- a/src/cpp/user_input.hpp +++ b/src/cpp/user_input.hpp @@ -2,5 +2,5 @@ class UserInput { public: - char InputLetter() const; + char InputLetter() const; }; diff --git a/src/cpp/word_display.cpp b/src/cpp/word_display.cpp index f1e1f3b..f35c33a 100644 --- a/src/cpp/word_display.cpp +++ b/src/cpp/word_display.cpp @@ -2,17 +2,17 @@ #include WordDisplay::WordDisplay(size_t word_size) - : m_buffer(word_size, HIDDEN_LETTER) { - // initialize the buffer with hidden letters + : m_buffer(word_size, HIDDEN_LETTER) { + // initialize the buffer with hidden letters } void WordDisplay::Reveal(char letter, const std::vector& positions) { - for (auto pos : positions) { - m_buffer[pos] = letter; - } + for (auto pos : positions) { + m_buffer[pos] = letter; + } } void WordDisplay::Show() const { - std::cout << "The word: "; - std::cout << m_buffer << "\n\n"; + std::cout << "The word: "; + std::cout << m_buffer << "\n\n"; } diff --git a/src/cpp/word_display.hpp b/src/cpp/word_display.hpp index 65d004a..839e28e 100644 --- a/src/cpp/word_display.hpp +++ b/src/cpp/word_display.hpp @@ -5,12 +5,12 @@ class WordDisplay { public: - static constexpr char HIDDEN_LETTER = '?'; + static constexpr char HIDDEN_LETTER = '?'; - explicit WordDisplay(size_t word_size); + explicit WordDisplay(size_t word_size); - void Reveal(char letter, const std::vector& positions); - void Show() const; + void Reveal(char letter, const std::vector& positions); + void Show() const; private: - std::string m_buffer; + std::string m_buffer; }; From 3ed11d785c4d5f5a749f16ae2d12844e64c64e27 Mon Sep 17 00:00:00 2001 From: Vycheslav Zhdanovsky Date: Mon, 16 Mar 2020 22:06:19 +0300 Subject: [PATCH 4/5] Remove redundant captions --- src/cpp/game.cpp | 4 ---- src/cpp/progress_display.cpp | 4 ---- 2 files changed, 8 deletions(-) diff --git a/src/cpp/game.cpp b/src/cpp/game.cpp index 6d8a6c9..ff119c3 100644 --- a/src/cpp/game.cpp +++ b/src/cpp/game.cpp @@ -1,7 +1,3 @@ -// -// Created by zeronsix on 16.03.2020. -// - #include "game.hpp" constexpr size_t MAX_MISTAKES = 5; diff --git a/src/cpp/progress_display.cpp b/src/cpp/progress_display.cpp index 755024f..a4e4e29 100644 --- a/src/cpp/progress_display.cpp +++ b/src/cpp/progress_display.cpp @@ -1,7 +1,3 @@ -// -// Created by zeronsix on 16.03.2020. -// - #include "progress_display.hpp" #include From fecdadcb8adbcac16f0b32835b987b62d947aa30 Mon Sep 17 00:00:00 2001 From: Vycheslav Zhdanovsky Date: Thu, 19 Mar 2020 20:43:32 +0300 Subject: [PATCH 5/5] More refactoring --- src/cpp/attempt_display.cpp | 20 ++++++++++++++++++++ src/cpp/attempt_display.hpp | 14 ++++++++++++++ src/cpp/game.cpp | 29 +++++++++++++---------------- src/cpp/game.hpp | 10 ++++------ src/cpp/guessed_word.cpp | 4 ++-- src/cpp/guessed_word.hpp | 4 +++- src/cpp/guessing_attempt.cpp | 14 ++++++++++++++ src/cpp/guessing_attempt.hpp | 15 +++++++++++++++ src/cpp/progress_display.cpp | 23 ----------------------- src/cpp/progress_display.hpp | 15 --------------- src/cpp/result_display.cpp | 19 +++++++++++++++++++ src/cpp/result_display.hpp | 14 ++++++++++++++ src/cpp/word_display.cpp | 2 +- src/cpp/word_display.hpp | 3 ++- 14 files changed, 121 insertions(+), 65 deletions(-) create mode 100644 src/cpp/attempt_display.cpp create mode 100644 src/cpp/attempt_display.hpp create mode 100644 src/cpp/guessing_attempt.cpp create mode 100644 src/cpp/guessing_attempt.hpp delete mode 100644 src/cpp/progress_display.cpp delete mode 100644 src/cpp/progress_display.hpp create mode 100644 src/cpp/result_display.cpp create mode 100644 src/cpp/result_display.hpp diff --git a/src/cpp/attempt_display.cpp b/src/cpp/attempt_display.cpp new file mode 100644 index 0000000..be0b622 --- /dev/null +++ b/src/cpp/attempt_display.cpp @@ -0,0 +1,20 @@ +#include "attempt_display.hpp" +#include + +AttemptDisplay::AttemptDisplay(const GuessingAttempt& attempt, const Mistakes& mistakes) + : m_mistakes(mistakes) { + if (attempt.IsSuccessful()) { + ShowHit(); + } else { + ShowMiss(); + } +} + +void AttemptDisplay::ShowHit() const { + std::cout << "Hit!\n"; +} + +void AttemptDisplay::ShowMiss() const { + std::cout << "Missed, mistake " << m_mistakes.GetAmount() << " out of " + << m_mistakes.GetMaxAmount() << "\n"; +} diff --git a/src/cpp/attempt_display.hpp b/src/cpp/attempt_display.hpp new file mode 100644 index 0000000..f1469ac --- /dev/null +++ b/src/cpp/attempt_display.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include "guessing_attempt.hpp" +#include "mistakes.hpp" + +class AttemptDisplay { +public: + explicit AttemptDisplay(const GuessingAttempt& attempt, const Mistakes& mistakes); +private: + const Mistakes& m_mistakes; + + void ShowHit() const; + void ShowMiss() const; +}; diff --git a/src/cpp/game.cpp b/src/cpp/game.cpp index ff119c3..d4f9426 100644 --- a/src/cpp/game.cpp +++ b/src/cpp/game.cpp @@ -1,4 +1,7 @@ #include "game.hpp" +#include "guessing_attempt.hpp" +#include "result_display.hpp" +#include "attempt_display.hpp" constexpr size_t MAX_MISTAKES = 5; @@ -6,30 +9,24 @@ Game::Game() : m_guessed_word() , m_mistakes(MAX_MISTAKES) , m_word_display(m_guessed_word.GetSize()) - , m_user_input() - , m_progress_display(m_mistakes) { + , m_user_input() { } void Game::Play() { while (!IsOver()) { char letter = m_user_input.InputLetter(); - auto guessed = m_guessed_word.Guess(letter); - if (guessed.empty()) { - m_mistakes.MakeOne(); - m_progress_display.ShowMiss(); - } else { - m_progress_display.ShowHit(); - } - - m_word_display.Reveal(letter, guessed); + GuessingAttempt attempt( + letter, + m_guessed_word, + m_mistakes, + m_word_display + ); + + AttemptDisplay(attempt, m_mistakes); m_word_display.Show(); } - if (IsWon()) { - m_progress_display.ShowWin(); - } else { - m_progress_display.ShowLoss(); - } + ResultDisplay(*this); } bool Game::IsOver() const { diff --git a/src/cpp/game.hpp b/src/cpp/game.hpp index cace8ef..f86d35e 100644 --- a/src/cpp/game.hpp +++ b/src/cpp/game.hpp @@ -4,20 +4,18 @@ #include "mistakes.hpp" #include "word_display.hpp" #include "user_input.hpp" -#include "progress_display.hpp" class Game { public: Game(); void Play(); + + bool IsOver() const; + bool IsWon() const; + bool IsLost() const; private: GuessedWord m_guessed_word; Mistakes m_mistakes; WordDisplay m_word_display; UserInput m_user_input; - ProgressDisplay m_progress_display; - - bool IsOver() const; - bool IsWon() const; - bool IsLost() const; }; diff --git a/src/cpp/guessed_word.cpp b/src/cpp/guessed_word.cpp index 5d447e5..fe09062 100644 --- a/src/cpp/guessed_word.cpp +++ b/src/cpp/guessed_word.cpp @@ -14,8 +14,8 @@ GuessedWord::GuessedWord() , m_already_guessed() { } -std::vector GuessedWord::Guess(char letter) { - std::vector result; +GuessedVec GuessedWord::Guess(char letter) { + GuessedVec result; for (size_t pos = 0; pos < m_word.size(); ++pos) { if (m_word[pos] == letter) { diff --git a/src/cpp/guessed_word.hpp b/src/cpp/guessed_word.hpp index 5c38d65..5d05a8d 100644 --- a/src/cpp/guessed_word.hpp +++ b/src/cpp/guessed_word.hpp @@ -4,11 +4,13 @@ #include #include +using GuessedVec = std::vector; + class GuessedWord { public: GuessedWord(); - std::vector Guess(char letter); + GuessedVec Guess(char letter); size_t GetSize() const; bool IsGuessed() const; private: diff --git a/src/cpp/guessing_attempt.cpp b/src/cpp/guessing_attempt.cpp new file mode 100644 index 0000000..61403ec --- /dev/null +++ b/src/cpp/guessing_attempt.cpp @@ -0,0 +1,14 @@ +#include "guessing_attempt.hpp" + +GuessingAttempt::GuessingAttempt(char letter, GuessedWord& word, Mistakes& mistakes, WordDisplay& word_display) + : m_guessed(word.Guess(letter)) + , m_successful(!m_guessed.empty()) { + if (!m_successful) { + mistakes.MakeOne(); + } + word_display.Reveal(letter, m_guessed); +} + +bool GuessingAttempt::IsSuccessful() const { + return m_successful; +} diff --git a/src/cpp/guessing_attempt.hpp b/src/cpp/guessing_attempt.hpp new file mode 100644 index 0000000..2d35308 --- /dev/null +++ b/src/cpp/guessing_attempt.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "guessed_word.hpp" +#include "mistakes.hpp" +#include "word_display.hpp" + +class GuessingAttempt { +public: + GuessingAttempt(char letter, GuessedWord& word, Mistakes& mistakes, WordDisplay& display); + + bool IsSuccessful() const; +private: + GuessedVec m_guessed; + bool m_successful; +}; diff --git a/src/cpp/progress_display.cpp b/src/cpp/progress_display.cpp deleted file mode 100644 index a4e4e29..0000000 --- a/src/cpp/progress_display.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "progress_display.hpp" -#include - -ProgressDisplay::ProgressDisplay(Mistakes& mistakes) - : m_mistakes(mistakes) { -} - -void ProgressDisplay::ShowHit() const { - std::cout << "Hit!\n"; -} - -void ProgressDisplay::ShowMiss() const { - std::cout << "Missed, mistake " << m_mistakes.GetAmount() << " out of " - << m_mistakes.GetMaxAmount() << "\n"; -} - -void ProgressDisplay::ShowWin() const { - std::cout << "You won!\n"; -} - -void ProgressDisplay::ShowLoss() const { - std::cout << "You lost.\n"; -} diff --git a/src/cpp/progress_display.hpp b/src/cpp/progress_display.hpp deleted file mode 100644 index 9f6549b..0000000 --- a/src/cpp/progress_display.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include "mistakes.hpp" - -class ProgressDisplay { -public: - explicit ProgressDisplay(Mistakes& mistakes); - - void ShowHit() const; - void ShowMiss() const; - void ShowWin() const; - void ShowLoss() const; -private: - Mistakes& m_mistakes; -}; diff --git a/src/cpp/result_display.cpp b/src/cpp/result_display.cpp new file mode 100644 index 0000000..a00e5f1 --- /dev/null +++ b/src/cpp/result_display.cpp @@ -0,0 +1,19 @@ +#include "result_display.hpp" +#include "game.hpp" +#include + +ResultDisplay::ResultDisplay(const Game& game) { + if (game.IsWon()) { + ShowWin(); + } else { + ShowLoss(); + } +} + +void ResultDisplay::ShowWin() const { + std::cout << "You won!\n"; +} + +void ResultDisplay::ShowLoss() const { + std::cout << "You lost.\n"; +} diff --git a/src/cpp/result_display.hpp b/src/cpp/result_display.hpp new file mode 100644 index 0000000..d90f25a --- /dev/null +++ b/src/cpp/result_display.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include "mistakes.hpp" +#include "guessing_attempt.hpp" + +class Game; + +class ResultDisplay { +public: + explicit ResultDisplay(const Game& game); +private: + void ShowWin() const; + void ShowLoss() const; +}; diff --git a/src/cpp/word_display.cpp b/src/cpp/word_display.cpp index f35c33a..146c3b3 100644 --- a/src/cpp/word_display.cpp +++ b/src/cpp/word_display.cpp @@ -6,7 +6,7 @@ WordDisplay::WordDisplay(size_t word_size) // initialize the buffer with hidden letters } -void WordDisplay::Reveal(char letter, const std::vector& positions) { +void WordDisplay::Reveal(char letter, const GuessedVec& positions) { for (auto pos : positions) { m_buffer[pos] = letter; } diff --git a/src/cpp/word_display.hpp b/src/cpp/word_display.hpp index 839e28e..46a8243 100644 --- a/src/cpp/word_display.hpp +++ b/src/cpp/word_display.hpp @@ -1,5 +1,6 @@ #pragma once +#include "guessed_word.hpp" #include #include @@ -9,7 +10,7 @@ class WordDisplay { explicit WordDisplay(size_t word_size); - void Reveal(char letter, const std::vector& positions); + void Reveal(char letter, const GuessedVec& positions); void Show() const; private: std::string m_buffer;