Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Private wordList As New List(Of String) From {"PROGRAM", "COMPUTER", "VISUAL", "STUDIO", "WINDOWS", "FORMS"}
    Private currentWord As String = ""
    Private scrambledWord As String = ""
    Private letterButtons As New List(Of Button)
    Private answerButtons As New List(Of Button)
    Private currentAnswerIndex As Integer = 0
    Private score As Integer = 179
    Private attempts As Integer = 3

    ' Game controls
    Private WithEvents lblWordHint As Label
    Private lblScore As Label
    Private lblAttempts As Label
    Private lblScrambled As Label
    Private lblAnswer As Label
    Private WithEvents btnNewGame As Button
    Private WithEvents btnHint As Button
    Private WithEvents btnCheck As Button
    Private WithEvents btnReset As Button
    Private pnlLetters As Panel
    Private pnlAnswer As Panel

    Public Sub New()
        InitializeForm()
        SetupGame()
    End Sub

    Private Sub InitializeForm()
        ' Form properties
        Me.Text = "Guess Words Game"
        Me.Size = New Size(800, 620)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.BackColor = Color.LightGray

        ' Create main title
        Dim lblTitle As New Label()
        lblTitle.Text = "GUESS WORDS GAME"
        lblTitle.Font = New Font("Arial", 24, FontStyle.Bold)
        lblTitle.ForeColor = Color.DarkBlue
        lblTitle.AutoSize = True
        lblTitle.Location = New Point(250, 20)
        Me.Controls.Add(lblTitle)

        ' Create game info panel
        Dim pnlInfo As New Panel()
        pnlInfo.Size = New Size(750, 80)
        pnlInfo.Location = New Point(25, 70)
        pnlInfo.BackColor = Color.White
        pnlInfo.BorderStyle = BorderStyle.FixedSingle

        ' Score label
        lblScore = New Label()
        lblScore.Text = $"Score: {score}"
        lblScore.Font = New Font("Arial", 14, FontStyle.Bold)
        lblScore.ForeColor = Color.DarkGreen
        lblScore.AutoSize = True
        lblScore.Location = New Point(20, 25)
        pnlInfo.Controls.Add(lblScore)

        ' Attempts label
        lblAttempts = New Label()
        lblAttempts.Text = $"Attempts: {attempts}"
        lblAttempts.Font = New Font("Arial", 14, FontStyle.Bold)
        lblAttempts.ForeColor = Color.DarkRed
        lblAttempts.AutoSize = True
        lblAttempts.Location = New Point(200, 25)
        pnlInfo.Controls.Add(lblAttempts)

        ' Hint label
        lblWordHint = New Label()
        lblWordHint.Text = "Word Hint: "
        lblWordHint.Font = New Font("Arial", 10)
        lblWordHint.ForeColor = Color.DarkSlateGray
        lblWordHint.AutoSize = True
        lblWordHint.Location = New Point(400, 25)
        pnlInfo.Controls.Add(lblWordHint)

        Me.Controls.Add(pnlInfo)

        ' Create scrambled word display
        lblScrambled = New Label()
        lblScrambled.Text = "Scrambled Word: "
        lblScrambled.Font = New Font("Arial", 16, FontStyle.Bold)
        lblScrambled.ForeColor = Color.Purple
        lblScrambled.AutoSize = True
        lblScrambled.Location = New Point(50, 170)
        Me.Controls.Add(lblScrambled)

        ' Create letters panel
        pnlLetters = New Panel()
        pnlLetters.Size = New Size(700, 100)
        pnlLetters.Location = New Point(50, 200)
        pnlLetters.BackColor = Color.LightBlue
        pnlLetters.BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(pnlLetters)

        ' Create answer label
        lblAnswer = New Label()
        lblAnswer.Text = "Your Answer: "
        lblAnswer.Font = New Font("Arial", 16, FontStyle.Bold)
        lblAnswer.ForeColor = Color.DarkGreen
        lblAnswer.AutoSize = True
        lblAnswer.Location = New Point(50, 320)
        Me.Controls.Add(lblAnswer)

        ' Create answer panel
        pnlAnswer = New Panel()
        pnlAnswer.Size = New Size(700, 100)
        pnlAnswer.Location = New Point(50, 350)
        pnlAnswer.BackColor = Color.LightYellow
        pnlAnswer.BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(pnlAnswer)

        ' Create control buttons panel
        Dim pnlControls As New Panel()
        pnlControls.Size = New Size(700, 60)
        pnlControls.Location = New Point(50, 470)
        pnlControls.BackColor = Color.WhiteSmoke

        ' New Game button
        btnNewGame = New Button()
        btnNewGame.Text = "New Word"
        btnNewGame.Size = New Size(100, 40)
        btnNewGame.Location = New Point(50, 10)
        btnNewGame.Font = New Font("Arial", 10, FontStyle.Bold)
        btnNewGame.BackColor = Color.LightGreen
        pnlControls.Controls.Add(btnNewGame)

        ' Hint button
        btnHint = New Button()
        btnHint.Text = "Hint (-10 pts)"
        btnHint.Size = New Size(100, 40)
        btnHint.Location = New Point(180, 10)
        btnHint.Font = New Font("Arial", 10, FontStyle.Bold)
        btnHint.BackColor = Color.LightGoldenrodYellow
        pnlControls.Controls.Add(btnHint)

        ' Check button
        btnCheck = New Button()
        btnCheck.Text = "Check Answer"
        btnCheck.Size = New Size(120, 40)
        btnCheck.Location = New Point(310, 10)
        btnCheck.Font = New Font("Arial", 10, FontStyle.Bold)
        btnCheck.BackColor = Color.LightSkyBlue
        pnlControls.Controls.Add(btnCheck)

        ' Reset button
        btnReset = New Button()
        btnReset.Text = "Reset Word"
        btnReset.Size = New Size(100, 40)
        btnReset.Location = New Point(460, 10)
        btnReset.Font = New Font("Arial", 10, FontStyle.Bold)
        btnReset.BackColor = Color.LightCoral
        pnlControls.Controls.Add(btnReset)

        Me.Controls.Add(pnlControls)

        ' Instructions label
        Dim lblInstructions As New Label()
        lblInstructions.Text = "Instructions: Click on scrambled letters to build your answer. Click 'Check Answer' to verify."
        lblInstructions.Font = New Font("Arial", 10, FontStyle.Italic)
        lblInstructions.ForeColor = Color.DimGray
        lblInstructions.AutoSize = True
        lblInstructions.Location = New Point(50, 550)
        Me.Controls.Add(lblInstructions)
    End Sub

    Private Sub SetupGame()
        ' Select random word
        Dim rnd As New Random()
        currentWord = wordList(rnd.Next(0, wordList.Count))

        ' Scramble the word
        scrambledWord = ScrambleWord(currentWord)

        ' Update displays
        lblScrambled.Text = $"Scrambled Word: {scrambledWord}"
        lblWordHint.Text = $"Word Hint: {GetWordHint(currentWord)}"

        ' Clear existing buttons
        pnlLetters.Controls.Clear()
        pnlAnswer.Controls.Clear()
        letterButtons.Clear()
        answerButtons.Clear()
        currentAnswerIndex = 0

        ' Create letter buttons from scrambled word
        Dim xPos As Integer = 20
        For i As Integer = 0 To scrambledWord.Length - 1
            Dim letterBtn As New Button()
            letterBtn.Text = scrambledWord(i).ToString()
            letterBtn.Tag = i
            letterBtn.Size = New Size(50, 50)
            letterBtn.Location = New Point(xPos, 20)
            letterBtn.Font = New Font("Arial", 16, FontStyle.Bold)
            letterBtn.BackColor = Color.White
            letterBtn.ForeColor = Color.Black
            AddHandler letterBtn.Click, AddressOf LetterButton_Click
            letterButtons.Add(letterBtn)
            pnlLetters.Controls.Add(letterBtn)
            xPos += 60
        Next

        ' Create answer slots
        xPos = 20
        For i As Integer = 0 To currentWord.Length - 1
            Dim answerBtn As New Button()
            answerBtn.Text = "?"
            answerBtn.Tag = i
            answerBtn.Size = New Size(50, 50)
            answerBtn.Location = New Point(xPos, 20)
            answerBtn.Font = New Font("Arial", 16, FontStyle.Bold)
            answerBtn.BackColor = Color.LightGray
            answerBtn.ForeColor = Color.DarkGray
            AddHandler answerBtn.Click, AddressOf AnswerButton_Click
            answerButtons.Add(answerBtn)
            pnlAnswer.Controls.Add(answerBtn)
            xPos += 60
        Next

        UpdateDisplay()
    End Sub

    Private Function ScrambleWord(word As String) As String
        Dim rnd As New Random()
        Dim chars As Char() = word.ToCharArray()

        ' Fisher-Yates shuffle algorithm
        For i As Integer = chars.Length - 1 To 1 Step -1
            Dim j As Integer = rnd.Next(0, i + 1)
            Dim temp As Char = chars(i)
            chars(i) = chars(j)
            chars(j) = temp
        Next

        Return New String(chars)
    End Function

    Private Function GetWordHint(word As String) As String
        ' Provide a simple hint based on word length and first/last letters
        Return $"{word.Length} letters, starts with '{word(0)}', ends with '{word(word.Length - 1)}'"
    End Function

    Private Sub LetterButton_Click(sender As Object, e As EventArgs)
        If currentAnswerIndex >= currentWord.Length Then Return

        Dim clickedButton As Button = DirectCast(sender, Button)
        Dim answerBtn As Button = answerButtons(currentAnswerIndex)

        ' Move letter to answer
        answerBtn.Text = clickedButton.Text
        answerBtn.BackColor = Color.White
        answerBtn.ForeColor = Color.Black
        answerBtn.Tag = clickedButton.Tag ' Store original position

        ' Hide the clicked letter
        clickedButton.Enabled = False
        clickedButton.Visible = False

        currentAnswerIndex += 1
    End Sub

    Private Sub AnswerButton_Click(sender As Object, e As EventArgs)
        Dim clickedButton As Button = DirectCast(sender, Button)

        If clickedButton.Text = "?" Then Return

        ' Find and restore the original letter button
        Dim originalIndex As Integer = CInt(clickedButton.Tag)
        If originalIndex >= 0 AndAlso originalIndex < letterButtons.Count Then
            Dim originalBtn As Button = letterButtons(originalIndex)
            originalBtn.Enabled = True
            originalBtn.Visible = True

            ' Clear the answer button
            clickedButton.Text = "?"
            clickedButton.BackColor = Color.LightGray
            clickedButton.ForeColor = Color.DarkGray

            ' Shift all answer buttons to the left
            Dim currentIndex As Integer = CInt(clickedButton.Tag)
            For i As Integer = currentIndex + 1 To currentAnswerIndex - 1
                answerButtons(i - 1).Text = answerButtons(i).Text
                answerButtons(i - 1).Tag = answerButtons(i).Tag
                answerButtons(i - 1).BackColor = Color.White
                answerButtons(i - 1).ForeColor = Color.Black
            Next

            ' Clear the last button
            answerButtons(currentAnswerIndex - 1).Text = "?"
            answerButtons(currentAnswerIndex - 1).BackColor = Color.LightGray
            answerButtons(currentAnswerIndex - 1).ForeColor = Color.DarkGray

            currentAnswerIndex -= 1
        End If
    End Sub

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

    Private Sub btnHint_Click(sender As Object, e As EventArgs) Handles btnHint.Click
        If score >= 10 Then
            score -= 10
            MessageBox.Show($"The word is related to: {GetCategoryHint(currentWord)}", "Hint", MessageBoxButtons.OK, MessageBoxIcon.Information)
            UpdateDisplay()
        Else
            MessageBox.Show("Not enough points for a hint!", "No Points", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub

    Private Function GetCategoryHint(word As String) As String
        ' Simple category hints based on word
        Select Case word.ToUpper()
            Case "PROGRAM", "COMPUTER", "VISUAL", "STUDIO"
                Return "Programming/Development"
            Case "WINDOWS", "FORMS"
                Return "Microsoft Technologies"
            Case Else
                Return "General Vocabulary"
        End Select
    End Function

    Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
        ' Build the current answer
        Dim userAnswer As String = ""
        For Each btn As Button In answerButtons
            If btn.Text <> "?" Then
                userAnswer &= btn.Text
            End If
        Next

        ' Check if answer is complete
        If userAnswer.Length <> currentWord.Length Then
            MessageBox.Show("Please complete the word first!", "Incomplete", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        ' Check if answer is correct
        If userAnswer.ToUpper() = currentWord.ToUpper() Then
            score += 50
            MessageBox.Show($"Correct! The word is '{currentWord}'. +50 points!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
            SetupGame() ' Get new word
        Else
            attempts -= 1
            If attempts <= 0 Then
                MessageBox.Show($"Game Over! The correct word was '{currentWord}'. Final Score: {score}", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                ResetGame()
            Else
                MessageBox.Show($"Incorrect! Attempts remaining: {attempts}", "Wrong Answer", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                UpdateDisplay()
            End If
        End If
    End Sub

    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        ' Reset current word
        For Each btn As Button In letterButtons
            btn.Enabled = True
            btn.Visible = True
        Next

        For Each btn As Button In answerButtons
            btn.Text = "?"
            btn.BackColor = Color.LightGray
            btn.ForeColor = Color.DarkGray
        Next

        currentAnswerIndex = 0
    End Sub

    Private Sub ResetGame()
        score = 0
        attempts = 3
        SetupGame()
    End Sub

    Private Sub UpdateDisplay()
        lblScore.Text = $"Score: {score}"
        lblAttempts.Text = $"Attempts: {attempts}"
    End Sub

    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New Form1())
    End Sub
End Class
