gpt4 book ai didi

vb.net - 使用循环或 If 语句在 vb.net 中获得结果 'Win, Lose, Draw' 的“剪刀石头布”逻辑

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:31:50 24 4
gpt4 key购买 nike

我现在头晕目眩,这可能是因为我错过了一些基本逻辑。

我在 VB.NET 中有一个“剪刀石头布”程序,它可以为每个单独的游戏/回合给出正确的结果。我遇到的问题是在尝试确定 MATCH 的结果时,它也可以是“赢、输、平”。比赛是三局两胜制(前两局)和五局两局(前三局)。由于比赛的结果可能是平局,因此有多种组合/排列,例如:

  1. W, L, D
  2. 左、右、右
  3. 左、右、右
  4. 日、长、西
  5. W, D, L, ....等...

到目前为止,我有以下代码:

    Public Class GameForm
Private humanScore As Integer = 0
Private compScore As Integer = 0
Private drawScore As Integer = 0
Private totalGames As Integer = 0
Private totalGamesForWin As Integer = 0
Private totalGamesPlayed As Integer = 0
Private player1 = New PlayerHumanPlayer()
Private player2 = New PlayerComputerRandom()


Private Sub GameForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If GameTypeForm.cmboMatchDuration.SelectedItem = 0 Then
totalGames = 3
totalGamesForWin = 2
lblMatchInfor.Text = GlobalVariables.MatchTypeBestOf3Message
ElseIf (GameTypeForm.cmboMatchDuration.SelectedItem = 1) Then
totalGames = 5
totalGamesForWin = 3
lblMatchInfor.Text = GlobalVariables.MatchTypeBestOf5Message
End If

End Sub

Private Sub btnRock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRock.Click
findGameWinner("HumanPlayer", "Rock", "RandomComputer")
End Sub

Private Sub btnPaper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaper.Click
findGameWinner("HumanPlayer", "Paper", "RandomComputer")
End Sub


Private Sub btnScissors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnScissors.Click
findGameWinner("HumanPlayer", "Scissors", "RandomComputer")
End Sub

Public Sub findGameWinner(ByVal p1name As String, ByVal p1WeaponSelected As String, ByVal p2Name As String)

player1.Name = p1name
player1.pickWeapon(p1WeaponSelected) ' Should I be using the Rock Class???

player2.Name = p2Name
player2.pickWeapon()

Dim winner As Integer = player1.getWeapon().compareTo(player2.getWeapon())

Select Case winner
Case 1
updateScores(True, False)
findMatchWinner()
Case -1
updateScores(False, True)
findMatchWinner()
Case 0
updateScores(False, False)
findMatchWinner()
End Select
End Sub

Public Function updateScores(ByVal humanWon As Boolean, ByVal compWon As Boolean) As Integer

If humanWon = True Then
humanScore = humanScore + 1

'Update Human labels
lblPlayerScore.Text = humanScore.ToString()
'txtCompChoice.Text = txtCompChoice.Text + Environment.NewLine + Type.GetType(player2.pWeapon.ToString()).ToString()
txtCompChoice.Text = txtCompChoice.Text + Environment.NewLine + player2.pWeapon.ToString()
txtGameStatus.Text = txtGameStatus.Text + Environment.NewLine + player1.Name() + " wins!"

ElseIf compWon = True Then
compScore = compScore + 1

'Update Computer labels
lblCompScore.Text = compScore.ToString()
'txtCompChoice.Text = txtCompChoice.Text + Environment.NewLine + Type.GetType(player2.pWeapon.ToString()).ToString()
txtCompChoice.Text = txtCompChoice.Text + Environment.NewLine + player2.pWeapon.ToString()
txtGameStatus.Text = txtGameStatus.Text + Environment.NewLine + player2.Name() + " wins!"

Else
drawScore = drawScore + 1

'Update Draw labels
lblDrawGame.Text = drawScore.ToString()
'txtCompChoice.Text = txtCompChoice.Text + Environment.NewLine + Type.GetType(player2.pWeapon.ToString()).ToString()
txtCompChoice.Text = txtCompChoice.Text + Environment.NewLine + player2.pWeapon.ToString()
txtGameStatus.Text = txtGameStatus.Text + Environment.NewLine + "Draw!"

End If

totalGamesPlayed = totalGamesPlayed + 1

Return totalGamesPlayed
End Function


Public Function findMatchWinner() As String

If totalGamesPlayed <> totalGames Then
If humanScore = totalGamesForWin Then
lblMatchInfor.Text = GlobalVariables.HumanMacthWinMessage
clearForm()
ElseIf compScore = totalGamesForWin Then
lblMatchInfor.Text = GlobalVariables.CompMacthWinMessage
clearForm()
ElseIf totalGamesPlayed = totalGames - 1 Then
lblMatchInfor.Text = GlobalVariables.DeciderGameMessage
End If
ElseIf humanScore = totalGamesForWin Then
lblMatchInfor.Text = GlobalVariables.HumanMacthWinMessage
clearForm()
ElseIf compScore = totalGamesForWin Then
lblMatchInfor.Text = GlobalVariables.CompMacthWinMessage
clearForm()
ElseIf (drawScore = totalGamesPlayed) Then
lblMatchInfor.Text = GlobalVariables.DrawMacthWinMessage
clearForm()
End If

Return "Human OR Computer"
End Function

Public Sub clearForm()

End Sub

End Class

我以为我做得很好,直到我想起我完全忘记了平局/领带。从那时起我的脑袋就一直在打转,请问有人能解释一下如何让 findMatchWinner() 函数正常工作吗?

如有任何帮助,我们将不胜感激。

提前致谢

最佳答案

我建议,与其检查一名玩家赢得了多少场胜利并查看这是否是所玩回合数的预期金额,不如记录两个付款人的胜利并在最后进行比较。

如果玩家 A > 玩家 B,则玩家 A 获胜,如果他们相同,则平局。另外,请记住,一场 3 轮比赛并不需要赢 2 场才能获胜,因为玩家 A 可以赢一次,然后所有其他比赛都可能是平局。

关于vb.net - 使用循环或 If 语句在 vb.net 中获得结果 'Win, Lose, Draw' 的“剪刀石头布”逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13983907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com