65.9K
CodeProject 正在变化。 阅读更多。
Home

制作您自己的井字棋游戏

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.58/5 (6投票s)

2014年7月13日

CPOL

3分钟阅读

viewsIcon

25736

这将帮助您制作自己的高级井字棋游戏。

引言

如果你想制作自己的游戏,这里有一个简单的游戏,尽管对于一个简单的游戏来说,它也属于高级游戏。 这是针对 Visual Studio 的井字棋。 只要按照这些步骤操作,你就能成功制作出属于你自己的,令人惊叹的井字棋游戏。 只要下载链接,直到我说我完全完成了

背景

对于这个项目,你需要理解 5 种基本数据类型和布尔数据类型。 你需要熟悉你在表单上可以使用的大部分基本工具,例如选项框、命令按钮等。 那么,让我们开始吧。

使用代码

我们将从制作玩家对玩家的游戏开始,因为它更容易。

首先,打开 visual basic 6 并打开一个表单。 这将像一个主菜单表单。 接下来,将表单的名称更改为 "frmStartScreen"。

你将需要这些对象作为起始表单

  • 2 个文本框
  • 2 个标签框
  • 4 个命令按钮

将一个文本框标签为 "txtPlay1",另一个文本框标签为 "txtPlay2",将命令按钮标签为 "cmdStart"、"cmdInstruct"、"cmdAIgame" 和 "cmdQuit"

这是 "cmdInstruct" 按钮的代码

Private Sub cmdInstruct_Click()
frmInstructions.Show
End Sub 

现在是 "cmdStart" 按钮的代码

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"

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

对于 "cmdQuit" 按钮。

Private Sub cmdQuit_Click()
End
End Sub 

这是表单应该是什么样子的

 

现在打开第二个表单,将其命名为 "frmInstructions"。

你将需要:1 个标签框和 1 个命令按钮

将所有这些作为标签框的标题插入

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. 

它应该是这样的

并将此代码放入命令按钮中

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
  • lblTurn
  • lblWin1
  • lblWin2
  • lblTie

这些将是命令按钮的名称

  • cmdBox1
  • cmdBox2
  • cmdBox3
  • cmdBox4
  • cmdBox5
  • cmdBox6
  • cmdBox7
  • cmdBox8
  • cmdBox9
  • cmdClear
  • cmdRematch
  • cmdQuit

表单应该像这样

这是表单的全部代码

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"

更改表单中的标题,使其看起来像这样

这是该表单的代码

Option Explicit

' Start-up form for Tic-Tac-Toe game. The user chooses whether he or she
' wants to be "X" or "O" (by selecting the appropriate option button)
' and then clicks OK.

Private Sub Form_Load()
    CenterForm Me
End Sub


Private Sub cmdOK_Click()

    ' Set the global variables for the player's letter and the
    ' computer's letter, show the main form, and unload this one.
    
    gstrPlayerLetter = IIf(optXorO(0).Value, "X", "O")
    gstrComputerLetter = IIf(gstrPlayerLetter = "X", "O", "X")
    frmPvAI.Show
    Unload Me

End Sub 

现在,打开一个模块,命名为 "modPvAI" 并输入此代码。

Option Explicit

Public gstrPlayerLetter      As String * 1
Public gstrComputerLetter    As String * 1

'------------------------------------------------------------------------
 Public Sub CenterForm(objForm As Form)
'------------------------------------------------------------------------

    With objForm
        .Top = (Screen.Height - .Height) / 2
        .Left = (Screen.Width - .Width) / 2
    End With

  End Sub

现在,打开一个表单,将其命名为 "frmPvAI",并将这些对象放入其中

 

  • 1 个文本框
  • 20 个标签框
  • 2 个命令按钮
  • 1 个计时器
"txtPlayer" 应该是文本框的名称。

 

以下是需要更改名称的标签框的名称

 

  • lblBox(0)
  • lblBox(1)
  • lblBox(2)
  • lblBox(3)
  • lblBox(4)
  • lblBox(5)
  • lblBox(6)
  • lblBox(7)
  • lblBox(8)

 

  • lblWins
  • lblGamesPlayed
  • lblGamesWon
  • lblGamesLost
  • lblGamesTied

命令按钮的名称是 "cmdPlayAgain" 和 "cmdQuit"。

这是该表单的代码

Option Explicit

'************************************************************************************
'*                   Module-Level Variable Declarations                             *
'************************************************************************************

Private mintGamesPlayed         As Integer
Private mintGamesWon            As Integer
Private mintGamesLost           As Integer
Private mintGamesTied           As Integer
Private mblnGameOver            As Boolean

Private mintGameOutcome         As Integer
Private Const mintTIE_GAME      As Integer = 0
Private Const mintCOMPUTER_WINS As Integer = 1
Private Const mintPLAYER_WINS   As Integer = 2


'************************************************************************************
'*                                    Form Events                                   *
'************************************************************************************

Private Sub Form_Load()

    CenterForm Me
    
    StartNewGame
    
End Sub


Private Sub Form_Unload(Cancel As Integer)

    If MsgBox("Are you sure you want to quit?", _
              vbYesNo + vbQuestion, _
              "Quit Tic Tac Toe") = vbNo Then
        Cancel = 1
    End If
    
End Sub


Private Sub Label4_Click()

End Sub

'************************************************************************************
'*                                    Label Events                                  *
'************************************************************************************

Private Sub lblBox_Click(Index As Integer)

    ' In this event, evaluate which square of the gameboard the user clicked on,
    ' and act accordingly. (A "square" in this case is one of the 9 labels in the
    ' lblBox control array, indexed 0 to 8.)

    ' If the user tries to click on a square after the game is over,
    ' ignore the click ...
    If mblnGameOver Then Exit Sub
    
    ' If the user tries to click on a square that is already populated with an "X"
    ' or "O", ignore the click ...
    If lblBox(Index).Caption <> "" Then Exit Sub
    
    ' The player clicked on an empty square, so populate it with the player's letter
    ' ("X" or "O" depending on what they chose when the app started) ...
    lblBox(Index).Caption = gstrPlayerLetter
    
    ' Check to see if the player won with this move by testing the 8 possible ways to win.
    ' If the player won, call the GameOver routine; otherwise, check to see if this move
    ' caused the game to end in a tie, and if not, have the computer take its turn ...
    Select Case True
        Case PlayerWins(0, 1, 2):   GameOver mintPLAYER_WINS, 0
        Case PlayerWins(0, 4, 8):   GameOver mintPLAYER_WINS, 1
        Case PlayerWins(0, 3, 6):   GameOver mintPLAYER_WINS, 2
        Case PlayerWins(1, 4, 7):   GameOver mintPLAYER_WINS, 3
        Case PlayerWins(2, 5, 8):   GameOver mintPLAYER_WINS, 4
        Case PlayerWins(2, 4, 6):   GameOver mintPLAYER_WINS, 5
        Case PlayerWins(3, 4, 5):   GameOver mintPLAYER_WINS, 6
        Case PlayerWins(6, 7, 8):   GameOver mintPLAYER_WINS, 7
        Case Else:                  If Not TieGame Then TakeComputerTurn
    End Select
    
End Sub


'************************************************************************************
'*                             Command Button Events                                *
'************************************************************************************

Private Sub cmdPlayAgain_Click()
    
    StartNewGame

End Sub


Private Sub cmdQuit_Click()
    
    Unload Me

End Sub


Private Sub lblGamesWon_Click()

End Sub

'************************************************************************************
'*                                  Timer Event                                     *
'************************************************************************************

Private Sub tmrFlash_Timer()

    ' The timer is enabled when the game ends. On every timer interval, toggle the
    ' Visible property of the lblWins label, which reports the outcome of the
    ' game ("YOU WIN", "YOU LOSE", "IT'S A TIE"). Toggling the Visible property
    ' causes the blinking effect ...

    lblWins.Visible = Not lblWins.Visible

End Sub


'************************************************************************************
'*                                  Programmer-Defined                              *
'*                                   Subs & Functions                               *
'************************************************************************************

'------------------------------------------------------------------------------------
Private Sub StartNewGame()
'
'    This routine performs all the initialization tasks necessary for a new game...
'
'------------------------------------------------------------------------------------

    Dim intX            As Integer
    Dim intGoesFirst    As Integer
    
    ' Reset "game over" flag ...
    mblnGameOver = False
    
    ' Clear the 9 squares of the gameboard ...
    For intX = 0 To 8
        lblBox(intX).Caption = ""
    Next
    
    ' Make sure none of the 8 lines (one for each way to win) is visible ...
    For intX = 0 To 7
        linWin(intX).Visible = False
    Next
    
    ' Disable the timer and hide the flashing message; disable the "Play Again"
    ' button ...
    tmrFlash.Enabled = False
    lblWins.Visible = False
    cmdPlayAgain.Enabled = False
    
    ' Generate a random number (1 or 2) to see who goes first (1 = computer,
    ' 2 = player) ...
    Randomize
    intGoesFirst = Int(2 * Rnd + 1)

    ' Inform the user who is going first. If it's the computer, take the computer's
    ' turn ...
    If intGoesFirst = 1 Then
        MsgBox "I will go first this time.", , "New Game"
        TakeComputerTurn
    Else
        MsgBox "You go first this time.", , "New Game"
    End If

    
End Sub


'------------------------------------------------------------------------------------
Private Sub TakeComputerTurn()
'
'    This routine implements the computer's strategy to make a move ...
'
'------------------------------------------------------------------------------------

    Dim intX        As Integer
    Dim blnMoveMade As Boolean
    
    ' First, see if the computer can win with this move by placing its "X"
    ' or "O" within any of the 8 possible winning sequences. If so,
    ' announce it, and get out.
    If ComputerWins(0, 1, 2) Then GameOver mintCOMPUTER_WINS, 0: Exit Sub
    If ComputerWins(0, 4, 8) Then GameOver mintCOMPUTER_WINS, 1: Exit Sub
    If ComputerWins(0, 3, 6) Then GameOver mintCOMPUTER_WINS, 2: Exit Sub
    If ComputerWins(1, 4, 7) Then GameOver mintCOMPUTER_WINS, 3: Exit Sub
    If ComputerWins(2, 5, 8) Then GameOver mintCOMPUTER_WINS, 4: Exit Sub
    If ComputerWins(2, 4, 6) Then GameOver mintCOMPUTER_WINS, 5: Exit Sub
    If ComputerWins(3, 4, 5) Then GameOver mintCOMPUTER_WINS, 6: Exit Sub
    If ComputerWins(6, 7, 8) Then GameOver mintCOMPUTER_WINS, 7: Exit Sub
    
    ' If we couldn't win with this move, see if the player can win on his or
    ' her next move, and if so, block it, check to see if we have a tie, and
    ' get out ...
    If ComputerBlocks(0, 1, 2) Then TieGame: Exit Sub
    If ComputerBlocks(0, 4, 8) Then TieGame: Exit Sub
    If ComputerBlocks(0, 3, 6) Then TieGame: Exit Sub
    If ComputerBlocks(1, 4, 7) Then TieGame: Exit Sub
    If ComputerBlocks(2, 5, 8) Then TieGame: Exit Sub
    If ComputerBlocks(2, 4, 6) Then TieGame: Exit Sub
    If ComputerBlocks(3, 4, 5) Then TieGame: Exit Sub
    If ComputerBlocks(6, 7, 8) Then TieGame: Exit Sub

    ' If we get here, the computer could not win with this move and it is not
    ' necessary block. Therefore, choose a strategic location to place the
    ' computer's letter ...

    blnMoveMade = True
    
        ' First go for the middle square ...
    If lblBox(4).Caption = "" Then
        lblBox(4).Caption = gstrComputerLetter
        ' otherwise try the upper left-hand corner ...
    ElseIf lblBox(0).Caption = "" Then
        lblBox(0).Caption = gstrComputerLetter
        ' otherwise try the upper right-hand corner ...
    ElseIf lblBox(2).Caption = "" Then
        lblBox(2).Caption = gstrComputerLetter
        ' otherwise try the lower left-hand corner ...
    ElseIf lblBox(6).Caption = "" Then
        lblBox(6).Caption = gstrComputerLetter
        ' otherwise try the lower right-hand corner ...
    ElseIf lblBox(8).Caption = "" Then
        lblBox(8).Caption = gstrComputerLetter
        ' otherwise, if the computer's letter is occupying the middle
        ' square, go for the middle-left or middle-right square ...
    ElseIf lblBox(4).Caption = gstrComputerLetter Then
        If lblBox(3).Caption = "" Then
            lblBox(3).Caption = gstrComputerLetter
        ElseIf lblBox(5).Caption = "" Then
            lblBox(5).Caption = gstrComputerLetter
        Else
            blnMoveMade = False
        End If
    Else
        blnMoveMade = False
    End If
    
    ' If we could not make any of the moves above, simply use the next available
    ' square ...
    If Not blnMoveMade Then
        For intX = 0 To 8
            If lblBox(intX).Caption = "" Then
                lblBox(intX).Caption = gstrComputerLetter
                Exit For
            End If
        Next
    End If
    
    ' Check to see if the move that the computer just made caused the game
    ' to end with a tie ...
    TieGame
    
    
End Sub


'------------------------------------------------------------------------------------
Private Function PlayerWins(pintPos1 As Integer, _
                            pintPos2 As Integer, _
                            pintPos3 As Integer) _
As Boolean
'
'    This routine determines whether or not the player has just won by testing one
'    of the possible 8 ways to win ...
'
'------------------------------------------------------------------------------------

    ' If any square in the configuration being tested is blank, the player
    ' did not yet win ...
    If lblBox(pintPos1).Caption = "" _
    Or lblBox(pintPos2).Caption = "" _
    Or lblBox(pintPos3).Caption = "" Then
        PlayerWins = False
    Else
        ' If all three squares in the configuration being tested have the
        ' same value, then the player has won, otherwise game is still in
        ' progress ...
        If lblBox(pintPos1).Caption = lblBox(pintPos2).Caption _
        And lblBox(pintPos1).Caption = lblBox(pintPos3).Caption Then
            PlayerWins = True
        Else
            PlayerWins = False
        End If
    End If
    
End Function


'------------------------------------------------------------------------------------
Private Function ComputerWins(pintPos1 As Integer, _
                              pintPos2 As Integer, _
                              pintPos3 As Integer) _
As Boolean
'
'    This routine determines whether or not the computer can win on this move
'    by testing for an open spot within a sequence of one of the 8 possible
'    ways to win ...
'
'------------------------------------------------------------------------------------
    
    If lblBox(pintPos1).Caption = "" _
    And lblBox(pintPos2).Caption = gstrComputerLetter _
    And lblBox(pintPos3).Caption = gstrComputerLetter Then
    
        lblBox(pintPos1).Caption = gstrComputerLetter
        ComputerWins = True
        Exit Function
        
    End If
    
    
    If lblBox(pintPos1).Caption = gstrComputerLetter _
    And lblBox(pintPos2).Caption = "" _
    And lblBox(pintPos3).Caption = gstrComputerLetter Then
    
        lblBox(pintPos2).Caption = gstrComputerLetter
        ComputerWins = True
        Exit Function
        
    End If
    
    
    If lblBox(pintPos1).Caption = gstrComputerLetter _
    And lblBox(pintPos2).Caption = gstrComputerLetter _
    And lblBox(pintPos3).Caption = "" Then
    
        lblBox(pintPos3).Caption = gstrComputerLetter
        ComputerWins = True
        Exit Function
    
    End If
    
    
    ComputerWins = False
    
End Function


'------------------------------------------------------------------------------------
Private Function ComputerBlocks(pintPos1 As Integer, _
                                pintPos2 As Integer, _
                                pintPos3 As Integer) _
As Boolean
'
'    This routine determines whether or not the computer must block the player
'    to prevent the player from winning on his or her next move ...
'
'------------------------------------------------------------------------------------

    If lblBox(pintPos1).Caption = "" _
    And lblBox(pintPos2).Caption = gstrPlayerLetter _
    And lblBox(pintPos3).Caption = gstrPlayerLetter Then
    
        lblBox(pintPos1).Caption = gstrComputerLetter
        ComputerBlocks = True
        Exit Function
    
    End If
    
    
    If lblBox(pintPos1).Caption = gstrPlayerLetter _
    And lblBox(pintPos2).Caption = "" _
    And lblBox(pintPos3).Caption = gstrPlayerLetter Then
        
        lblBox(pintPos2).Caption = gstrComputerLetter
        ComputerBlocks = True
        Exit Function
    
    End If
    
    
    If lblBox(pintPos1).Caption = gstrPlayerLetter _
    And lblBox(pintPos2).Caption = gstrPlayerLetter _
    And lblBox(pintPos3).Caption = "" Then
        
        lblBox(pintPos3).Caption = gstrComputerLetter
        ComputerBlocks = True
        Exit Function
    
    End If
    
    ComputerBlocks = False
    
End Function


'------------------------------------------------------------------------------------
Private Function TieGame() As Boolean
'
'    This routine determines whether or not we have a tie game, by seeing whether
'    or not all the squares are populated. If they are, then it's a tie game,
'    because we would have determined that either the computer or the player
'    had won before getting here.
'
'------------------------------------------------------------------------------------

    Dim intX    As Integer
    
    For intX = 0 To 8
        If lblBox(intX).Caption = "" Then
            TieGame = False
            Exit Function
        End If
    Next
    
    TieGame = True
    
    GameOver mintTIE_GAME
    
End Function


'------------------------------------------------------------------------------------
Private Sub GameOver(pintGameOutcome As Integer, _
                     Optional pintLineIndex As Integer)
'
'    This routine displays a blinking message indicating which of the three ways the
'    game ended (the player either won, lost, or it was a tie). It also draws a line
'    through the winning combination, and updates the game stats.
'
'------------------------------------------------------------------------------------

    Dim strOutcomeMsg   As String
    
    If pintGameOutcome = mintTIE_GAME Then
        strOutcomeMsg = "IT'S A TIE !!!"
        mintGamesTied = mintGamesTied + 1
    Else
        linWin(pintLineIndex).Visible = True
        If pintGameOutcome = mintCOMPUTER_WINS Then
            strOutcomeMsg = "YOU LOSE !!!"
            mintGamesLost = mintGamesLost + 1
        Else
            strOutcomeMsg = "YOU WIN !!!"
            mintGamesWon = mintGamesWon + 1
        End If
    End If

    lblWins.Caption = strOutcomeMsg
    lblWins.Visible = True
    tmrFlash.Enabled = True

    cmdPlayAgain.Enabled = True

    ' Update the Stats ...
    mintGamesPlayed = mintGamesPlayed + 1
    lblGamesPlayed.Caption = CStr(mintGamesPlayed)
    lblGamesWon.Caption = CStr(mintGamesWon)
    lblGamesLost.Caption = CStr(mintGamesLost)
    lblGamesTied.Caption = CStr(mintGamesTied)

    mblnGameOver = True

End Sub

现在你完成了。 所以去和别人玩,或者如果你足够好,就玩电脑。

关注点

人工智能非常难,我需要为学校项目制作它。 到目前为止,我的版本是我班上最好的井字棋游戏,现在你们也可以做到这一点了。

历史

在此处保持您所做的任何更改或改进的实时更新。

© . All rights reserved.