怎么做剪刀石头布的编程用vb

时间:2025-01-25 14:47:26 游戏攻略

方法一:使用随机数和条件判断

```vb

Private Sub Command1_Click()

Dim a As Integer, b As Integer

Randomize

a = -1

b = Int(Rnd() * 3 + 1)

Label1.Caption = "石头"

Select Case a - b

Case -2

Label2.Caption = "布"

MsgBox "电脑胜"

Case 0

Label2.Caption = "石头"

MsgBox "平"

Case -1

Label2.Caption = "剪刀"

MsgBox "玩家胜"

End Select

End Sub

Private Sub Command2_Click()

Dim a As Integer, b As Integer

Randomize

a = 0

b = Int(Rnd() * 3 + 1)

Label1.Caption = "剪刀"

Select Case a - b

Case 1

Label2.Caption = "布"

MsgBox "玩家胜"

Case 0

Label2.Caption = "剪刀"

MsgBox "平"

Case -1

Label2.Caption = "石头"

MsgBox "电脑胜"

End Select

End Sub

Private Sub Command3_Click()

Dim a As Integer, b As Integer

Randomize

a = 1

b = Int(Rnd() * 3 + 1)

Label1.Caption = "布"

Select Case a - b

Case 1

Label2.Caption = "布"

MsgBox "平"

Case 0

Label2.Caption = "布"

MsgBox "玩家胜"

Case -1

Label2.Caption = "布"

MsgBox "电脑胜"

End Select

End Sub

```

方法二:使用枚举和Switch语句(推荐)

```vb

Public Enum Choice

Rock

Paper

Scissors

End Enum

Private Sub Command1_Click()

Dim result As Choice

result = ChooseComputerChoice()

Label1.Caption = result.ToString()

Select Case result

Case Choice.Rock

Label2.Caption = "布"

MsgBox "电脑胜"

Case Choice.Paper

Label2.Caption = "石头"

MsgBox "平"

Case Choice.Scissors

Label2.Caption = "剪刀"

MsgBox "玩家胜"

End Select

End Sub

Private Function ChooseComputerChoice() As Choice

Dim random As New Random()

Dim computerChoice As Integer = random.Next(1, 4)

Select Case computerChoice

Case 1

Return Choice.Rock

Case 2

Return Choice.Paper

Case 3

Return Choice.Scissors

End Select

End Function

```

方法三:使用图片和事件处理