Playing the Game

The game is played by searching the chart for two adjacent images in a single row or column that can be swapped in order to create a score sequence. The user selects an image by first selecting a series and then selecting a specific point within that series; that is, it takes two single clicks to select a single point if a series has not already been selected. When the user selects an image (or any chart component), it triggers the Select() event of the Chart object. This is where I have entered the code that collects the specific Point objects representing the chart markers selected by the user.

For the purposes of the Alienated Game, I am interested in selections that result in values for all three arguments (ElementID, Argl, and Arg2) Excel passes to the Select() event. Specifically, I am looking for ElementID=3 (VBA-defined constant xlSeries), and values of Argl and Arg2 that are between 1 and 10. When these conditions are satisfied, the function procedure AssignSelection() is called to assign the index values of the Series and Point objects selected by the user to the module-level variables pt1Series, pt1Point, pt2Series, and pt2Point. If the selection is valid (the user selected adjacent, non-diagonal images), then the procedure continues with a call to the ImageSwap() function procedure (listed later). The ImageSwap() procedure returns a Boolean value indicating whether or not a successful swap occurred (it fails if it doesn't produce a score sequence). If the swap is successful, the ProcessChart() sub procedure is called to start the whole process of scoring, removing, and updating the chart. If the swap is unsuccessful, the user must choose two new images.

Private Sub Chart_Select(ByVal ElementID As Long, ByVal Argl As Long, ByVal Arg2 As Long) 'Catch player's selection of individual points Dim msg As ChartTitle Dim swapSuccessful As Boolean Static selection As Integer

'If the first selection only selects a series 'then exit the sub.

Exit Sub End If

'Collect points selected by the player. Validate 2nd point. 'Exit the sub if point 2 is not validated.

Set msg = Sheets("Alienated").ChartTitle If ElementID = xlSeries And Arg2 > 0 Then

If Not AssignSelection(selection, Argl, Arg2) Then Exit Sub End If

'Swap, score, and replace images.

swapSuccessful = ImageSwap If swapSuccessful Then ProcessChart

Else selection = 0 Exit Sub End If msg.Text = "Select Two More Aliens" End Sub

The AssignSelection() function procedure assigns the index values of the Series and Point objects selected by the user to the module-level variables ptlSeries, ptlPoint, pt2Series, and pt2Point. These variables are needed by other procedures that help swap the images selected by the user. The procedure is divided into two parts in an If/ElseIf decision structure. The If block assigns the first image selected by the user and the Elself block assigns the second image after it is validated.

Private Function AssignSelection(selection As Integer, seriesNum As Long, _ ptNum As Long) As Boolean Dim msg As ChartTitle

Set msg = Sheets("Alienated").ChartTitle

'Collect first point.

If selection = 0 Then ptlSeries = seriesNum ptlPoint = ptNum msg.Text = "One Alien Selected"

selection = selection + 1

AssignSelection = False

Exit Function

'Collect 2nd point if valid.

Elself selection = 1 Then

If Not ValidatePt2(seriesNum, ptNum) Then AssignSelection = False selection = 0

Else pt2Series = seriesNum pt2Point = ptNum msg.Text = "Two Aliens Selected" AssignSelection = True selection = 0 End If End If End Function

The function procedure ValidatePt() validates the second image selected by the user only in that the selections must be adjacent images within the same row or column. If the second image selected by the user is not valid then the user must start over and select two new images.

Private Function ValidatePt2(Arg1 As Long, Arg2 As Long) As Boolean Dim msg As ChartTitle

Set msg = Sheets("Alienated").ChartTitle

'Test value of point 2 to ensure its in adjacent row or 'column cannot be diagonal to point 1.

ValidatePt2 = True

If (Abs(pt1Series - Argl) > 1 Or Abs(pt1Point - Arg2) > 1) Or _ (Abs(pt1Series - Arg1) = 1 And (pt1Point <> Arg2)) Or _ ((pt1Series = Arg1) And (pt1Point = Arg2)) Then msg.Text = "You must select adjacent cells." ValidatePt2 = False End If End Function

The function procedure ImageSwap() is called from the Select() event and serves to swap the images selected by the user. First, the values in the image map are swapped with a call to the ImageMapSwap() sub procedure. Next, the image map is scanned with a call to the ScanImages() function procedure in order to check if the swap is valid. Recall that the ScanImages() function procedure returns a variable array whose elements are Range objects from the image map that represent score sequences. If the call to ScanImages() returns a value, then the user's selection is valid and the whole process of swapping, scoring, and removing images continues with a call to the ProcessChart() sub procedure in the Select() event. If the swap does not result in any score sequences, then the image map is returned to its original state with another call to ImageMapSwap(), a message is displayed to the user, and the ImageSwap() function returns false to the calling procedure.

Private Function ImageSwap() As Boolean Dim msg As ChartTitle Dim MapRanges() As Range

Set msg = Sheets("Alienated").ChartTitle

ImageMapSwap MapRanges = ScanImages

If (Not MapRanges) <> -1 Then 'Swapped images should result in score. TwoImageSwap ImageSwap = True


ImageMapSwap 'First swap did not result in scored ranges.

msg.Text = "Selection must create 3 or more sequential aliens." ImageSwap = False End If End Function

The last two procedures listed for the Alienated Game are ImageMapSwap() and TwoImageSwap() which swap the two values in the image map and the two images in the chart that correspond to the user's selection. These are both straightforward swapping procedures.

Private Sub TwoImageSwap()

Dim serieslPts As Points, series2Pts As Points Dim wsMap As Worksheet

'Initialize variables.

On Error GoTo SwapError

Set serieslPts = Sheets("Alienated").SeriesCollection(pt1Series).Points Set series2Pts = Sheets("Alienated").SeriesCollection(pt2Series).Points Set wsMap = Worksheets("ImageMap")

'Swap images.

series1Pts(pt1Point).Fill.UserPicture PictureFile:=filePath & _ wsMap.Cells(pt1Series + 1, ptlPoint + 1).Value & ".png" series2Pts(pt2Point).Fill.UserPicture PictureFile:=filePath & _ wsMap.Cells(pt2Series + 1, pt2Point + 1).Value & ".png"

Exit Sub SwapError:

MsgBox "An error occurred while swapping images. The game must end." & vbCrLf & Err.Description, vbOKOnly, "Error: " & Err.Number

End Sub

Private Sub ImageMapSwap() Dim tempInt As Integer Dim wsMap As Worksheet

Set wsMap = Worksheets("ImageMap")

'Swap numbers mapped to selected images.

tempInt = wsMap.Cells(pt1Series + 1, ptlPoint + 1) wsMap.Cells(pt1Series + 1, ptlPoint + 1) = _

wsMap.Cells(pt2Series + 1, pt2Point + 1) wsMap.Cells(pt2Series + 1, pt2Point + 1) = tempInt End Sub

That concludes the Alienated Game. I had a lot of fun writing it and hope you enjoy playing it and adding your own features.

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