0

我正在编写一个井字游戏程序,并且我已经设置了棋盘,并且该游戏适用于大多数人。决定谁赢。我现在要做的是在确定获胜者后,会弹出一个消息框并显示谁获胜。我希望消息框包含两个按钮。一个带有文字“Ok for new game”的按钮和带有文字“Cancel to exit”的第二个按钮。我正在使用 Visual Basic 2010 Express。

这是我的代码:

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
                MessageBox.Show(rows(x)(0) & "'s winsssss!", "We have a winner!", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
            If 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 Sub
End Class
4

3 回答 3

2

最简单的方法是创建一个新Form的作为MessageBox. 有很多API要定制的,MessageBox但会给你额外的时间。所以这就是你要做的。

  • 创建一个新表单
  • 将 MaximizeButton、MinimizeButton 和 ShowOnTitleBar 设置为False
  • 设置FormBorderStyle为 FixedDialog 所以它不会很大
  • 在其上添加两个按钮(Ok for new gameCancel to exit
  • 设置StartUpPositionCenterForm

将此代码添加到On for New Game Button点击事件

DialogResult = DialogResult.Yes

将此代码添加到Cancel to exit点击事件

DialogResult = DialogResult.No

游戏结束后,尝试调用您创建的新表单。

Dim xForm as new frmAsk ' Assuming that frmAsk is the name of your new form
If xForm.showDialog = DialogResult.Yes Then
    ' New game here
Else
    ' exit game
End If

希望这对您有所帮助。

于 2012-04-11T23:54:46.993 回答
1

你为什么不试试这个:

If Msgbox("Your text here") <> MsgboxResult.Yes then
    Exit
Else
    Your code there to continue the game.
End if

希望这可以帮助。

于 2012-04-12T02:33:01.753 回答
0

试试这个

Select Case msgbox("You Win! Do you want to play another game?",vbYesNo)
    case vbYes
        'add code to start a new game
    case vbNo
        'add code to exit your app
        Unload Me
End Select
于 2012-04-12T17:31:55.317 回答