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