home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectPlay / Memory / PlayForm.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  33.6 KB  |  928 lines

  1. VERSION 5.00
  2. Begin VB.Form frmGameBoard 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "DirectPlay Memory"
  5.    ClientHeight    =   7200
  6.    ClientLeft      =   3150
  7.    ClientTop       =   2400
  8.    ClientWidth     =   8745
  9.    Icon            =   "PlayForm.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    ScaleHeight     =   480
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   583
  15.    StartUpPosition =   2  'CenterScreen
  16.    Begin VB.Timer tmrTerminate 
  17.       Enabled         =   0   'False
  18.       Interval        =   10
  19.       Left            =   8985
  20.       Top             =   1680
  21.    End
  22.    Begin VB.Timer tmrResign 
  23.       Enabled         =   0   'False
  24.       Interval        =   10
  25.       Left            =   8985
  26.       Top             =   1200
  27.    End
  28.    Begin VB.CommandButton cmdExit 
  29.       Cancel          =   -1  'True
  30.       Caption         =   "E&xit"
  31.       BeginProperty Font 
  32.          Name            =   "Verdana"
  33.          Size            =   9.75
  34.          Charset         =   0
  35.          Weight          =   700
  36.          Underline       =   0   'False
  37.          Italic          =   0   'False
  38.          Strikethrough   =   0   'False
  39.       EndProperty
  40.       Height          =   615
  41.       Left            =   6720
  42.       TabIndex        =   9
  43.       Top             =   1740
  44.       Visible         =   0   'False
  45.       Width           =   1995
  46.    End
  47.    Begin VB.Frame Frame1 
  48.       BeginProperty Font 
  49.          Name            =   "Verdana"
  50.          Size            =   9.75
  51.          Charset         =   0
  52.          Weight          =   700
  53.          Underline       =   0   'False
  54.          Italic          =   0   'False
  55.          Strikethrough   =   0   'False
  56.       EndProperty
  57.       Height          =   1455
  58.       Index           =   1
  59.       Left            =   6720
  60.       TabIndex        =   3
  61.       Top             =   1760
  62.       Width           =   1935
  63.       Begin VB.Label LabelScore 
  64.          Alignment       =   2  'Center
  65.          Caption         =   "0"
  66.          BeginProperty Font 
  67.             Name            =   "Verdana"
  68.             Size            =   36
  69.             Charset         =   0
  70.             Weight          =   700
  71.             Underline       =   0   'False
  72.             Italic          =   0   'False
  73.             Strikethrough   =   0   'False
  74.          EndProperty
  75.          Height          =   975
  76.          Index           =   1
  77.          Left            =   120
  78.          TabIndex        =   5
  79.          Top             =   360
  80.          Width           =   1695
  81.       End
  82.    End
  83.    Begin VB.Frame Frame1 
  84.       BeginProperty Font 
  85.          Name            =   "Verdana"
  86.          Size            =   9.75
  87.          Charset         =   0
  88.          Weight          =   700
  89.          Underline       =   0   'False
  90.          Italic          =   0   'False
  91.          Strikethrough   =   0   'False
  92.       EndProperty
  93.       Height          =   1455
  94.       Index           =   2
  95.       Left            =   6720
  96.       TabIndex        =   2
  97.       Top             =   3400
  98.       Width           =   1935
  99.       Begin VB.Label LabelScore 
  100.          Alignment       =   2  'Center
  101.          Caption         =   "0"
  102.          BeginProperty Font 
  103.             Name            =   "Verdana"
  104.             Size            =   36
  105.             Charset         =   0
  106.             Weight          =   700
  107.             Underline       =   0   'False
  108.             Italic          =   0   'False
  109.             Strikethrough   =   0   'False
  110.          EndProperty
  111.          Height          =   975
  112.          Index           =   2
  113.          Left            =   120
  114.          TabIndex        =   6
  115.          Top             =   360
  116.          Width           =   1695
  117.       End
  118.    End
  119.    Begin VB.Frame Frame1 
  120.       BeginProperty Font 
  121.          Name            =   "Verdana"
  122.          Size            =   9.75
  123.          Charset         =   0
  124.          Weight          =   700
  125.          Underline       =   0   'False
  126.          Italic          =   0   'False
  127.          Strikethrough   =   0   'False
  128.       EndProperty
  129.       Height          =   1455
  130.       Index           =   3
  131.       Left            =   6720
  132.       TabIndex        =   1
  133.       Top             =   5040
  134.       Width           =   1935
  135.       Begin VB.Label LabelScore 
  136.          Alignment       =   2  'Center
  137.          Caption         =   "0"
  138.          BeginProperty Font 
  139.             Name            =   "Verdana"
  140.             Size            =   36
  141.             Charset         =   0
  142.             Weight          =   700
  143.             Underline       =   0   'False
  144.             Italic          =   0   'False
  145.             Strikethrough   =   0   'False
  146.          EndProperty
  147.          Height          =   975
  148.          Index           =   3
  149.          Left            =   120
  150.          TabIndex        =   7
  151.          Top             =   360
  152.          Width           =   1695
  153.       End
  154.    End
  155.    Begin VB.Frame Frame1 
  156.       Caption         =   "Turns"
  157.       BeginProperty Font 
  158.          Name            =   "Verdana"
  159.          Size            =   9.75
  160.          Charset         =   0
  161.          Weight          =   700
  162.          Underline       =   0   'False
  163.          Italic          =   0   'False
  164.          Strikethrough   =   0   'False
  165.       EndProperty
  166.       Height          =   1455
  167.       Index           =   0
  168.       Left            =   6720
  169.       TabIndex        =   0
  170.       Top             =   120
  171.       Width           =   1935
  172.       Begin VB.Label LabelScore 
  173.          Alignment       =   2  'Center
  174.          Caption         =   "0"
  175.          BeginProperty Font 
  176.             Name            =   "Verdana"
  177.             Size            =   36
  178.             Charset         =   0
  179.             Weight          =   700
  180.             Underline       =   0   'False
  181.             Italic          =   0   'False
  182.             Strikethrough   =   0   'False
  183.          EndProperty
  184.          Height          =   975
  185.          Index           =   0
  186.          Left            =   120
  187.          TabIndex        =   4
  188.          Top             =   360
  189.          Width           =   1695
  190.       End
  191.    End
  192.    Begin VB.Label lblChat 
  193.       Caption         =   "Press Enter to chat, Alt+F4 to resign."
  194.       BeginProperty Font 
  195.          Name            =   "Verdana"
  196.          Size            =   9.75
  197.          Charset         =   0
  198.          Weight          =   400
  199.          Underline       =   0   'False
  200.          Italic          =   0   'False
  201.          Strikethrough   =   0   'False
  202.       EndProperty
  203.       Height          =   570
  204.       Left            =   105
  205.       TabIndex        =   8
  206.       Top             =   6570
  207.       Width           =   8700
  208.    End
  209.    Begin VB.Image Image1 
  210.       BorderStyle     =   1  'Fixed Single
  211.       Height          =   1005
  212.       Index           =   35
  213.       Left            =   5520
  214.       Stretch         =   -1  'True
  215.       Top             =   5520
  216.       Width           =   1005
  217.    End
  218.    Begin VB.Image Image1 
  219.       BorderStyle     =   1  'Fixed Single
  220.       Height          =   1005
  221.       Index           =   34
  222.       Left            =   4440
  223.       Stretch         =   -1  'True
  224.       Top             =   5520
  225.       Width           =   1005
  226.    End
  227.    Begin VB.Image Image1 
  228.       BorderStyle     =   1  'Fixed Single
  229.       Height          =   1005
  230.       Index           =   33
  231.       Left            =   3360
  232.       Stretch         =   -1  'True
  233.       Top             =   5520
  234.       Width           =   1005
  235.    End
  236.    Begin VB.Image Image1 
  237.       BorderStyle     =   1  'Fixed Single
  238.       Height          =   1005
  239.       Index           =   32
  240.       Left            =   2280
  241.       Stretch         =   -1  'True
  242.       Top             =   5520
  243.       Width           =   1005
  244.    End
  245.    Begin VB.Image Image1 
  246.       BorderStyle     =   1  'Fixed Single
  247.       Height          =   1005
  248.       Index           =   31
  249.       Left            =   1200
  250.       Stretch         =   -1  'True
  251.       Top             =   5520
  252.       Width           =   1005
  253.    End
  254.    Begin VB.Image Image1 
  255.       BorderStyle     =   1  'Fixed Single
  256.       Height          =   1005
  257.       Index           =   30
  258.       Left            =   120
  259.       Stretch         =   -1  'True
  260.       Top             =   5520
  261.       Width           =   1005
  262.    End
  263.    Begin VB.Image Image1 
  264.       BorderStyle     =   1  'Fixed Single
  265.       Height          =   1005
  266.       Index           =   29
  267.       Left            =   5520
  268.       Stretch         =   -1  'True
  269.       Top             =   4440
  270.       Width           =   1005
  271.    End
  272.    Begin VB.Image Image1 
  273.       BorderStyle     =   1  'Fixed Single
  274.       Height          =   1005
  275.       Index           =   28
  276.       Left            =   4440
  277.       Stretch         =   -1  'True
  278.       Top             =   4440
  279.       Width           =   1005
  280.    End
  281.    Begin VB.Image Image1 
  282.       BorderStyle     =   1  'Fixed Single
  283.       Height          =   1005
  284.       Index           =   27
  285.       Left            =   3360
  286.       Stretch         =   -1  'True
  287.       Top             =   4440
  288.       Width           =   1005
  289.    End
  290.    Begin VB.Image Image1 
  291.       BorderStyle     =   1  'Fixed Single
  292.       Height          =   1005
  293.       Index           =   26
  294.       Left            =   2280
  295.       Stretch         =   -1  'True
  296.       Top             =   4440
  297.       Width           =   1005
  298.    End
  299.    Begin VB.Image Image1 
  300.       BorderStyle     =   1  'Fixed Single
  301.       Height          =   1005
  302.       Index           =   25
  303.       Left            =   1200
  304.       Stretch         =   -1  'True
  305.       Top             =   4440
  306.       Width           =   1005
  307.    End
  308.    Begin VB.Image Image1 
  309.       BorderStyle     =   1  'Fixed Single
  310.       Height          =   1005
  311.       Index           =   24
  312.       Left            =   120
  313.       Stretch         =   -1  'True
  314.       Top             =   4440
  315.       Width           =   1005
  316.    End
  317.    Begin VB.Image Image1 
  318.       BorderStyle     =   1  'Fixed Single
  319.       Height          =   1005
  320.       Index           =   23
  321.       Left            =   5520
  322.       Stretch         =   -1  'True
  323.       Top             =   3360
  324.       Width           =   1005
  325.    End
  326.    Begin VB.Image Image1 
  327.       BorderStyle     =   1  'Fixed Single
  328.       Height          =   1005
  329.       Index           =   22
  330.       Left            =   4440
  331.       Stretch         =   -1  'True
  332.       Top             =   3360
  333.       Width           =   1005
  334.    End
  335.    Begin VB.Image Image1 
  336.       BorderStyle     =   1  'Fixed Single
  337.       Height          =   1005
  338.       Index           =   21
  339.       Left            =   3360
  340.       Stretch         =   -1  'True
  341.       Top             =   3360
  342.       Width           =   1005
  343.    End
  344.    Begin VB.Image Image1 
  345.       BorderStyle     =   1  'Fixed Single
  346.       Height          =   1005
  347.       Index           =   20
  348.       Left            =   2280
  349.       Stretch         =   -1  'True
  350.       Top             =   3360
  351.       Width           =   1005
  352.    End
  353.    Begin VB.Image Image1 
  354.       BorderStyle     =   1  'Fixed Single
  355.       Height          =   1005
  356.       Index           =   19
  357.       Left            =   1200
  358.       Stretch         =   -1  'True
  359.       Top             =   3360
  360.       Width           =   1005
  361.    End
  362.    Begin VB.Image Image1 
  363.       BorderStyle     =   1  'Fixed Single
  364.       Height          =   1005
  365.       Index           =   18
  366.       Left            =   120
  367.       Stretch         =   -1  'True
  368.       Top             =   3360
  369.       Width           =   1005
  370.    End
  371.    Begin VB.Image Image1 
  372.       BorderStyle     =   1  'Fixed Single
  373.       Height          =   1005
  374.       Index           =   17
  375.       Left            =   5520
  376.       Stretch         =   -1  'True
  377.       Top             =   2280
  378.       Width           =   1005
  379.    End
  380.    Begin VB.Image Image1 
  381.       BorderStyle     =   1  'Fixed Single
  382.       Height          =   1005
  383.       Index           =   16
  384.       Left            =   4440
  385.       Stretch         =   -1  'True
  386.       Top             =   2280
  387.       Width           =   1005
  388.    End
  389.    Begin VB.Image Image1 
  390.       BorderStyle     =   1  'Fixed Single
  391.       Height          =   1005
  392.       Index           =   15
  393.       Left            =   3360
  394.       Stretch         =   -1  'True
  395.       Top             =   2280
  396.       Width           =   1005
  397.    End
  398.    Begin VB.Image Image1 
  399.       BorderStyle     =   1  'Fixed Single
  400.       Height          =   1005
  401.       Index           =   14
  402.       Left            =   2280
  403.       Stretch         =   -1  'True
  404.       Top             =   2280
  405.       Width           =   1005
  406.    End
  407.    Begin VB.Image Image1 
  408.       BorderStyle     =   1  'Fixed Single
  409.       Height          =   1005
  410.       Index           =   13
  411.       Left            =   1200
  412.       Stretch         =   -1  'True
  413.       Top             =   2280
  414.       Width           =   1005
  415.    End
  416.    Begin VB.Image Image1 
  417.       BorderStyle     =   1  'Fixed Single
  418.       Height          =   1005
  419.       Index           =   12
  420.       Left            =   120
  421.       Stretch         =   -1  'True
  422.       Top             =   2280
  423.       Width           =   1005
  424.    End
  425.    Begin VB.Image Image1 
  426.       BorderStyle     =   1  'Fixed Single
  427.       Height          =   1005
  428.       Index           =   11
  429.       Left            =   5520
  430.       Stretch         =   -1  'True
  431.       Top             =   1200
  432.       Width           =   1005
  433.    End
  434.    Begin VB.Image Image1 
  435.       BorderStyle     =   1  'Fixed Single
  436.       Height          =   1005
  437.       Index           =   10
  438.       Left            =   4440
  439.       Stretch         =   -1  'True
  440.       Top             =   1200
  441.       Width           =   1005
  442.    End
  443.    Begin VB.Image Image1 
  444.       BorderStyle     =   1  'Fixed Single
  445.       Height          =   1005
  446.       Index           =   9
  447.       Left            =   3360
  448.       Stretch         =   -1  'True
  449.       Top             =   1200
  450.       Width           =   1005
  451.    End
  452.    Begin VB.Image Image1 
  453.       BorderStyle     =   1  'Fixed Single
  454.       Height          =   1005
  455.       Index           =   8
  456.       Left            =   2280
  457.       Stretch         =   -1  'True
  458.       Top             =   1200
  459.       Width           =   1005
  460.    End
  461.    Begin VB.Image Image1 
  462.       BorderStyle     =   1  'Fixed Single
  463.       Height          =   1005
  464.       Index           =   7
  465.       Left            =   1200
  466.       Stretch         =   -1  'True
  467.       Top             =   1200
  468.       Width           =   1005
  469.    End
  470.    Begin VB.Image Image1 
  471.       BorderStyle     =   1  'Fixed Single
  472.       Height          =   1005
  473.       Index           =   6
  474.       Left            =   120
  475.       Stretch         =   -1  'True
  476.       Top             =   1200
  477.       Width           =   1005
  478.    End
  479.    Begin VB.Image Image1 
  480.       BorderStyle     =   1  'Fixed Single
  481.       Height          =   1005
  482.       Index           =   5
  483.       Left            =   5520
  484.       Stretch         =   -1  'True
  485.       Top             =   120
  486.       Width           =   1005
  487.    End
  488.    Begin VB.Image Image1 
  489.       BorderStyle     =   1  'Fixed Single
  490.       Height          =   1005
  491.       Index           =   4
  492.       Left            =   4440
  493.       Stretch         =   -1  'True
  494.       Top             =   120
  495.       Width           =   1005
  496.    End
  497.    Begin VB.Image Image1 
  498.       BorderStyle     =   1  'Fixed Single
  499.       Height          =   1005
  500.       Index           =   3
  501.       Left            =   3360
  502.       Stretch         =   -1  'True
  503.       Top             =   120
  504.       Width           =   1005
  505.    End
  506.    Begin VB.Image Image1 
  507.       BorderStyle     =   1  'Fixed Single
  508.       Height          =   1005
  509.       Index           =   2
  510.       Left            =   2280
  511.       Stretch         =   -1  'True
  512.       Top             =   120
  513.       Width           =   1005
  514.    End
  515.    Begin VB.Image Image1 
  516.       BorderStyle     =   1  'Fixed Single
  517.       Height          =   1005
  518.       Index           =   1
  519.       Left            =   1200
  520.       Stretch         =   -1  'True
  521.       Top             =   120
  522.       Width           =   1005
  523.    End
  524.    Begin VB.Image Image1 
  525.       BorderStyle     =   1  'Fixed Single
  526.       Height          =   1005
  527.       Index           =   0
  528.       Left            =   120
  529.       Stretch         =   -1  'True
  530.       Top             =   120
  531.       Width           =   1005
  532.    End
  533. Attribute VB_Name = "frmGameBoard"
  534. Attribute VB_GlobalNameSpace = False
  535. Attribute VB_Creatable = False
  536. Attribute VB_PredeclaredId = True
  537. Attribute VB_Exposed = False
  538. Option Explicit
  539. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  540. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  541. '  File:       PlayForm.frm
  542. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  543. Implements DirectPlay8Event
  544. 'Here is where all of the main gameplay will be taking place.
  545. Private Const mlMaxText As Long = 50
  546. 'Keep track of what the first cell picked was
  547. Private fFirstPick As Boolean
  548. Private lFirstCell As Long
  549. Private fGame As Boolean
  550. Private lTurnCount As Long
  551. Private mfResign As Boolean
  552. Private mlTerminateCode As Long
  553. Private Sub cmdExit_Click()
  554.     'Game over, we wanna leave
  555.     Unload Me
  556. End Sub
  557. ' Keystroke handler
  558. ' Enter: open Chat dialog
  559. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  560.     Dim sMsg As String, lOffset As Long
  561.     Dim oBuf() As Byte
  562.     If (KeyCode = vbKeyReturn) And (gbNumPlayers > 1) Then
  563.         'Lets chat
  564.         sMsg = InputBox$("Enter the text you want to send:", "Chat Message")
  565.         If sMsg = vbNullString Then Exit Sub
  566.         If Len(sMsg) > mlMaxText Then
  567.             sMsg = Left$(sMsg, mlMaxText)
  568.         End If
  569.         'Send our chat
  570.         lOffset = NewBuffer(oBuf)
  571.         AddDataToBuffer oBuf, CByte(MSG_CHAT), SIZE_BYTE, lOffset
  572.         AddStringToBuffer oBuf, sMsg, lOffset
  573.         SendMessage oBuf
  574.     End If
  575. End Sub
  576. Private Sub Form_Load()
  577.     ' Initialize scoreboard
  578.     If gbNumPlayers > 1 Then DPlayEventsForm.RegisterCallback Me
  579.     InitLocalGame
  580.     ' Erase chat prompt if only one player.
  581.     If gbNumPlayers = 1 Then
  582.         lblChat.Caption = vbNullString
  583.         cmdExit.Visible = True
  584.         SetupBoard
  585.     Else
  586.         ' Put user name on caption bar to ease debugging of multiple sessions on one machine
  587.         Me.Caption = Me.Caption & " - " & gsUserName
  588.         If gfHost Then Me.Caption = Me.Caption & " (HOST) - Your turn"
  589.     End If
  590. End Sub
  591. Private Sub Form_Unload(Cancel As Integer)
  592.     mfResign = True
  593.     If Not (DPlayEventsForm Is Nothing) Then DPlayEventsForm.DoSleep 50
  594.     Cleanup
  595.     frmIntro.Visible = True
  596.     frmIntro.EnableButtons True
  597. End Sub
  598. ' This is where the action takes place. In each turn the player clicks on two empty squares,
  599. ' making their pictures visible. The two pictures revealed in the previous turn are hidden
  600. ' as soon as the first square is clicked, unless they are a match. The player can click on
  601. ' an unmatched picture to begin the turn, in which case it remains visible.
  602. ' A message is broadcast whenever a square is shown or hidden.
  603. Private Sub Image1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
  604.     Dim fGameOver As Boolean
  605.     Dim lCount As Long, lOffset As Long
  606.     Dim oBuf() As Byte
  607.     ' Not your turn, bub.
  608.     If gbNumPlayers > 1 Then If glPlayerIDs(glCurrentPlayer) <> glMyPlayerID Then Exit Sub
  609.     If Button = vbLeftButton Then 'Button = Left
  610.         ' If picture already showing and this is second pick, ignore click.
  611.         ' If picture showing and is already one of a match, ignore click.
  612.         If Image1(Index).Picture <> 0 And ((Not fFirstPick) Or gfMatchedCells(Index)) Then
  613.             Exit Sub
  614.         End If
  615.         
  616.         If fFirstPick Then ' First Pick
  617.         ' Hide previous picks unless they were a match.
  618.             For lCount = 0 To NumCells - 1
  619.                 If Not gfMatchedCells(lCount) Then 'Not Matched
  620.                     Set Image1(lCount).Picture = Nothing
  621.                 End If 'Not Matched
  622.             Next lCount
  623.             ' Tell the other players to update the display. We don't specify which
  624.             ' squares, but just tell them to hide unmatched squares.
  625.             If gbNumPlayers > 1 Then 'NumPlayers > 1
  626.                 lOffset = NewBuffer(oBuf)
  627.                 AddDataToBuffer oBuf, CByte(MSG_HIDEPIECES), SIZE_BYTE, lOffset
  628.                 SendMessage oBuf
  629.             End If 'NumPlayers > 1
  630.             ' Remember this one
  631.             lFirstCell = Index
  632.             fFirstPick = False
  633.             ShowPic Index
  634.         Else
  635.             ShowPic Index
  636.             ' Second pick
  637.             fFirstPick = True  ' Reset for next time
  638.             ' In solitaire game, show number of turns as score
  639.             If gbNumPlayers = 1 Then '1 Player?
  640.                 lTurnCount = lTurnCount + 1
  641.                 frmGameBoard.LabelScore(0).Caption = lTurnCount
  642.             End If '1 Player?
  643.             ' Check for match
  644.             If gbPicArray(lFirstCell) = gbPicArray(Index) Then
  645.                 ' There was a match
  646.                 gfMatchedCells(Index) = True
  647.                 gfMatchedCells(lFirstCell) = True
  648.                 ' Check for win and increment score (# of matches)
  649.                 fGameOver = IsGameOver
  650.                 ' Increment score display only in multiplayer.
  651.                 ' For solitaire, the score is the turn count.
  652.                 If gbNumPlayers > 1 Then
  653.                     'Update the scoreboard for multiplayer games
  654.                     UpdateScoreboard
  655.                     lOffset = NewBuffer(oBuf)
  656.                     AddDataToBuffer oBuf, CByte(MSG_MATCHED), SIZE_BYTE, lOffset
  657.                     'Get the array of matchings cells in
  658.                     For lCount = 0 To NumCells - 1
  659.                         AddDataToBuffer oBuf, gfMatchedCells(lCount), LenB(gfMatchedCells(lCount)), lOffset
  660.                     Next
  661.                     ' Get scores into message
  662.                     For lCount = 0 To MaxPlayers - 1
  663.                         AddDataToBuffer oBuf, gbPlayerScores(lCount), LenB(gbPlayerScores(lCount)), lOffset
  664.                     Next
  665.                     SendMessage oBuf
  666.                 End If ' DirectPlay exists
  667.             Else
  668.                 ' There was no match.
  669.                 ' Broadcast turn-end message
  670.             
  671.                 If gbNumPlayers > 1 Then
  672.                     lOffset = NewBuffer(oBuf)
  673.                     AddDataToBuffer oBuf, CByte(MSG_TURNEND), SIZE_BYTE, lOffset
  674.                     SendMessage oBuf
  675.             
  676.                     ' Pass control to next player & advance scoreboard highlight
  677.                     AdvanceTurn
  678.                 End If  'More than one player
  679.             
  680.             End If ' match or no match
  681.             
  682.             ' If solitaire win, offer choice to play again
  683.             If fGameOver And gbNumPlayers = 1 Then
  684.                 If MsgBox("Play again?", vbYesNo, "Game Over") = vbNo Then End
  685.                 SetupBoard
  686.                 InitLocalGame
  687.             End If
  688.         End If
  689.     End If
  690. End Sub
  691. ' Update scores and check for win
  692. Public Function IsGameOver() As Boolean
  693.     Dim lCount As Integer, Response As Integer
  694.     Dim fEnd As Boolean
  695.     gbPlayerScores(glCurrentPlayer) = gbPlayerScores(glCurrentPlayer) + 1
  696.     ' If any cells are still blank, game is not over
  697.     fEnd = True
  698.     For lCount = 0 To NumCells - 1
  699.         If Not gfMatchedCells(lCount) Then
  700.             fEnd = False
  701.         End If
  702.     Next lCount
  703.     IsGameOver = fEnd
  704. End Function
  705. ' Game initialization for all players, including setting up the scoreboard for the
  706. ' current number and order of players. Global game initialization (setting up the pieces)
  707. ' is handled by the host through SetupBoard.
  708. Public Sub InitLocalGame()
  709.     Dim lCount As Integer
  710.     Dim PlayerInfo As DPN_PLAYER_INFO
  711.     fFirstPick = True
  712.     lTurnCount = 0
  713.     ' Highlight current player
  714.     glCurrentPlayer = 0
  715.     Frame1(glCurrentPlayer).ForeColor = vbHighlight
  716.     LabelScore(glCurrentPlayer).ForeColor = vbHighlight
  717.     ' Hide superfluous scoreboxes and initialize scores
  718.     For lCount = 0 To MaxPlayers - 1
  719.         gbPlayerScores(lCount) = 0
  720.         If lCount >= gbNumPlayers Then
  721.             Frame1(lCount).Visible = False
  722.         Else
  723.             Frame1(lCount).Visible = True
  724.             LabelScore(lCount).Caption = 0
  725.         End If
  726.     Next lCount
  727.     ' Get names of players and label scoreboxes. The correct order has been
  728.     ' stored in the gPlayerIDs array, which is initialized by the host
  729.     ' and passed to the other players.
  730.     If gbNumPlayers > 1 Then
  731.         For lCount = 0 To gbNumPlayers - 1
  732.             PlayerInfo = dpp.GetPeerInfo(glPlayerIDs(lCount))
  733.             Frame1(lCount).Caption = PlayerInfo.Name
  734.             Frame1(lCount).Tag = glPlayerIDs(lCount)
  735.             If PlayerInfo.lPlayerFlags And DPNPLAYER_LOCAL Then
  736.                 glMyPlayerID = glPlayerIDs(lCount)
  737.             End If
  738.         Next lCount
  739.     End If
  740.     ' Erase the pictures and matches
  741.     For lCount = 0 To NumCells - 1
  742.         Image1(lCount).Picture = Nothing
  743.         gfMatchedCells(lCount) = False
  744.     Next lCount
  745. End Sub
  746. Private Sub tmrResign_Timer()
  747.     tmrResign.Enabled = False
  748.     MsgBox "All other players have resigned.  You win!", vbOKOnly Or vbInformation, "Winner"
  749.     DPlayEventsForm.CloseForm Me
  750. End Sub
  751. Public Sub UpdateScoreboard()
  752.     Dim lCount As Integer
  753.     For lCount = 0 To MaxPlayers - 1
  754.       LabelScore(lCount).Caption = gbPlayerScores(lCount)
  755.     Next lCount
  756. End Sub
  757. Private Sub UpdateChat(ByVal sText As String, sUser As String)
  758.     'We need to update the chat window
  759.     lblChat.Caption = sUser & " says: " & sText
  760. End Sub
  761. Public Sub AdvanceTurn()
  762.     If Me.Visible Then
  763.         ' Remove highlight from scorebox for last player
  764.         Frame1(glCurrentPlayer).ForeColor = vbButtonText
  765.         LabelScore(glCurrentPlayer).ForeColor = vbButtonText
  766.     End If
  767.     ' Advance the current player. Try till we find one that exists.
  768.     ' Players who resigned are now 0 in gPlayerIDs.
  769.     Do
  770.         glCurrentPlayer = glCurrentPlayer + 1
  771.         If glCurrentPlayer = MaxPlayers Then glCurrentPlayer = 0
  772.     Loop Until glPlayerIDs(glCurrentPlayer) <> 0
  773.     If Me.Visible Then
  774.         ' Highlight scorebox for active player
  775.         Frame1(glCurrentPlayer).ForeColor = vbHighlight
  776.         LabelScore(glCurrentPlayer).ForeColor = vbHighlight
  777.         UpdateScoreboard
  778.     End If
  779.     Me.Caption = "DirectPlay Memory - " & gsUserName
  780.     If gfHost Then Me.Caption = Me.Caption & " (HOST)"
  781.     If glPlayerIDs(glCurrentPlayer) = glMyPlayerID Then
  782.         Me.Caption = Me.Caption & " - Your turn"
  783.     End If
  784. End Sub
  785. Private Sub ShowPic(ByVal Index As Integer)
  786.     Dim oBuf() As Byte, lOffset As Long
  787.     ' Show the picture you clicked on
  788.     Image1(Index).Picture = frmPics.Image1(gbPicArray(Index)).Picture
  789.     ' Broadcast message to show picture
  790.     If gbNumPlayers > 1 Then 'NumPlayers > 1
  791.         lOffset = NewBuffer(oBuf)
  792.         AddDataToBuffer oBuf, CByte(MSG_SHOWPIECE), SIZE_BYTE, lOffset
  793.         AddDataToBuffer oBuf, CByte(Index), SIZE_BYTE, lOffset
  794.         SendMessage oBuf
  795.     End If 'NumPlayers > 1
  796. End Sub
  797. Private Sub tmrTerminate_Timer()
  798.     tmrTerminate.Enabled = False
  799.     If mfResign Then Exit Sub
  800.     If mlTerminateCode = DPNERR_HOSTTERMINATEDSESSION Then
  801.         MsgBox "The host has terminated this session.  This sample will now exit.", vbOKOnly Or vbInformation, "Exiting"
  802.     Else
  803.         MsgBox "This session has been lost.  This sample will now exit.", vbOKOnly Or vbInformation, "Exiting"
  804.     End If
  805.     DPlayEventsForm.CloseForm Me
  806. End Sub
  807. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  808.     'VB requires that we must implement *every* member of this interface
  809. End Sub
  810. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  811.     'VB requires that we must implement *every* member of this interface
  812. End Sub
  813. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  814.     'VB requires that we must implement *every* member of this interface
  815. End Sub
  816. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  817.     'VB requires that we must implement *every* member of this interface
  818. End Sub
  819. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  820.     'VB requires that we must implement *every* member of this interface
  821. End Sub
  822. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  823.     gbNumPlayers = gbNumPlayers + 1
  824.     If gbNumPlayers = 1 And mfResign = False Then 'Everyone has resigned, you win!
  825.         tmrResign.Enabled = True
  826.     End If
  827.     ' If current player quit, advance to next
  828.     If glPlayerIDs(glCurrentPlayer) = lPlayerID Then AdvanceTurn
  829. End Sub
  830. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  831.     'VB requires that we must implement *every* member of this interface
  832. End Sub
  833. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  834.     Dim lCount As Long
  835.     Dim fAdvance As Boolean
  836.     On Error Resume Next
  837.     gbNumPlayers = gbNumPlayers - 1
  838.     If gbNumPlayers = 1 And mfResign = False Then 'Everyone has resigned, you win!
  839.         tmrResign.Enabled = True
  840.     End If
  841.     ' If current player quit, advance to next
  842.     If glPlayerIDs(glCurrentPlayer) = lPlayerID Then fAdvance = True
  843.     'Remove this player ID from the list of users
  844.     If gbNumPlayers > 1 Then
  845.         For lCount = 0 To gbNumPlayers + 1
  846.             If Frame1(lCount).Tag = lPlayerID Then
  847.                 Frame1(lCount).Visible = False
  848.             End If
  849.             'Remove this player ID from the list of users
  850.             If glPlayerIDs(lCount) = lPlayerID Then glPlayerIDs(lCount) = 0
  851.         Next lCount
  852.     End If
  853.     If fAdvance Then AdvanceTurn
  854. End Sub
  855. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  856.     'We don't want anyone to see this game once it's started... Disallow it.
  857.     fRejectMsg = True
  858. End Sub
  859. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  860.     'VB requires that we must implement *every* member of this interface
  861. End Sub
  862. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  863.     If lNewHostID = glMyPlayerID Then gfHost = True
  864. End Sub
  865. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  866.     'We don't want anyone connecting while we're already playing the game.. Disallow it.
  867.     fRejectMsg = True
  868. End Sub
  869. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  870.     'VB requires that we must implement *every* member of this interface
  871. End Sub
  872. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  873.     'VB requires that we must implement *every* member of this interface
  874. End Sub
  875. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  876.     Dim lCount As Long, lOffset As Long
  877.     Dim bMsg As Byte
  878.     Dim bPiece As Byte, fMatched As Boolean, bScore As Byte
  879.     Dim sChat As String, sPlayer As String
  880.     'Here we will go through the messages
  881.     'The first item in our byte array is the MSGID we passed in
  882.     With dpnotify
  883.     GetDataFromBuffer .ReceivedData, bMsg, LenB(bMsg), lOffset
  884.     Select Case bMsg
  885.     Case MSG_SHOWPIECE
  886.         ' Show a tile that has been clicked
  887.         GetDataFromBuffer .ReceivedData, bPiece, LenB(bPiece), lOffset
  888.         frmGameBoard.Image1(bPiece).Picture = frmPics.Image1(gbPicArray(bPiece)).Picture
  889.       
  890.     Case MSG_HIDEPIECES
  891.         ' Hide unmatched pieces because player has made the first pick.
  892.         For lCount = 0 To NumCells - 1
  893.             If Not gfMatchedCells(lCount) Then
  894.                 Image1(lCount).Picture = Nothing
  895.             End If
  896.         Next lCount
  897.     Case MSG_MATCHED
  898.     ' Retrieve matched cells array
  899.         For lCount = 0 To NumCells - 1
  900.             GetDataFromBuffer .ReceivedData, fMatched, LenB(fMatched), lOffset
  901.             gfMatchedCells(lCount) = fMatched
  902.         Next lCount
  903.         
  904.         ' Retrieve player scores array
  905.         For lCount = 0 To MaxPlayers - 1
  906.             GetDataFromBuffer .ReceivedData, bScore, LenB(bScore), lOffset
  907.             gbPlayerScores(lCount) = bScore
  908.         Next lCount
  909.         ' Display current score
  910.         frmGameBoard.UpdateScoreboard
  911.     Case MSG_TURNEND
  912.         AdvanceTurn
  913.     Case MSG_CHAT
  914.     ' Display chat message
  915.         sPlayer = dpp.GetPeerInfo(dpnotify.idSender).Name
  916.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  917.         UpdateChat sChat, sPlayer
  918.     End Select
  919.     End With
  920. End Sub
  921. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  922.     'VB requires that we must implement *every* member of this interface
  923. End Sub
  924. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  925.     mlTerminateCode = dpnotify.hResultCode
  926.     tmrTerminate.Enabled = True
  927. End Sub
  928.