Part 40: Hex Wars: An autopsy (Part 4)
Hex War: An Autopsy: Part 4Last time, all we did is get ready to play a tone. It just took a while to explain how. Can we do better this time? Let's find out.
code:
30 DIM J, K, HT, HB, CT, CB, J1, J2, A, B, C, D, E
40 DIM ARMY(31, 6, 1), BTL(64, 1, 3), MAP(9, 9, 2), FQ(20, 1), NX(1), C(2)
50 CN = 12:DIM CIT(CN, 1)
Line 30 is a bit odd, in that it DIMs non-array variables. This is technically legal (even if some references say otherwise), but pointless, as ordinary variables are created and initialized when you first use them. (About the only reason I can think of to do this is if memory's tight and you want to make sure your program won't crash when it tries to use the variables later.) Incidentally, you don't have to DIM arrays either, as long as you want a single-dimensioned array with 11 elements (0-10). As you can see, though, all of the arrays defined here have a different size.
So what are all these variables? Well, in order of definition:
- J and K are temporary variables, used for multiple purposes.
- HT and HB are used to identify a particular hexagonal cell on the map. The subroutine that moves the cursor to a cell uses these. (The game uses T and B, not DIMmed here, to identify cells; these values are mapped to X and Y coordinates as needed. HT and HB are named in reference to this.)
- CT and CB are also used as cell references in a few different places in the program.
- J1, J2, A, B, C, D, and E are temporary variables.
- ARMY is an array used to keep track of information about each army in the game. (Note that, internally, the name of the array is AR; variable names are a single letter, optionally followed by a single letter or digit. ARTICHOKE(x, y, z) would refer to the same array. Calling it ARMY is therefore an aid to the programmer's memory.) ARMY(x, y, z) contains the following information about the xth army of player z (remember, internally player 2 is player 0):
- y = 0: The number of robots in the army that are ready to fight. (This is what's displayed as the army's strength.)
- y = 1: The number of robots in the army that are injured. When the army is no longer in combat, its injured robots are transferred to the reinforcement queue and will become available again in five turns. If an army is wiped out, its injured robots are captured by the enemy and placed into their reinforcement queue, where they will take seven turns to be repaired and reprogrammed.
- y = 2: The number of robots in the army that are lightly dazed. These robots will be available to fight again next turn. If the army is defeated before then, these robots are captured by the enemy and put in the reinforcement queue; reprogramming them takes three turns.
- y = 3: The number of robots in the army that are heavily dazed. All robots dazed in battle are first heavily dazed; after one turn, they become lightly dazed. Reprogramming heavily dazed robots also takes three turns.
- y = 4: The T coordinate of the army.
- y = 5: The B coordinate of the army.
- y = 6: Whether or not the army cannot move (1 means the army is immobilized). Armies can't move if (a) they've used all of their movement points for the turn or (b) they're adjacent to an enemy army.
- y = 0: The number of robots in the army that are ready to fight. (This is what's displayed as the army's strength.)
- BTL is an array used to keep track of information about ongoing battles. (Again, the internal name of the array is BT.) BTL(x, y, z) contains the following information about the xth battle going on:
- z = 0: The number of the army of player y that's in this battle.
- z = 1: The number of robots in that army that could potentially be vaporized this turn. Vaporized robots are permanently removed from play.
- z = 2: The number of robots in that army that could potentially be injured this turn.
- z = 3: The number of robots in that army that could potentially be dazed this turn.
I'll get into the gritty details of how many robots actually suffer those fates when I get to the actual battle code, but a dice roll is involved for each category; based on the dice roll, player 1's army loses x% of its possible losses, while player 2's army loses (100-x)% of its possible losses.
- z = 0: The number of the army of player y that's in this battle.
- MAP (internally, MA) is an array used to keep track of information about the map. MAP(t, b, x) contains the following information about the map cell (t, b):
- x = 0: The number of the army present in the cell, if any.
- x = 1: The number of the owner of the cell, if any. Cells start neutral - a value of 0 - and gain a value of 1 (for player 0) or 2 (for player 1) when visited by an army. If there is an army in this cell, it therefore belongs to this player.
- x = 2: Whether or not this cell contains a city. Oddly enough, this value is only used once in the code, to make the cursor red when it's over a cell with a city in it. For all other purposes, the CIT array, defined below, is used.
Here's a reference for the map, by the way. Cells are numbered as T.B.
- x = 0: The number of the army present in the cell, if any.
- FQ is an array used to keep track of reinforcements. FQ(x, y) is the number of reinforcements for player y that will arrive in x + 1 turns.
A few notes on this:
- Only the first nine turns' worth of reinforcements are shown at any time.
- The only time reinforcements are put in the queue beyond seven turns (injured robots captured from the enemy) is at the beginning of the game, when this array is loaded with random reinforcements. Once those reinforcements run out, there are no more random ones.
- In addition to capturing and/or repairing robots, each turn you also gain a number of robots equal to the number of map cells you control. These robots take two turns to enter play.
- Only the first nine turns' worth of reinforcements are shown at any time.
- NX is an array used to keep track of how many armies each player controls. NX(x) is the number of armies player x controls plus one.
- C is an array used to count things. Specifically, it's used when checking victory conditions to tally up the number of cities or hexes controlled or occupied by each player, depending on the game mode. C(0) counts neutral cells, C(1) counts cells owned by player 0, and C(2) counts cells owned by player 1. This corresponds to the possible values of MAP(t, b, 1).
- CIT (internally, CI) is an array used to keep track of the coordinates of each city in the game. CIT(x, 0) is the T coordinate of the xth city, while CIT(x, 1) is the B coordinate. (Oddly enough, CIT(0, x) is not used.)