Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

data array??

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
alloydog
LXF regular


Joined: Thu Apr 07, 2005 8:32 pm
Posts: 600

PostPosted: Thu Jul 27, 2006 5:17 pm    Post subject: data array?? Reply with quote

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:
                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?
Back to top
View user's profile Send private message
GMorgan
LXF regular


Joined: Thu Jan 12, 2006 6:58 pm
Posts: 684
Location: South Wales, UK

PostPosted: Thu Jul 27, 2006 5:36 pm    Post subject: RE: data array?? Reply with quote

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

Code:
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:

weapon allweapons[x];


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

Code:

allweapons[x].numhands = y;

or
Code:

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.//
Back to top
View user's profile Send private message
alloydog
LXF regular


Joined: Thu Apr 07, 2005 8:32 pm
Posts: 600

PostPosted: Fri Jul 28, 2006 8:08 am    Post subject: RE: data array?? Reply with quote

Thanks, that's given me somewhere to start from!
Back to top
View user's profile Send private message
GMorgan
LXF regular


Joined: Thu Jan 12, 2006 6:58 pm
Posts: 684
Location: South Wales, UK

PostPosted: Fri Jul 28, 2006 9:05 pm    Post subject: RE: data array?? Reply with quote

Another thing, remember to place the struct definition in the files header rather than inside a function.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast