Description:
This is a console game I wrote in C++. It’s a mastermind-like game with a hidden word, which the player has to guess. When a player enters a guess, the game tells him how many letters are correct and in the right position, and how many are correct, but in the wrong position.
The player wins if he enters a correct guess, before his tries run out. At the end of the game, the player can choose to play again or not.
Started: December 2017
Finished: February 2018
Links:
Code:
The WordGame class handles the game logic. This class determines if a player’s guess is valid or not and checks if there are any correct letters. The class also keeps track of the maximum tries that the player has left and if the game is won or lost.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/* This class handles the game logic. */ #pragma once #include <iostream> #include <string> struct CorrectLetterCount { int CorrectPlaceCount = 0; int IncorrectPlaceCount = 0; }; enum class GuessStatus { Invalid, OK, NotIsogram, NotCorrectLength, InvalidCharacters, }; enum class GameStatus { Won, Lost }; class WordGame { public: WordGame(); int getMaxTries() const; int getCurrentTry() const; int getHiddenWordLength(); GameStatus getGameStatus(const CorrectLetterCount count); GuessStatus checkInputValidity(std::string&) const; CorrectLetterCount submitGuess(std::string&); void reset(); private: const std::string HIDDEN_WORD = "teacup"; // Edit the word here! const int MAX_TRIES = 15; int maxTries; int currentTry; std::string hiddenWord; bool isIsogram(std::string&) const; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
#include "stdafx.h" #include "WordGame.h" WordGame::WordGame() { reset(); } int WordGame::getMaxTries() const { return MAX_TRIES; } int WordGame::getCurrentTry() const { return currentTry; } int WordGame::getHiddenWordLength() { int length = 0; for (std::string::iterator it = hiddenWord.begin(); it < hiddenWord.end(); it++) { length++; } return length; } GameStatus WordGame::getGameStatus(const CorrectLetterCount count) { return count.CorrectPlaceCount == getHiddenWordLength() ? GameStatus::Won : GameStatus::Lost; } void WordGame::reset() { currentTry = 1; maxTries = getMaxTries(); hiddenWord = HIDDEN_WORD; } GuessStatus WordGame::checkInputValidity(std::string & guess) const { if (!isIsogram(guess)) { return GuessStatus::NotIsogram; } else if (guess.length() != hiddenWord.length()) { return GuessStatus::NotCorrectLength; } else { return GuessStatus::OK; } } // Receives a VALID guess, increments turn, and returns count CorrectLetterCount WordGame::submitGuess(std::string & guess) { currentTry++; CorrectLetterCount count; int index = 0; for (std::string::iterator it = guess.begin(); it < guess.end(); it++) { if (hiddenWord[index] != '/0') { // If chars match and they are in the same place if (*it == hiddenWord[index]) { count.CorrectPlaceCount++; } // If they are in the word, but in a different place else if (hiddenWord.find(*it) != std::string::npos) { count.IncorrectPlaceCount++; } } index++; } return count; } bool WordGame::isIsogram(std::string & guess) const { int index = 0; for (std::string::iterator it = guess.begin(); it < guess.end(); it++) { int found = guess.find(*it); // Check if this char isn't in another place in the word if (found != std::string::npos && found != index) { return false; } index++; } return true; } |