HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Hangman on the C++ommand line

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linetheommandhangman

Problem

I'm wondering if my code is just a tad too long to post here, but if the only consequence seems to be getting high-level reviews, then that's perfectly fine.

This is my implementation of Hangman in C++, split up into a handful of classes. I'm familiar with the syntax of curly brace languages but lack experience with the quirks and specialties of C++, which is why I'm posting my code here asking if it follows the common best practices.

Specifically, here's what I'm most interested in knowing:

  • Am I using .h and .cpp files in the right way? I'm declaring methods in the former and defining them in the latter, and I keep them in subfolders called "header" and "src" respectively.



  • Is there a widely accepted naming convention for variables and functions? Right now I'm using_underscores for functions (because I plan on doing stuff with Allegro and feel it would make sense to follow its naming scheme), and camelCasing variables because they're easier to distinguish from functions that way.



  • You may or may not notice, but I pretty much ported this code over from a Java version I made earlier. Classes like WordChooser and CharReader may have made more sense there, but in C++ they seem rather too small to deserve being classes of their own. Would they be better off as functions instead, and if so, where should they go? In main.cpp?



main.cpp

#include 

int main()
{
    GameManager gm;
    gm.start_game();

    return 0;
}


GameManager.h

#pragma once

#include "Hangman.h"
#include "CharReader.h"

class GameManager {

public:
    void start_game();

private:
    Hangman hangman;
    CharReader reader;
    char guess;

    void play_game();
    void handle_guess();
    void handle_win();
    void handle_lose();
    void read_input_into_guess();
    void print_game_state_info();
    bool handle_replay();

};


GameManager.cpp

```
#include
#include "Hangman.h"
#include "GameManager.h"

void GameManager::start_game()
{
bool replay;
do
{

Solution

Excellent code.

I would restructure the code to obviate the need for private member functions in GameManager.

Create a global function, start_game. Use the global function from main and leave the details of how start_game is implemented as purely encapsulated detail.

GameManager.h

#pragma once
extern void start_game();


main.cpp

#include 

int main()
{
    start_game();
    return 0;
}


Then, in GameManager.cc, you can define GameManager as a class or even a struct. You add member functions that capture distinct functionality and don't have to worry about whether a function needs to be private or public

The implementation of start_game() can be:

void start_game()
{
    GameManager gm;
    gm.start_game();
}


With this approach, main.cpp doesn't need to have any knowledge of GameManager as a class.

If there is a possibility of conflicting global names, you can put start_game in a unique namespace

GameManager.h

#pragma once

namespace GameManagerNS
{
   extern void start_game();
}


and change the implementation to:

void GameManagerNS::start_game()
{
    GameManager gm;
    gm.start_game();
}

Code Snippets

#pragma once
extern void start_game();
#include <GameManager.h>

int main()
{
    start_game();
    return 0;
}
void start_game()
{
    GameManager gm;
    gm.start_game();
}
#pragma once

namespace GameManagerNS
{
   extern void start_game();
}
void GameManagerNS::start_game()
{
    GameManager gm;
    gm.start_game();
}

Context

StackExchange Code Review Q#78362, answer score: 4

Revisions (0)

No revisions yet.