By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Why won't this work.

P: n/a
I am writing a game and I read units into a map manager and there they
are placed on a grind and if they are in the same space they will
fight. I have an attacker vector and defender fector and if they have
units they will fight. That all works but once they fight to combat
affects don't seem to take affect. I tested my program at earlier
states.

if(red){
for(int lp = 0; lp != rteam.size(); lp++){
m.atIn(rteam[lp]);} //reads in the attracking units to map
panager
for(int lp = 0; lp != yteam.size(); lp++){
m.dtIn(yteam[lp]);} //reads defender
} else { //same but for the other turn
for(int lp = 0; lp != rteam.size(); lp++){
m.atIn(yteam[lp]);}
for(int lp = 0; lp != yteam.size(); lp++){
m.dtIn(rteam[lp]);}
}
m.fight(); //directs units in the same space to fingt.
//rteam[1].tomark(); <-this works units are killed here
//yteam[1].disbersed(); <-this also works units are dispered
correctly
kill(rteam); //kills any red units
kill(yteam); //kills any purple units
m.clear(); //clears the map manager.
changeTurn(red, ppl); //changes red, purple turns

All my stuff works here in the main loop but once I send units rteam,
yteam to an object a refrences they don't get the combat affects:

Here is some sample code:

void mapmgt::atIn(unit& u){
int x = u.getXloc();
int y = u.getYloc();
h[y][x].at.push_back(u);

struct hold{
vector<unit>at; //holds the attackers
vector<unit>dt; //holds the defenders
float getAt(); //adds attacking factors
float getDt(); //addus up defending factors
void killA(); //attacker eliminated
void dispA(); //attacker disbersed
void dispD(); //defenders same as attackers
void killD();
};

void mapmgt::fight(){
float atk = 0;
float def = 0;
float odds = 0;
for(int lp1 = 0; lp1 != ylen; lp1++){
for(int lp2 = 0; lp2 != xlen; lp2++){
if(h[lp2][lp1].at.size() 0 && h[lp2][lp1].dt.size() 0){
atk = h[lp2][lp1].getAt();
def = h[lp2][lp1].getDt();
odds = atk/def;
int roll = (rand()%6)+1;
//int mod = t[b->GetSpace(lp2,lp1)].getDef(); <-this dosn't
work, seperat issue
//roll+=mod;
int res = tbl.result(roll, odds);
if(res == 0){h[lp2][lp1].killA();} //atk elim <-these don't
seem to be working
if(res == 1){h[lp2][lp1].dispA();} //atk disp imposing the
combat results
if(res == 2){h[lp2][lp1].dispD();} //def disp on the units
if(res == 3){h[lp2][lp1].killD();} //def elim

MessageBox(NULL, "There is fighting!", "simulation!", MB_OK);
}
}
}

}

Example of combat effects, works in the main loop but not here and this
is my main problem:

void hold::killA(){
MessageBox(NULL, "Atk elim!", "simulation!", MB_OK);
for(int lp = 0; lp != at.size(); lp++){
at[lp].tomark();
}
}

Sep 19 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
JoeC wrote:
I am writing a game and I read units into a map manager and there they
are placed on a grind and if they are in the same space they will
fight. I have an attacker vector and defender fector and if they have
units they will fight. That all works but once they fight to combat
affects don't seem to take affect. I tested my program at earlier
states.

if(red){
for(int lp = 0; lp != rteam.size(); lp++){
[snip]

Problem #1: your index is going past the end of the vector. Use <
rteam.size(), not != rteam.size().

If there are more problems, post a complete but minimal sample that
demonstrates the problem (i.e., one that we can cut and paste unchanged
into our editors to see the problem). Also see these guidelines for
posting code that doesn't work:

http://parashift.com/c++-faq-lite/ho...t.html#faq-5.8

Cheers! --M

Sep 19 '06 #2

P: n/a

mlimber wrote:
JoeC wrote:
I am writing a game and I read units into a map manager and there they
are placed on a grind and if they are in the same space they will
fight. I have an attacker vector and defender fector and if they have
units they will fight. That all works but once they fight to combat
affects don't seem to take affect. I tested my program at earlier
states.

if(red){
for(int lp = 0; lp != rteam.size(); lp++){
[snip]

Problem #1: your index is going past the end of the vector. Use <
rteam.size(), not != rteam.size().

If there are more problems, post a complete but minimal sample that
demonstrates the problem (i.e., one that we can cut and paste unchanged
into our editors to see the problem). Also see these guidelines for
posting code that doesn't work:
OK that is an easy start.

Sep 19 '06 #3

P: n/a

mlimber wrote:
JoeC wrote:
I am writing a game and I read units into a map manager and there they
are placed on a grind and if they are in the same space they will
fight. I have an attacker vector and defender fector and if they have
units they will fight. That all works but once they fight to combat
affects don't seem to take affect. I tested my program at earlier
states.

if(red){
for(int lp = 0; lp != rteam.size(); lp++){
[snip]

Problem #1: your index is going past the end of the vector. Use <
rteam.size(), not != rteam.size().

If there are more problems, post a complete but minimal sample that
demonstrates the problem (i.e., one that we can cut and paste unchanged
into our editors to see the problem). Also see these guidelines for
posting code that doesn't work:
OK that is an easy start but didn't solve my problem.

Sep 19 '06 #4

P: n/a

JoeC wrote:
mlimber wrote:
JoeC wrote:
I am writing a game and I read units into a map manager and there they
are placed on a grind and if they are in the same space they will
fight. I have an attacker vector and defender fector and if they have
units they will fight. That all works but once they fight to combat
affects don't seem to take affect. I tested my program at earlier
states.
>
if(red){
for(int lp = 0; lp != rteam.size(); lp++){
[snip]

Problem #1: your index is going past the end of the vector. Use <
rteam.size(), not != rteam.size().

If there are more problems, post a complete but minimal sample that
demonstrates the problem (i.e., one that we can cut and paste unchanged
into our editors to see the problem). Also see these guidelines for
posting code that doesn't work:

OK that is an easy start but didn't solve my problem.
Then follow the instructions second part of my previous response.

Cheers! --M

Sep 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.