首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tie的tic tac toe编码

tie的tic tac toe编码
EN

Stack Overflow用户
提问于 2012-04-12 20:13:39
回答 1查看 1.2K关注 0票数 1

我有一个tic tac toe程序,我已经把一切都按照它应该的方式工作了。但是,我在这种编码中遇到的最后一个问题是,我似乎无法弄清楚什么时候会出现如何编码的问题。这是我到目前为止所拥有的。

代码语言:javascript
复制
Public Class Form1

Private turn As Integer = 1
Private play() As String = {"O", "X"}
Private board(2, 2) As String

Private Structure arrayIndex
    Dim x As Integer
    Dim y As Integer
End Structure

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For x As Integer = 1 To 9
        Dim b As New Button With { _
            .Width = 80, _
            .Height = 80, _
            .Text = "", _
            .Location = New Point(60 + (((x - 1) Mod 3) * 80), 60 + (((x - 1) \ 3) * 80)), _
            .Tag = New arrayIndex With {.x = (x - 1) Mod 3, .y = (x - 1) \ 3}}
        Me.Controls.Add(b)
        AddHandler b.Click, AddressOf buttons_click

    Next
    Me.SetClientSizeCore(360, 360)

End Sub

Private Sub buttons_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If DirectCast(sender, Button).Text <> "" Then Return
    DirectCast(sender, Button).Text = play(turn Mod 2)
    Dim index As arrayIndex = DirectCast(DirectCast(sender, Button).Tag, arrayIndex)
    board(index.x, index.y) = play(turn Mod 2)
    turn += 1
    winner()
End Sub

Private Sub winner()
    Dim rows(7) As String
    rows(0) = board(0, 0) & board(1, 0) & board(2, 0)
    rows(1) = board(0, 1) & board(1, 1) & board(2, 1)
    rows(2) = board(0, 2) & board(1, 2) & board(2, 2)
    rows(3) = board(0, 0) & board(0, 1) & board(0, 2)
    rows(4) = board(1, 0) & board(1, 1) & board(1, 2)
    rows(5) = board(2, 0) & board(2, 1) & board(2, 2)
    rows(6) = board(0, 0) & board(1, 1) & board(2, 2)
    rows(7) = board(2, 0) & board(1, 1) & board(0, 2)

    For x As Integer = 0 To 7
        If rows(x).Length = 3 AndAlso (rows(x)(0) = rows(x)(1) AndAlso rows(x)(0) = rows(x)(2)) Then
            If MessageBox.Show(rows(x)(0) & "'s winsssss!", "We have a winner!", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = DialogResult.OK Then
                turn = 1
                ReDim board(2, 2)
                For Each ctrl As Control In Controls
                    ctrl.Text = ""
                Next
                Return
            Else
                Me.Close()


            End If

        End If
    Next

End Sub


End Class
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-12 20:18:47

一种方法是,如果所有的空间都满了,没有赢家,那么就会有平局。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10123342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档