Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A refactored C++ version #28

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/cpp/attempt_display.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "attempt_display.hpp"
#include <iostream>

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";
}
14 changes: 14 additions & 0 deletions src/cpp/attempt_display.hpp
Original file line number Diff line number Diff line change
@@ -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;
};
43 changes: 43 additions & 0 deletions src/cpp/game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "game.hpp"
#include "guessing_attempt.hpp"
#include "result_display.hpp"
#include "attempt_display.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() {
}

void Game::Play() {
while (!IsOver()) {
char letter = m_user_input.InputLetter();
GuessingAttempt attempt(
letter,
m_guessed_word,
m_mistakes,
m_word_display
);

AttemptDisplay(attempt, m_mistakes);
m_word_display.Show();
}

ResultDisplay(*this);
}

bool Game::IsOver() const {
return IsWon() || IsLost();
}

bool Game::IsWon() const {
return m_guessed_word.IsGuessed();
}

bool Game::IsLost() const {
return m_mistakes.IsMaxAmountReached();
}

21 changes: 21 additions & 0 deletions src/cpp/game.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "guessed_word.hpp"
#include "mistakes.hpp"
#include "word_display.hpp"
#include "user_input.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;
};
40 changes: 40 additions & 0 deletions src/cpp/guessed_word.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "guessed_word.hpp"
#include <string>
#include <cstdlib>

static const std::vector<std::string> 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() {
}

GuessedVec GuessedWord::Guess(char letter) {
GuessedVec 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();
}
20 changes: 20 additions & 0 deletions src/cpp/guessed_word.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <string>
#include <vector>
#include <set>

using GuessedVec = std::vector<size_t>;

class GuessedWord {
public:
GuessedWord();

GuessedVec Guess(char letter);
size_t GetSize() const;
bool IsGuessed() const;
private:
std::string m_word;
std::size_t m_guessed_amount;
std::set<char> m_already_guessed;
};
14 changes: 14 additions & 0 deletions src/cpp/guessing_attempt.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
15 changes: 15 additions & 0 deletions src/cpp/guessing_attempt.hpp
Original file line number Diff line number Diff line change
@@ -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;
};
10 changes: 10 additions & 0 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "game.hpp"
#include <ctime>
#include <cstdlib>

int main() {
std::srand(std::time(nullptr));
Game().Play();

return 0;
}
22 changes: 22 additions & 0 deletions src/cpp/mistakes.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
16 changes: 16 additions & 0 deletions src/cpp/mistakes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <cstddef>

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;
};
19 changes: 19 additions & 0 deletions src/cpp/result_display.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "result_display.hpp"
#include "game.hpp"
#include <iostream>

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";
}
14 changes: 14 additions & 0 deletions src/cpp/result_display.hpp
Original file line number Diff line number Diff line change
@@ -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;
};
9 changes: 9 additions & 0 deletions src/cpp/user_input.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "user_input.hpp"
#include <iostream>

char UserInput::InputLetter() const {
char c = 0;
std::cout << "Guess a letter: ";
std::cin >> c;
return c;
}
6 changes: 6 additions & 0 deletions src/cpp/user_input.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

class UserInput {
public:
char InputLetter() const;
};
18 changes: 18 additions & 0 deletions src/cpp/word_display.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "word_display.hpp"
#include <iostream>

WordDisplay::WordDisplay(size_t word_size)
: m_buffer(word_size, HIDDEN_LETTER) {
// initialize the buffer with hidden letters
}

void WordDisplay::Reveal(char letter, const GuessedVec& positions) {
for (auto pos : positions) {
m_buffer[pos] = letter;
}
}

void WordDisplay::Show() const {
std::cout << "The word: ";
std::cout << m_buffer << "\n\n";
}
17 changes: 17 additions & 0 deletions src/cpp/word_display.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "guessed_word.hpp"
#include <vector>
#include <string>

class WordDisplay {
public:
static constexpr char HIDDEN_LETTER = '?';

explicit WordDisplay(size_t word_size);

void Reveal(char letter, const GuessedVec& positions);
void Show() const;
private:
std::string m_buffer;
};