This is a simple but complete console application where I experiment with genetic algorithm to try to figure out the best way to construct a battle mech that would win in a simple turn-based combat.
I welcome your criticism and pointers about what I could improve in the code above: architecture-wise, technique-wise, presentation-wise, and whatever else catches your interest. To provide at least a sample code as per the local rules, this is the method to make two mechs meet in combat:
// Match::match()
// ==============
// A static method that can be used to execute a match between the two provided mechs.
// Return value: 0 = draw, 1 = mech #1 wins, 2 = mech #2 wins.
int Match::match (Mech& m1, Mech& m2) {
m1.resetCombatValues();
m2.resetCombatValues();
int turn = 0;
static const int turnLimit = 25;
// Each turn:
while (turn < turnLimit) {
m1.actCombatTurn (m2);
m2.actCombatTurn (m1);
bool mech1Alive = m1.isAlive();
bool mech2Alive = m2.isAlive();
if (!mech1Alive && !mech2Alive) return 0;
if (!mech1Alive) return 2;
if (!mech2Alive) return 1;
turn++;
}
// Turn limit reached.
return 0;
}
turnLimit
or wantturn
to go backward (would that even make sense in your context? BTW,turn++
looks awkward, change it to++turn
), I would usesize_t
instead ofint
forturn
andturnLimit
: en.cppreference.com/w/cpp/types/size_t – Matt May 25 '13 at 13:56mech1Alive
ormech2Alive
but rather their opposites (e.g.,mech1Dead
ormech2Dead
) -- note that you're always negating them in yourif
statements, so it would improve code clarity to do so straight away. – Matt May 25 '13 at 13:59