473,890 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Logic question...

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.

Sep 4 '06
73 4335

Phlip wrote:
Bart wrote:
Moonlit wrote:
comp.games.deve lopment.program ming is probably more appropiate for this
question.
Why? His question was more or less about program logic, using mostly
standard C++ (save for one function). That's more on-topic than many
other posts here.

Only the OP should guess which group will provide the best answer.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
I think MY code works. It is a bit more complex and cumbersom than I
would like but I am pushing ahead on next part of the game.

Sep 6 '06 #11
Thanks Nathan :-)

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Nathan Mates" <na****@visi.co mwrote in message
news:12******** *****@corp.supe rnews.com...
In article <BU************ ****@newssvr25. news.prodigy.ne t>,
Phlip <ph******@yahoo .comwrote:
>>Only the OP should guess which group will provide the best answer.

Posters are entitled to guess, true. But, correcting guesses is the
job of newsgroup regulars, and prefectly reasonable.

Nathan Mates
--
<*Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A.
Heinlein

Sep 7 '06 #12
Moonlit wrote:
Thanks Nathan :-)
You correctly indicated a good newsgroup. Nobody doubted that.

Except Bart, who claimed you shouldn't have bounced.

I defended you, by saying you didn't bounce, you suggested.

Then Nathan attempted to refute my defense, which he didn't understand.

So I think your thanks are due me, not Nathan. ;-)

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Sep 7 '06 #13
Hi,

Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Phlip" <ph******@yahoo .comwrote in message
news:zQ******** *********@newss vr13.news.prodi gy.com...
Nathan Mates wrote:
>>>Only the OP should guess which group will provide the best answer.

Posters are entitled to guess, true. But, correcting guesses is the
job of newsgroup regulars, and prefectly reasonable.

The only topicality criteria relevant to a questioner is what newsgroup
will provide the best question. Once we suggestion one, we can no longer
declare whether an equally topical question belongs here or there. That
was the context.

When you offer another newsgroup, always reinforce that topicality is in
the poster's best interests. The answer "your question is off topic here
go away" doesn't increase the odds of useful participation.
I didn't see anyone say in this thread "your question is off topic here go
away"
I merely suggested a better newsgroup.

And yes if I quote from the FAQ "Ultimately this means your question must be
answerable by looking into the C++ language definition as determined by the
ISO/ANSI C++ Standard document, and by planned extensions and adjustments".
Well it isn't answerable by looking in the C++ language definition. So
basically the question is off topic. Yet you didn't see an OT in my header,
simply because I have better things to do than to police newsgroups. Besides
that if you look at my responses in the past you will notice that I try to
help whether OT or not OT.

I suggested a better newsgroup and thats all there is, the rest is a result
of your imagination, really.

>
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!

Regards, Ron AF Greve.
Sep 7 '06 #14
Moonlit wrote:
I merely suggested a better newsgroup.
I never said you didn't. However...
And yes if I quote from the FAQ "Ultimately this means your question must
be answerable by looking into the C++ language definition as determined by
the ISO/ANSI C++ Standard document, and by planned extensions and
adjustments". Well it isn't answerable by looking in the C++ language
definition. So basically the question is off topic. Yet you didn't see an
OT in my header, simply because I have better things to do than to police
newsgroups. Besides that if you look at my responses in the past you will
notice that I try to help whether OT or not OT.
Ultimately, the FAQ was written by a human, and it is not authoritative.

Ultimately, the term "ultimately " means "at the end of a chain of events".
So if I ask about the Class Factory Pattern, or MVC, or huffman compression,
and I ask in platform-neutral terms, then ultimately the exact answer will
depend on the C++ ISO Standard. Hence, by the criteria of the
non-authoritative FAQ, such questions are on-topic.

This newsgroup is not news:comp.std.c ++ , and posters should always pick the
narrowest newsgroup for their questions. So questions that only relate to
interpretation of The Standard belong there, not here.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Sep 7 '06 #15
In article <44************ **********@news .xs4all.nl>, "Moonlit" <news
moonlit xs4all nlsays...

[ ... ]
I didn't see anyone say in this thread "your question is off topic here go
away"
I merely suggested a better newsgroup.
I'm not even sure that's true -- at least to me, it seems that most of
the discussion has centered primarily around how to express his ideas in
C++, not so much about the fundamental design of the game itself. That
being the case, I think the questions are really more topical here --
though it may be that enough game programming is done in C++ that it
would also fit well there.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 7 '06 #16
In article <11************ **********@b28g 2000cwb.googleg roups.com>,
en*****@yahoo.c om says...

[ ... ]
This is all experiemtal. I am not sure how to implenmt an odds table.
All this is just test to see if the prvious code worked. What I want
to do is implemnt an odds table

1:3 1:2 1:1 2:1 3:1 4:1 5:1
0 ae ae ae ae ae ad ad
1 ae ae ae ae ad ad dd
2 ae ae ae ad ad dd dd
3 ae ae ad ad dd dd de
4 ae ae ad dd dd de de
5 ae dd dd dd ad de de

ae =attacker eliminated
ad = attacker dispersed
dd = defebder dispersed
de = defender eliminated
enum results { AttElim, AttDisp, DefDisp, DefElim };

results table[5][7] = {
{ AttElim, AttElim, AttElim, AttElim, AttElim, AttDisp, AttDisp},
{ AttElim, AttElim, AttElim, AttElim, AttDisp, AttDisp, AttDisp},
// ...

At least as I understand it, you'd then have to convert your odds to an
integer (something along the lines I used in show_odds might work).
You'd apparently index into the array above using the value on the die
in one dimension and the odds in the other dimension. Alternatively, you
might be able to simply multiply the odds by the die throw, and figure
out the result mathematically (your table looks like the relationship is
basically linear, so this probably wouldn't be terribly difficult.
I can see the differences from my code and can see the weakness of my
programming. I am not fimilliar with this kind of programming. Are
these algorithms? I am a bit confused and would have not come up with
this. Are there smaller experiment programs I can write to better
understand how to do things like that?
As it worked out, did this code more or less "bottom-up" -- i.e. looked
at your one long function, broke it down into some smaller functions,
and then composed a function that would call those in turn.

Generally, I'd advise doing more or less the opposite of that: start by
writing a description of what needs to happen in relatively general
terms. Especially at first, I'd advise breaking out pencil and paper for
this part of the task. Doing worry a lot about it being particular neat
or tidy, just get the general ideas of what needs to happen. In a lot of
cases, something on the order of an outline or bulleted list works
reasoanbly -- i.e. try to come up with a _few_ works that generally
describe a step.

Then take each of those steps, and do roughly the same thing again --
break it down into a number of simpler steps, each of which can still be
described in a few words.

When you've gotten to the point that everything is broken down into
relatively simple steps, the coding is (hopefully) pretty easy. The top-
level outline/list transforms more or less directly into the top-level
function. Probably nearly every line there will end up as a function
call. Each of the subordinate descriptions becomes another function as
well -- until each function has been broken down to the point that it's
a trivial little bit of code you can read/write/understand quite easily.

A few points: I'm really serious about the pencil and paper part -- if
you're at the computer, it's tempting to write code instead. At some
point, you might consider using one of the cool Computer Aided Software
Engineering (CASE) tools to draw UML or something like that -- but for
the moment, I wouldn't advise it. Despite decades of work, CASE tools
still really aren't the best for quickly roughing out a design; once you
have a relatively complete design, they can work nicely for making
incremental improvements, playing around with the effect of various
changes, and so on -- but that comes a bit later as a rule.
Yes, I just couldn't figure out how to do that. I simply want units in
the same space to add up attack factors then add up defedning factors
then have them fight. I put each attaking unit in some vector. Then
see it they were in the same locations as other units. Then I went to
the defending units and compared them with the other array and if they
are in the same location, put them together int he fightclass.
Okay -- for the moment, that's probably perfectly fine. One minor
refinement that might be helpful would be after you've done the above,
scan back through all the fightclass objects and simply remove any that
have an empty defender vector. That may not be the most elegant way to
produce the result, but at least that should be the result you're
looking for -- sometime later you can come up with a cleaner way of
getting the result.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 7 '06 #17

Jerry Coffin wrote:
In article <44************ **********@news .xs4all.nl>, "Moonlit" <news
moonlit xs4all nlsays...

[ ... ]
I didn't see anyone say in this thread "your question is off topic here go
away"
I merely suggested a better newsgroup.

I'm not even sure that's true -- at least to me, it seems that most of
the discussion has centered primarily around how to express his ideas in
C++, not so much about the fundamental design of the game itself. That
being the case, I think the questions are really more topical here --
though it may be that enough game programming is done in C++ that it
would also fit well there.

--
Later,
Jerry.
Not the whole game but this function. I am not a profetional
programmer but I do the best I can. I am wrighting this conbat routine
and it seems very complex and I have trouble keeping track of what I am
doing. That is not good. I am wondering if there is an easier way to
do what I am doing. I have been adding to my game, what I am doing
appears to work.
>
The universe is a figment of its own imagination.
Sep 7 '06 #18

Phlip wrote:
Moonlit wrote:
I merely suggested a better newsgroup.

I never said you didn't. However...
And yes if I quote from the FAQ "Ultimately this means your question must
be answerable by looking into the C++ language definition as determined by
the ISO/ANSI C++ Standard document, and by planned extensions and
adjustments". Well it isn't answerable by looking in the C++ language
definition. So basically the question is off topic. Yet you didn't see an
OT in my header, simply because I have better things to do than to police
newsgroups. Besides that if you look at my responses in the past you will
notice that I try to help whether OT or not OT.

Ultimately, the FAQ was written by a human, and it is not authoritative.

Ultimately, the term "ultimately " means "at the end of a chain of events".
So if I ask about the Class Factory Pattern, or MVC, or huffman compression,
and I ask in platform-neutral terms, then ultimately the exact answer will
depend on the C++ ISO Standard. Hence, by the criteria of the
non-authoritative FAQ, such questions are on-topic.

This newsgroup is not news:comp.std.c ++ , and posters should always pick the
narrowest newsgroup for their questions. So questions that only relate to
interpretation of The Standard belong there, not here.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
I am just trying to write a game and I feel I am getting a bit over my
head but still being a hard head, I make it work and push on. I am
sure what most programmers do. The only way to get experience is to do
it. It is one thing to learn syntax but it is another to apply good
programming practice in large projects. What my problem is is that I
program in a vaccuum, I don't have people with more experience letting
me know that there are better ways to do what I am trying to do. So
what I do is put this stuff out there and hope some one who has seen
simmiliar problems can give me some advice. I have already gotten some
good advice for other parts of the program. I would like to get this
part done then the game will be complete and I can expand it or go back
and improve the code so that it is easier to expand. If somone would
like to see the whole program en*****@yahoo.c om and I will send the
latests version program out.

Sep 7 '06 #19
In article <11************ **********@e3g2 000cwe.googlegr oups.com>,
JoeC <en*****@yahoo. comwrote:
>Not the whole game but this function. I am not a profetional
programmer but I do the best I can. I am wrighting this conbat
routine and it seems very complex and I have trouble keeping track
of what I am doing. That is not good. I am wondering if there is
an easier way to do what I am doing. I have been adding to my game,
what I am doing appears to work.
Managing "who's where" is best separated from the "combat routine."
Most games have some code (and data structures) dedicated to keeping
track of units on the map. This means that units know where they are,
and the map manager also knows which units are where. If your map is
just a 2D array of a fixed, "reasonable " size (under a million
entries), you can just make a 2D array of entries, and each entry is
just a linked list of all units in that cell. If you're working on
PCs, just burn some memory upfront, and work on optimizing it later--
plenty of data structures exist to manage sparse datasets.

You should also separate out code to to map manager so that you can
say "find all units within radius R of point X,Y" and it'll go do that
for you. That's code for the map manager, not a "combat routine," as
you'll probably want your UI code to be able to do the same thing--
clicking on a square selects all units in that square, or shift-drag
selects all units in a user-specified rectangle. If you're going to
use the same kinds of functionality in multiple places, then break it
out to a common place.

Nathan Mates

--
<*Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
Sep 7 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

60
7335
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm The detector requires javascript 1.0 to work. This translates to netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)
6
1574
by: Ted Singh | last post by:
(I apologize in advance if this is an obvious question, my experience is more with console or client-only apps) I am trying to build an web-based HTML UI, which will work with a web-server on the same machine (i.e., http://localhost/...). However, I do not want the logic source to be visible to the user to prevent customer support problems, intellectual property issues, etc. The requirements strictly call for a web-based UI running...
0
1909
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that sound strange. "Exception are divided into logic errors and runtime errors. Logic errors represent static errors that can be prevented and detected at compile time. Runtime errors represent dynamic errors that can be detected only at runtime."
2
1511
by: Simon Harvey | last post by:
Hi all, I was wondering how most developers handle the initial startup of their Windows Forms applications? When you make an application in Visual Studio, the IDE places Main in the form that VS makes for you. When I make my applications I sometimes feel uneasy about leaving the startup logic in the Form - it sort of seems the wrong place for it. I'm by no means an expert on such things (which is why I'm writing this post), but it...
3
1530
by: mca | last post by:
Hi everyone, I'm new to asp.net and i have a question about separating the html code from the programming code. i have an unknown numbers of entries in my table. I want to make a hyperlink for every entry in my table. So i query the database and get for example 3 entries back. So in a while loop i can make 3 hyperlinks with response.write(.......) etc.
24
14428
by: cassetti | last post by:
Here's the issue: I have roughly 20 MS excel spreadsheets, each row contains a record. These records were hand entered by people in call centers. The problem is, there can and are duplicate phone numbers, and emails and addresses even person names. I need to sift through all this data (roughly 300,000+ records and use fuzzy logic to break it down, so that i have only unique records.
6
2013
by: Simon Harvey | last post by:
Hi everyone, We have a need to make a Windows Forms (2.0) client application that will be installed on our clients site. The data that the application uses needs to be centrally available to a potentially large number of other sites, which would seem to leave us with our traditional approach, or having a central database server on a dedicated server someplace.
14
3582
by: rabbitrun | last post by:
Hi Everyone, I work for a financial company. I am planning to give a presentation to rest of the development team (15 people) here on moving server side logic to client-side javascript for an internal intranet application rewrite. This approach will definitely stir up hot debate from hardcore server-side Java folks who wants to do UI stuff even on the server!. Since I am pretty much known as the JS or UI Guy of the group, my Boss...
29
2243
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a blank contract, fill in the data and then save a "new" contract. I have a method in my contract class called "Save" which is called like this... dim oContract as new Contract
15
2452
by: bruno.desthuilliers | last post by:
On 27 juin, 18:09, "John Salerno" <johnj...@NOSPAMgmail.comwrote: For which definitions of "content" and "logic" ??? The point of mvc is to keep domain logic separated from presentation logic, not to remove logic from presentation (which just couldn't work). Templating systems are for presentation logic. Whether they work by embedding an existing complete programmation language or by providing they're own specialised mini-language (or...
0
9979
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6058
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4682
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.