Player Selections Placing Ships and Firing at the Computer

The program requirements state that the user must select his or her choices for ship and target locations by selecting specific cells on the worksheet. The user's selections for ship locations and targets are captured by the SelectionChange() event procedure of the Worksheet object. Specifically, the Target argument passed to the SelectionChange() event procedure holds the range selected by the user. The code in this procedure will have to distinguish between the two types of selections the user might make (i.e., ship placement or targeting of computer's ships). Custom procedures will be needed for validating the user's selection, marking the location of the user's ships, and marking the location of the user's targets.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'Test if player is firing at the computer.

'This is first because LocatePlayerShip will turn gameStarted variable to true.

If gameStarted Then PlayerFire Target

'Test if player is setting his/her ships before game starts.

If allowSelection Then LocatePlayerShip Target End Sub

The SelectionChange() event procedure consists of two procedure calls based on the values of the allowSelection and gameStarted variables. If allowSelection is true, then the user is selecting worksheet cells for locating his or her ships. If gameStarted is true, then the user has placed his/her ships and is firing at targets on the computer's grid. It is critical that at no time during the execution of the program that the values of both variables are true.

Private Sub LocatePlayerShip(Target As Range)

'Capture user's selections for ship locations. If selection is 'valid then color it blue and display message for next ship.

Dim errMsg As String

If RangeValid(Target, "Player", errMsg) Then Target.Interior.Color = RGB(0, 255, 255) numShipsPlaced = numShipsPlaced + 1 If (numShipsPlaced < NUMSHIPS) Then

Range("Output").Value = "Place your " & ships(numShipsPlaced) & ": Select " & shipSize(numShipsPlaced) & " contiguous cells"

Else allowSelection = False PlaceComputerShips gameStarted = True

Range("Output").Value = "You may begin" End If

Else

MsgBox errMsg End If End Sub

The LocatePlayerShip() sub procedure is called from the SelectionChange() event procedure and passed the user's cell selection in the form of the range variable Target. The primary goal of the LocatePlayerShip() sub procedure is to validate and mark the user's selection for a ship. To accomplish this goal, the Boolean value returned by the RangeValid() function procedure (listed later) is used as the conditional statement in an If/Then/Else code block. If the user's selection is valid then the interior color of the selection is colored light blue, and the numShipsPlaced variable is incremented by one. If the user's selection is invalid then the RangeValid() procedure sets the value of the string variable errMsg that was passed (by reference) as an argument. This error message is then output to the user in a message box. The nested If/Then/Else code block tests if the user has placed all five ships. If not, then a message is output to place the next ship. If the user has placed all five ships, then the PlaceComputerShips() sub procedure is called in order to generate the location of the computer's ships, the gameStarted variable is set to true, and the user is informed that he/she can begin firing. Figure 5.18 shows the Battlecell worksheet after the user has placed four ships.

The Battlecell worksheet after the user has placed four of five ships.

The Battlecell worksheet after the user has placed four of five ships.

Private Sub

PlayerFire(Target As Range)

'If player is firing at computer, then record the shot as 'a hit or miss and track the number of hits.

Dim errMsg As String

If TargetValid(Target, "Computer", errMsg) Then

PlayWav ActiveWorkbook.Path & "\Sounds\cannon.wav"

HitOrMiss Target

ComputerFire

Else

MsgBox errMsg End If End Sub

The PlayerFire() sub procedure is used to validate and mark the user's target selection when firing at the computer. This procedure is also called from the SelectionChange() event procedure and passed the user's cell selection as the range variable Target. It is also very similar in form to LocatePlayerShip() sub procedure with an If/Then/Else code block that first validates the user's selection. If the selection is valid, a sound file is played (more later) and the procedure marks the user's targets on the computer's grid by coloring them green or red via a call to the HitOrMiss() sub procedure. This is immediately followed by a call to the ComputerFire() sub procedure which simulates the computer's turn at firing back at the user. If the user's selection is invalid, then a message box outputs an error message.

Private Sub HitOrMiss(Target As Range) 'Tests if player scores a hit. If so, then game ends Static numTargetHits As Integer

'Test if players attack scored a hit.

If Target.Value = "X" Then

Target.Interior.Color = RGB(255, 0, 0)

PlayWav ActiveWorkbook.Path & "\Sounds\explode.wav"

numTargetHits = numTargetHits + 1

If (numTargetHits = 17) Then 'Test for end of game.

Range("Output").Value = "You've sunk all of my ships." PlayWav ActiveWorkbook.Path & "\Sounds\playerwins.wav" GameOver End If

Else

Target.Interior.Color = RGB(0, 0, 255) End If End Sub

The HitOrMiss() sub procedure is called from PlayerFire() and serves to test whether or not the user has scored a hit against the computer. Hits are scored by coloring the cell red when the target cell holds an X; otherwise the cell is colored blue. The procedure also tracks the number of hits scored by the user with the static integer variable numTargetHits. When the number of hits reaches 17, then the user wins and the game is over. Figure 5.19 shows the Battlecell worksheet after the user and computer have each scored several misses and one hit.

The Battlecell worksheet for a game in progress.

The Battlecell worksheet for a game in progress.

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