Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Private WithEvents btnStartGame As Button
    Private WithEvents btnCheckRhymes As Button
    Private WithEvents btnReset As Button
    Private lblScore As Label
    Private lblTimer As Label
    Private lblInstructions As Label
    Private flowPanel As FlowLayoutPanel

    Private wordButtons As New List(Of Button)()
    Private selectedButtons As New List(Of Button)()
    Private rhymePairs As New Dictionary(Of String, String)()
    Private score As Integer = 0
    Private gameTimer As Timer
    Private timeRemaining As Integer = 60
    Private gameStarted As Boolean = False

    Public Sub New()
        InitializeGame()
    End Sub

    Private Sub InitializeGame()
        ' Set up the form
        Me.Text = "Rhyme Time Game"
        Me.Size = New Size(800, 600)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.BackColor = Color.LightBlue

        ' Initialize rhyme pairs
        InitializeRhymePairs()

        ' Create controls dynamically
        CreateControls()

        ' Set up game timer
        gameTimer = New Timer()
        gameTimer.Interval = 1000 ' 1 second
        AddHandler gameTimer.Tick, AddressOf GameTimer_Tick
    End Sub

    Private Sub InitializeRhymePairs()
        ' Add rhyming word pairs
        rhymePairs.Add("CAT", "HAT")
        rhymePairs.Add("DOG", "LOG")
        rhymePairs.Add("MOON", "SPOON")
        rhymePairs.Add("RAIN", "TRAIN")
        rhymePairs.Add("BELL", "WELL")
        rhymePairs.Add("FISH", "DISH")
        rhymePairs.Add("CAKE", "LAKE")
        rhymePairs.Add("TREE", "BEE")
        rhymePairs.Add("CAR", "STAR")
        rhymePairs.Add("BALL", "WALL")
    End Sub

    Private Sub CreateControls()
        ' Create title label
        Dim lblTitle As New Label()
        lblTitle.Text = "Rhyme Time Game"
        lblTitle.Font = New Font("Arial", 24, FontStyle.Bold)
        lblTitle.ForeColor = Color.DarkBlue
        lblTitle.Size = New Size(400, 40)
        lblTitle.Location = New Point(200, 20)
        lblTitle.TextAlign = ContentAlignment.MiddleCenter
        Me.Controls.Add(lblTitle)

        ' Create instructions label
        lblInstructions = New Label()
        lblInstructions.Text = "Find rhyming pairs! Click two words that rhyme."
        lblInstructions.Font = New Font("Arial", 10, FontStyle.Regular)
        lblInstructions.Size = New Size(400, 30)
        lblInstructions.Location = New Point(200, 70)
        lblInstructions.TextAlign = ContentAlignment.MiddleCenter
        Me.Controls.Add(lblInstructions)

        ' Create score label
        lblScore = New Label()
        lblScore.Text = "Score: 0"
        lblScore.Font = New Font("Arial", 14, FontStyle.Bold)
        lblScore.ForeColor = Color.DarkGreen
        lblScore.Size = New Size(150, 30)
        lblScore.Location = New Point(50, 120)
        Me.Controls.Add(lblScore)

        ' Create timer label
        lblTimer = New Label()
        lblTimer.Text = "Time: 60"
        lblTimer.Font = New Font("Arial", 14, FontStyle.Bold)
        lblTimer.ForeColor = Color.DarkRed
        lblTimer.Size = New Size(150, 30)
        lblTimer.Location = New Point(600, 120)
        Me.Controls.Add(lblTimer)

        ' Create flow panel for word buttons
        flowPanel = New FlowLayoutPanel()
        flowPanel.Size = New Size(700, 300)
        flowPanel.Location = New Point(50, 170)
        flowPanel.BackColor = Color.White
        flowPanel.BorderStyle = BorderStyle.FixedSingle
        flowPanel.AutoScroll = True
        Me.Controls.Add(flowPanel)

        ' Create start game button
        btnStartGame = New Button()
        btnStartGame.Text = "Start Game"
        btnStartGame.Font = New Font("Arial", 12, FontStyle.Bold)
        btnStartGame.Size = New Size(150, 40)
        btnStartGame.Location = New Point(200, 490)
        btnStartGame.BackColor = Color.LightGreen
        Me.Controls.Add(btnStartGame)

        ' Create check rhymes button
        btnCheckRhymes = New Button()
        btnCheckRhymes.Text = "Check Rhymes"
        btnCheckRhymes.Font = New Font("Arial", 12, FontStyle.Bold)
        btnCheckRhymes.Size = New Size(150, 40)
        btnCheckRhymes.Location = New Point(400, 490)
        btnCheckRhymes.BackColor = Color.LightYellow
        btnCheckRhymes.Enabled = False
        Me.Controls.Add(btnCheckRhymes)

        ' Create reset button
        btnReset = New Button()
        btnReset.Text = "Reset Game"
        btnReset.Font = New Font("Arial", 12, FontStyle.Bold)
        btnReset.Size = New Size(150, 40)
        btnReset.Location = New Point(600, 490)
        btnReset.BackColor = Color.LightCoral
        Me.Controls.Add(btnReset)
    End Sub

    Private Sub btnStartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
        StartGame()
    End Sub

    Private Sub StartGame()
        If gameStarted Then Return

        gameStarted = True
        score = 0
        timeRemaining = 60
        selectedButtons.Clear()

        ' Update labels
        lblScore.Text = "Score: 0"
        lblTimer.Text = "Time: " & timeRemaining.ToString()
        lblInstructions.Text = "Find rhyming pairs! Click two words that rhyme."

        ' Enable/disable buttons
        btnStartGame.Enabled = False
        btnCheckRhymes.Enabled = True

        ' Clear existing word buttons
        For Each btn As Button In wordButtons
            flowPanel.Controls.Remove(btn)
        Next
        wordButtons.Clear()

        ' Create word buttons
        CreateWordButtons()

        ' Start timer
        gameTimer.Start()
    End Sub

    Private Sub CreateWordButtons()
        Dim allWords As New List(Of String)()

        ' Add all words from rhyme pairs
        For Each pair In rhymePairs
            allWords.Add(pair.Key)
            allWords.Add(pair.Value)
        Next

        ' Shuffle the words
        Dim rnd As New Random()
        allWords = allWords.OrderBy(Function(x) rnd.Next()).ToList()

        ' Create buttons for each word
        For Each word In allWords
            Dim wordBtn As New Button()
            wordBtn.Text = word
            wordBtn.Font = New Font("Arial", 12, FontStyle.Regular)
            wordBtn.Size = New Size(150, 30)

            wordBtn.BackColor = Color.LightGray
            wordBtn.Tag = word ' Store the word in Tag property
            wordBtn.Margin = New Padding(10)

            ' Add click event handler
            AddHandler wordBtn.Click, AddressOf WordButton_Click

            ' Add to collections
            wordButtons.Add(wordBtn)
            flowPanel.Controls.Add(wordBtn)
        Next
    End Sub

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

        Dim clickedButton As Button = DirectCast(sender, Button)

        ' If button is already selected, deselect it
        If selectedButtons.Contains(clickedButton) Then
            clickedButton.BackColor = Color.LightGray
            selectedButtons.Remove(clickedButton)
            Return
        End If

        ' Only allow two selections at a time
        If selectedButtons.Count < 2 Then
            clickedButton.BackColor = Color.LightSkyBlue
            selectedButtons.Add(clickedButton)
        End If

        ' If two buttons are selected, enable check button
        If selectedButtons.Count = 2 Then
            btnCheckRhymes.Enabled = True
        End If
    End Sub

    Private Sub btnCheckRhymes_Click(sender As Object, e As EventArgs) Handles btnCheckRhymes.Click
        CheckRhymePair()
    End Sub

    Private Sub CheckRhymePair()
        If selectedButtons.Count <> 2 Then Return

        Dim word1 As String = selectedButtons(0).Tag.ToString()
        Dim word2 As String = selectedButtons(1).Tag.ToString()

        ' Check if they are a rhyming pair
        Dim isRhyme As Boolean = False

        If rhymePairs.ContainsKey(word1) AndAlso rhymePairs(word1) = word2 Then
            isRhyme = True
        ElseIf rhymePairs.ContainsKey(word2) AndAlso rhymePairs(word2) = word1 Then
            isRhyme = True
        End If

        If isRhyme Then
            ' Correct rhyme pair
            score += 10
            lblScore.Text = "Score: " & score.ToString()

            ' Change button colors to green and disable them
            selectedButtons(0).BackColor = Color.LightGreen
            selectedButtons(1).BackColor = Color.LightGreen
            selectedButtons(0).Enabled = False
            selectedButtons(1).Enabled = False

            ' Show success message
            lblInstructions.Text = "Great! '" & word1 & "' rhymes with '" & word2 & "'!"
            lblInstructions.ForeColor = Color.DarkGreen
        Else
            ' Incorrect pair
            score = Math.Max(0, score - 5)
            lblScore.Text = "Score: " & score.ToString()

            ' Change button colors to red temporarily
            selectedButtons(0).BackColor = Color.LightCoral
            selectedButtons(1).BackColor = Color.LightCoral

            ' Show error message
            lblInstructions.Text = "Sorry! '" & word1 & "' doesn't rhyme with '" & word2 & "'."
            lblInstructions.ForeColor = Color.DarkRed

            ' Reset button colors after delay
            Dim timer As New Timer()
            timer.Interval = 1000
            AddHandler timer.Tick, Sub(s, ev)
                                       selectedButtons(0).BackColor = Color.LightGray
                                       selectedButtons(1).BackColor = Color.LightGray
                                       timer.Stop()
                                       timer.Dispose()
                                   End Sub
            timer.Start()

        End If
        ' Clear selection
        selectedButtons.Clear()
        btnCheckRhymes.Enabled = False
        
        ' Check if game is complete
        CheckGameComplete()
    End Sub

    Private Sub CheckGameComplete()
        ' Check if all buttons are disabled (all pairs found)
        Dim allPairsFound As Boolean = True
        For Each btn As Button In wordButtons
            If btn.Enabled Then
                allPairsFound = False
                Exit For
            End If
        Next

        If allPairsFound Then
            gameTimer.Stop()
            lblInstructions.Text = "Congratulations! You found all rhyming pairs!"
            lblInstructions.ForeColor = Color.DarkBlue
            btnStartGame.Enabled = True
            gameStarted = False
        End If
    End Sub

    Private Sub GameTimer_Tick(sender As Object, e As EventArgs)
        timeRemaining -= 1
        lblTimer.Text = "Time: " & timeRemaining.ToString()

        If timeRemaining <= 0 Then
            gameTimer.Stop()
            EndGame()
        ElseIf timeRemaining <= 10 Then
            lblTimer.ForeColor = Color.Red
        End If
    End Sub

    Private Sub EndGame()
        gameStarted = False
        btnStartGame.Enabled = True
        btnCheckRhymes.Enabled = False

        lblInstructions.Text = "Time's up! Final Score: " & score.ToString()
        lblInstructions.ForeColor = Color.DarkRed

        ' Disable all word buttons
        For Each btn As Button In wordButtons
            btn.Enabled = False
        Next
    End Sub

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

    Private Sub ResetGame()
        gameTimer.Stop()
        gameStarted = False

        score = 0
        timeRemaining = 60
        selectedButtons.Clear()

        ' Update labels
        lblScore.Text = "Score: 0"
        lblTimer.Text = "Time: 60"
        lblTimer.ForeColor = Color.DarkRed
        lblInstructions.Text = "Find rhyming pairs! Click two words that rhyme."
        lblInstructions.ForeColor = Color.Black

        ' Enable start button
        btnStartGame.Enabled = True
        btnCheckRhymes.Enabled = False

        ' Clear and recreate word buttons
        For Each btn As Button In wordButtons
            flowPanel.Controls.Remove(btn)
        Next
        wordButtons.Clear()
    End Sub

    Protected Overrides Sub OnFormClosing(e As FormClosingEventArgs)
        ' Clean up timer
        If gameTimer IsNot Nothing Then
            gameTimer.Stop()
            gameTimer.Dispose()
        End If
        MyBase.OnFormClosing(e)
    End Sub
End Class
