data array??

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

data array??

Postby alloydog » Thu Jul 27, 2006 4:17 pm

I'm not a 100% sure if I'm on the right track...

I am going to try and right a programme (for a wargame) that calculates the damage done by a weapon. The weapon has several 'characterisitics', being:
• name
• one- or two-handed
• number of dice to roll (if one-handed)
• number of dice to roll (if two-handed)
• bonus points (if one-handed)
• bonus points (if two-handed)

Some weapons can be used only two-handed, or only single-handed. for example:

Code: Select all
                ONE-HANDED    TWO-HANDED
TYPE           DICE   BONUS  DICE   BONUS
Broadsword      -       -     5       2
Bastardsword    3       2     4       0
Sword           3       0     -       -


How would I put this information so that a C++ programme could use it?

The aim is for the used just to select the weapon used and the programme returns a value for the damage done.

Is data array the correct term?
User avatar
alloydog
LXF regular
 
Posts: 600
Joined: Thu Apr 07, 2005 7:32 pm

RE: data array??

Postby GMorgan » Thu Jul 27, 2006 4:36 pm

Personally I'd create a struct to account for the weapon. Prehaps something like this.

Code: Select all
typedef struct{
string name;
int numhands;
int weapontype;
int basedamage;
}weapon;


Here name is obvious, numhands is the handedness of the weapon so obviously 1 and 2 are the correct values. The value weapontype would contain an index for the weapon. So sword may enter 1, mace 2, bow 3 etc. Variable basedamage would give you your lowest damage value.

Just add fields for all the other posibilities then create an array of them. So your array might be created like this.

Code: Select all
weapon allweapons[x];


Where x is the number of entries. To access the different fields you use the following.

Code: Select all
allweapons[x].numhands = y;

or
Code: Select all
y = allweapons[x].numhands;


When called like that all the normal operations work on them as they would for the normal datatypes including assignments as LV's and RV's as above or for use in comparisons.

//edit - a quick tip about arrays. They are indexed from 0 so 0 is the first entry not 1.//
GMorgan
LXF regular
 
Posts: 684
Joined: Thu Jan 12, 2006 6:58 pm
Location: South Wales, UK

RE: data array??

Postby alloydog » Fri Jul 28, 2006 7:08 am

Thanks, that's given me somewhere to start from!
User avatar
alloydog
LXF regular
 
Posts: 600
Joined: Thu Apr 07, 2005 7:32 pm

RE: data array??

Postby GMorgan » Fri Jul 28, 2006 8:05 pm

Another thing, remember to place the struct definition in the files header rather than inside a function.
GMorgan
LXF regular
 
Posts: 684
Joined: Thu Jan 12, 2006 6:58 pm
Location: South Wales, UK


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron