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

Mini console based life simulation thing

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

Problem

I made this mini life simulation thing. I got the idea from the Eloquent JavaScript textbook, and just kinda ran with it. I've been programming for a little less than year and this is probably the largest thing I've made to date, so I'm looking for a little feedback.

If there's anything you see that can be improved, please let me know.

main.cpp

``
#include
#include
#include
#include
#include
#include
#include

#include "terrariums.h"

/*==============================================================================
CONSTANT GAME VARIABLES
==============================================================================*/

const bool ALLOW_DIAGONAL_DIRECTIONS = true;

const char EMPTY_SYM = ' ';
const char ROCK_SYM = '@';
const char DUMBBUG_SYM = 'o';
const char M_SMARTBUG_SYM = 'X';
const char F_SMARTBUG_SYM = 'x';
const char DUMBBUGEGG_SYM = 'e';
const char SMARTBUGEGG_SYM = 'a';
const char SMALLPLANT_SYM = '\'';

/*
y|
|
| o
|_______
x
*/

struct Vec2 {
int x;
int y;

Vec2()
{
x = 0;
y = 0;
}
Vec2(int _x, int _y)
{
x = _x;
y = _y;
}
};
Vec2 operator + (Vec2 u, Vec2 v)
{
Vec2 result;
result.x = u.x + v.x;
result.y = u.y + v.y;

return result;
}
void operator += (Vec2 &u, Vec2 v)
{
u.x += v.x;
u.y += v.y;
}
Vec2 operator * (Vec2 u, int s)
{
Vec2 result;
result.x = u.x * s;
result.y = u.y * s;

return result;
}
Vec2 operator *= (Vec2 &u, int s)
{
u.x *= s;
u.y *= s;
}

//directions map is global
enum Direction { n=0, ne=4, e=1, se=5, s=2, sw=6, w=3, nw=7 };
std::map directions;

enum Action { nothing, walk, walktofood, changedirection, eat,
layegg, grow, hatch, die };

/*
o . ___---___ .
. .--\ --. . . .
./.;_.\ __/~ \.
/; /
-' __\ . \
. . / ,--' / . .;

Solution

Comments

The ASCII art comments are certainly apropos, given the purpose of the program. I find them amusing. The one labelling the axes is useful.

On the other hand, there isn't much of an explanation of how each simulated entity is supposed to behave. The only way to find out is to slog through the code.

Warnings

You should always strive to write code that compiles without warnings. Using clang++ -Wall, I get…

main.cpp:75:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^


The method signature should be Vec2& Vec2::operator=(int s), and it should return this. Similarly, your += operator should return a Vec2& to support chaining.

main.cpp:570:20: warning: 5 enumeration values not handled in switch: 'nothing', 'walktofood', 'changedirection'... [-Wswitch]
            switch(b->act())
                   ^
main.cpp:617:20: warning: 8 enumeration values not handled in switch: 'nothing', 'walk', 'walktofood'... [-Wswitch]
            switch(e->act())
                   ^
main.cpp:642:20: warning: enumeration values 'nothing', 'grow', and 'hatch' not handled in switch [-Wswitch]
            switch(b->act(t))
                   ^
main.cpp:702:20: warning: 8 enumeration values not handled in switch: 'nothing', 'walk', 'walktofood'... [-Wswitch]
            switch(e->act())
                   ^
main.cpp:730:20: warning: 7 enumeration values not handled in switch: 'nothing', 'walk', 'walktofood'... [-Wswitch]
            switch(p->act(t))
                   ^


If the remaining cases are deliberately unhandled, then put a default: / no-op / at the end of the switch. If the remaining cases are impossible, then put a default: that either throws an exception or makes an assertion.

main.cpp:19:12: warning: unused variable 'ROCK_SYM' [-Wunused-const-variable]
const char ROCK_SYM = '@';
           ^
7 warnings generated.


You could just delete that line or comment it out.

Organization

Your main() is much too long. The event loop shouldn't contain all the logic for plants, dumb bugs, smart bugs, and their eggs. All of those should be subclasses of an Inhabitant virtual base class, whose act(Terrarium &t) method is polymorphic. In other words, each inhabitant object should autonomously do its thing at each time step.

Code Snippets

main.cpp:75:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
main.cpp:570:20: warning: 5 enumeration values not handled in switch: 'nothing', 'walktofood', 'changedirection'... [-Wswitch]
            switch(b->act())
                   ^
main.cpp:617:20: warning: 8 enumeration values not handled in switch: 'nothing', 'walk', 'walktofood'... [-Wswitch]
            switch(e->act())
                   ^
main.cpp:642:20: warning: enumeration values 'nothing', 'grow', and 'hatch' not handled in switch [-Wswitch]
            switch(b->act(t))
                   ^
main.cpp:702:20: warning: 8 enumeration values not handled in switch: 'nothing', 'walk', 'walktofood'... [-Wswitch]
            switch(e->act())
                   ^
main.cpp:730:20: warning: 7 enumeration values not handled in switch: 'nothing', 'walk', 'walktofood'... [-Wswitch]
            switch(p->act(t))
                   ^
main.cpp:19:12: warning: unused variable 'ROCK_SYM' [-Wunused-const-variable]
const char ROCK_SYM = '@';
           ^
7 warnings generated.

Context

StackExchange Code Review Q#85739, answer score: 2

Revisions (0)

No revisions yet.