Sign in to follow this  
alvarofrank

Army in Arrays or Matrix?

Recommended Posts

alvarofrank    122
Greeting from a relative new member here, I am currently learning C# and as side study i have been developing a Simple Game Design to apply concepts and ideas as i learn them. here Image Hosted by ImageShack.us For the Main global Settings I use a class. To administrate the gold i use another called bank, and so on. I also have many properties Setters and getters to modify values as needed. My main problem now are The units. Each Squadron (of archers horses or infantry) of units has its own stats, and is conformed of 6 single units. Each unit can affected by the terrain. So when it comes to creating the units my brain freezes. Each squadron will be placed on a map viewed as chess board occupying 6 squares. Each one is a unit. Each Army is conformed of 15 units top. My problem is to Represent each squadron or unit individually. If i create 15 Archers for example, each squadron should have its own stats. but should i store on a matrix the reference to its stats or create a matrix inside a matrix for this? Another concern is the name of the squadrons itself, if i use a template for archers, each time its created they should have their own Name/reference so its stats can be modified and accessed as necessary (the morale for example). How can that be done automatically? static void Archer() .... archer() = new Archer archer() = new archer2 .....? If anyone has suggestions on how to handle each unit, Squadron and Stats.

Share this post


Link to post
Share on other sites
Black Knight    769
If I'm not mistaken C# has "collections" like ArrayLists and other stuff much like the C++ vector,list stuff.
You can use those collections to store the army of the player.
And iterate over the list to update modify each unit inside it.


Share this post


Link to post
Share on other sites
Dekasa    127
Well... why don't you make them all classes, themselves?

So say, each player gets an army class, which in it has an array with 15 "spots" that each spot is a reference to a squadron. Squadron itself is a class, which holds an array of 6 soldiers.

Examples:



public class squadron
{

Unit[] units;
String name;
Point position; //whatever you use to say where this is, this is it.

public squadron()
{
units = new Unit[6];
this.name = random name from pre-generated list, or however you assign them.
}

private Unit search(int key)
{
for(int i = 0; i < units.Length; i++)
{
if(units[i].key == key)
return units[i]; //return the unit you searched for.
}
//other things that squadrons are responsible for (line order of the units, that stuff)
}



And the Unit class.



public class Unit
{
string type;
int attack, defense, health, other stats; //you could also use an array for all of these
string state; //this is for whether they're attacking, charging, anything like that

public Unit(string type, int health, int attack, int defense, etc.)
{
this.type = type; //set your archer, spear, whatever stuff.
//assign all other stats to what is sent in.
}


Not sure if any of that helps at all, but I don't see why you need an array, or matrix, or anything. Just have the squadron class have methods that iterates through the array and has each unit attack or whatever. Any properties that are entirely squad based, have it stored in the squad. Army can also be a class, probably with a linked list of all the squadrons, and an int # of how many squads there are in the army, so if they try to buy/build a new squad, and there are already the max amount, it won't let them. Army holds all the squadrons & manipulates their values by iteration or a search function, squad holds all the individual units & manipulates them the same way, and unit holds health, attack, things like that. If this doesn't make sense, say so, and I'll try to clarify.

Share this post


Link to post
Share on other sites
alvarofrank    122
Your suggestions look great, i will try to implement them as i can. here is an example of a unit i have so far:
Quote:

class Unit
{
private string Type;
private int pHealth;
private int pAttack;
private int pDefense;
private int pCharge;
private int pMorale;
private int Range;
private int Speed;
private int Posx;
private int Posy;

public void Die()
{
pHealth = 0;
Posx = -1;
Posy = -1;
}
public void TakeDamage(int Damage)
{
if(pDefense < Damage)
pHealth = pHealth + pDefense - Damage;
}
public void MoveFw()
{
if (Posx != 60)
Posy = Posy + 1;
}
public void MoveBk()
{
if(Posx != 0)
Posy = Posy - 1;
}
public void MoveRg()
{
if (Posx != 60)
Posx = Posx + 1;
}
public void MoveLf()
{
if (Posx != 0)
Posx = Posx - 1;
}

public int Attack
{
get
{
return pAttack;
}
}
public int Charge()
{
Posy = Posy + 2;
return pAttack;
}
}

attacking will be handled by a battle operator that calculates every units position, health, etc. I dont know if there are mor effcient ways to handle this becuase between 2 players i could have a maximum of 90 units per player, 15 squadrons, and a lot of variables. Thats a lot in paper but i dont know in real memory.

Share this post


Link to post
Share on other sites
Dekasa    127
Well, each has maybe 20 integers, times 90, comes to 1800, times 32 bits, comes to 58,000 bits, or just over 8,000 bytes, which is about 8 megabytes, so.... insignificant, probably. I suppose you could use short, or byte if you needed to cut down on memory, but then you have to cast all over the place, I'd just leave it how it is. Fix it later if it's necessary.

Just looking over your code...

die()
this should probably take place in the army level, then go to squad, then go to the unit. Each would have a Die() function, and each one would remove that unit from its own knowledge of it, and call the Die() of whatever's below it.

Since you mentioned you were new to C#, I'll mention this.

If you need to take PosX and add a number to it, you do this:
PosX = PosX + number;

All you NEED to do, is this:
PosX += number;

That works with subtraction ( -= ), multiplication ( *= ), and division ( /= ). Maybe other things, too. It's odd, but makes things look nicer. You don't have to use it, if you don't want, though.

It should work nicely, that way. Hope that helps.

Share this post


Link to post
Share on other sites
alvarofrank    122
thnks for the advice!
Quote:
That works with subtraction ( -= ), multiplication ( *= ), and division ( /= ). Maybe other things, too. It's odd, but makes things look nicer. You don't have to use it, if you don't want, though.

I will implement some other methods on the squad class that will be more general purpose and i will need to override the move method for units that move faster etc. I must also add several gets and sets methods to retrieve data like the Attack point in order to use them in other units to deal the damage. Its going slowly but i hope the pieces fit together later on.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this