I’ve enjoyed playing W. R. Hutsell’s series of turn-based war games since childhood. Mr. Hutsell generously provided me with the source code to these games and has allowed me to release that code under an MIT license. The first game I’ve worked on is VGA Civil War Strategy which was written in several iterations of Microsoft’s BASIC. You can find the code repository I’ll be using throughout the upcoming series of posts on GitHub.
What Series of Posts?
I’m looking to document for myself and others the process of refactoring, testing, and otherwise working with legacy code. You can find other reasons I’m undertaking this project in the README.md for the code repository.
First Steps
After creating a fresh GitHub repository I added the README.md closely followed by a .gitattributes file. For my purposes the file is quite simple: .exe binary .vga binary
. This ensures that Git doesn’t attempt to convert the binary files into text files.
My next step was to add all of the files relating to VGA Civil War Strategy that W.R. Hutsell provided me. I’ve then created a branch called snapshot-original that contains these original files. This branch won’t be changed and will be an easy way for anyone to access the earliest available version of the source code.
Diving Into the Code
There are several code files for this project, namely:
- CWS14.BI
- CWSMENU.BAS
- CWSTRAT.BAS
- CWSTRAT2.BAS
The BI file is a BASIC INCLUDE file and allows one to create declarations for subroutines, functions, variables, etc. that are shared across multiple files. For example, each of the .BAS files above includes the line: ' $INCLUDE: 'cws14.bi'
.
Our main entry point for the program is CWSTRAT.BAS
. Lets start there.
DEFINT A-Z
Microsoft BASIC’s default data type is SINGLE. This oftentimes provides unnecessary precision for variables. One can change the default data type to integer using DEFINT A-Z
. The compiler will now treat any variables that isn’t explicitly typed as an integer instead of single.
DECLARE
Used to DECLARE
a sub or procedure. It isn’t required but one can include parameters in the declaration. For example: DECLARE SUB armxy (x%, y%, z%)
.
DIM SHARED
The DIM
statement declares a variable and allocates memory for the variable.
When SHARED
is added to DIM
it allows the variable to be shared across the entire “module” (file).
In this case we have two arrays being declared, the first array (graphic
) starts at 1 and goes to 1564. The latter (Ncap
) goes from 0 to 60. Not that Ncap
starts at 0 because that is the default starting index for arrays in MS BASIC (and most other languages) whereas graphic
starts at 1 because it is explicitly set to start at 1 rather than 0.
Including Files
As we’ve already noted earlier, one can include a file (in this case a .bi file, but it could be a .bas file as well) using the syntax: ' $INCLUDE: 'nameoffile.ext'
, e.g. ' $INCLUDE: 'myfile.bas'
.
We Didn’t Change Anything?
Correct. At this point we are primarily attempting to understand the code as it is rather than change it. More to come.