big multi-dimensional arrays vs. OOP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

big multi-dimensional arrays vs. OOP

Post by liljester »

Ok, i have a design question.

I'm currently working on a project (online game) where we have armies that will do battle. now everyone in these armies are individual, and the battle will be played out by pitting these people against each other, until one army is defeated.

i start off by getting a list of attackers and defenders... so the main array and the combat order array looks like this

Code: Select all

$combat[unit_id][team] = "Attacker";
$combat[unit_id][name] = "name";
$combat[unit_id][dex] = $dexterity;
$combat[unit_id][per] = $perception;
$combat[unit_id][hp] = $hp;
$combat[unit_id][ac] = $ac;
$combat[unit_id][weapon_id] = $weapon_id;
$combat[unit_id][armor_id] = $armor_id;
$combat_order[unit_id] = $perception;
i do this for both attackers and defenders, making 2 arrays, the $combat array has all the units with all thier stats i need, the $combat_order array is sorted into the proper order (arsort) then i extract the keys so that i then have an array for battle order.

now i have all i need to go to battle...

so now for my question: is there an effecient way to do this in an object oriented manor? i've tried to think of a way, but im at a loss...

I have a lil OO experiance, but its limited to a few simple classes like a database abstraction layer, and a user class to track security and such.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

OOP encapsulates something behind an interface.

The first step is to imagine the kind of interface you would like. In a sense the beauty of OOP is that you can pretty much design any interface you like.

Can you summarise this by writing out the class's public methods - don't try to write any code as yet?
Last edited by McGruff on Mon Mar 08, 2004 11:17 am, edited 1 time in total.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Generally, the question "Array vs OOP" doesn't exist. Arrays are data-repositories, OOP is a programming style. Two very different pair of shoes.

There are some nice tutorials about OOP out there, among them:

http://www.zend.com/zend/tut/tutorial-johnson.php
http://www.zend.com/zend/tut/class-intro.php
http://www.zend.com/zend/tut/tutorial-johnson2.php

Alternatively: google :)
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

patrikG, you got hung up on the title of my post, not the actual question =) i understand what oop is (i really do!), and the meaning of an array... i listed the array to give an example of the kind of data im working with.

so let me refine my question...

is there a better way to do what im doing by making use of a class or classes?
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

I could see how an Offense, Defense, Combat class could be beneficial.
lastcraft
Forum Commoner
Posts: 80
Joined: Sat Jul 12, 2003 10:31 pm
Location: London

Re: big multi-dimensional arrays vs. OOP

Post by lastcraft »

Hi...

It sounds like a complex problem :).
liljester wrote:i start off by getting a list of attackers and defenders... so the main array and the combat order array looks like this
The advantage of OO is thet it allows you to express the problem and solution in a language that you can create yourself...

Code: Select all

$unit = $army->detach(array('infantry' => 100, 'sappers' => 5));
$unit->setOrders(new Manouver(23, 10));
Apart from the array (and a ForceDescription class could be used instead) all the words here refer to things that have meaning in the problem space. If you were trying to do the above two steps with arrays can you imagine the amount of clutter and mess you would get into?

The language you end up evolving is called the domain model. This is the big win.

yours, Marcus
eletrium
Forum Commoner
Posts: 34
Joined: Tue Feb 10, 2004 3:38 pm

Post by eletrium »

Here's a hint: There is an Object Design tool that allows you to design a set of classes, then automatically implements them in your choice of C++, Java, or Delphi.

Think general to begin with, which is why McGruff said don't write any code yet.

Btw I agree with what everyone above has said, it's good advice. I am just going to rephrase it from a different angle in case it clicks better seeing it explained from multiple angles.

An object is simply a manner in which you can lump data with the relevant functions to operate on that data.

If combat were to be done on a soldier to soldier basis, a Soldier class would be good to look at. The Data would be stuff like rifle type, ammo amount, health left. The functions might be stuff like FireGun which would decrement the ammo by 1 then use the Rifle type to calculate the actual effect of firing the rifle.

Ask yourself what is the related data that might be helpful to group together. Don't go all analysis paralysis on this either. Early attempts at object design are destined for crappiness, but we all go through that. Just plan on lots of revisions your first time around and don't worry about being perfect.

When I start programming something, I ask myself: what is the data, and what do I want to do with it?
Post Reply