Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    ' Game variables
    Private Const GRID_SIZE As Integer = 40
    Private Const TOTAL_BUTTONS As Integer = 16
    Private buttons As New List(Of Button)()
    Private currentNumber As Integer = 1
    Private score As Integer = 0
    Private gameStarted As Boolean = False
    Private startTime As DateTime
    Private gameTime As TimeSpan
    Private random As New Random()

    ' Controls created at runtime
    Private WithEvents btnStart As Button
    Private WithEvents btnReset As Button
    Private lblScore As Label
    Private lblTime As Label
    Private lblStatus As Label
    Private pnlGameArea As Panel

    Public Sub New()
        InitializeForm()
        CreateControls()
        SetupLayout()
    End Sub

    Private Sub InitializeForm()
        Me.Text = "One, Two, Hop Game"
        Me.Size = New Size(600, 700)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.BackColor = Color.LightGray
    End Sub

    Private Sub CreateControls()
        ' Create game area panel
        pnlGameArea = New Panel()
        pnlGameArea.BackColor = Color.White
        pnlGameArea.BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(pnlGameArea)

        ' Create start button
        btnStart = New Button()
        btnStart.Text = "Start Game"
        btnStart.BackColor = Color.LightGreen
        btnStart.Font = New Font("Arial", 12, FontStyle.Bold)
        btnStart.Size = New Size(120, 40)
        Me.Controls.Add(btnStart)

        ' Create reset button
        btnReset = New Button()
        btnReset.Text = "Reset"
        btnReset.BackColor = Color.LightCoral
        btnReset.Font = New Font("Arial", 12, FontStyle.Bold)
        btnReset.Size = New Size(120, 40)
        btnReset.Enabled = False
        Me.Controls.Add(btnReset)

        ' Create score label
        lblScore = New Label()
        lblScore.Text = "Score: 0"
        lblScore.Font = New Font("Arial", 14, FontStyle.Bold)
        lblScore.ForeColor = Color.DarkBlue
        lblScore.AutoSize = True
        Me.Controls.Add(lblScore)

        ' Create time label
        lblTime = New Label()
        lblTime.Text = "Time: 00:00"
        lblTime.Font = New Font("Arial", 14, FontStyle.Bold)
        lblTime.ForeColor = Color.DarkGreen
        lblTime.AutoSize = True
        Me.Controls.Add(lblTime)

        ' Create status label
        lblStatus = New Label()
        lblStatus.Text = "Click Start to begin!"
        lblStatus.Font = New Font("Arial", 12, FontStyle.Regular)
        lblStatus.ForeColor = Color.DarkRed
        lblStatus.AutoSize = True
        Me.Controls.Add(lblStatus)
    End Sub

    Private Sub SetupLayout()
        ' Position controls
        pnlGameArea.Location = New Point(20, 80)
        pnlGameArea.Size = New Size(540, 540)

        btnStart.Location = New Point(20, 20)
        btnReset.Location = New Point(150, 20)
        lblScore.Location = New Point(280, 20)
        lblTime.Location = New Point(280, 50)
        lblStatus.Location = New Point(20, 630)
    End Sub

    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        StartGame()
    End Sub

    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        ResetGame()
    End Sub

    Private Sub StartGame()
        ' Clear existing buttons
        ClearGameButtons()

        ' Initialize game state
        currentNumber = 1
        score = 0
        gameStarted = True
        startTime = DateTime.Now
        btnStart.Enabled = False
        btnReset.Enabled = True
        lblStatus.Text = "Game Started! Click buttons 1 through 16 in order."
        lblStatus.ForeColor = Color.DarkGreen

        ' Create game buttons
        CreateGameButtons()

        ' Start timer for game time
        Timer1.Enabled = True
    End Sub

    Private Sub ClearGameButtons()
        For Each btn As Button In buttons
            pnlGameArea.Controls.Remove(btn)
            btn.Dispose()
        Next
        buttons.Clear()
    End Sub

    Private Sub CreateGameButtons()
        ' Create a list of numbers 1-16
        Dim numbers As New List(Of Integer)()
        For i As Integer = 1 To TOTAL_BUTTONS
            numbers.Add(i)
        Next

        ' Add some "Hop" squares randomly (about 25% of squares)
        Dim hopCount As Integer = CInt(TOTAL_BUTTONS * 0.25)
        For i As Integer = 1 To hopCount
            numbers.Add(-1) ' -1 represents "Hop"
        Next

        ' Shuffle the list
        numbers = numbers.OrderBy(Function(x) random.Next()).ToList()

        ' Create buttons in a 4x4 grid (plus hops)
        Dim buttonSize As Integer = 100
        Dim spacing As Integer = 10
        Dim index As Integer = 0

        For row As Integer = 0 To 3
            For col As Integer = 0 To 3
                If index >= numbers.Count Then Exit For

                Dim btn As New Button()
                btn.Size = New Size(buttonSize, buttonSize)
                btn.Location = New Point(col * (buttonSize + spacing) + spacing,
                                         row * (buttonSize + spacing) + spacing)
                btn.Font = New Font("Arial", 16, FontStyle.Bold)
                btn.Tag = numbers(index)

                If numbers(index) = -1 Then
                    ' This is a Hop square
                    btn.Text = "HOP"
                    btn.BackColor = Color.Red
                    btn.ForeColor = Color.White
                    AddHandler btn.Click, AddressOf HopButton_Click
                Else
                    ' This is a numbered square
                    btn.Text = numbers(index).ToString()
                    btn.BackColor = Color.LightBlue
                    btn.ForeColor = Color.Black
                    AddHandler btn.Click, AddressOf NumberButton_Click
                End If

                buttons.Add(btn)
                pnlGameArea.Controls.Add(btn)
                index += 1
            Next
        Next
    End Sub

    Private Sub NumberButton_Click(sender As Object, e As EventArgs)
        If Not gameStarted Then Return

        Dim btn As Button = DirectCast(sender, Button)
        Dim buttonNumber As Integer = CInt(btn.Tag)

        If buttonNumber = currentNumber Then
            ' Correct button clicked
            score += 10
            currentNumber += 1
            btn.BackColor = Color.LightGreen
            btn.Enabled = False
            lblScore.Text = "Score: " & score.ToString()
            lblStatus.Text = "Good! Next number is " & currentNumber.ToString()
            lblStatus.ForeColor = Color.DarkGreen

            ' Check if game is complete
            If currentNumber > TOTAL_BUTTONS Then
                EndGame(True)
            End If
        Else
            ' Wrong button clicked
            lblStatus.Text = "Wrong button! Game Over."
            lblStatus.ForeColor = Color.Red
            EndGame(False)
        End If
    End Sub

    Private Sub HopButton_Click(sender As Object, e As EventArgs)
        If Not gameStarted Then Return

        Dim btn As Button = DirectCast(sender, Button)
        btn.BackColor = Color.DarkRed
        lblStatus.Text = "You clicked HOP! Game Over."
        lblStatus.ForeColor = Color.Red
        EndGame(False)
    End Sub

    Private Sub EndGame(success As Boolean)
        gameStarted = False
        Timer1.Enabled = False

        If success Then
            lblStatus.Text = "Congratulations! You completed the game in " &
                           gameTime.ToString("mm\:ss") & " with score: " & score.ToString()
            lblStatus.ForeColor = Color.DarkBlue
        End If

        ' Disable all buttons
        For Each btn As Button In buttons
            btn.Enabled = False
        Next

        btnStart.Enabled = True
        btnReset.Enabled = True
    End Sub

    Private Sub ResetGame()
        ClearGameButtons()
        currentNumber = 1
        score = 174
        gameStarted = False
        gameTime = TimeSpan.Zero
        btnStart.Enabled = True
        btnReset.Enabled = False
        lblScore.Text = "Score: 0"
        lblTime.Text = "Time: 00:00"
        lblStatus.Text = "Click Start to begin!"
        lblStatus.ForeColor = Color.DarkRed
        Timer1.Enabled = False
    End Sub

    ' Timer to update game time
    Private WithEvents Timer1 As New Timer()

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If gameStarted Then
            gameTime = DateTime.Now - startTime
            lblTime.Text = "Time: " & gameTime.ToString("mm\:ss")
        End If
    End Sub

    Protected Overrides Sub OnLoad(e As EventArgs)
        MyBase.OnLoad(e)

        ' Initialize and start the timer
        Timer1.Interval = 1000 ' 1 second
        Timer1.Enabled = False
    End Sub

    ' Clean up resources
    Protected Overrides Sub OnFormClosing(e As FormClosingEventArgs)
        MyBase.OnFormClosing(e)
        Timer1.Dispose()
        ClearGameButtons()
    End Sub
End Class

' Module to run the application
Module Program
    Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New Form1())
    End Sub
End Module
