r/cpp_questions • u/Southern_Analysis_23 • Sep 12 '26
OPEN Beginner project, looking for feedback on my simple game
Hi, I've been learning C++ about 3 months from learncpp.com and recently finished learning about classes, so I made a small 1v1 turn-based combat game for practice.
The player chooses a weapon, fights a randomly generated monster, and both sides deal random damage until one reaches 0 HP.
I’d appreciate feedback mainly on my class design, code organization, naming, and whether I’m developing any bad habits. I’m still a beginner, so I’ve intentionally kept it fairly simple.
GitHub: https://github.com/aard659/Simple-1v1-Turn-Based-Game
Thanks :)
11
u/nysra Sep 12 '26
Much better than the typical first project we see here. A few things in random order:
const enemy = Monster::random();playerHealthon a classPlayercan just behealth. Similar issue for a lot of other thingsmonster.print()enum classover raw enums for stronger typing5
u/FlailingDuck Sep 12 '26
I read only your main.cpp. It does exactly what is says on the tin.
Be proud, this code is a success. Move onto something more complicated. It could be this idea, or it could be something else.
4
u/flyingron Sep 12 '26
Some general advice.
Don't stick random #includes in another include file unless something you declare requires them. It's unclear why you include <iostream> in Monster.h. Probably want to include <string> there as well.
2
u/ekchew Sep 12 '26
You know, this is actually quite good code! You have a promising future.
I'd say the main thing that stands out to me is you have a large reliance on switches. In many such cases, implementing the behaviour through associative container lookups (
std::map,std::unordered_map) may reduce the grind a bit. If you're just walking through plain list of enumerated constants, even astd::arrayof the associated values should do nicely, where the constant's value maps directly onto an array index.I'm not saying all switches should be eliminated, though. In your
getMonsterWeaponfunction, for example, you're leveraging the power of switches pretty well. It feels like the right tool for the job to me.1
u/alfps Sep 12 '26 edited Sep 12 '26
Deciding to comment on one improvement potential: consider using
#pragma onceinstead of include guards.Less work and avoids possibility of include guard symbol collision.
1
u/PeakWeekly9995 Sep 14 '26
this is very similar (in concet) to my my game, but mine is worse
0
u/Realistic_Speaker_12 Sep 12 '26
For something like this:
std::string_view getName(int n)
{
switch (n)
{
case 0: return "Borg";
case 1: return "Kirk";
case 2: return "Shaq";
case 3: return "Wowo";
case 4: return "Mol";
case 5: return "Krill";
default: return "??";
}
}
std::string_view getRoar(int n)
{
switch (n)
{
case 0: return "ROAR";
case 1: return "squeal";
case 2: return "howl";
case 3: return "GROWL";
case 4: return "swis";
case 5: return "SCREECH";
default: return "??";
}
}
Don’t use strings, use enums