岩石,纸,剪刀在vb.net

时间:2013-02-19 21:33:35

标签: vb.net boolean

我在完成RPS游戏的代码时遇到了一些困难。说明如下: C。主按钮单击代码。 一世。您需要使用输入框提示用户输入三个字母中的一个,如前所述。 II。从自定义方法中获取随机字母。 III。将用户的信件和随机计算机信件传递给您的其他方法。 IV。如果它返回true,告诉用户他们在您添加到表单的标签中赢了,否则告诉他们他们丢失在该标签中。

我认为我正确地做了1和2但是我遇到了3和4的问题。有人可以帮忙!!

这是我目前的代码

Public Class Form1

    Dim randomObject As New Random()

    Private Property user As String

    Private Property computer As Random

    Function GetRandomRPSLetter() As String
        randomObject.Next(1, 4)
        Dim computer As Integer = randomObject.Next(1, 4)
        If computer = 1 Then
            Return "r"
        ElseIf computer = 2 Then
            Return "p"
        Else
            Return "s"
        End If

    End Function
    Function GetRPSRoundWinner(ByVal user As String, ByVal computer As String) As String
        If user = "r" Then
            If computer = "r" Then
                Return False
            ElseIf computer = "p" Then
                Return False
            Else
                Return True
            End If
        ElseIf user = "p" Then
            If computer = "r" Then
                Return True
            ElseIf computer = "p" Then
                Return False
            Else
                Return False
            End If
        Else
            If computer = "r" Then
                Return False
            ElseIf computer = "p" Then
                Return True
            Else
                Return False
            End If
        End If
    End Function

    Private Sub Button1_click(ByVal sender As System.Object,
                                    ByVal e As System.EventArgs) Handles Button1.Click

        user = InputBox("Please input r, p, or s to play")

        GetRandomRPSLetter()



    End Sub
    Sub GetRPSRoundWinner()
        If GetRPSRoundWinner(True) Then Return
        Label1.Text = "You win"

    End Sub

    Private Function GetRPSRoundWinner(p1 As Boolean) As Boolean
        Throw New NotImplementedException
    End Function

End Class

现在我调用了GetRPSRoundWinner

Private Sub Button1_click(ByVal sender As System.Object,
                                ByVal e As System.EventArgs) Handles Button1.Click

    user = InputBox("Please input r, p, or s to play")

    GetRandomRPSLetter()

    GetRPSRoundWinner()



End Sub

有人可以帮我比较一下吗?

非常感谢所有人!

3 个答案:

答案 0 :(得分:1)

  • 您的GetRPSRoundWinner方法应该返回Boolean而不是String。转动Option Strict On可以缓解这些错误。
  • 您在按钮单击事件处理程序中调用GetRandomRPSLetter方法,但之后您永远不会从中获取返回值并使用它来将用户的输入与随机生成的计算机移动进行比较。
  • 您永远不会从任何地方拨打GetRPSRoundWinner
  • 您的GetRPSRoundWinner方法有三次重载,但其中只有一种实际上有用。

答案 1 :(得分:0)

我做了一些改动,对我有用。我没有测试你的功能是否得到了正确的赢家,但我认为它是:

Public Class Form1

    Dim randomObject As New Random()

    Private Property user As String

    Private Property computer As Random

    Function GetRandomRPSLetter() As String
        randomObject.Next(1, 4)
        Dim computer As Integer = randomObject.Next(1, 4)
        If computer = 1 Then
            Return "r"
        ElseIf computer = 2 Then
            Return "p"
        Else
            Return "s"
        End If

    End Function
    Function ComputeWinner(ByVal user As String, ByVal computer As String) As String
        If user = "r" Then
            If computer = "r" Then
                Return False
            ElseIf computer = "p" Then
                Return False
            Else
                Return True
            End If
        ElseIf user = "p" Then
            If computer = "r" Then
                Return True
            ElseIf computer = "p" Then
                Return False
            Else
                Return False
            End If
        Else
            If computer = "r" Then
                Return False
            ElseIf computer = "p" Then
                Return True
            Else
                Return False
            End If
        End If
    End Function

    Private Sub Button1_click(ByVal sender As System.Object,
                                    ByVal e As System.EventArgs) Handles Button1.Click

        user = InputBox("Please input r, p, or s to play")

        Dim ComputerLetter = GetRandomRPSLetter()

        GetRPSRoundWinner(user, ComputerLetter)

    End Sub
    Sub GetRPSRoundWinner(userLetter As String, computerLetter As String)
        If ComputeWinner(userLetter, computerLetter) Then
            Label1.Text = "You win"
        Else
            Label1.Text = "You loose! "
        End If

    End Sub
End Class

不要简单地复制和粘贴代码。请仔细阅读,以便了解不同之处。

答案 2 :(得分:0)

我想向您展示如何使用Enum并利用其能够隐式转换为Integer

以下是我的示例代码的结果:

desktop

以及带注释的主模块的来源:

Private rnd As New Random()
''' <summary>
''' Contain values for rock, paper, scissors in a specific
''' numeric order for simplifying with win/loss table
''' </summary>
Public Enum GameHand
    None = -1
    Paper = 0
    Scissors = 1
    Rock = 2
End Enum
''' <summary>
''' Contain values for the result of the game
''' </summary>
Public Enum GameResult
    None = -1
    Loss = 2
    Draw = 0
    Win = 1
End Enum

''' <summary>
''' Compares two RPS hands and returns a win or a loss.
''' If the two hands are the same, or either one is None then the function returns 0
''' </summary>
''' <param name="hand">The human hand (input value)</param>
''' <param name="cpu">The computer hand (output value)</param>
''' <returns>Win, Loss or Draw</returns>
Public Function Play(ByVal hand As GameHand, ByRef cpu As GameHand) As GameResult
    ' Shall we play?
    If hand = GameHand.None Then Return GameResult.None
    ' Pick a random hand
    cpu = rnd.Next(0, 2)
    ' Is it a draw?
    If hand = cpu Then Return GameResult.Draw
    ' win/loss table
    '
    '      P   S   R    
    ' P |      L   W |  
    ' S |  W       L |  
    ' R |  L   W     |  
    '
    ' count how many step away is one hand from the other
    ' in the sequence P S R P S R P S ...
    Dim diff As Integer = (hand + 3 - cpu) Mod 3
    ' two steps is a loss, and one step is a win
    ' Return win or loss
    Return diff
End Function

Sub Main()

    Dim usr As String, win As Integer = 0
    Dim hand As GameHand, cpu As GameHand
    Do
        usr = InputBox("Enter game hand (r/p/s) or quit (q)", "Rock Paper Scissors").ToLower()
        ' Interpret user input 
        Select Case usr
            Case "r"
                hand = GameHand.Rock
            Case "p"
                hand = GameHand.Paper
            Case "s"
                hand = GameHand.Scissors
            Case Else
                hand = GameHand.None
        End Select
        'Play against computer and report back results.
        'Also report computer hand with `ByRef` parameter
        Dim result As GameResult = Play(hand, cpu)
        Console.WriteLine(String.Format("Play {0} against {1} with result {2}", hand, cpu, result))
        'Add wins and subtract losses
        If result = GameResult.Win Then
            win += 1
        ElseIf result = GameResult.Loss Then
            win -= 1
        End If
        ' Check for quit.
    Loop Until usr.Equals("q")
    Console.WriteLine("Player win-loss = " & win)

End Sub