Initializing Battlecell and Starting the Game

The worksheet module named Battlesheet contains most of the game's code because this worksheet serves as the user interface and contains the Command Button control that starts play. The code begins with a few module-level variables that are used in multiple procedures in this module.

Option Explicit

Private allowSelection As Boolean Private gameStarted As Boolean Private ships As Variant Private Const NUMSHIPS = 5

The module level variables allowSelection and gameStarted are used by the program to distinguish between the two different types of cell selections made by the user and whether or not the game is active. The first type of selection occurs when the user places his or her ships and the second type of selection occurs when the user selects a target on the computer's grid. These variables should be initialized at the start of the game and altered after the user has placed his/her ships.

You may recall in Chapter 2 that I recommended you not use variant variables because variants can slow program execution speed and make your program more difficult to read. Well, sometimes variant variables are just a little too convenient, and here is one example. The variable ships is declared as a variant because I intend to use this variable to hold an array of strings representing the types of ships (carrier, battleship, and so on). Unlike most programming languages, VBA does not allow arrays to be initialized in a declaration, so I have to use a variant variable and VBA's Array() function to initialize the array (see InitializeGame() sub procedure). Alternatively, I could declare a string array with five elements (Private ships(4) As String), but then I have to initialize each element separately (ships(0) = "Carrier", ships(1) = "Battleship", and so on) and I find that annoying (decide your own preference).

Private Sub cmdStart_Click() cmdStart.Enabled = False InitializeGame ClearBoard

Range("Output").Value = "Place your " & ships(numShipsPlaced) & _

": Select " & shipSize(numShipsPlaced) & " contiguous cells"

End Sub

The code entered in the Click() event of the Command Button control is short and simple. First the Command Button control is disabled before two sub procedures are called to initialize program variables and clear the game board. The ClearBoard() sub procedure is the same procedure called from the BeforeClose() event of the Workbook object. The last statement outputs a message to the user indicating what ship must be placed. The variables shipSize and numShipsPlaced are global variables declared in a standard module (listed later) and represent the total number of cells that make up each ship and the number of ships already placed by the user, respectively. The variable shipSize is another array variable of type variant.

Private Sub InitializeGame()

'Initialize variables for starting the game.

ships = Array("Carrier", "Battleship", "Destroyer", "Submarine", "Patrol Boat") shipSize = Array(5, 4, 3, 3, 2) Set pRange = Range("Player") Set cRange = Range("Computer") numShipsPlaced = 0 allowSelection = True gameStarted = False End Sub

The InitalizeGame() sub procedure initializes the module level and global variables used by the program. The only two variables I have not already discussed are pRange and cRange which are both global variables (type range) used to represent the 10 by 10 grids for the player and computer, respectively. Note that the range B2:K11 was defined in the Excel application with the name "Player" and the range O2:X11 was defined with the name "Computer". Figure 5.17 shows the Battlecell worksheet with the "Player" range selected.

Defined name for the selected range

The Battlecell worksheet with the "Player" range selected.

Defined name for the selected range

The Battlecell worksheet with the "Player" range selected.

public Sub ClearBoard()

'Clear the game grids and output cell.

Dim bothGrids As Range

Set bothGrids = Application.Union(Range("Player"), Range("Computer")) With bothGrids

.ClearContents

.Interior.Colorlndex = xlNone End With

Range("Output").Value = "" End Sub

The code in the sub procedure ClearBoard() effectively clears the player's and computer's grids of colors and values, and also clears the merged cells (J14:P15 defined with the name "Output" in the Excel application) of any help message that might be displayed. This procedure must have public scope so that it may be called from the BeforeClose() event of the Workbook object.

The range variable bothGrids is set to reference the combination of two ranges (the two ranges defined with the names "Player" and "Computer" in the application) using the Union() method of the Application object. This object variable is then used in a With/End With structure to clear the contents and background color of the player's and computer's grids. Note that I cannot use the pRange and cRange variables here because it is possible that this code may be triggered from the BeforeClose() event of the Workbook object before a game is started when these variables have not been initialized.

Biorhythm Awareness

Biorhythm Awareness

Who else wants to take advantage of biorhythm awareness to avoid premature death, escape life threatening diseases, eliminate most of your life altering mistakes and banish catastrophic events from your life.

Get My Free Ebook


Post a comment