我如何检查 tic tac toe 游戏 visual basic 中是否没有赢家
how can I check if there is no winner in a tic tac toe game visual basic
这是一款用户对用户井字游戏;我如何实现一个循环来检查游戏中是否有平局而不是使用 if/else 语句?
如果不是,有没有办法通过循环使这段代码更短?
到目前为止,这是我的代码。游戏准确地检查是否有赢家,但不知道什么时候没有赢家。
Public Class Form1
Dim choice As Boolean
Dim playerXwins, playerOwins As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Panel1.Enabled = False
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
Button5.Enabled = False
Button6.Enabled = False
Button7.Enabled = False
Button8.Enabled = False
Button9.Enabled = False
ExitButton.Enabled = False
End Sub
Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
Me.Close()
End Sub
Private Sub PlayButton_Click(sender As Object, e As EventArgs) Handles PlayButton.Click
PlayButton.Enabled = False
Panel1.Enabled = True
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
Button5.Enabled = True
Button6.Enabled = True
Button7.Enabled = True
Button8.Enabled = True
Button9.Enabled = True
ExitButton.Enabled = True
End Sub
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
If choice = False Then
CType(sender, Button).Text = "X"
choice = True
Else
CType(sender, Button).Text = "O"
choice = False
End If
ChoosingWinner()
End Sub
Private Sub ChoosingWinner()
If Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" Then
MsgBox("Player X Won this Round")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
ElseIf Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "X" Then
MsgBox("Player X Won Round")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
ElseIf Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button3.Text = "X" And Button5.Text = "X" And Button7.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button4.Text = "X" And Button5.Text = "X" And Button6.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button1.Text = "O" And Button2.Text = "O" And Button3.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button1.Text = "O" And Button4.Text = "O" And Button7.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button1.Text = "O" And Button5.Text = "O" And Button9.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button2.Text = "O" And Button5.Text = "O" And Button8.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button3.Text = "O" And Button6.Text = "O" And Button9.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button3.Text = "O" And Button5.Text = "O" And Button7.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button4.Text = "O" And Button5.Text = "O" And Button6.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button7.Text = "O" And Button8.Text = "O" And Button9.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
End Sub
End Class
把你所有的If
变成一个If-Elif
结构,在最后的Elif
(在所有检查是否有人赢了之后),检查是否所有的按钮已被使用,如果已使用,则平局。
这是一款用户对用户井字游戏;我如何实现一个循环来检查游戏中是否有平局而不是使用 if/else 语句?
如果不是,有没有办法通过循环使这段代码更短?
到目前为止,这是我的代码。游戏准确地检查是否有赢家,但不知道什么时候没有赢家。
Public Class Form1
Dim choice As Boolean
Dim playerXwins, playerOwins As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Panel1.Enabled = False
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
Button5.Enabled = False
Button6.Enabled = False
Button7.Enabled = False
Button8.Enabled = False
Button9.Enabled = False
ExitButton.Enabled = False
End Sub
Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
Me.Close()
End Sub
Private Sub PlayButton_Click(sender As Object, e As EventArgs) Handles PlayButton.Click
PlayButton.Enabled = False
Panel1.Enabled = True
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
Button5.Enabled = True
Button6.Enabled = True
Button7.Enabled = True
Button8.Enabled = True
Button9.Enabled = True
ExitButton.Enabled = True
End Sub
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
If choice = False Then
CType(sender, Button).Text = "X"
choice = True
Else
CType(sender, Button).Text = "O"
choice = False
End If
ChoosingWinner()
End Sub
Private Sub ChoosingWinner()
If Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" Then
MsgBox("Player X Won this Round")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
ElseIf Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "X" Then
MsgBox("Player X Won Round")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
ElseIf Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button3.Text = "X" And Button5.Text = "X" And Button7.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button4.Text = "X" And Button5.Text = "X" And Button6.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "X" Then
MsgBox("Player X Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerXwins = playerXwins + 1
Label2.Text = playerXwins
End If
If Button1.Text = "O" And Button2.Text = "O" And Button3.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button1.Text = "O" And Button4.Text = "O" And Button7.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button1.Text = "O" And Button5.Text = "O" And Button9.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button2.Text = "O" And Button5.Text = "O" And Button8.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button3.Text = "O" And Button6.Text = "O" And Button9.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button3.Text = "O" And Button5.Text = "O" And Button7.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button4.Text = "O" And Button5.Text = "O" And Button6.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
If Button7.Text = "O" And Button8.Text = "O" And Button9.Text = "O" Then
MsgBox("Player O Won!")
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
playerOwins = playerOwins + 1
Label1.Text = playerOwins
End If
End Sub
End Class
把你所有的If
变成一个If-Elif
结构,在最后的Elif
(在所有检查是否有人赢了之后),检查是否所有的按钮已被使用,如果已使用,则平局。