Beginner's code, What do you think?? (monster battle)

Hi guys,

I'm trying to learn C++ for a few weeks now, I try to read and understand books. However, as several other C++-beginners I kinda felt like I am not progressing or learning, at all. Sometimes I felt like reading and actually trying out all the codes in those books doen't teach me anything. Anyhow, so I decided to imagine a problem or something like that and solve it in C++. After creating and solving simple problems with strings, numbers and functions I thought I might try something harder...
I am not sure, maybe this step is way too big for me, well check it out:

I tried to make a round-based text-monster-battle-game. Kind of like Pok


  • Object-oriented design is hard.

    However players and monsters are naturally objects. Probably they should both inherit from the same base class, combatant.

    Objects are different from each other. So a lightbulb is different from the Sun is different from a light on a car. The difficulty of object-oriented design is that ypuve got to decide wether these differences are best expressed by parameters, or by code. So you could argue that a lighbulb, the Sun, and a car light are all "lights" and can be expressed by colour and intensity. Or you could argue that lightbulbs need special logic because they have a position whilst the Sun only has direction, and car lights need even more logic as they falsh on and off, need to move with the automobile they are attached to, and so on. There's no right answer for every program, it depends how you are using the lights.

    In the same way, I'd guess that a base class of "combatant" and two derived classes of "player" and "monster" is right for your Pokemon game. However without sitting down for a long time and really working through the requirements, I can't be sure of that. That's why object-oriented design is hard to do.
  • Sure, I'll give you some feedback with references to the literature (so do not kill the messenger). Don't take it too hard: you are a beginner, so you are not to be blamed that you do not know everything.

    You said that you have read some books, but I cannot see that you have read litter or literature or that you do not follow the literature's advices. Because, except for the class design, I do not see a preferred coding standard, for example:
    - You use arrays, where you prefer to use std::vector ([1-3])
    - You are not const correct ([4-9])

    You might want to view the five most important C++ books or other book advice at this website:

    Or check out the C++ FAQ lite (Google for it).

    Good luck, Bilderbikkel


    [1] Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6, chapter 76: 'Use vector by default. Otherwise, choose an appropriate container', chapter 77: 'Use vector and string instead of arrays.'
    [2] Marshall Cline,Greg Lomow and Mike Girou. C++ FAQs. ISBN: 0-201-3098301, FAQ 28.02: 'Are arrays good or evil?'(Answer: 'Arrays are evil').
    [3] Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4 Chapter C.14.11 'Prefer vector over array'.
    [4] Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4 7.9.3: 'Use const extensively and consistently'
    [5] Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 3: 'Use const whenever possible'
    [6] Jarrod Hollingworth, Bob Swart, Mark Cashman, Paul Gustavson. Sams C++ Builder 6 Developer's Guide. ISBN: 0-672-32480-6. Chapter 3: 'Understand and use const in your code'
    [7] Jesse Liberty. Sams teach yourself C++ in 24 hours. ISBN: 0-672-32224-2. Hour 8, chapter 'Const member functions': 'Use const whenever possible.'
    [8] Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 2: 'Prefer consts, enums and inlines to #defines'
    [9] Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 15: 'Use const proactively'.
  • Well... vectors are no doubt more beginner-friendly than arrays. But still, there are plenty of cases where you wouldn't use vectors, embedded programming for example. Stroustrup & co are really no good role models for how to make embedded programs.
  • Ok it is a simple program, but no matter the (valid) remarks in those other post, you seem to have grasped the concept of "private encapsulation", which is perhaps the single most important part when doing object-oriented programming. Whole program is also in standard C++ and therefore completely portable.

    So to me it seems you are heading in the right direction :-)
  • Dear Lundin,

    Thanks for always showing the embedded programming kind of way. But it is in my humble didactic opinion a better idea to give the rule-of-thumb first and only. You want this fellow programmer to start using arrays in his exemplary Pokemon game (that is: a not-embedded program)? I hope you don't.

    > Stroustrup & co are really no good role models for how
    > to make embedded programs.
    Actually, they are: they say 'prefer std::vector over array by default', instead of 'always use...'. In Stroustrup's latest book 'Programming' he dedicates an entire chapter to embedded programming, where he states not to use std::vectors then.

    See ya, Bilderbikkel
  • Alright, thanks a bunch. I will check out those books you mentioned.
  • Thanks for your feedback. Yes, the program is rather simple and I have really improved it. Perhaps the easiest improvement was the deletion of the AI class. I noticed that I do not need to have an AI class. I could simply "stamp out" an AI using the Player class.

    I did read the other posts but I'm having a hard time understanding why someone would prefer vectors over arrays.
  • I actually find arrays easier than vectors. Arrays seem to be more flexible but maybe I am wrong due to the fact that I do not know the whole potential of arrays and vectors.

    So my question is why would someone prefer one over the other?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!