查看單個文章
舊 2005-07-07, 04:51 PM   #5 (permalink)
魔術王子
版區管理員
 
魔術王子 的頭像
榮譽勳章
UID - 115097
在線等級: 級別:42 | 在線時長:1994小時 | 升級還需:27小時級別:42 | 在線時長:1994小時 | 升級還需:27小時
註冊日期: 2004-01-13
住址: 魔術學園
文章: 2990
精華: 0
現金: 14972 金幣
資產: 2678602 金幣
預設

OnLine3_ab.frm內容(負責猜數字遊戲)
語法:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmAB 
   Caption         =   "猜數字遊戲"
   ClientHeight    =   2445
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   3510
   LinkTopic       =   "Form1"
   ScaleHeight     =   2445
   ScaleWidth      =   3510
   StartUpPosition =   3  '系統預設值
   Begin VB.CommandButton Command2 
      Caption         =   "離開"
      Height          =   375
      Left            =   120
      TabIndex        =   3
      Top             =   1920
      Width           =   1095
   End
   Begin MSWinsockLib.Winsock Winsock1 
      Left            =   2880
      Top             =   1200
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
   Begin VB.TextBox Text2 
      Height          =   2175
      Left            =   1320
      Locked          =   -1  'True
      MultiLine       =   -1  'True
      ScrollBars      =   2  '垂直捲軸
      TabIndex        =   2
      Top             =   120
      Width           =   2055
   End
   Begin VB.CommandButton Command1 
      Caption         =   "我猜"
      Height          =   375
      Left            =   120
      TabIndex        =   1
      Top             =   840
      Width           =   1095
   End
   Begin VB.TextBox Text1 
      BeginProperty Font 
         Name            =   "Fixedsys"
         Size            =   24
         Charset         =   136
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   600
      Left            =   120
      MaxLength       =   4
      TabIndex        =   0
      Top             =   120
      Width           =   1095
   End
End
Attribute VB_Name = "frmAB"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Public sServerIP As String '對方主機的IP
Public sName As String '使用者暱稱
Public sNum As String
Dim bYourTurn As Boolean '是不是換你

Private Sub Command1_Click()
    Winsock1.SendData "A" & Text1.Text
End Sub

Private Sub Command2_Click()
    If Winsock1.State = sckConnected Then
        Winsock1.SendData "E"
        DoEvents
    End If
    End
End Sub

Private Sub Form_Activate()
    Text2.Text = Text2.Text + sNum + vbCrLf
End Sub

'********** 連結 **********
Public Sub subConnect()
    If bIfServer = True Then '當做伺服端
        With Winsock1
            .LocalPort = 2467 '自訂值,但值要在1024以上
            .Bind
            .Listen
        End With
        Text2.Text = Text2.Text + "等待連結..." + vbCrLf

        frmAB.Caption = frmAB.Caption & "---伺服端"
    Else '當做用戶端
        With Winsock1
            .RemoteHost = sServerIP
            .RemotePort = 2467
            .Connect
        End With
        Text2.Text = Text2.Text + "正在連結..." + vbCrLf
        frmAB.Caption = frmAB.Caption & "---用戶端"
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Command2_Click
End Sub

Private Sub Text2_Change()
    Text2.SelStart = Len(Text2.Text)
End Sub

Private Sub Winsock1_Connect()
    Winsock1.SendData "C" & sName
    Text2.Text = Text2.Text + "連線中..." + vbCrLf
    Text2.Text = Text2.Text + "對方先下..." + vbCrLf
    Command1.Enabled = False
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    Winsock1.Close
    Winsock1.Accept requestID
    Winsock1.SendData "C" & sName
    Text2.Text = Text2.Text + "連線中..." + vbCrLf
    Text2.Text = Text2.Text + "你先下..." + vbCrLf
    bYourTurn = True
    Command1.Enabled = True
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim sMsgCheck As String
    Dim sMsgString As String
    Dim loc As Integer
    Dim an As Integer
    Dim bn As Integer
    Dim i As Integer
    Dim j As Integer
    Winsock1.GetData sMsgString, vbString, bytesTotal
    
    sMsgCheck = Mid(sMsgString, 1, 1)
    sMsgString = Mid(sMsgString, 2, Len(sMsgString) - 1)
    
    Select Case sMsgCheck
        Case "A"
            Text2.Text = Text2.Text + "對方猜: " + sMsgString + vbCrLf
            For i = 1 To 4
                If Mid(sMsgString, i, 1) = Mid(sNum, i, 1) Then an = an + 1
                For j = 1 To 4
                    If Mid(sMsgString, i, 1) = Mid(sNum, j, 1) Then
                        If (i <> j) Then bn = bn + 1
                    End If
                Next j
            Next i
            If (an = 4) Then
                Winsock1.SendData "L"
                Text2.Text = Text2.Text + "您輸了..." + vbCrLf
                Command1.Enabled = False
            Else
                Winsock1.SendData "B" & sMsgString & " => " & an & "A" & bn & "B"
                Text2.Text = Text2.Text + "換你下..." + vbCrLf
                Text1.Text = ""
                Text1.SetFocus
                bYourTurn = True
                Command1.Enabled = True
            End If
        Case "B"
            Text2.Text = Text2.Text + sMsgString + vbCrLf
            bYourTurn = False
            Command1.Enabled = False
        Case "C"
            If bIfServer = True Then
                Text2.Text = Text2.Text + sMsgString + " 上來了" + vbCrLf
            Else
                Text2.Text = Text2.Text + "已連到 " + sMsgString + vbCrLf
            End If
        Case "L"
            Text2.Text = Text2.Text + Text1.Text + "正確答案^_^" + vbCrLf
            Text2.Text = Text2.Text + "您贏了..." + vbCrLf
            Command1.Enabled = False
        Case "E"
            Text2.Text = Text2.Text + "對方已經離線" + vbCrLf
    End Select
End Sub
最後還有OnLine3.bas
語法:
Attribute VB_Name = "Module1"
Option Explicit

Public bIfServer As Boolean

Sub Main()
    Load frmLinkType
    frmLinkType.Show
End Sub
魔術王子 目前離線  
送花文章: 1530, 收花文章: 1555 篇, 收花: 3894 次
回覆時引用此帖