The Journey of a New Computer Programmer

A longtime computer-literate who wants to learn "real" programming language(s).

Saturday, May 12, 2007

A typical section of ND combat code


a_ca_lost = int(b_fi_FP/a_ca_AR)
b_ca_lost = int(a_fi_FP/b_ca_AR)

if a_ca_lost > a_ca_left:
a_ca_lost = a_ca_left

if b_ca_lost > b_ca_left:
b_ca_lost = b_ca_left

a_ca_left = a_ca_left - a_ca_lost
b_ca_left = b_ca_left - b_ca_lost


This is a typical section of code from my current approach at the problem of writing the ND combat code.

This the fighter-versus-carrier code. It has two sections, really: Fleet A's fighters versus Fleet B's carriers, and Fleet B's fighters versus Fleet A's carriers.

(According to my rules, enemy ships of a similar type should fire simultaneously, that's why I don't have one after the other in separate sections)

a_fi_FP has already been defined as the firepower of a Fleet A fighter, times the total number of Fighters Fleet A has left. (I suppose *total* firepower would be more accurate.) b_ca_AR has been set up to simply pull the correct 'armor' value from the ship-stats list.

The int() makes sure that a fractional amount of a ship isn't destroyed (that would be, in terms of 'flavor', impossible.) The 'if a_ca_lost > a_fi_left' type of statement exists in cases of overkill - where B's firepower is more than enough to eliminate A's armor. 'a_ca_lost = a_ca_left'. means that Fleet A loses all the carriers it had left. In terms of 'flavor', that is quite possible, but losing more than you had is not.

Say 120 fighters are shooting at 2 carriers.
The firepower of each fighter is 12, times 120 is 1440.
The armor of each carrier is 700.

Let's look at it this way (I believe this section will now make it seem more logical what's going on here):

Each fighter has a gun that can rattle off 12 shots. The entire fleet of fighters can rattle off 1,440 shots.
Each carrier can take 700 shots before it is destroyed.

1,440/700 = 2, remainder 40.
int(1440/700) = 2.

700 shots can be fired at an enemy carrier. Since no more are needed to get rid of it, that carrier is sunk. The fighters have 740 shots left over, and after firing off 700 of those, they can sink 1 more carrier, and have 40 left. Since those 40 aren't enough to destroy another carrier, they (the 'remainder') accomplish nothing.

3 Comments:

At 3:27 PM, Blogger Lee said...

You can place your code inside HTML "pre" tags to preserve the whitespace. That will make it much easier to read.

 
At 5:51 PM, Blogger Alan said...

Brilliant!
Also, the 'pre' tage changes the font slightly, making the code stand out on the page.
More generally, what does this tag do?

 
At 8:40 AM, Blogger Lee said...

pre = preformatted text

http://www.w3schools.com/tags/tag_pre.asp

 

Post a Comment

<< Home