用VB编一个与电脑玩石头剪刀布的游戏

窗体上画控件:单选框cOption(0)、cOption(1)、cOption(2),按钮Command1,标签Label1。

加代码:

Dim SelectNumber As Integer

Private Sub Command1_Click()

Dim ComputerNumber As Integer

Randomize

ComputerNumber = Int(Rnd * 3)

Select Case ComputerNumber

Case 0: Label1.Caption = "计算机出剪刀"

Case 1: Label1.Caption = "计算机出石头"

Case 2: Label1.Caption = "计算机出布"

End Select

If SelectNumber = ComputerNumber Then

Label1.Caption = Label1.Caption & vbCrLf & "平局"

Else

Select Case SelectNumber

Case 0

If ComputerNumber = 1 Then

Label1.Caption = Label1.Caption & vbCrLf & "计算机赢"

Else

Label1.Caption = Label1.Caption & vbCrLf & "你赢"

End If

Case 1

If ComputerNumber = 2 Then

Label1.Caption = Label1.Caption & vbCrLf & "计算机赢"

Else

Label1.Caption = Label1.Caption & vbCrLf & "你赢"

End If

Case 2

If ComputerNumber = 0 Then

Label1.Caption = Label1.Caption & vbCrLf & "计算机赢"

Else

Label1.Caption = Label1.Caption & vbCrLf & "你赢"

End If

End Select

End If

End Sub

Private Sub Form_Load()

cOption(0).Caption = "剪刀"

cOption(1).Caption = "石头"

cOption(2).Caption = "布"

Command1.Caption = "开始"

Label1.Caption = ""

End Sub

Private Sub cOption_Click(Index As Integer)

SelectNumber = Index

End Sub