I am writing a game and I am having a challenge with my combat

function. All I want to do is find out how to group pieces that are in

the same space. There are two sides and all the units that are in the

same space fight. I want to add up the attack factors and defending

factors in the same space then figure out the odds so I can roll

against an odds table. Basically each piece holds its own x and y loc.

Here is what I have right now:

void fight(vector<un it>& at, vector<unit>& dt,

board * b , terrain * trn ){

vector<fightcla ssfighting;

/* adds attacking units to the fightclass vector if units are in the

same space they are to grouped in the same fight class elemet of

the

vector. */

for(int atu = 0; atu != at.size(); atu++){

if(fighting.siz e() == 0){

fightclass ft;

ft.addAu(at[atu]);

fighting.push_b ack(ft);

} else {

for(int lp = 0; lp != fighting.size() ; lp++){

if(at[atu].getXloc() != fighting[lp].getX() &&

at[atu].getYloc() != fighting[lp].getY()){

fightclass ft;

ft.addAu(at[atu]);

fighting.push_b ack(ft);

} else {

fighting[lp].addAu(at[atu]);

}

}

}

}

/* Adds defending units to the fightclass array. If x and y locs are

the same as attacking locations (are in the same space) they are

added to array for combat */

for(int dtu = 0; dtu != dt.size(); dtu++){

for(int lp = 0; lp != fighting.size() ; lp++){

if(dt[dtu].getXloc() == fighting[lp].getX() &&

dt[dtu].getYloc() == fighting[lp].getY()){

fighting[lp].addDu(dt[dtu]);

}

}

}

// Combat routine

for(int lp = 0; lp != fighting.size() ; lp++){ //handles combat

if(fighting[lp].canfight()){

int df = b->GetSpace(fight ing[lp].getX(), fighting[lp].getY());

float odds =

fighting[lp].getAtk()/fighting[lp].getDef();

//gets the defense bonus for the terrain in the space where

//combat takes place

int roll = rand() - trn[df].defend();

//get the die roll modified for terrain

odds = fighting[lp].getAtk() / fighting[lp].getDef();

//gets the attack to defence ratio.

if(odds < .5){

MessageBox(NULL , "Fighting! 1:3", "Info!", MB_OK);

return;

}

if(odds < 1){

MessageBox(NULL , "Fighting! 1:2", "Info!", MB_OK);

return;

}

if(odds < 2){

MessageBox(NULL , "Fighting! 1:1", "Info!", MB_OK);

return;

}

if(odds < 3){

MessageBox(NULL , "Fighting! 2:1", "Info!", MB_OK);

return;

}

if(odds < 4){

MessageBox(NULL , "Fighting! 3:1", "Info!", MB_OK);

return;

}

if(odds < 5){

MessageBox(NULL , "Fighting! 4:1", "Info!", MB_OK);

return;

}

if(odds < 6){

MessageBox(NULL , "Fighting! 5:1", "Info!", MB_OK);

return;

}

}

}

for(int lp = 0; lp != fighting.size() ; lp++){

fighting[lp].done();

}

fighting.clear( );

}

class fightclass{

/* Fightclass holds two arrays of units. The two arrays represent

units

in the same space elgible for combat. Array at represents the

attacking

forces and dt represents the defending units */

int xl;

int yl;

bool sides2; /* indicates if there are units in both attack and

defend

if there are units in both attack and defend then

combat

is to take place between the opposing sides. */

vector<unit>at; //attack units

vector<unit>dt; //defending units

public:

fightclass();

void addAu(unit au); //adds an addacking unit

void addDu(unit du); //adds a defending unit

int getX(){return xl;} //return the x coord

int getY(){return yl;} //returns the y coord

float getAtk(); //returns the total attack factors for one space

float getDef(); //returns the total defending factors for one space

bool canfight(); //if there are both attacking and defending units

void done(); //clears vectores after the turn

};

fightclass::fig htclass(){

xl = 0;

yl = 0;

}

void fightclass::add Au(unit u){

at.push_back(u) ;

xl = u.getXloc();

yl = u.getYloc();

}

void fightclass::add Du(unit u){

dt.push_back(u) ;

}

bool fightclass::can fight(){

if(at.size() && dt.size()){

return true;

}else {return false;}

}

float fightclass::get Atk(){

float total;

for(int lp = 0; lp != at.size(); lp++){

total +=at[lp].getAttack();

}

return total;

}

I know the logic is not the best but it is the best I can do. This is

tricky and I am pretty confused. I would like to find some way of

making the logic steps easier. There may be some simple error that I

overlooked or it can be totally screwed up.