2

我在完成 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

谁能帮助我如何比较它们?

非常感谢大家!

4

3 回答 3

1
  • 您的GetRPSRoundWinner方法应该返回 aBoolean而不是 a String。车削Option Strict On将减轻这些类型的错误。
  • 您在按钮单击事件处理程序中调用您的GetRandomRPSLetter方法,但是您永远不会从中获取返回值并使用它来将用户的输入与随机生成的计算机移动进行比较。
  • 你永远不会GetRPSRoundWinner从任何地方打电话。
  • 您的GetRPSRoundWinner方法有三个重载,但实际上只有一个是有用的。
于 2013-02-19T21:42:23.147 回答
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

不要简单地复制和粘贴代码。请仔细阅读它,以便您可以看到差异。

于 2013-02-19T23:14:21.293 回答
0

我想向您展示如何使用 anEnum并利用Integer对它的隐式转换。

这是我的示例代码的结果:

桌面

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

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
于 2013-02-20T15:29:57.867 回答