Vb.net 如何缩短此程序
我已经制作了Ultimate Tic-Tac-Toe,它工作得很好,但是有超过200行代码,其中大部分都是重复的,我不确定我应该如何使用我尝试过但失败的数组。所以我把它拿走了 下面是部分代码(我不得不删除大部分代码,因为它有76768个字符),如果太长,请原谅Vb.net 如何缩短此程序,vb.net,visual-studio-2012,Vb.net,Visual Studio 2012,我已经制作了Ultimate Tic-Tac-Toe,它工作得很好,但是有超过200行代码,其中大部分都是重复的,我不确定我应该如何使用我尝试过但失败的数组。所以我把它拿走了 下面是部分代码(我不得不删除大部分代码,因为它有76768个字符),如果太长,请原谅 Public Class Form1 Dim count, temp, ch, temp2, v1, v2, v3, v4, v5, v6, v7, v8, v9 Dim listOfTextBox As New List(Of Text
Public Class Form1
Dim count, temp, ch, temp2, v1, v2, v3, v4, v5, v6, v7, v8, v9
Dim listOfTextBox As New List(Of TextBox)
Public Sub Start_UP() Handles Me.Load
Disable_ALL()
End Sub
Private Sub cmdStart_Click(sender As Object, e As EventArgs) Handles cmdStart.Click
cmdStart.Enabled = False
Enable_ALL()
Winner_Display.Text = Nothing
ch = 1
count = 0
temp = 0
temp2 = 0
v1 = "0"
v2 = "0"
v3 = "0"
v4 = "0"
v5 = "0"
v6 = "0"
v7 = "0"
v8 = "0"
v9 = "0"
End Sub 'start
Public Sub totalcheckwin()
If v1 = "O" And v2 = "O" And v3 = "O" Or
v4 = "O" And v5 = "O" And v6 = "O" Or
v7 = "O" And v8 = "O" And v9 = "O" Or
v1 = "O" And v4 = "O" And v7 = "O" Or
v2 = "O" And v5 = "O" And v8 = "O" Or
v3 = "O" And v6 = "O" And v9 = "O" Or
v1 = "O" And v5 = "O" And v9 = "O" Or
v3 = "O" And v5 = "O" And v7 = "O" Then
Disable_All()
Winner_Display.Text = "O Wins"
End If
If v1 = "X" And v2 = "X" And v3 = "X" Or
v4 = "X" And v5 = "X" And v6 = "X" Or
v7 = "X" And v8 = "X" And v9 = "X" Or
v1 = "X" And v4 = "X" And v7 = "X" Or
v2 = "X" And v5 = "X" And v8 = "X" Or
v3 = "X" And v6 = "X" And v9 = "X" Or
v1 = "X" And v5 = "X" And v9 = "X" Or
v3 = "X" And v5 = "X" And v7 = "X" Then
Disable_All()
Winner_Display.Text = "X Wins"
End If
End Sub
Public Sub checkcount()
Hiden_texbox.Focus()
count = count + 1
If count = 3 Then
count = 1
End If
If count = 1 Then
temp = "O"
Else
temp = "X"
End If
locksqure()
End Sub 'locks squres
Public Sub locksqure()
If temp2 = "sq1" Then
If v1 = "O" Or v1 = "X" Then
Enable_All
Disable_A()
Else
Disable_All
Enable_A()
End If
End If
If temp2 = "sq2" Then
If v2 = "O" Or v2 = "X" Then
Enable_All()
Disable_B()
Else
Disable_All()
Enable_B()
End If
End If
' you should get the drift of this bit of code
permlock()
totalcheckwin()
End Sub
Public Sub permlock()
If TextBox1.Text = "O" Or TextBox1.Text = "X" Then
TextBox1.Enabled = False
End If
'this code repeats till the textbox is at 81
End Sub
Public Sub Disable_ALL()
Disable_A()
Disable_B()
Disable_C()
Disable_D()
Disable_E()
Disable_F()
Disable_G()
Disable_H()
Disable_I()
End Sub
Public Sub Disable_A()
For value As Integer = 1 To 9
dim textbox as new TextBox
textbox.Name= "TextBox"+value.tostring
listOfTextBox.add(textbox)
If (value = 9) Then
Exit For
End If
Next
listofTextBoxes.ForEach(Sub(o)
o.BackColor = Color.lightGray
o.Enabled = False
o.Name = "TextBoxId"
End Sub)
End Sub 'disable squre
'This part repeats 9 times and the textbox court goes to 81
Public Sub Enable_ALL()
Enable_A()
Enable_B()
Enable_C()
Enable_D()
Enable_E()
Enable_F()
Enable_G()
Enable_H()
Enable_I()
End Sub
Public Sub Enable_A()
For value As Integer = 1 To 9
dim textbox as new TextBox
textbox.Name= "TextBox"+value.tostring
listOfTextBox.add(textbox)
If (value = 9) Then
Exit For
End If
Next
listofTextBoxes.ForEach(Sub(o)
o.BackColor = Color.White
o.Enabled = True
o.Name = "TextBoxId"
End Sub)
End Sub 'enable squre
'This part repeats 9 times and the textbox court goes to 81
'the next part just checks if a person has got 3 in a row and repeats 9 times
Public Sub checkwin()
If TextBox1.Text = "O" And TextBox2.Text = "O" And TextBox3.Text = "O" Or
TextBox4.Text = "O" And TextBox5.Text = "O" And TextBox6.Text = "O" Or
TextBox7.Text = "O" And TextBox8.Text = "O" And TextBox9.Text = "O" Or
TextBox1.Text = "O" And TextBox4.Text = "O" And TextBox7.Text = "O" Or
TextBox2.Text = "O" And TextBox5.Text = "O" And TextBox8.Text = "O" Or
TextBox3.Text = "O" And TextBox6.Text = "O" And TextBox9.Text = "O" Or
TextBox1.Text = "O" And TextBox5.Text = "O" And TextBox9.Text = "O" Or
TextBox3.Text = "O" And TextBox5.Text = "O" And TextBox7.Text = "O" Then
TextBox1.Text = "O"
TextBox2.Text = "O"
TextBox3.Text = "O"
TextBox4.Text = "O"
TextBox5.Text = "X"
TextBox6.Text = "O"
TextBox7.Text = "O"
TextBox8.Text = "O"
TextBox9.Text = "O"
Disable_A()
v1 = "O"
End If
If TextBox1.Text = "X" And TextBox2.Text = "X" And TextBox3.Text = "X" Or
TextBox4.Text = "X" And TextBox5.Text = "X" And TextBox6.Text = "X" Or
TextBox7.Text = "X" And TextBox8.Text = "X" And TextBox9.Text = "X" Or
TextBox1.Text = "X" And TextBox4.Text = "X" And TextBox7.Text = "X" Or
TextBox2.Text = "X" And TextBox5.Text = "X" And TextBox8.Text = "X" Or
TextBox3.Text = "X" And TextBox6.Text = "X" And TextBox9.Text = "X" Or
TextBox1.Text = "X" And TextBox5.Text = "X" And TextBox9.Text = "X" Or
TextBox3.Text = "X" And TextBox5.Text = "X" And TextBox7.Text = "X" Then
TextBox1.Text = "X"
TextBox2.Text = "O"
TextBox3.Text = "X"
TextBox4.Text = "O"
TextBox5.Text = "X"
TextBox6.Text = "O"
TextBox7.Text = "X"
TextBox8.Text = "O"
TextBox9.Text = "X"
Disable_A()
v1 = "X"
End If
locksqure()
End Sub
Private Sub resetall()
Enable_All()
End Sub
Private Sub retext()
TextBox1.Text = Nothing
TextBox2.Text = Nothing
TextBox3.Text = Nothing
' goes till textbox is at 81
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ch = 0
resetall()
retext()
cmdStart.Enabled = True
End Sub
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
If ch = 1 Then
temp2 = "sq1"
checkcount()
TextBox1.Text = temp
TextBox1.Enabled = False
checkwin()
End If
End Sub '1 squre
Private Sub TextBox2_Click(sender As Object, e As EventArgs) Handles TextBox2.Click
If ch = 1 Then
temp2 = "sq2"
checkcount()
TextBox2.Text = temp
TextBox2.Enabled = False
checkwin()
End If
End Sub
Private Sub TextBox3_Click(sender As Object, e As EventArgs) Handles TextBox3.Click
If ch = 1 Then
temp2 = "sq3"
checkcount()
TextBox3.Text = temp
TextBox3.Enabled = False
checkwin()
End If
End Sub
Private Sub TextBox4_Click(sender As Object, e As EventArgs) Handles TextBox4.Click
If ch = 1 Then
temp2 = "sq4"
checkcount()
TextBox4.Text = temp
TextBox4.Enabled = False
checkwin()
End If
End Sub
Private Sub TextBox5_Click(sender As Object, e As EventArgs) Handles TextBox5.Click
If ch = 1 Then
temp2 = "sq5"
checkcount()
TextBox5.Text = temp
TextBox5.Enabled = False
checkwin()
End If
End Sub
Private Sub TextBox6_Click(sender As Object, e As EventArgs) Handles TextBox6.Click
If ch = 1 Then
temp2 = "sq6"
checkcount()
TextBox6.Text = temp
TextBox6.Enabled = False
checkwin()
End If
End Sub
Private Sub TextBox7_Click(sender As Object, e As EventArgs) Handles TextBox7.Click
If ch = 1 Then
temp2 = "sq7"
checkcount()
TextBox7.Text = temp
TextBox7.Enabled = False
checkwin()
End If
End Sub
Private Sub TextBox8_Click(sender As Object, e As EventArgs) Handles TextBox8.Click
If ch = 1 Then
temp2 = "sq8"
checkcount()
TextBox8.Text = temp
TextBox8.Enabled = False
checkwin()
End If
End Sub
Private Sub TextBox9_Click(sender As Object, e As EventArgs) Handles TextBox9.Click
If ch = 1 Then
temp2 = "sq9"
checkcount()
TextBox9.Text = temp
TextBox9.Enabled = False
checkwin()
End If
End Sub
'this repeats 9 for a total 81 textbox's
末级
您应该添加一些for循环。 为所有文本框创建列表
Dim listOfTextBox as new list(of TextBox)
For value As Integer = 1 To 10
dim textbox as new TextBox
textbox.Name= "TextBox"+value.tostring
listOfTextBox.add(textbox)
If (value = 10) Then
Exit For
End If
Next
之后,您可以将Enable_A()更改为如下内容:
listofTextBoxes.ForEach(Sub(o)
o.BackColor = Color.White
o.Enabled = True
o.Name = "TextBoxId"
End Sub)
好了,我想代码假定您的文本框在表单上的顺序如下
1 2 3 10 11 12 19 20 21
4 5 6 13 14 15 22 23 24
7 8 9 16 17 18 25 25 27
28 29 30 37 38 39 46 47 48
31 32 33 40 41 42 49 50 51
34 35 36 43 44 45 52 53 54
55 56 57 64 65 66 73 74 75
58 59 60 67 68 69 76 77 78
61 62 63 70 71 72 79 80 81
哦,包括我的评论,这个文件现在只有17000字节左右:)
”好的。我希望你不介意,但我已经改变了几个名字,使游戏更有趣
'如果您在六个月后重新使用它,则可以进行维护:)
导入System.Text.RegularExpressions
公开课表格1
'删除了修订代码中现在未使用的几个变量
昏暗的游戏(8,2,2)作为文本框
Dim\u count,\u ch,\u currentGameId为整数
Dim_gameStatus(8)作为EnumGameStatus
Dim_currentplayer作为字符串
私有枚举状态
XWin
奥温
残缺的
画
结束枚举
显示的私有子表单1_(发送者作为对象,e作为事件参数)处理我。显示
初始化array()
初始化Eclickhandlers()
禁用_All()
端接头
私有子初始值设定项array()
将txtboxs作为新列表(文本框的)进行调整
对于控件中的每个txtbx As对象
将tbox设置为文本框
如果txtbx的类型为TextBox,则
tbox=CType(txtbx,文本框)
如果tbox.Name.Contains(“文本框”),则
txtboxs.Add(tbox)
如果结束
如果结束
下一个
将txtboxnum设置为整数
Dim txtboxname作为字符串
对于游戏,整数=0到8
对于作为整数的行=0到2
对于整型框=0到2
对于i,整数=0到80
txtboxnum=游戏*9+行*3+框+1
txtboxname=“TextBox”&txtboxnum.ToString
如果txtboxs(i).Name=txtboxname,则
游戏(游戏、线、盒)=txtboxs(i)
退出
如果结束
下一个
下一个
下一个
下一个
端接头
私有子禁用_All()
对于游戏中的每个tbox作为文本框
tbox.BackColor=Color.LightGray
tbox.Enabled=False
下一个
端接头
专用子启用_All()
对于gameId,整数=0到8
启用游戏(游戏ID)
下一个
端接头
私有子禁用_游戏(游戏ID为整数)
对于作为整数的行=0到2
对于列作为整数=0到2
游戏(游戏ID、行、列)。已启用=False
游戏(游戏ID、线、列)。背景色=颜色。浅灰色
下一个
下一个
端接头
私有子启用_游戏(gameId为整数)
对于作为整数的行=0到2
对于列作为整数=0到2
带游戏(游戏ID、线、列)
如果.Text=”“,则
游戏(游戏ID、行、列)。已启用=真
游戏(游戏ID、线、列)。背景色=颜色。白色
如果结束
以
下一个
下一个
端接头
私有子cmdStart\u单击(发件人作为对象,e作为事件参数)处理cmdStart。单击
cmdStart.Enabled=False
启用_All()
Winner\u Display.Text=无
_ch=1
_计数=0
_currentGameId=0
_游戏状态=
{EnumGameStatus.Complete,EnumGameStatus.Complete,EnumGameStatus.Complete,EnumGameStatus.Complete,
EnumGameStatus.不完整,EnumGameStatus.不完整,EnumGameStatus.不完整,EnumGameStatus.不完整,
EnumGameStatus.complete}
端接头
私有子TotalCheckWin()
如果_
(_gameStatus(0)=EnumGameStatus.OWin和_gameStatus(1)=EnumGameStatus.OWin和_gameStatus(2)=EnumGameStatus.OWin)或
(_gameStatus(3)=EnumGameStatus.OWin和_gameStatus(4)=EnumGameStatus.OWin和_gameStatus(5)=EnumGameStatus.OWin)或
(_gameStatus(6)=EnumGameStatus.OWin和_gameStatus(7)=EnumGameStatus.OWin和_gameStatus(8)=EnumGameStatus.OWin)或
(_gameStatus(0)=EnumGameStatus.OWin和_gameStatus(3)=EnumGameStatus.OWin和_gameStatus(6)=EnumGameStatus.OWin)或
(_gameStatus(1)=EnumGameStatus.OWin和_gameStatus(4)=EnumGameStatus.OWin和_gameStatus(7)=EnumGameStatus.OWin)或
(_gameStatus(2)=EnumGameStatus.OWin和_gameStatus(5)=EnumGameStatus.OWin和_gameStatus(8)=EnumGameStatus.OWin)或
(_gameStatus(0)=EnumGameStatus.OWin和_gameStatus(4)=EnumGameStatus.OWin和_gameStatus(8)=EnumGameStatus.OWin)或
(_gameStatus(2)=EnumGameStatus.OWin和_gameStatus(4)=EnumGameStatus.OWin和_gameStatus(6)=EnumGameStatus.OWin)然后
禁用_All()
Winner\u Display.Text=“O Wins”
如果结束
如果_
(_gameStatus(0)=EnumGameStatus.XWin和_gameStatus(1)=EnumGameStatus.XWin和_gameStatus(2)=EnumGameStatus.XWin)或
(_gameStatus(3)=EnumGameStatus.XWin和_gameStatus(4)=EnumGameStatus.XWin和_gameStatus(5)=EnumGameStatus.XWin)或
(_gameStatus(6)=EnumGameStatus.XWin和_gameStatus(7)=EnumGameStatus.XWin和_gameStatus(8)=EnumGameStatus.XWin)或
(_gameStatus(0)=EnumGameStatus.XWin和_gameStatus(3)=EnumGameStatus.XWin和_gameStatus(6)=EnumGameStatus.XWin)或
(_gameStatus(1)=EnumGameStatus.XWin和_gameStatus(4)=EnumGameStatus.XWin和_gameStatus(7)=EnumGameStatus.XWin)或
' OK. I hope you dont mind, but I've altered a couple of names to make the game more
'maintainable if you come back to it in six months time :)
Imports System.Text.RegularExpressions
Public Class Form1
'removed several variables that are now unused in the revised code
Dim games(8, 2, 2) As TextBox
Dim _count, _ch, _currentGameId As Integer
Dim _gameStatus(8) As EnumGameStatus
Dim _currentplayer As String
Private Enum EnumGameStatus
XWin
OWin
Incomplete
Draw
End Enum
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
InitializeArray()
initializeClickHandlers()
Disable_All()
End Sub
Private Sub InitializeArray()
Dim txtboxes As New List(Of TextBox)
For Each txtbx As Object In Controls
Dim tbox As TextBox
If TypeOf txtbx Is TextBox Then
tbox = CType(txtbx, TextBox)
If tbox.Name.Contains("TextBox") Then
txtboxes.Add(tbox)
End If
End If
Next
Dim txtboxnum As Integer
Dim txtboxname As String
For game As Integer = 0 To 8
For line As Integer = 0 To 2
For box As Integer = 0 To 2
For i As Integer = 0 To 80
txtboxnum = game * 9 + line * 3 + box + 1
txtboxname = "TextBox" & txtboxnum.ToString
If txtboxes(i).Name = txtboxname Then
games(game, line, box) = txtboxes(i)
Exit For
End If
Next
Next
Next
Next
End Sub
Private Sub Disable_All()
For Each tbox As TextBox In games
tbox.BackColor = Color.LightGray
tbox.Enabled = False
Next
End Sub
Private Sub Enable_All()
For gameId As Integer = 0 To 8
Enable_Game(gameId)
Next
End Sub
Private Sub Disable_Game(gameId As Integer)
For line As Integer = 0 To 2
For column As Integer = 0 To 2
games(gameId, line, column).Enabled = False
games(gameId, line, column).BackColor = Color.LightGray
Next
Next
End Sub
Private Sub Enable_Game(gameId As Integer)
For line As Integer = 0 To 2
For column As Integer = 0 To 2
With games(gameId, line, column)
If .Text = "" Then
games(gameId, line, column).Enabled = True
games(gameId, line, column).BackColor = Color.White
End If
End With
Next
Next
End Sub
Private Sub cmdStart_Click(sender As Object, e As EventArgs) Handles cmdStart.Click
cmdStart.Enabled = False
Enable_All()
Winner_Display.Text = Nothing
_ch = 1
_count = 0
_currentGameId = 0
_gameStatus =
{EnumGameStatus.Incomplete, EnumGameStatus.Incomplete, EnumGameStatus.Incomplete, EnumGameStatus.Incomplete,
EnumGameStatus.Incomplete, EnumGameStatus.Incomplete, EnumGameStatus.Incomplete, EnumGameStatus.Incomplete,
EnumGameStatus.Incomplete}
End Sub
Private Sub TotalCheckWin()
If _
(_gameStatus(0) = EnumGameStatus.OWin And _gameStatus(1) = EnumGameStatus.OWin And _gameStatus(2) = EnumGameStatus.OWin) Or
(_gameStatus(3) = EnumGameStatus.OWin And _gameStatus(4) = EnumGameStatus.OWin And _gameStatus(5) = EnumGameStatus.OWin) Or
(_gameStatus(6) = EnumGameStatus.OWin And _gameStatus(7) = EnumGameStatus.OWin And _gameStatus(8) = EnumGameStatus.OWin) Or
(_gameStatus(0) = EnumGameStatus.OWin And _gameStatus(3) = EnumGameStatus.OWin And _gameStatus(6) = EnumGameStatus.OWin) Or
(_gameStatus(1) = EnumGameStatus.OWin And _gameStatus(4) = EnumGameStatus.OWin And _gameStatus(7) = EnumGameStatus.OWin) Or
(_gameStatus(2) = EnumGameStatus.OWin And _gameStatus(5) = EnumGameStatus.OWin And _gameStatus(8) = EnumGameStatus.OWin) Or
(_gameStatus(0) = EnumGameStatus.OWin And _gameStatus(4) = EnumGameStatus.OWin And _gameStatus(8) = EnumGameStatus.OWin) Or
(_gameStatus(2) = EnumGameStatus.OWin And _gameStatus(4) = EnumGameStatus.OWin And _gameStatus(6) = EnumGameStatus.OWin) Then
Disable_All()
Winner_Display.Text = "O Wins"
End If
If _
(_gameStatus(0) = EnumGameStatus.XWin And _gameStatus(1) = EnumGameStatus.XWin And _gameStatus(2) = EnumGameStatus.XWin) Or
(_gameStatus(3) = EnumGameStatus.XWin And _gameStatus(4) = EnumGameStatus.XWin And _gameStatus(5) = EnumGameStatus.XWin) Or
(_gameStatus(6) = EnumGameStatus.XWin And _gameStatus(7) = EnumGameStatus.XWin And _gameStatus(8) = EnumGameStatus.XWin) Or
(_gameStatus(0) = EnumGameStatus.XWin And _gameStatus(3) = EnumGameStatus.XWin And _gameStatus(6) = EnumGameStatus.XWin) Or
(_gameStatus(1) = EnumGameStatus.XWin And _gameStatus(4) = EnumGameStatus.XWin And _gameStatus(7) = EnumGameStatus.XWin) Or
(_gameStatus(2) = EnumGameStatus.XWin And _gameStatus(5) = EnumGameStatus.XWin And _gameStatus(8) = EnumGameStatus.XWin) Or
(_gameStatus(0) = EnumGameStatus.XWin And _gameStatus(4) = EnumGameStatus.XWin And _gameStatus(8) = EnumGameStatus.XWin) Or
(_gameStatus(2) = EnumGameStatus.XWin And _gameStatus(4) = EnumGameStatus.XWin And _gameStatus(6) = EnumGameStatus.XWin) Then
Disable_All()
Winner_Display.Text = "X Wins"
End If
End Sub
Private Sub NextPlayer()
_count = _count + 1
If _count = 3 Then
_count = 1
End If
If _count = 1 Then
_currentplayer = "O"
Else
_currentplayer = "X"
End If
End Sub
Private Sub MarkGameOWin(gameId As Integer)
For line As Integer = 0 To 2
For column As Integer = 0 To 2
games(gameId, line, column).Text = "O"
games(gameId, 1, 1).Text = "X"
Next
_gameStatus(gameId) = EnumGameStatus.OWin
Disable_Game(gameId)
Next
End Sub
Private Sub MarkGameXWin(gameId As Integer)
games(gameId, 0, 0).Text = "X"
games(gameId, 0, 1).Text = "O"
games(gameId, 0, 2).Text = "X"
games(gameId, 1, 0).Text = "O"
games(gameId, 1, 1).Text = "X"
games(gameId, 1, 2).Text = "O"
games(gameId, 2, 0).Text = "X"
games(gameId, 2, 1).Text = "O"
games(gameId, 2, 2).Text = "X"
_gameStatus(gameId) = EnumGameStatus.XWin
Disable_Game(gameId)
End Sub
Private Sub MarkGameDraw(gameId As Integer)
games(gameId, 0, 0).Text = ""
games(gameId, 0, 1).Text = ""
games(gameId, 0, 2).Text = ""
games(gameId, 1, 0).Text = ""
games(gameId, 1, 1).Text = ""
games(gameId, 1, 2).Text = ""
games(gameId, 2, 0).Text = ""
games(gameId, 2, 1).Text = ""
games(gameId, 2, 2).Text = ""
_gameStatus(gameId) = EnumGameStatus.Draw
Disable_Game(gameId)
End Sub
Private Function CheckWin(gameId As Integer) As EnumGameStatus
CheckWin = EnumGameStatus.Incomplete
Dim _loopOnce As Boolean = True
Do
'check for horizontal win
For line As Integer = 0 To 2
If games(gameId, line, 0).Text = "X" AndAlso games(gameId, line, 1).Text = "X" AndAlso games(gameId, line, 2).Text = "X" Then
CheckWin = EnumGameStatus.XWin
Exit Do
ElseIf _
games(gameId, line, 0).Text = "O" AndAlso games(gameId, line, 1).Text = "O" AndAlso games(gameId, line, 2).Text = "O" Then
CheckWin = EnumGameStatus.OWin
Exit Do
End If
Next
'check for vertical win
For column As Integer = 0 To 2
If games(gameId, 0, column).Text = "X" AndAlso games(gameId, 1, column).Text = "X" AndAlso games(gameId, 2, column).Text = "X" Then
CheckWin = EnumGameStatus.XWin
Exit Do
ElseIf games(gameId, 0, column).Text = "O" AndAlso games(gameId, 1, column).Text = "O" AndAlso games(gameId, 2, column).Text = "O" Then
CheckWin = EnumGameStatus.OWin
Exit Do
End If
Next
'check for Left to right diagonal win
If games(gameId, 0, 0).Text = "X" AndAlso games(gameId, 1, 1).Text = "X" AndAlso games(gameId, 2, 2).Text = "X" Then
CheckWin = EnumGameStatus.XWin
Exit Do
ElseIf games(gameId, 0, 0).Text = "O" AndAlso games(gameId, 1, 1).Text = "O" AndAlso games(gameId, 2, 2).Text = "O" Then
CheckWin = EnumGameStatus.OWin
Exit Do
End If
'check for Right to Left diagonal win
If games(gameId, 2, 0).Text = "X" AndAlso games(gameId, 1, 1).Text = "X" AndAlso games(gameId, 0, 2).Text = "X" Then
CheckWin = EnumGameStatus.XWin
Exit Do
ElseIf games(gameId, 2, 0).Text = "O" AndAlso games(gameId, 1, 1).Text = "O" AndAlso games(gameId, 0, 2).Text = "O" Then
CheckWin = EnumGameStatus.OWin
Exit Do
End If
'check if all squares filled with no winner
'altered this to check for a draw if no squares were left
'instead of using gamemove in my previous code
Dim squaresLeft As Integer = 9
For line As Integer = 0 To 2
For column As Integer = 0 To 2
If games(gameId, line, column).Text <> "" Then
squaresLeft -= 1
End If
Next
Next
If squaresLeft = 0 Then
CheckWin = EnumGameStatus.Draw
End If
Loop Until _loopOnce = True
'return values and update game status ad movex
_gameStatus(gameId) = CheckWin
Return CheckWin
End Function
Private Sub ResetAll()
For gameId As Integer = 0 To 8
Disable_Game(gameId)
Next
End Sub
Private Sub Retext()
For gameId As Integer = 0 To 8
For line As Integer = 0 To 2
For column As Integer = 0 To 2
games(gameId, line, column).Text = Nothing
Next
Next
Next
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
_ch = 0
Retext()
ResetAll()
cmdStart.Enabled = True
End Sub
Private Function GetCurrentGame(clickedTextBox As TextBox) As Integer
Dim txtboxnum As Integer = (CInt(Regex.Match(clickedTextBox.Name, "\d+").Value) - 1)
GetCurrentGame = txtboxnum \ 9
End Function
'added this to work out which the next game block should be
Private Function GetNextGame(clickedTextbox As TextBox) As Integer
Dim txtboxnum As Integer = (CInt(Regex.Match(clickedTextbox.Name, "\d+").Value) - 1)
GetNextGame = txtboxnum Mod 9
End Function
'altered some code to avoid errors if Option Strict is turned on
'also changed order of code to handle the checking of game status
'etc
Private Sub TextBoxClickHandler(sender As Object, e As EventArgs)
Dim txtbox As TextBox = CType(sender, TextBox)
Dim gameStatus As EnumGameStatus
'added this and the subsequent resume layout to disable
'repaints every time a text box is enables/disabled
'while processing the click
SuspendLayout()
If _ch = 1 Then
NextPlayer()
_currentGameId = GetCurrentGame(txtbox)
txtbox.Text = _currentplayer
txtbox.Enabled = False
gameStatus = CheckWin(_currentGameId)
If Not gameStatus = EnumGameStatus.Incomplete Then
Select Case gameStatus
Case EnumGameStatus.Draw
MarkGameDraw(_currentGameId)
Case EnumGameStatus.XWin
MarkGameXWin(_currentGameId)
Case EnumGameStatus.OWin
MarkGameOWin(_currentGameId)
End Select
TotalCheckWin()
End If
'check next game is incomplete and disable all others
'else enable all incomplete games
_currentGameId = GetNextGame(txtbox)
If _gameStatus(_currentGameId) = EnumGameStatus.Incomplete Then
Disable_All()
Enable_Game(_currentGameId)
Else
Disable_All()
For gameId As Integer = 0 To 8
If _gameStatus(gameId) = EnumGameStatus.Incomplete Then
Enable_Game(gameId)
End If
Next
End If
End If
Hidden_txtbox.Focus()
ResumeLayout()
Refresh()
End Sub
Private Sub initializeClickHandlers()
For Each txtbox As TextBox In games
AddHandler txtbox.Click, AddressOf TextBoxClickHandler
Next
End Sub
End Class