使自己的圈圈叉叉的游戏

介绍 如果你想创造自己的游戏,那么这里有一个简单的,尽管它对于一个简单的游戏来说是高级的。这是Visual Studio的井字游戏。只要遵循这些步骤,你将成功地使你自己的惊人的井字游戏。只要下载链接,直到我说我完全完成 背景 对于这个项目,你需要了解5种基本的数据类型和布尔数据类型。你需要熟悉你可以在表单上使用的大多数基本工具,比如选项框、命令按钮等等。让我们开始吧。 使用的代码 我们将从创造玩家vs玩家游戏开始,因为这更容易。 首先,打开visual basic 6并打开一个窗体。这就像一个主菜单窗体。接下来,将表单的名称改为“frmStartScreen”。 你将需要这些对象的开始形式: 2个文本框2个标签框4个命令按钮 将命令按钮标记为“cmdStart”,“cmdInstruct”,“cmdAIgame”和“cmdQuit” 下面是“cmdInstruct”按钮的代码 隐藏,复制Code

Private Sub cmdInstruct_Click()
frmInstructions.Show
End Sub 

现在是"cmdStart"按钮 隐藏,复制Code

Private Sub cmdStart_Click()
frmTicTacToe.Show
frmTicTacToe.txtPlayer1.Text = txtPlay1.Text
frmTicTacToe.txtPlayer2.Text = txtPlay2.Text
frmTicTacToe.lblTurn.Caption = frmTicTacToe.txtPlayer1.Text
End Sub 

“cmdAIgame” 隐藏,复制Code

Private Sub cmdAIgame_Click()
frmPvAIWelcome.Show
frmPvAI.txtPlayer.Text = txtPlay1.Text
End Sub  

以及"cmdQuit"按钮。 隐藏,复制Code

Private Sub cmdQuit_Click()
End
End Sub 

表单应该是这样的: , 现在打开第二个表单,命名为“frmInstructions”。 你将需要:一个标签框,和一个命令按钮 插入所有这些作为标签框的标题: 隐藏,复制Code

The rules are simple. Enter your name(s) in the textbox(es) in the screen. Make sure you have a different name as your opponent or the game won't work properly. Decide who is player 1 and who is player 2. Then play the game. After the round is over. Press Clear to start a new game and press Rematch to play another game. With a rematch, player 2 will go first in the second game, player 1 in the 3rd and so on. If you are playing AI, then press PvAI in the start screen. Be careful, this AI is an expert and will not lose a game if messed with. The same thing will happen with that game. the player goes first and then the AI will go first and so on. To Start Press OK and press the other buttons in the program to begin. 

这是它应该看起来的样子: 然后输入命令按钮的代码: 隐藏,复制Code

Private Sub Command1_Click()
frmInstructions.Hide
End Sub

现在来看最重要的部分。 您将需要打开另一个表格,并输入: 2文本框27标签框12命令按钮1定时器12行 这些将是文本框的名称 txtplayer1 txtplayer2 这些将是标签盒的名称 lblx1 lblx2 lblx3 lblx4 lblx5 lblx6 lblx7 lblx8 lblx9 lblo1 lblo2 lblo3 lblo4 lblo5 lblo6 lblo7 lblo8 lblo9 lblwin1 lblwin2 这些将是命令按钮的名称: cmdbox1 cmdbox2 cmdbox3 cmdbox4 cmdbox5 cmdbox6 cmdbox7 cmdbox8 cmdbox9 cmdbox9 cmdbox1 cmdbox2 cmdbox3 cmdbox4 cmdbox5 cmdbox6 cmdbox7 cmdbox8 cmdbox9 cmdbox9 表单应该是这样的: 这里是整个代码的形式: 隐藏,收缩,复制Code

Sub Xwin()
MsgBox "Congradulations Player 1! You Win!"
lblWin1 = Val(lblWin1.Caption) + 1
cmdRematch.Visible = True
cmdQuit.Visible = True
Call Disable
End Sub

Sub Owin()
MsgBox "Congradulations Player 2! You Win!"
lblWin2 = Val(lblWin2.Caption) + 1
cmdRematch.Visible = True
cmdQuit.Visible = True
Call Disable
End Sub

Sub TieGame()
MsgBox "It's a Tie!"
lblTie = Val(lblTie.Caption) + 1
cmdRematch.Visible = True
cmdQuit.Visible = True
End Sub

Sub endings()
If (lblX1.Visible = True And lblX2.Visible = True And lblX3.Visible = True) Then
    Line13.Visible = True
    Line13.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblX1.Visible = True And lblX4.Visible = True And lblX7.Visible = True) Then
    Line11.Visible = True
    Line11.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblX1.Visible = True And lblX5.Visible = True And lblX9.Visible = True) Then
    Line12.Visible = True
    Line12.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblX2.Visible = True And lblX5.Visible = True And lblX8.Visible = True) Then
    Line6.Visible = True
    Line6.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblX3.Visible = True And lblX5.Visible = True And lblX7.Visible = True) Then
    Line9.Visible = True
    Line9.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblX4.Visible = True And lblX5.Visible = True And lblX6.Visible = True) Then
    Line10.Visible = True
    Line10.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblX3.Visible = True And lblX6.Visible = True And lblX9.Visible = True) Then
    Line8.Visible = True
    Line8.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblX7.Visible = True And lblX8.Visible = True And lblX9.Visible = True) Then
    Line7.Visible = True
    Line7.ZOrder (0)
    Timer1.Enabled = True
End If
If (lblO1.Visible = True And lblO2.Visible = True And lblO3.Visible = True) Then
    Line13.Visible = True
    Line13.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblO1.Visible = True And lblO4.Visible = True And lblO7.Visible = True) Then
    Line11.Visible = True
    Line11.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblO1.Visible = True And lblO5.Visible = True And lblO9.Visible = True) Then
    Line12.Visible = True
    Line12.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblO2.Visible = True And lblO5.Visible = True And lblO8.Visible = True) Then
    Line6.Visible = True
    Line6.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblO3.Visible = True And lblO5.Visible = True And lblO7.Visible = True) Then
    Line9.Visible = True
    Line9.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblO4.Visible = True And lblO5.Visible = True And lblO6.Visible = True) Then
    Line10.Visible = True
    Line10.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblO3.Visible = True And lblO6.Visible = True And lblO9.Visible = True) Then
    Line8.Visible = True
    Line8.ZOrder (0)
    Timer1.Enabled = True
ElseIf (lblO7.Visible = True And lblO8.Visible = True And lblO9.Visible = True) Then
    Line7.Visible = True
    Line7.ZOrder (0)
    Timer1.Enabled = True
End If
End Sub

Sub reset()
cmdBox1.Caption = ""
cmdBox2.Caption = ""
cmdBox3.Caption = ""
cmdBox4.Caption = ""
cmdBox5.Caption = ""
cmdBox6.Caption = ""
cmdBox7.Caption = ""
cmdBox8.Caption = ""
cmdBox9.Caption = ""
cmdBox1.Enabled = True
cmdBox2.Enabled = True
cmdBox3.Enabled = True
cmdBox4.Enabled = True
cmdBox5.Enabled = True
cmdBox6.Enabled = True
cmdBox7.Enabled = True
cmdBox8.Enabled = True
cmdBox9.Enabled = True
cmdBox1.Visible = True
cmdBox2.Visible = True
cmdBox3.Visible = True
cmdBox4.Visible = True
cmdBox5.Visible = True
cmdBox6.Visible = True
cmdBox7.Visible = True
cmdBox8.Visible = True
cmdBox9.Visible = True
lblX1.Visible = False
lblX2.Visible = False
lblX3.Visible = False
lblX4.Visible = False
lblX5.Visible = False
lblX6.Visible = False
lblX7.Visible = False
lblX8.Visible = False
lblX9.Visible = False
lblO1.Visible = False
lblO2.Visible = False
lblO3.Visible = False
lblO4.Visible = False
lblO5.Visible = False
lblO6.Visible = False
lblO7.Visible = False
lblO8.Visible = False
lblO9.Visible = False
Line6.Visible = False
Line7.Visible = False
Line8.Visible = False
Line9.Visible = False
Line10.Visible = False
Line11.Visible = False
Line12.Visible = False
Line13.Visible = False
Timer1.Enabled = False
End Sub

Sub Disable()
cmdBox1.Enabled = False
cmdBox2.Enabled = False
cmdBox3.Enabled = False
cmdBox4.Enabled = False
cmdBox5.Enabled = False
cmdBox6.Enabled = False
cmdBox7.Enabled = False
cmdBox8.Enabled = False
cmdBox9.Enabled = False
End Sub

Private Sub cmdBox1_Click()
Dim win9
If lblTurn.Caption = txtPlayer1.Text Then
    lblTurn.Caption = txtPlayer2.Text
    cmdBox1.Enabled = False
    cmdBox1.Visible = False
    lblX1.Visible = True
ElseIf lblTurn.Caption = txtPlayer2.Text Then
    lblTurn.Caption = txtPlayer1.Text
    cmdBox1.Enabled = False
    cmdBox1.Visible = False
    lblO1.Visible = True
End If

If (lblX1.Visible = True And lblX2.Visible = True And lblX3.Visible = True) Or (lblX1.Visible = True And lblX4.Visible = True And lblX7.Visible = True) Or (lblX1.Visible = True And lblX5.Visible = True And lblX9.Visible = True) Then
    Call Xwin
    Call endings
ElseIf (lblO1.Visible = True And lblO2.Visible = True And lblO3.Visible = True) Or (lblO1.Visible = True And lblO4.Visible = True And lblO7.Visible = True) Or (lblO1.Visible = True And lblO5.Visible = True And lblO9.Visible = True) Then
    Call Owin
    Call endings
ElseIf tie Then
    Call TieGame
End If
End Sub

Private Sub cmdBox2_Click()
If lblTurn.Caption = txtPlayer1.Text Then
    lblTurn.Caption = txtPlayer2.Text
    cmdBox2.Enabled = False
    cmdBox2.Visible = False
    lblX2.Visible = True
ElseIf lblTurn.Caption = txtPlayer2.Text Then
    lblTurn.Caption = txtPlayer1.Text
    cmdBox2.Enabled = False
    cmdBox2.Visible = False
    lblO2.Visible = True
End If

If (lblX1.Visible = True And lblX2.Visible = True And lblX3.Visible = True) Or (lblX2.Visible = True And lblX5.Visible = True And lblX8.Visible = True) Then
    Call Xwin
    Call endings
ElseIf (lblO1.Visible = True And lblO2.Visible = True And lblO3.Visible = True) Or (lblO2.Visible = True And lblO5.Visible = True And lblO8.Visible = True) Then
    Call Owin
    Call endings
ElseIf cmdBox1.Enabled = False And cmdBox2.Enabled = False And cmdBox3.Enabled = False And cmdBox4.Enabled = False And cmdBox5.Enabled = False And cmdBox6.Enabled = False And cmdBox7.Enabled = False And cmdBox8.Enabled = False And cmdBox9.Enabled = False Then
    Call TieGame
End If
End Sub

Private Sub cmdBox3_Click()
If lblTurn.Caption = txtPlayer1.Text Then
    lblTurn.Caption = txtPlayer2.Text
    cmdBox3.Enabled = False
    cmdBox3.Visible = False
    lblX3.Visible = True
ElseIf lblTurn.Caption = txtPlayer2.Text Then
    lblTurn.Caption = txtPlayer1.Text
    cmdBox3.Enabled = False
    cmdBox3.Visible = False
    lblO3.Visible = True
End If

If (lblX1.Visible = True And lblX2.Visible = True And lblX3.Visible = True) Or (lblX3.Visible = True And lblX5.Visible = True And lblX7.Visible = True) Or (lblX4.Visible = True And lblX5.Visible = True And lblX6.Visible = True) Then
    Call Xwin
    Call endings
ElseIf (lblO1.Visible = True And lblO2.Visible = True And lblO3.Visible = True) Or (lblO3.Visible = True And lblO5.Visible = True And lblO7.Visible = True) Or (lblO4.Visible = True And lblO5.Visible = True And lblO6.Visible = True) Then
    Call Owin
    Call endings
ElseIf cmdBox1.Enabled = False And cmdBox2.Enabled = False And cmdBox3.Enabled = False And cmdBox4.Enabled = False And cmdBox5.Enabled = False And cmdBox6.Enabled = False And cmdBox7.Enabled = False And cmdBox8.Enabled = False And cmdBox9.Enabled = False Then
    Call TieGame
End If
End Sub

Private Sub cmdBox4_Click()
If lblTurn.Caption = txtPlayer1.Text Then
    cmdBox4.Caption = "X"
    lblTurn.Caption = txtPlayer2.Text
    cmdBox4.Enabled = False
    cmdBox4.Visible = False
    lblX4.Visible = True
ElseIf lblTurn.Caption = txtPlayer2.Text Then
    cmdBox4.Caption = "O"
    lblTurn.Caption = txtPlayer1.Text
    cmdBox4.Enabled = False
    cmdBox4.Visible = False
    lblO4.Visible = True
End If

If (lblX1.Visible = True And lblX4.Visible = True And lblX7.Visible = True) Or (lblX4.Visible = True And lblX5.Visible = True And lblX6.Visible = True) Then
    Call Xwin
    Call endings
ElseIf (lblO1.Visible = True And lblO4.Visible = True And lblO7.Visible = True) Or (lblO4.Visible = True And lblO5.Visible = True And lblO6.Visible = True) Then
    Call Owin
    Call endings
ElseIf cmdBox1.Enabled = False And cmdBox2.Enabled = False And cmdBox3.Enabled = False And cmdBox4.Enabled = False And cmdBox5.Enabled = False And cmdBox6.Enabled = False And cmdBox7.Enabled = False And cmdBox8.Enabled = False And cmdBox9.Enabled = False Then
    Call TieGame
End If
End Sub

Private Sub cmdBox5_Click()
If lblTurn.Caption = txtPlayer1.Text Then
    cmdBox5.Caption = "X"
    lblTurn.Caption = txtPlayer2.Text
    cmdBox5.Enabled = False
    cmdBox5.Visible = False
    lblX5.Visible = True
ElseIf lblTurn.Caption = txtPlayer2.Text Then
    cmdBox5.Caption = "O"
    lblTurn.Caption = txtPlayer1.Text
    cmdBox5.Enabled = False
    cmdBox5.Visible = False
    lblO5.Visible = True
End If

If (lblX1.Visible = True And lblX5.Visible = True And lblX9.Visible = True) Or (lblX2.Visible = True And lblX5.Visible = True And lblX8.Visible = True) Or (lblX3.Visible = True And lblX5.Visible = True And lblX7.Visible = True) Or (lblX4.Visible = True And lblX5.Visible = True And lblX6.Visible = True) Then
    Call Xwin
    Call endings
ElseIf (lblO1.Visible = True And lblO5.Visible = True And lblO9.Visible = True) Or (lblO2.Visible = True And lblO5.Visible = True And lblO8.Visible = True) Or (lblO3.Visible = True And lblO5.Visible = True And lblO7.Visible = True) Or (lblO4.Visible = True And lblO5.Visible = True And lblO6.Visible = True) Then
    Call Owin
    Call endings
ElseIf cmdBox1.Enabled = False And cmdBox2.Enabled = False And cmdBox3.Enabled = False And cmdBox4.Enabled = False And cmdBox5.Enabled = False And cmdBox6.Enabled = False And cmdBox7.Enabled = False And cmdBox8.Enabled = False And cmdBox9.Enabled = False Then
    Call TieGame
End If
End Sub

Private Sub cmdBox6_Click()
If lblTurn.Caption = txtPlayer1.Text Then
    cmdBox6.Caption = "X"
    lblTurn.Caption = txtPlayer2.Text
    cmdBox6.Enabled = False
    cmdBox6.Visible = False
    lblX6.Visible = True
ElseIf lblTurn.Caption = txtPlayer2.Text Then
    cmdBox6.Caption = "O"
    lblTurn.Caption = txtPlayer1.Text
    cmdBox6.Enabled = False
    cmdBox6.Visible = False
    lblO6.Visible = True
End If

If (lblX4.Visible = True And lblX5.Visible = True And lblX6.Visible = True) Or (lblX4.Visible = True And lblX5.Visible = True And lblX6.Visible = True) Then
    Call Xwin
    Call endings
ElseIf (lblO4.Visible = True And lblO5.Visible = True And lblO6.Visible = True) Or (lblO3.Visible = True And lblO6.Visible = True And lblO9.Visible = True) Then
    Call Owin
    Call endings
ElseIf cmdBox1.Enabled = False And cmdBox2.Enabled = False And cmdBox3.Enabled = False And cmdBox4.Enabled = False And cmdBox5.Enabled = False And cmdBox6.Enabled = False And cmdBox7.Enabled = False And cmdBox8.Enabled = False And cmdBox9.Enabled = False Then
    Call TieGame
End If
End Sub

Private Sub cmdBox7_Click()
If lblTurn.Caption = txtPlayer1.Text Then
    cmdBox7.Caption = "X"
    lblTurn.Caption = txtPlayer2.Text
    cmdBox7.Enabled = False
    cmdBox7.Visible = False
    lblX7.Visible = True
ElseIf lblTurn.Caption = txtPlayer2.Text Then
    cmdBox7.Caption = "O"
    lblTurn.Caption = txtPlayer1.Text
    cmdBox7.Enabled = False
    cmdBox7.Visible = False
    lblO7.Visible = True
End If

If (lblX1.Visible = True And lblX4.Visible = True And lblX7.Visible = True) Or (lblX3.Visible = True And lblX5.Visible = True And lblX7.Visible = True) Or (lblX7.Visible = True And lblX8.Visible = True And lblX9.Visible = True) Then
    Call Xwin
    Call endings
ElseIf (lblO1.Visible = True And lblO4.Visible = True And lblO7.Visible = True) Or (lblO3.Visible = True And lblO5.Visible = True And lblO7.Visible = True) Or (lblO7.Visible = True And lblO8.Visible = True And lblO9.Visible = True) Then
    Call Owin
    Call endings
ElseIf cmdBox1.Enabled = False And cmdBox2.Enabled = False And cmdBox3.Enabled = False And cmdBox4.Enabled = False And cmdBox5.Enabled = False And cmdBox6.Enabled = False And cmdBox7.Enabled = False And cmdBox8.Enabled = False And cmdBox9.Enabled = False Then
    Call TieGame
End If
End Sub

Private Sub cmdBox8_Click()
If lblTurn.Caption = txtPlayer1.Text Then
    cmdBox8.Caption = "X"
    lblTurn.Caption = txtPlayer2.Text
    cmdBox8.Enabled = False
    cmdBox8.Visible = False
    lblX8.Visible = True
ElseIf lblTurn.Caption = txtPlayer2.Text Then
    cmdBox8.Caption = "O"
    lblTurn.Caption = txtPlayer1.Text
    cmdBox8.Enabled = False
    cmdBox8.Visible = False
    lblO8.Visible = True
End If

If (lblX2.Visible = True And lblX5.Visible = True And lblX8.Visible = True) Or (lblX7.Visible = True And lblX8.Visible = True And lblX9.Visible = True) Then
    Call Xwin
    Call endings
ElseIf (lblO2.Visible = True And lblO5.Visible = True And lblO8.Visible = True) Or (lblO7.Visible = True And lblO8.Visible = True And lblO9.Visible = True) Then
    Call Owin
    Call endings
ElseIf cmdBox1.Enabled = False And cmdBox2.Enabled = False And cmdBox3.Enabled = False And cmdBox4.Enabled = False And cmdBox5.Enabled = False And cmdBox6.Enabled = False And cmdBox7.Enabled = False And cmdBox8.Enabled = False And cmdBox9.Enabled = False Then
    Call TieGame
End If
End Sub

Private Sub cmdBox9_Click()
If lblTurn.Caption = txtPlayer1.Text Then
    cmdBox9.Caption = "X"
    lblTurn.Caption = txtPlayer2.Text
    cmdBox9.Enabled = False
    cmdBox9.Visible = False
    lblX9.Visible = True
ElseIf lblTurn.Caption = txtPlayer2.Text Then
    cmdBox9.Caption = "O"
    lblTurn.Caption = txtPlayer1.Text
    cmdBox9.Enabled = False
    cmdBox9.Visible = False
    lblO9.Visible = True
End If

If (lblX1.Visible = True And lblX5.Visible = True And lblX9.Visible = True) Or (lblX3.Visible = True And lblX6.Visible = True And lblX9.Visible = True) Or (lblX7.Visible = True And lblX8.Visible = True And lblX9.Visible = True) Then
    Call Xwin
    Call endings
ElseIf (lblO1.Visible = True And lblO5.Visible = True And lblO9.Visible = True) Or (lblO3.Visible = True And lblO6.Visible = True And lblO9.Visible = True) Or (lblO7.Visible = True And lblO8.Visible = True And lblO9.Visible = True) Then
    Call Owin
    Call endings
ElseIf cmdBox1.Enabled = False And cmdBox2.Enabled = False And cmdBox3.Enabled = False And cmdBox4.Enabled = False And cmdBox5.Enabled = False And cmdBox6.Enabled = False And cmdBox7.Enabled = False And cmdBox8.Enabled = False And cmdBox9.Enabled = False Then
    Call TieGame
End If
End Sub

Private Sub cmdClear_Click()
Call reset
lblWin1.Caption = 0
lblWin2.Caption = 0
txtPlayer1 = ""
txtPlayer2 = ""
lblTurn.Caption = ""
frmTicTacToe.Hide
frmStartScreen.Show
frmStartScreen.txtPlay1.SetFocus

End Sub

Private Sub cmdPlay_Click()
If txtPlayer1 = "" Or txtPlayer2 = "" Then
    MsgBox "Enter your names or start a new game."
End If
If txtPlayer1.Text = txtPlayer2.Text Then
    MsgBox "Enter another name Player 1 or Player 2."
Else
    lblTurn.Caption = txtPlayer1.Text
End If
End Sub

Private Sub cmdQuit_Click()
End
End Sub

Private Sub cmdRematch_Click()
Call reset
If (Val(lblWin1.Caption) + Val(lblWin2.Caption) + Val(lblTie.Caption)) Mod 2 = 1 Then
    lblTurn.Caption = txtPlayer2.Text
ElseIf (Val(lblWin1.Caption) + Val(lblWin2.Caption)) Mod 2 = 0 Then
    lblTurn.Caption = txtPlayer1.Text
End If
End Sub

Private Sub Form_Load()

End Sub

Private Sub Timer1_Timer()

If (lblX1.Visible = True And lblX2.Visible = True And lblX3.Visible = True) Then
    Me.Line13.Visible = Not Me.Line13.Visible
ElseIf (lblX1.Visible = True And lblX4.Visible = True And lblX7.Visible = True) Then
    Me.Line11.Visible = Not Me.Line11.Visible
ElseIf (lblX1.Visible = True And lblX5.Visible = True And lblX9.Visible = True) Then
    Me.Line12.Visible = Not Me.Line12.Visible
ElseIf (lblX2.Visible = True And lblX5.Visible = True And lblX8.Visible = True) Then
    Me.Line6.Visible = Not Me.Line6.Visible
ElseIf (lblX3.Visible = True And lblX5.Visible = True And lblX7.Visible = True) Then
    Me.Line9.Visible = Not Me.Line9.Visible
ElseIf (lblX4.Visible = True And lblX5.Visible = True And lblX6.Visible = True) Then
    Me.Line10.Visible = Not Me.Line10.Visible
ElseIf (lblX3.Visible = True And lblX6.Visible = True And lblX9.Visible = True) Then
    Me.Line8.Visible = Not Me.Line8.Visible
ElseIf (lblX7.Visible = True And lblX8.Visible = True And lblX9.Visible = True) Then
    Me.Line7.Visible = Not Me.Line7.Visible
ElseIf (lblO1.Visible = True And lblO2.Visible = True And lblO3.Visible = True) Then
    Me.Line13.Visible = Not Me.Line13.Visible
ElseIf (lblO1.Visible = True And lblO4.Visible = True And lblO7.Visible = True) Then
    Me.Line11.Visible = Not Me.Line11.Visible
ElseIf (lblO1.Visible = True And lblO5.Visible = True And lblO9.Visible = True) Then
    Me.Line12.Visible = Not Me.Line12.Visible
ElseIf (lblO2.Visible = True And lblO5.Visible = True And lblO8.Visible = True) Then
    Me.Line6.Visible = Not Me.Line6.Visible
ElseIf (lblO3.Visible = True And lblO5.Visible = True And lblO7.Visible = True) Then
    Me.Line9.Visible = Not Me.Line9.Visible
ElseIf (lblO4.Visible = True And lblO5.Visible = True And lblO6.Visible = True) Then
    Me.Line10.Visible = Not Me.Line10.Visible
ElseIf (lblO3.Visible = True And lblO6.Visible = True And lblO9.Visible = True) Then
    Me.Line8.Visible = Not Me.Line8.Visible
ElseIf (lblO7.Visible = True And lblO8.Visible = True And lblO9.Visible = True) Then
    Me.Line7.Visible = Not Me.Line7.Visible
End If
End Sub

现在您已经完成了玩家对玩家的部分,现在让我们进入玩家对计算机的部分。 首先,打开一个表单,命名为“frmPvAIWelcome”,将以下对象放入表单: 1帧2选择框1命令按钮 两个选项框的名称分别为“optXorO(0)”和“optXorO(1)”,命令按钮应命名为“cmdOK” 更改表单的标题,使其看起来像这样: 这是这个表格的代码: 隐藏,复制CodeOption显式 “井字游戏的启动表单。”用户选择他或她 '想要成为' X '或' O '(选择适当的选项按钮) ',然后单击OK。 私人潜艇Form_Load () CenterForm我 终止子 私人潜艇cmdOK_Click () '为玩家的字母和设置全局变量 “电脑的信,出示主要表格,把这封信卸了。 gstrPlayerLetter = IIf (optXorO(0)。价值,“X”、“O”) IIf(gstrPlayerLetter = "X", "O", "X") frmPvAI.Show 卸载我 终止子 现在,打开一个模块,将其命名为“modPvAI”并输入以下代码。 隐藏,复制CodeOption显式 公共gstrPlayerLetter作为字符串* 1 公共gstrComputerLetter为字符串* 1 ”, 公共子中心表单(objForm作为表单) ”, 与objForm .Top =(屏幕。Height - .Height) / 2 .Left =(屏幕。Width - .Width) / 2 结尾 终止子 现在,打开一个表单,命名为“frmPvAI”,并将这些对象放入其中: , 一个文本框20标签框2个命令按钮1定时器 “txtPlayer”应该是文本框的名称。 , 下面是需要更改名称的标签盒的名称: , lblBox(0) lblBox(1) lblBox(2) lblBox(3) lblBox(4) lblBox(5) lblBox(6) lblBox(7) lblBox(8) , 赢了,玩了,赢了,输了 命令按钮的名称为“cmdPlayAgain”和“cmdQuit”。 以下是表格代码: 隐藏,收缩,复制CodeOption显式 “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ` *模块级变量声明* “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 私有mintgamesplay为整数 私有mintGamesWon为整数 私有mintGamesLost为整数 私有mintGamesTied为整数 Private mblnGameOver为布尔值 私有mintgameresult为整数 私人Const mintTIE_GAME整数= 0 私人Const mintCOMPUTER_WINS整数= 1 私人Const mintPLAYER_WINS整数= 2 “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ‘* *形成事件 “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 私人潜艇Form_Load () CenterForm我 StartNewGame 终止子 私人潜艇Form_Unload(取消为整数) 如果对话框(“你确定你想辞职?”,_ vbYesNo + vbQuestion _ “退出圈圈叉叉”)= vbNo 取消= 1 如果 终止子 私人潜艇Label4_Click () 终止子 “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ‘* *标签事件 “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 私人潜艇lblBox_Click(指数为整数) ”在这种情况下,评估方棋盘用户点击, ”并采取相应行动。(“广场”在这种情况下是9的标签之一 lblBox控制数组索引0 - 8)。 如果用户试图点击广场游戏结束后, “忽略点击… 如果mblnGameOver退出子 如果用户试图点击广场已经填充一个“X” ”或“O”,忽略了点击… 如果lblBox(指数)。标题& lt;比;”“然后退出子 玩家点击一个空的平方,所以填充它的玩家的信 ”(“X”或“O”取决于他们选择当应用程序开始)…… lblBox(指数)。标题= gstrPlayerLetter ”看看球员赢得了这个举动测试8赢得的可行方法。 如果玩家获胜,叫败阵的例程;否则,看看这一举动 导致游戏结束的领带,如果没有,电脑把它转… 选择案例真实 例PlayerWins(0, 1, 2):败阵mintPLAYER_WINS, 0 案例PlayerWins(0、4、8):败阵mintPLAYER_WINS, 1 例PlayerWins(0、3、6):败阵mintPLAYER_WINS, 2 案例PlayerWins(1、4、7):败阵mintPLAYER_WINS, 3 例PlayerWins(2、5、8):败阵mintPLAYER_WINS, 4 例PlayerWins(2、4、6):败阵mintPLAYER_WINS, 5 案例PlayerWins(3、4、5):败阵mintPLAYER_WINS, 6 例PlayerWins(6、7、8):败阵mintPLAYER_WINS, 7 其他情况:如果不是TieGame那么TakeComputerTurn 最终选择 终止子 “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ‘* *命令按钮事件 “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 私人潜艇cmdPlayAgain_Click () StartNewGame 终止子 私人潜艇cmdQuit_Click () 卸载我 终止子 私人潜艇lblGamesWon_Click () 终止子 “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ‘* *计时器事件 “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 私人潜艇tmrFlash_Timer () 游戏结束的时候启用的计时器。在每一个定时器间隔,切换 lblWins的可见属性标签,报告的结果 “游戏(“你赢了”,“你输了”,“这是一条领带”)。切换可见属性 “原因闪烁的效果… lblWins。可见=不是lblWins.Visible 终止子 “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ‘* Programmer-Defined * ‘*潜艇,功能* “* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ”, 私人潜艇StartNewGame () ” 这个例程执行所有必要的初始化任务的新游戏…… ” ”, 暗intX为整数 暗intGoesFirst为整数 “重置“游戏结束”国旗… mblnGameOver = False 清除9平方的棋盘…… intX = 0到8 lblBox (intX)。标题= " " 下一个 确保没有一个8行(一个为每个方法赢得)是可见的。 intX = 0到7 linWin (intX)。可见= False 下一个 “禁用计时器和隐藏消息闪烁;禁用“打球” ”按钮…… tmrFlash。启用= False lblWins。可见= False cmdPlayAgain。启用= False 生成一个随机数(1或2),看谁先(1 =电脑, ' 2 =玩家)… 随机化 intGoesFirst = Int(2 * Rnd + 1) 通知用户谁先去。如果是电脑,那就用电脑 “把… 如果intGoesFirst = 1,则 “这次我先走”,“新游戏” TakeComputerTurn 其他的 “这次你先来”,“新游戏” 如果 终止子 ”, 私人潜艇TakeComputerTurn () ” 这一程序实现了计算机的策略,使其采取行动…… ” ”, Dim intX为整数 Dim blnMoveMade为布尔值 “首先,看电脑在这一步棋里放上‘X’,看它是否能赢。 或“O”在8种可能的获胜序列中的任意一种。如果是这样, “说出来,走吧。 如果ComputerWins(0,1,2),则GameOver mintCOMPUTER_WINS, 0:退出Sub 如果ComputerWins(0,4,8),那么GameOver mintCOMPUTER_WINS, 1:退出Sub 如果ComputerWins(0,3,6),则GameOver mintCOMPUTER_WINS, 2:退出Sub 如果computer_wins(1,4,7),则GameOver mintCOMPUTER_WINS, 3:退出Sub 如果ComputerWins(2,5,8),那么GameOver mintCOMPUTER_WINS, 4:退出Sub 如果ComputerWins(2,4,6),那么GameOver mintCOMPUTER_WINS, 5:退出Sub 如果ComputerWins(3,4,5),那么GameOver mintCOMPUTER_WINS, 6:退出Sub 如果ComputerWins(6,7,8),那么GameOver mintCOMPUTER_WINS, 7:退出Sub “如果我们这一步走不赢,看看玩家是否可以用他的or来赢。 “她的下一步行动,如果是,阻止它,看看我们是否有平局,和 “滚出去…… 如果ComputerBlocks(0、1、2),则TieGame:退出子程序 如果ComputerBlocks(0、4、8),则TieGame:退出子程序 如果ComputerBlocks(0、3、6),则TieGame:退出子程序 如果ComputerBlocks(1,4,7),则TieGame:退出子程序 如果ComputerBlocks(2,5,8),则TieGame:退出子程序 如果ComputerBlocks(2,4,6),则TieGame:退出子程序 如果ComputerBlocks(3,4,5),则TieGame:退出子程序 如果ComputerBlocks(6、7、8),则TieGame: Exit Sub 如果我们走到这一步,电脑不可能赢,它也没有赢 “必要块。因此,选择一个战略位置来放置 “电脑的信…… blnMoveMade = True “首先走中间那个方格…… 如果lblBox(4)。说明= ""然后 lblBox(4)。标题= gstrComputerLetter 否则,试试左上角…… ElseIf lblBox(0)。说明= ""然后 lblBox(0)。标题= gstrComputerLetter “否则,试试右上角…… ElseIf lblBox(2)。说明= ""然后 lblBox(2)。标题= gstrComputerLetter 否则,试试左下角…… ElseIf lblBox(6)。说明= ""然后 lblBox(6)。标题= gstrComputerLetter “否则,试试右下角…… ElseIf lblBox(8)。说明= ""然后 lblBox(8)。标题= gstrComputerLetter 否则,如果电脑的字母占据了中间位置 “方形,选择中间的左边或中间的右边的方形…… ElseIf lblBox(4)。说明= gstrComputerLetter Then 如果lblBox(3)。说明= ""然后 lblBox(3)。标题= gstrComputerLetter ElseIf lblBox(5)。说明= ""然后 lblBox(5)。标题= gstrComputerLetter 其他的 blnMoveMade = False 如果 其他的 blnMoveMade = False 如果 如果以上任何一个动作我们都做不到,那就使用下一个动作 “广场… 如果没有blnMoveMade那么 intX = 0到8 如果lblBox (intX)。说明= ""然后 lblBox (intX)。标题= gstrComputerLetter 退出 如果 下一个 如果 检查一下电脑刚才的移动是否导致了这个游戏 “以领带结尾…… TieGame 终止子 ”, 私有函数PlayerWins(pintPos1为整数,_ pintPos2为整数,_ pintPos3作为整数)_ 作为布尔 ” 这个程序通过测试一个球员来决定他是否刚刚赢了 “在八种可能的获胜方式中…… ” ”, 如果正在测试的配置中有任何方块是空的,玩家就会被告知 “还没有赢…… 如果lblBox (pintPos1)。说明= "" _ 或lblBox (pintPos2)。说明= "" _ 或lblBox (pintPos3)。说明= ""然后 PlayerWins = False 其他的 如果测试中的三个方格都有 相同的值,那么玩家就赢了,否则游戏还在 的进步…… 如果lblBox (pintPos1)。标题= lblBox (pintPos2)。标题_ 和lblBox (pintPos1)。标题= lblBox (pintPos3)。标题然后 玩erwin = True 其他的 PlayerWins = False 如果 如果 结束函数 ”, 私有函数ComputerWins(pintPos1为整数,_ pintPos2为整数,_ pintPos3作为整数)_ 作为布尔 ” 这一程序将决定电脑在这一步中是否能获胜 通过在8种可能的序列中选择一个空位来测试 “赢的方法…… ” ”, 如果lblBox (pintPos1)。说明= "" _ 和lblBox (pintPos2)。说明= gstrComputerLetter _ 和lblBox (pintPos3)。说明= gstrComputerLetter Then lblBox (pintPos1)。标题= gstrComputerLetter ComputerWins = True 退出函数 如果 如果lblBox (pintPos1)。说明= gstrComputerLetter _ 和lblBox (pintPos2)。说明= "" _ 和lblBox (pintPos3)。说明= gstrComputerLetter Then lblBox (pintPos2)。标题= gstrComputerLetter ComputerWins = True 退出函数 如果 如果lblBox (pintPos1)。说明= gstrComputerLetter _ 和lblBox (pintPos2)。说明= gstrComputerLetter _ 和lblBox (pintPos3)。说明= ""然后 lblBox (pintPos3)。标题= gstrComputerLetter ComputerWins = True 退出函数 如果 ComputerWins = False 结束函数 ”, 私有函数ComputerBlocks(pintPos1为整数,_ pintPos2为整数,_ pintPos3作为整数)_ 作为布尔 ” 这个程序决定电脑是否必须阻止该玩家 为了防止玩家在下一个动作中获胜…… ” ”, 如果lblBox (pintPos1)。说明= "" _ 和lblBox (pintPos2)。说明= gstrPlayerLetter _ 和lblBox (pintPos3)。说明= gstrPlayerLetter Then lblBox (pintPos1)。标题= gstrComputerLetter ComputerBlocks = True 退出函数 如果 如果lblBox (pintPos1)。说明= gstrPlayerLetter _ 和lblBox (pintPos2)。说明= "" _ 和lblBox (pintPos3)。说明= gstrPlayerLetter Then lblBox (pintPos2)。标题= gstrComputerLetter ComputerBlocks = True 退出函数 如果 如果lblBox (pintPos1)。说明= gstrPlayerLetter _ 和lblBox (pintPos2)。说明= gstrPlayerLetter _ 和lblBox (pintPos3)。说明= ""然后 lblBox (pintPos3)。标题= gstrComputerLetter ComputerBlocks = True 退出函数 如果 ComputerBlocks = False 结束函数 ”, 私有函数TieGame()作为布尔值 ” 他说,这种例行程序通过看比赛是否会平局,来决定比赛是否会打成平手 或者不是所有的广场都有人。如果是,那就是平局, “因为我们会决定是电脑还是玩家 “在来这里之前已经赢了。 ” ”, Dim intX为整数 intX = 0到8 如果lblBox (intX)。说明= ""然后 TieGame = False 退出函数 如果 下一个 TieGame = True 败阵mintTIE_GAME 结束函数 ”, Private Sub GameOver(pintgameresult为整数,_ 可选的pintLineIndex为整数) ” 这个程序会显示一条闪烁的信息,指示三种方式中的哪一种可以让你的手机在屏幕上显示 游戏结束(玩家要么赢了,要么输了,要么打成平手)。它还画了一条线 ,并更新游戏数据。 ” ”, strOutcomeMsg作为字符串 如果pintgameresult = mintTIE_GAME,则 “平局!!” = mintGamesTied + 1 其他的 linWin (pintLineIndex)。可见= True 如果pintgameresult = mintCOMPUTER_WINS Then “你输了!!” mintGamesLost = mintGamesLost + 1 其他的 “你赢了!!” mintGamesWon = mintGamesWon + 1 如果 如果 lblWins。标题= strOutcomeMsg lblWins。可见= True tmrFlash。启用= True cmdPlayAgain。启用= True “更新数据…… mintGamesPlayed = mintGamesPlayed + 1 lblGamesPlayed。标题=装运箱(mintGamesPlayed) lblGamesWon。标题=装运箱(mintGamesWon) lblGamesLost。标题=装运箱(mintGamesLost) lblGamesTied。标题=装运箱(mintGamesTied) mblnGameOver = True 终止子 现在你完成了。所以,如果你足够好,继续和别人或电脑玩吧。 的兴趣点 人工智能太难了,我需要把它用于学校的一个项目。我的版本是我们班上最好的井字游戏现在你们也可以玩了。 历史 随时更新您在这里所做的任何更改或改进。 本文转载于:http://www.diyabc.com/frontweb/news2285.html

原文地址:https://www.cnblogs.com/Dincat/p/13457447.html