THE GAME ROOM
By Tom Repstad
In this new column we hope to accomplish a forum where readers can get reviews of games as well as information regarding the theory of games, hints, and tricks on how to write creative and interesting game programs. If you have any particular questions or problems about computer games, drop us a line, and we'll try to answer them through this column. In this issue I would like to address the problem of initializing computer games in particular a method I have found that allows the computer to randomly distribute objects (ships, armies, treasures, etc... ) around a playfield It is assumed at this point that parts of the playfield are restricted (ie. not allowed for play for some reason).
For example let's say we are going to a naval battle game. One of the first things to do would be designing the playfield I have laid out mine to took something like this:
The area's with an 'X' inside are islands, and are not allowed for play, Let us assume that we don't always want the same number of islands or to place them in a fixed position each time. The reason being twofold:
1) Maintaining a constant playfield can lead to the development of 'quick win' strategies on the part of the human player.
2) It can get quite boring when the player knows ahead of time \A hat he can expect from the computer game.
The problem basically becomes how do we vary the playfield and place the computer's ship around the playfield randomly each time the game is played? There are further complications that are not readily apparent at this point, namely:
1) How do we randomly distribute n number of islands
2) How do we prevent overlapping of islands
3) How do we tell the computer where these islands are
4) And how are we going to randomly distribute our ships about the playfield without putting any in the islands.
This would appear to be a formidable programming task, however, we can accomplish the entire procedure as well as having established a data base that we can easily access later in the program in less than 50 lines of code. Difficult you say? Not if you have 'SETEMUP' & 'ISITIN', two nifty subroutines that will do the bulk of all the work for us. We will take a look at them shortly but first lets take a took at problem # 1. A simple random number generator function can give us any number of random X,Y coordinates for n number of islands. What we would do prior to generating the random X, Y coordinates is to create a table of corner points that define our islands with a maximum of maybe 15 corner points per island. These cornerpoints would be expressed in the table as relative positions from the center of the island, we would in turn then add the random X,Y coordinates we just generated to the individual cornerpoints as a displacement value. We can also randomly select which of the island shapes we want from the island table.
Thus we would be effectively placing an island in the playfield that has a centerpoint of the random X,Y we just generated. As for problem #2, if we limit the size of the islands to fit within some relative radius, we can then use a simple distance check between the two X,Y coordinates that define the centerpoints of our islands. Note that each new island must be checked against all preceding islands.
This takes care of problems 1 and 2 , but 3 and 4 are a different matter. We cannot use the radius approach to place the ships around the playfield, remember we are defining the islands by a set of cornerpoints, the radius approach would cause much space that is valid to appear as being invalid to the computer. (To check this out draw an island, then draw a circle around the center of the island, making sure that no part of the circle touches the island!)