frmGame.vb

 ' Game graphics code.

Imports System.Drawing.Drawing2D

Public Class FrmGameSurface
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    Friend WithEvents pieceBox As System.Windows.Forms.PictureBox
    Friend WithEvents btnNewGame As System.Windows.Forms.Button
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents lblTimer As System.Windows.Forms.Label
    Friend WithEvents btnHelp As System.Windows.Forms.Button
    Friend WithEvents btnNext As System.Windows.Forms.Button
    Friend WithEvents btnPrevious As System.Windows.Forms.Button
    Friend WithEvents PicBxQueen As System.Windows.Forms.PictureBox
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem10 As System.Windows.Forms.MenuItem
    Friend WithEvents PicBxMark As System.Windows.Forms.PictureBox
    Friend WithEvents PicBxCross As System.Windows.Forms.PictureBox
    Friend WithEvents PicBxLightTileb As System.Windows.Forms.PictureBox
    Friend WithEvents PicBxLightTile2b As System.Windows.Forms.PictureBox
    Friend WithEvents PicBxDarkTile1 As System.Windows.Forms.PictureBox
    Friend WithEvents PicBxDarkTile2 As System.Windows.Forms.PictureBox
    Friend WithEvents PicBxOk As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FrmGameSurface))
        Me.pieceBox = New System.Windows.Forms.PictureBox
        Me.btnNewGame = New System.Windows.Forms.Button
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.lblTimer = New System.Windows.Forms.Label
        Me.btnHelp = New System.Windows.Forms.Button
        Me.btnNext = New System.Windows.Forms.Button
        Me.btnPrevious = New System.Windows.Forms.Button
        Me.PicBxQueen = New System.Windows.Forms.PictureBox
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.MenuItem2 = New System.Windows.Forms.MenuItem
        Me.MenuItem10 = New System.Windows.Forms.MenuItem
        Me.MenuItem4 = New System.Windows.Forms.MenuItem
        Me.MenuItem5 = New System.Windows.Forms.MenuItem
        Me.MenuItem7 = New System.Windows.Forms.MenuItem
        Me.MenuItem3 = New System.Windows.Forms.MenuItem
        Me.MenuItem8 = New System.Windows.Forms.MenuItem
        Me.MenuItem6 = New System.Windows.Forms.MenuItem
        Me.MenuItem9 = New System.Windows.Forms.MenuItem
        Me.PicBxMark = New System.Windows.Forms.PictureBox
        Me.PicBxCross = New System.Windows.Forms.PictureBox
        Me.PicBxLightTileb = New System.Windows.Forms.PictureBox
        Me.PicBxLightTile2b = New System.Windows.Forms.PictureBox
        Me.PicBxDarkTile1 = New System.Windows.Forms.PictureBox
        Me.PicBxDarkTile2 = New System.Windows.Forms.PictureBox
        Me.PicBxOk = New System.Windows.Forms.PictureBox
        Me.SuspendLayout()
        '
        'pieceBox
        '
        Me.pieceBox.BackColor = System.Drawing.Color.Transparent
        Me.pieceBox.Location = New System.Drawing.Point(0, 0)
        Me.pieceBox.Name = "pieceBox"
        Me.pieceBox.Size = New System.Drawing.Size(405, 360)
        Me.pieceBox.TabIndex = 3
        Me.pieceBox.TabStop = False
        '
        'btnNewGame
        '
        Me.btnNewGame.BackColor = System.Drawing.Color.AliceBlue
        Me.btnNewGame.Cursor = System.Windows.Forms.Cursors.Hand
        Me.btnNewGame.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.btnNewGame.Location = New System.Drawing.Point(124, 372)
        Me.btnNewGame.Name = "btnNewGame"
        Me.btnNewGame.TabIndex = 4
        Me.btnNewGame.Text = "New Game"
        '
        'Timer1
        '
        Me.Timer1.Interval = 1000
        '
        'lblTimer
        '
        Me.lblTimer.AutoSize = True
        Me.lblTimer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lblTimer.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblTimer.ForeColor = System.Drawing.Color.FromArgb(CType(128, Byte), CType(128, Byte), CType(255, Byte))
        Me.lblTimer.Location = New System.Drawing.Point(4, 372)
        Me.lblTimer.Name = "lblTimer"
        Me.lblTimer.Size = New System.Drawing.Size(48, 23)
        Me.lblTimer.TabIndex = 5
        Me.lblTimer.Text = "00:00"
        '
        'btnHelp
        '
        Me.btnHelp.BackColor = System.Drawing.Color.AliceBlue
        Me.btnHelp.Cursor = System.Windows.Forms.Cursors.Hand
        Me.btnHelp.Location = New System.Drawing.Point(208, 372)
        Me.btnHelp.Name = "btnHelp"
        Me.btnHelp.TabIndex = 6
        Me.btnHelp.Text = "Show me!"
        '
        'btnNext
        '
        Me.btnNext.BackColor = System.Drawing.Color.LightSteelBlue
        Me.btnNext.Cursor = System.Windows.Forms.Cursors.Hand
        Me.btnNext.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.btnNext.ForeColor = System.Drawing.Color.White
        Me.btnNext.Location = New System.Drawing.Point(332, 372)
        Me.btnNext.Name = "btnNext"
        Me.btnNext.Size = New System.Drawing.Size(20, 23)
        Me.btnNext.TabIndex = 8
        Me.btnNext.Text = ">"
        Me.btnNext.Visible = False
        '
        'btnPrevious
        '
        Me.btnPrevious.BackColor = System.Drawing.Color.LightSteelBlue
        Me.btnPrevious.Cursor = System.Windows.Forms.Cursors.Hand
        Me.btnPrevious.Enabled = False
        Me.btnPrevious.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.btnPrevious.ForeColor = System.Drawing.Color.White
        Me.btnPrevious.Location = New System.Drawing.Point(308, 372)
        Me.btnPrevious.Name = "btnPrevious"
        Me.btnPrevious.Size = New System.Drawing.Size(20, 23)
        Me.btnPrevious.TabIndex = 7
        Me.btnPrevious.Text = "<"
        Me.btnPrevious.Visible = False
        '
        'PicBxQueen
        '
        Me.PicBxQueen.Image = CType(resources.GetObject("PicBxQueen.Image"), System.Drawing.Image)
        Me.PicBxQueen.Location = New System.Drawing.Point(364, 364)
        Me.PicBxQueen.Name = "PicBxQueen"
        Me.PicBxQueen.Size = New System.Drawing.Size(35, 35)
        Me.PicBxQueen.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PicBxQueen.TabIndex = 9
        Me.PicBxQueen.TabStop = False
        Me.PicBxQueen.Visible = False
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem7})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem10, Me.MenuItem4, Me.MenuItem5})
        Me.MenuItem1.Text = "&File"
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 0
        Me.MenuItem2.Shortcut = System.Windows.Forms.Shortcut.F2
        Me.MenuItem2.Text = "&New Game"
        '
        'MenuItem10
        '
        Me.MenuItem10.Index = 1
        Me.MenuItem10.Shortcut = System.Windows.Forms.Shortcut.F4
        Me.MenuItem10.Text = "&Hide character"
        '
        'MenuItem4
        '
        Me.MenuItem4.Index = 2
        Me.MenuItem4.Text = "-"
        '
        'MenuItem5
        '
        Me.MenuItem5.Index = 3
        Me.MenuItem5.Shortcut = System.Windows.Forms.Shortcut.AltF4
        Me.MenuItem5.Text = "E&xit"
        '
        'MenuItem7
        '
        Me.MenuItem7.Index = 1
        Me.MenuItem7.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem3, Me.MenuItem8, Me.MenuItem6, Me.MenuItem9})
        Me.MenuItem7.Text = "&Help"
        '
        'MenuItem3
        '
        Me.MenuItem3.Index = 0
        Me.MenuItem3.Shortcut = System.Windows.Forms.Shortcut.F3
        Me.MenuItem3.Text = "&Show all possible formations"
        '
        'MenuItem8
        '
        Me.MenuItem8.Index = 1
        Me.MenuItem8.Shortcut = System.Windows.Forms.Shortcut.F1
        Me.MenuItem8.Text = "&Review the instructions"
        '
        'MenuItem6
        '
        Me.MenuItem6.Index = 2
        Me.MenuItem6.Text = "-"
        '
        'MenuItem9
        '
        Me.MenuItem9.Index = 3
        Me.MenuItem9.Text = "&About 8Queens..."
        '
        'PicBxMark
        '
        Me.PicBxMark.Image = CType(resources.GetObject("PicBxMark.Image"), System.Drawing.Image)
        Me.PicBxMark.Location = New System.Drawing.Point(364, 364)
        Me.PicBxMark.Name = "PicBxMark"
        Me.PicBxMark.Size = New System.Drawing.Size(35, 35)
        Me.PicBxMark.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PicBxMark.TabIndex = 10
        Me.PicBxMark.TabStop = False
        Me.PicBxMark.Visible = False
        '
        'PicBxCross
        '
        Me.PicBxCross.Image = CType(resources.GetObject("PicBxCross.Image"), System.Drawing.Image)
        Me.PicBxCross.Location = New System.Drawing.Point(364, 364)
        Me.PicBxCross.Name = "PicBxCross"
        Me.PicBxCross.Size = New System.Drawing.Size(35, 35)
        Me.PicBxCross.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PicBxCross.TabIndex = 11
        Me.PicBxCross.TabStop = False
        Me.PicBxCross.Visible = False
        '
        'PicBxLightTileb
        '
        Me.PicBxLightTileb.Image = CType(resources.GetObject("PicBxLightTileb.Image"), System.Drawing.Image)
        Me.PicBxLightTileb.Location = New System.Drawing.Point(364, 364)
        Me.PicBxLightTileb.Name = "PicBxLightTileb"
        Me.PicBxLightTileb.Size = New System.Drawing.Size(35, 35)
        Me.PicBxLightTileb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PicBxLightTileb.TabIndex = 12
        Me.PicBxLightTileb.TabStop = False
        Me.PicBxLightTileb.Visible = False
        '
        'PicBxLightTile2b
        '
        Me.PicBxLightTile2b.Image = CType(resources.GetObject("PicBxLightTile2b.Image"), System.Drawing.Image)
        Me.PicBxLightTile2b.Location = New System.Drawing.Point(364, 364)
        Me.PicBxLightTile2b.Name = "PicBxLightTile2b"
        Me.PicBxLightTile2b.Size = New System.Drawing.Size(35, 35)
        Me.PicBxLightTile2b.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PicBxLightTile2b.TabIndex = 13
        Me.PicBxLightTile2b.TabStop = False
        Me.PicBxLightTile2b.Visible = False
        '
        'PicBxDarkTile1
        '
        Me.PicBxDarkTile1.Image = CType(resources.GetObject("PicBxDarkTile1.Image"), System.Drawing.Image)
        Me.PicBxDarkTile1.Location = New System.Drawing.Point(364, 364)
        Me.PicBxDarkTile1.Name = "PicBxDarkTile1"
        Me.PicBxDarkTile1.Size = New System.Drawing.Size(35, 35)
        Me.PicBxDarkTile1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PicBxDarkTile1.TabIndex = 14
        Me.PicBxDarkTile1.TabStop = False
        Me.PicBxDarkTile1.Visible = False
        '
        'PicBxDarkTile2
        '
        Me.PicBxDarkTile2.Image = CType(resources.GetObject("PicBxDarkTile2.Image"), System.Drawing.Image)
        Me.PicBxDarkTile2.Location = New System.Drawing.Point(364, 364)
        Me.PicBxDarkTile2.Name = "PicBxDarkTile2"
        Me.PicBxDarkTile2.Size = New System.Drawing.Size(35, 35)
        Me.PicBxDarkTile2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PicBxDarkTile2.TabIndex = 15
        Me.PicBxDarkTile2.TabStop = False
        Me.PicBxDarkTile2.Visible = False
        '
        'PicBxOk
        '
        Me.PicBxOk.Image = CType(resources.GetObject("PicBxOk.Image"), System.Drawing.Image)
        Me.PicBxOk.Location = New System.Drawing.Point(364, 364)
        Me.PicBxOk.Name = "PicBxOk"
        Me.PicBxOk.Size = New System.Drawing.Size(35, 35)
        Me.PicBxOk.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PicBxOk.TabIndex = 16
        Me.PicBxOk.TabStop = False
        Me.PicBxOk.Visible = False
        '
        'FrmGameSurface
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.Lavender
        Me.ClientSize = New System.Drawing.Size(406, 403)
        Me.Controls.Add(Me.PicBxOk)
        Me.Controls.Add(Me.PicBxDarkTile2)
        Me.Controls.Add(Me.PicBxDarkTile1)
        Me.Controls.Add(Me.PicBxLightTile2b)
        Me.Controls.Add(Me.PicBxLightTileb)
        Me.Controls.Add(Me.PicBxCross)
        Me.Controls.Add(Me.PicBxMark)
        Me.Controls.Add(Me.PicBxQueen)
        Me.Controls.Add(Me.btnPrevious)
        Me.Controls.Add(Me.btnNext)
        Me.Controls.Add(Me.btnHelp)
        Me.Controls.Add(Me.lblTimer)
        Me.Controls.Add(Me.btnNewGame)
        Me.Controls.Add(Me.pieceBox)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.MaximizeBox = False
        Me.Menu = Me.MainMenu1
        Me.Name = "FrmGameSurface"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "8Queens solutions"
        Me.ResumeLayout(False)

    End Sub

#End Region
#Region "Variables"

    ' ArrayList for board tile images
    Dim mChessTile As ArrayList = New ArrayList

    ' ArrayList for chess pieces
    Dim mChessPieces As ArrayList = New ArrayList

    ' ArrayList for help pieces
    Dim mHelpPieces As ArrayList = New ArrayList

    ' Define index for selected piece
    Dim mSelectedIndex As Integer = -1

    ' Board array
    Dim mBoard As Integer(,) = New Integer(7, 7) {}

    ' This determines the cursor type
    Private HW_Cursor As Boolean = False

    ' Saves the queens original place
    ' as it moves to be returned while placed at wrong field
    Private r As Point

    ' Cross will be put here
    Private crossPoint As Point

    ' Ok to put Cross
    Private crossFlag As Boolean = False

    ' Queen exists on board but its moved again
    Private movedQueen As Boolean = False

    ' For timer1
    Private second, minute As Integer

    ' Define chess tile size in pixels
    Private Const TILESIZE As Integer = 45
#End Region
#Region "Game"
#Region "Paint"

    ' display board in form OnPaint event
    Protected Overrides Sub OnPaint(ByVal paintEvent _
       As PaintEventArgs)
        ' obtain graphics object
        Dim graphicsObject As Graphics = paintEvent.Graphics
        Dim brush As SolidBrush = New SolidBrush(Color.Lavender)
        Dim row, column As Integer

        For row = 0 To mBoard.GetUpperBound(0)

            For column = 0 To mBoard.GetUpperBound(1)

                ' draw image specified in board array
                graphicsObject.DrawImage( _
                   CType(mChessTile(mBoard(row, column)), _
                   Image), New Point(TILESIZE * column, _
                   TILESIZE * row))
            Next

        Next
        graphicsObject.FillRectangle(brush, 362, 0, 50, 362)

    End Sub ' OnPaint
    ' handle pieceBox pain event
    Private Sub pieceBox_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pieceBox.Paint

        Dim i As Integer
        Dim cross As Bitmap = PicBxCross.Image
        Dim grObj As Graphics = e.Graphics
        ' Marks the queen with a "Q" letter
        Dim Mark As Bitmap = PicBxMark.Image

        ' draw all pieces
        For i = 0 To mChessPieces.Count - 1
            Getpiece(i).Draw(e.Graphics)
        Next

        For i = 0 To mHelpPieces.Count - 1
            GetHelpPiece(i).Draw(e.Graphics)
        Next

        ' if point should be crossed then apply
        If crossFlag Then
            grObj.DrawImage(cross, crossPoint)
            crossFlag = False
        End If
        For i = 0 To 5
            If MarkedQueensArr(i, 2) <> 0 Then
                Dim Markpoint As Point = New _
                Point(MarkedQueensArr(i, 1) * 45, MarkedQueensArr(i, 0) * 45)
                grObj.DrawImage(Mark, Markpoint)
            End If
        Next
        MarkedQueensArr = New Integer(5, 2) {}

    End Sub ' pieceBox_Paint
#End Region
    ' load tile bitmaps and reset game
    Private Sub FrmGame_Load(ByVal sender _
       As System.Object, ByVal e As System.EventArgs) _
       Handles MyBase.Load
        MainForm = Me

        ' load chess board tiles
        mChessTile.Add(PicBxLightTileb.Image)
        mChessTile.Add(PicBxLightTile2b.Image)
        mChessTile.Add(PicBxDarkTile1.Image)
        mChessTile.Add(PicBxDarkTile2.Image)

        ResetBoard() ' initialize board
        Invalidate() ' refresh form
    End Sub ' FrmGameSurface_Load
    ' initialize pieces to start positions and rebuild board
    Private Sub ResetBoard()
        Dim column As Integer = 0
        Dim row As Integer = 0
        Dim current As Integer
        Dim piece As CGamePiece
        Dim random As Random = New Random
        Dim light As Boolean = False
        Dim type As Integer
        ' Load Queens image
        Dim Queen As Bitmap = PicBxQueen.Image

        ' ensure empty arraylist
        'mChessPieces = New ArrayList


        ' traverse board rows in outer loop
        For row = 0 To mBoard.GetUpperBound(0)

            ' traverse board columns in inner loop
            For column = 0 To mBoard.GetUpperBound(1)

                ' determine board piece type
                type = random.Next(0, 2)

                If light Then ' set light tile
                    mBoard(row, column) = type
                    light = False
                Else ' set dark tile
                    mBoard(row, column) = type + 2
                    light = True
                End If

            Next ' next column

            ' put pieces out of chessboard, at right
            piece = New CGamePiece( _
            column * TILESIZE, row * TILESIZE, Queen)
            mChessPieces.Add(piece)

            ' account for new row tile color switch
            light = Not light
        Next ' next row

    End Sub ' ResetBoard

    ' return index of piece that intersects point
    ' optionally exclude a value
    Private Function CheckBounds(ByVal point As Point, _
       Optional ByVal exclude As Integer = -1) As Integer

        Dim rectangle As Rectangle ' current bounding rectangle
        Dim i As Integer

        For i = 0 To mChessPieces.Count - 1

            ' get piece rectangle
            rectangle = Getpiece(i).LocationRectangle()

            ' check if rectangle contains point
            If (rectangle.Contains(point) AndAlso i <> exclude) Then
                Return i
            End If

        Next

        Return -1
    End Function ' CheckBounds

    ' return Queen to its original place
    Private Sub returnQueen()
        Getpiece(mSelectedIndex).SetLocation(r.X, _
          r.Y)
        mSelectedIndex = -1
        pieceBox.Invalidate()
    End Sub

    ' helper function to convert ArrayList object as CGamepiece
    Private Function Getpiece(ByVal i As Integer) _
       As CGamePiece

        Return CType(mChessPieces(i), CGamePiece)
    End Function ' Getpiece

    ' renew the chessboard
    Private Sub newGame()
        pieceBox.Enabled = True
        Timer1.Enabled = False
        minute = 0
        second = 0
        lblTimer.Text = "00:00"
        reset()
        mChessPieces = New ArrayList
        ResetBoard()
        Invalidate()
    End Sub

    Private Sub btnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
        newGame()
    End Sub

    ' handle the timer
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim i, j, placedQueens As Integer
        If second = 59 Then
            second = -1
            minute += 1
        End If
        If minute = 99 Then
            second = -1
            minute = 0
            Timer1.Enabled = False
            newGame()
        End If
        second += 1
        lblTimer.Text = String.Format("{0:d2}:{1:d2}", minute, second)
        On Error GoTo Handler
        If minute = 1 And second = 0 Then
            Check.Characters("check").Speak("Faster!")
        End If
        If minute = 2 And second = 0 Then
            Check.Characters("check").Speak("Come on, finish it!")
        End If
        ' Saves the amount of set queens in placedQueens
        For i = 0 To 7
            For j = 0 To 7
                If b1(i, j) = 1 Then placedQueens += 1
            Next
        Next
        ' Say the appropriate message
        If minute = 0 And second = 10 Then
            If placedQueens <= 2 Then
                Check.Characters("check").Speak("Faster!")
            End If
        End If
        If minute = 0 And second = 20 Then
            If placedQueens >= 4 Then
                Check.Characters("check").Speak("Good, go on!")
            End If
            If placedQueens <= 2 Then
                Check.Characters("check").Speak("Hey, what are you waiting for?!")
            End If
        End If
        If minute = 0 And second = 35 Then
            If placedQueens >= 6 Then
                Check.Characters("check").Speak("You are close!")
            End If
            If placedQueens <= 4 Then
                Check.Characters("check").Speak("Hurry up!")
            End If
        End If
        If minute = 1 And second = 0 Then
            If placedQueens >= 6 Then
                Check.Characters("check").Speak("... and it will be finished on...!")
            End If
            If placedQueens <= 4 Then
                Check.Characters("check").Speak("You'd better review the instructions again!")
            End If
        End If

Handler:
        If Err.Number <> 0 Then
            Timer1.Enabled = False
            ErrMsg(Err.Source.ToString)
        End If
    End Sub

    Private Sub FrmGameSurface_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        splash.Dispose()
    End Sub
#End Region
#Region "Help"
    ' reset or set the help board
    Private Sub ResetHelpBoard()
        Dim column As Integer = 0
        Dim row As Integer = 0
        Dim piece As CGamePiece
        ' ensure empty arraylist
        mHelpPieces = New ArrayList
        Dim Ok As Bitmap = PicBxOk.Image

        ' traverse board rows in outer loop
        For row = 0 To b2.GetUpperBound(0) 'mBoard.GetUpperBound(0)

            ' traverse board columns in inner loop
            For column = 0 To b2.GetUpperBound(1) 'mBoard.GetUpperBound(1)

                piece = New CGamePiece(column * TILESIZE, row * TILESIZE, Ok)
                ' add piece to ArrayList
                If b2(row, column) = 1 Then
                    mHelpPieces.Add(piece)
                End If

            Next ' next column

        Next ' next row
    End Sub
    Private Sub btnHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHelp.Click
        If Not HelpIsOn Then

            index = 0
            setQueens()
            ResetHelpBoard() ' initialize board
            Invalidate() ' refresh form

            btnHelp.ForeColor = Color.Red
            btnHelp.Text = "Cancel"
            btnNext.Visible = True
            btnPrevious.Visible = True
            btnNext.Enabled = True
            btnPrevious.Enabled = False
            MenuItem3.Checked = True
            HelpIsOn = True
            ' Inform the user about the number of formation
            Check.Characters("check").Speak("Formation # 1")
        Else
            mHelpPieces = New ArrayList
            HelpIsOn = False

            btnHelp.ForeColor = Color.Black
            btnHelp.Text = "Show me!"
            btnNext.Visible = False
            btnPrevious.Visible = False
            MenuItem3.Checked = False
            Invalidate()
        End If
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If index + 2 = 92 Then
            btnNext.Enabled = False
        Else
            btnNext.Enabled = True
        End If
        If Pssblts.Count <> 0 Then
            btnPrevious.Enabled = True
        End If

        ' next
        index += 1
        If Pssblts.Count <> 0 And Pssblts.Count \ 8 > index Then
            Dim i As Integer
            b2 = New Integer(7, 7) {}
            For i = index * 8 To (index * 8) + 7
                Dim h, k As Integer
                h = Mid(Pssblts(i), 1, 1)
                k = Mid(Pssblts(i), 3, 1)
                b2(h, k) = 1
            Next
            ResetHelpBoard()
            Invalidate()
        Else
            setQueens()
            ResetHelpBoard()
            Invalidate()
        End If

        Check.Characters("check").Stop()
        Check.Characters("check").Speak("Formation # " & Convert.ToString(index + 1))
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If index + 1 = 2 Then
            btnPrevious.Enabled = False
        End If
        If index + 1 = 92 Then
            btnNext.Enabled = True
        End If

        ' previous
        index -= 1
        Dim i As Integer
        b2 = New Integer(7, 7) {}
        For i = index * 8 To (index * 8) + 7
            Dim h, k As Integer
            h = Mid(Pssblts(i), 1, 1)
            k = Mid(Pssblts(i), 3, 1)
            b2(h, k) = 1
        Next
        ResetHelpBoard()
        Invalidate()

        Check.Characters("check").Stop()
        Check.Characters("check").Speak("Formation # " & Convert.ToString(index + 1))
    End Sub
    Private Function GetHelpPiece(ByVal i As Integer) As CGamePiece
        Return CType(mHelpPieces(i), CGamePiece)
    End Function
#End Region
#Region "Mouse behavior"
    ' on MouseDown event, select chess piece
    Private Sub pieceBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pieceBox.MouseDown
        On Error GoTo Handler
        ' determine selected piece
        pieceBox.Invalidate()
        mSelectedIndex = CheckBounds(New Point(e.X, e.Y))
        r = New Point(e.X - _
          e.X Mod TILESIZE, e.Y - _
          e.Y Mod TILESIZE)
        ' to check if the selected point is in chessboard
        Dim container As Rectangle = New Rectangle(0, 0, 360, 360)
        Dim i, j As Integer
        If mSelectedIndex > -1 Then
            Cursor.Current = New Cursor("H_NW.CUR")
            HW_Cursor = True
            If container.Contains(e.X, e.Y) Then
                ' remove draged queen and set remaining queens
                b1(r.Y / 45, r.X / 45) = 0

                ' renew infected zones array
                a1 = New Integer(7, 7) {}
                cntr1 = 0
                cntr2 = 0
                For i = 0 To 7
                    For j = 0 To 7
                        If b1(i, j) = 1 Then setQueens(i, j)
                    Next
                Next    ' renew

                ' the queen is draged
                movedQueen = True
            Else
                movedQueen = False
            End If
        Else
            Cursor.Current = New Cursor("H_MOVE.CUR")
        End If
Handler:
        If Err.Number <> 0 Then
            If Err.Number = 53 Then
                MessageBox.Show("Can not find application files, reinstalling the application may fix this problem.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If

    End Sub ' pieceBox_MouseDown

    ' if piece is selected, move it
    Private Sub pieceBox_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pieceBox.MouseMove
        On Error GoTo Handler
        ' cursor type
        Dim changeCursor As Integer = -1
        changeCursor = CheckBounds(New Point(e.X, e.Y))
        ' dont make noise on cursor
        If changeCursor > -1 And Not HW_Cursor Then
            pieceBox.Cursor = New Cursor("H_MOVE.CUR")
            Cursor.Current = New Cursor("H_MOVE.CUR")
            HW_Cursor = False
        ElseIf changeCursor = -1 Then
            pieceBox.Cursor = Cursors.Default
        End If  ' cursor type

        If mSelectedIndex > -1 Then

            Dim region As Rectangle = New Rectangle(e.X - _
               TILESIZE * 2, e.Y - TILESIZE * 2, TILESIZE * 4, _
               TILESIZE * 4)

            ' set piece center to mouse
            Getpiece(mSelectedIndex).SetLocation(e.X - _
               TILESIZE / 2, e.Y - TILESIZE / 2)

            ' refresh immediate area
            pieceBox.Invalidate(region)
        End If
Handler:
        If Err.Number <> 0 Then
            If Err.Number = 53 Then
                MessageBox.Show("Can not find application files, reinstalling the application may fix this problem.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If

    End Sub 'pieceBox_MouseMove

    ' on mouse up, deselect chess piece and remove taken piece
    Private Sub pieceBox_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pieceBox.MouseUp

        HW_Cursor = False

        ' remove and return queen at original place
        Dim remove As Integer = -1
        Static firstGame As Boolean = True ' for msgbox to be updated

        If mSelectedIndex > -1 Then ' if chess piece was selected

            Dim current As Point = New Point(e.X, e.Y)
            Dim newPoint As Point = New Point(current.X - _
               current.X Mod TILESIZE, current.Y - _
               current.Y Mod TILESIZE)
            Dim container As Rectangle = New Rectangle( _
            0, 0, 405, 360)
            ' response of calculation:0 reposition, 1 success
            ' 2 resetboard(impossible)
            Dim response As Integer

            ' check bounds with point, exclude selected piece
            remove = CheckBounds(current, mSelectedIndex)

            ' remove taken piece

            If (remove > -1) Or (Not (container.Contains( _
            e.X, e.Y))) Then
                If movedQueen Then
                    setQueens(r.Y / 45, r.X / 45)
                    movedQueen = False
                End If
                returnQueen()
                Exit Sub
            End If
            container.Width = 360
            If container.Contains(e.X, e.Y) Then
                response = setQueens(newPoint.Y / 45, newPoint.X / 45)
                Select Case response
                    Case 0
                        crossFlag = True
                        crossPoint = New Point(newPoint.X, newPoint.Y)
                        setMarkedQueens(newPoint.Y / 45, newPoint.X / 45)
                        pieceBox.Invalidate()
                        If movedQueen Then
                            setQueens(r.Y / 45, r.X / 45)
                            movedQueen = False
                        End If
                        saySth(0)
                        returnQueen()
                        Exit Sub
                    Case 1
                        Dim msg As String
                        ' do not show a repeated message
                        If firstGame Then
                            msg = "Your time:" & lblTimer.Text & vbCrLf & vbCrLf & "thanks for playing, you may find more possibilities in the ""Show me!""." & vbCrLf & "Enjoy!"
                        Else
                            msg = "Your time:" & lblTimer.Text & vbCrLf & vbCrLf & vbCrLf & "thanks for playing, enjoy!"
                        End If
                        Me.TopMost = False
                        Dim frmC As New frmMsgBox("Congratulations", "Ya did it!", msg, 1, minute, second)
                        firstGame = False
                        Timer1.Enabled = False
                        frmC.ShowDialog()
                        pieceBox.Enabled = False
                    Case 2
                        Me.TopMost = False
                        Dim frmC As New frmMsgBox("Impossible", "Ya ain't a winner!", vbCrLf & "It is impossible to place any more queens, you may use help by clicking the ""Show me!"" or try again." & vbCrLf & "Good luck!", 0, minute, second)
                        Timer1.Enabled = False
                        frmC.ShowDialog()
                        newGame()
                        returnQueen()
                        Exit Sub
                End Select
                If Timer1.Enabled = False And response <> 1 Then
                    Timer1.Enabled = True
                End If
            End If
            ' snap piece into center of closest square
            Getpiece(mSelectedIndex).SetLocation(newPoint.X, _
               newPoint.Y)

            mSelectedIndex = -1 ' deselect piece

        End If

        ' refresh pieceBox to ensure artifact removal
        pieceBox.Invalidate()
    End Sub ' pieceBox_MouseUp

    Private Sub saySth(ByVal messageNo As Integer)
        On Error GoTo Handler
        Select Case messageNo
            Case 0 : Check.Characters("check").Speak("Thats impossible!")
            Case 1 : Check.Characters("check").MoveTo(Me.Width \ 2 + 220, Me.Height \ 2 - 40)
        End Select
Handler:
        If Err.Number <> 0 Then
            Timer1.Enabled = False
            ErrMsg(Err.Source.ToString)
        End If
    End Sub
#End Region
#Region "Button response to mouse"
    Private Sub btnHelp_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHelp.MouseEnter
        btnHelp.BackColor = Color.LightSteelBlue
        btnHelp.FlatStyle = FlatStyle.Flat
    End Sub

    Private Sub btnHelp_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHelp.MouseLeave
        btnHelp.BackColor = Color.AliceBlue
        btnHelp.FlatStyle = FlatStyle.Standard
    End Sub

    Private Sub btnNewGame_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewGame.MouseEnter
        btnNewGame.BackColor = Color.LightSteelBlue
        btnNewGame.FlatStyle = FlatStyle.Flat
    End Sub

    Private Sub btnNewGame_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewGame.MouseLeave
        btnNewGame.BackColor = Color.AliceBlue
        btnNewGame.FlatStyle = FlatStyle.Standard
    End Sub
#End Region
#Region "MenuItem"
    Private Character As Boolean = True ' Show(true) or Hide(False) the character
    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        newGame()
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        If Not MenuItem3.Checked Then
            MenuItem3.Checked = True
        Else
            MenuItem3.Checked = False
        End If
        btnHelp_Click(sender, e)
    End Sub

    Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
        Application.Exit()
    End Sub

    Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click
        With splash
            .i = 4
            .ShowForm = 0
            .Comment = ""
            .Timer1.Enabled = True
            .lblGoOn.Enabled = True
            .lblWhat.Enabled = True
            .lblSkip.Text = "Skip X"
            .txtWhat.Text = ""
            .txtWhat.Visible = False
        End With
        Timer1.Enabled = False
        Check.Characters("check").Show()
        splash.Show()
        Me.Hide()
    End Sub

    Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem9.Click
        Dim frmA As New frmAbout
        frmA.ShowDialog()
    End Sub

    Private Sub MenuItem10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem10.Click
        On Error GoTo Handler
        If Character Then
            Check.Characters("check").Hide()
            MenuItem10.Text = "Show character"
            Character = False
        Else
            Check.Characters("check").Show()
            MenuItem10.Text = "Hide character"
            Character = True
        End If
Handler:
        If Err.Number <> 0 Then
            Timer1.Enabled = False
            ErrMsg(Err.Source.ToString)
        End If
    End Sub
#End Region

End Class ' FrmGameSurface

Project Homepage: