我该怎样缩短这个程序

时间:2015-12-08 02:49:16

标签: vb.net visual-studio-2012

我已经制作了Ultimate Tic Tac Toe,它工作正常,但有超过200行代码,其中大部分是重复的,我不知道我应该如何使用我尝试过的但是失败的数组。所以我删除了它 这是部分代码(我必须删除大部分代码,因为它是76768个字符)抱歉,如果它太长了

function foo{T<:Real}(n::Integer)

    # come code to generate variable vec

    return vec::Vector{T}

end

结束班

Heres an image of what it looks like

2 个答案:

答案 0 :(得分:0)

你应该添加一些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 :(得分:0)

确定完成 - 我认为 - 代码假定您的文本框以特定顺序出现在您的表单上

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字节:)

' 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