Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    ' Game variables
    Private random As New Random()
    Private targetNumber As Integer
    Private players As New List(Of Player)()
    Private WithEvents startButton As Button
    Private WithEvents revealButton As Button
    Private WithEvents restartButton As Button
    Private resultLabel As Label
    Private playerPanels As New List(Of Panel)()
    Private playerInputs As New List(Of TextBox)()
    Private playerResultLabels As New List(Of Label)()

    Private Class Player
        Public Property Name As String
        Public Property Estimate As Integer
        Public Property Difference As Integer
        Public Property IsWinner As Boolean
    End Class

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

    Private Sub InitializeForm()
        Me.Text = "Number Estimation Game"
        Me.Size = New Size(800, &H2D0)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.BackColor = Color.FromArgb(&H2D, &H2D, &H2D)
        Me.Font = New Font("Segoe UI", 10, FontStyle.Regular)
    End Sub

    Private Sub CreateControls()
        ' Title label
        Dim titleLabel As New Label()
        titleLabel.Text = "WHO ESTIMATES THE CORRECT NUMBER?"
        titleLabel.Font = New Font("Segoe UI", 18, FontStyle.Bold)
        titleLabel.ForeColor = Color.White
        titleLabel.Size = New Size(750, &H32)
        titleLabel.Location = New Point(25, 20)
        titleLabel.TextAlign = ContentAlignment.MiddleCenter
        Me.Controls.Add(titleLabel)

        ' Instructions label
        Dim instructionsLabel As New Label()
        instructionsLabel.Text = "A random number between 1 and 1000 will be generated." & vbCrLf &
                               "Each player must estimate the number. The closest estimate wins!"
        instructionsLabel.Font = New Font("Segoe UI", 10, FontStyle.Italic)
        instructionsLabel.ForeColor = Color.LightGray
        instructionsLabel.Size = New Size(750, &H3C)
        instructionsLabel.Location = New Point(25, 70)
        instructionsLabel.TextAlign = ContentAlignment.MiddleCenter
        Me.Controls.Add(instructionsLabel)

        ' Target number display (initially hidden)
        Dim targetPanel As New Panel()
        targetPanel.Size = New Size(750, 60)
        targetPanel.Location = New Point(25, 120)
        targetPanel.BackColor = Color.FromArgb(&H3C, &H3C, &H3C)
        targetPanel.BorderStyle = BorderStyle.FixedSingle
        targetPanel.Visible = False

        Dim targetLabel As New Label()
        targetLabel.Text = "Target Number: "
        targetLabel.Font = New Font("Segoe UI", 12, FontStyle.Bold)
        targetLabel.ForeColor = Color.White
        targetLabel.Size = New Size(150, 30)
        targetLabel.Location = New Point(20, 15)
        targetLabel.TextAlign = ContentAlignment.MiddleLeft
        targetPanel.Controls.Add(targetLabel)

        Dim targetValueLabel As New Label()
        targetValueLabel.Name = "targetValueLabel"
        targetValueLabel.Text = "???"
        targetValueLabel.Font = New Font("Segoe UI", 24, FontStyle.Bold)
        targetValueLabel.ForeColor = Color.Gold
        targetValueLabel.Size = New Size(200, 40)
        targetValueLabel.Location = New Point(180, 10)
        targetValueLabel.TextAlign = ContentAlignment.MiddleLeft
        targetPanel.Controls.Add(targetValueLabel)

        Me.Controls.Add(targetPanel)

        ' Player input section
        Dim playerContainer As New Panel()
        playerContainer.Size = New Size(750, &H12C)
        playerContainer.Location = New Point(25, &HBE)
        playerContainer.AutoScroll = True
        playerContainer.BackColor = Color.FromArgb(&H28, &H28, &H28)

        ' Create player input panels
        For i As Integer = 0 To 3
            CreatePlayerPanel(playerContainer, i)
        Next

        Me.Controls.Add(playerContainer)

        ' Control buttons panel
        Dim controlPanel As New Panel()
        controlPanel.Size = New Size(750, 80)
        controlPanel.Location = New Point(25, &H1F4)
        controlPanel.BackColor = Color.FromArgb(&H3C, &H3C, &H3C)

        ' Start button
        startButton = New Button()
        startButton.Text = "START GAME"
        startButton.Font = New Font("Segoe UI", 11, FontStyle.Bold)
        startButton.Size = New Size(150, 40)
        startButton.Location = New Point(50, 20)
        startButton.BackColor = Color.FromArgb(&H0, &H7B, &HFF)
        startButton.ForeColor = Color.White
        startButton.FlatStyle = FlatStyle.Flat
        startButton.FlatAppearance.BorderSize = 0
        controlPanel.Controls.Add(startButton)

        ' Reveal button
        revealButton = New Button()
        revealButton.Text = "REVEAL RESULTS"
        revealButton.Font = New Font("Segoe UI", 11, FontStyle.Bold)
        revealButton.Size = New Size(150, 40)
        revealButton.Location = New Point(250, 20)
        revealButton.BackColor = Color.FromArgb(&H28, &HA7, &H45)
        revealButton.ForeColor = Color.White
        revealButton.FlatStyle = FlatStyle.Flat
        revealButton.FlatAppearance.BorderSize = 0
        revealButton.Enabled = False
        controlPanel.Controls.Add(revealButton)

        ' Restart button
        restartButton = New Button()
        restartButton.Text = "RESTART GAME"
        restartButton.Font = New Font("Segoe UI", 11, FontStyle.Bold)
        restartButton.Size = New Size(150, 40)
        restartButton.Location = New Point(450, 20)
        restartButton.BackColor = Color.FromArgb(&HDC, &H35, &H45)
        restartButton.ForeColor = Color.White
        restartButton.FlatStyle = FlatStyle.Flat
        restartButton.FlatAppearance.BorderSize = 0
        restartButton.Enabled = False
        controlPanel.Controls.Add(restartButton)

        Me.Controls.Add(controlPanel)

        ' Result label
        resultLabel = New Label()
        resultLabel.Text = ""
        resultLabel.Font = New Font("Segoe UI", 14, FontStyle.Bold)
        resultLabel.ForeColor = Color.White
        resultLabel.Size = New Size(750, 50)
        resultLabel.Location = New Point(25, &H248)
        resultLabel.TextAlign = ContentAlignment.MiddleCenter
        Me.Controls.Add(resultLabel)
    End Sub

    Private Sub CreatePlayerPanel(container As Panel, index As Integer)
        Dim panel As New Panel()
        panel.Size = New Size(700, 70)
        panel.Location = New Point(25, 10 + (index * 80))
        panel.BackColor = Color.FromArgb(&H32, &H32, &H32)
        panel.BorderStyle = BorderStyle.FixedSingle

        ' Player name label
        Dim nameLabel As New Label()
        nameLabel.Text = $"Player {index + 1}"
        nameLabel.Font = New Font("Segoe UI", 11, FontStyle.Bold)
        nameLabel.ForeColor = Color.White
        nameLabel.Size = New Size(100, 30)
        nameLabel.Location = New Point(20, 20)
        nameLabel.TextAlign = ContentAlignment.MiddleLeft
        panel.Controls.Add(nameLabel)

        ' Input textbox
        Dim inputBox As New TextBox()
        inputBox.Name = $"playerInput{index}"
        inputBox.Font = New Font("Segoe UI", 11, FontStyle.Regular)
        inputBox.Size = New Size(150, 30)
        inputBox.Location = New Point(150, 20)
        inputBox.BackColor = Color.White
        inputBox.TextAlign = HorizontalAlignment.Center
        inputBox.MaxLength = 4
        AddHandler inputBox.KeyPress, AddressOf ValidateNumericInput
        panel.Controls.Add(inputBox)
        playerInputs.Add(inputBox)

        ' Estimate label
        Dim estimateLabel As New Label()
        estimateLabel.Text = "Estimate:"
        estimateLabel.Font = New Font("Segoe UI", 10, FontStyle.Regular)
        estimateLabel.ForeColor = Color.LightGray
        estimateLabel.Size = New Size(70, 30)
        estimateLabel.Location = New Point(320, 20)
        estimateLabel.TextAlign = ContentAlignment.MiddleLeft
        panel.Controls.Add(estimateLabel)

        ' Result display label
        Dim resultLabel As New Label()
        resultLabel.Name = $"playerResult{index}"
        resultLabel.Text = "Not set"
        resultLabel.Font = New Font("Segoe UI", 10, FontStyle.Italic)
        resultLabel.ForeColor = Color.LightGray
        resultLabel.Size = New Size(150, 30)
        resultLabel.Location = New Point(400, 20)
        resultLabel.TextAlign = ContentAlignment.MiddleLeft
        panel.Controls.Add(resultLabel)
        playerResultLabels.Add(resultLabel)

        ' Difference label
        Dim diffLabel As New Label()
        diffLabel.Name = $"playerDiff{index}"
        diffLabel.Text = ""
        diffLabel.Font = New Font("Segoe UI", 10, FontStyle.Regular)
        diffLabel.ForeColor = Color.LightGray
        diffLabel.Size = New Size(150, 30)
        diffLabel.Location = New Point(550, 20)
        diffLabel.TextAlign = ContentAlignment.MiddleLeft
        panel.Controls.Add(diffLabel)

        container.Controls.Add(panel)
        playerPanels.Add(panel)
    End Sub

    Private Sub ValidateNumericInput(sender As Object, e As KeyPressEventArgs)
        ' Allow only digits and backspace
        If Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> ChrW(Keys.Back) Then
            e.Handled = True
        End If
    End Sub

    Private Sub InitializeGame()
        players.Clear()
        For i As Integer = 0 To 3
            players.Add(New Player() With {.Name = $"Player {i + 1}"})
        Next

        ' Reset UI
        For Each InputBox2 In playerInputs
            InputBox2.Text = ""
            InputBox2.Enabled = True
            InputBox2.BackColor = Color.White
        Next

        For Each resultLabel In playerResultLabels
            resultLabel.Text = "Not set"
            resultLabel.ForeColor = Color.LightGray
        Next

        resultLabel.Text = ""
            startButton.Enabled = True
            revealButton.Enabled = False
            restartButton.Enabled = False

            ' Hide target panel
            Me.Controls.OfType(Of Panel).FirstOrDefault(Function(p) p.Controls.ContainsKey("targetValueLabel"))?.Hide()
    End Sub

    Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles startButton.Click
        ' Generate random target number
        targetNumber = random.Next(1, 1001)

        ' Show target panel with masked number
        Dim targetPanel = Me.Controls.OfType(Of Panel).FirstOrDefault(Function(p) p.Controls.ContainsKey("targetValueLabel"))
        If targetPanel IsNot Nothing Then
            Dim targetValueLabel = TryCast(targetPanel.Controls("targetValueLabel"), Label)
            If targetValueLabel IsNot Nothing Then
                targetValueLabel.Text = "???"
                targetValueLabel.ForeColor = Color.Gold
            End If
            targetPanel.Show()
        End If

        ' Enable reveal button
        revealButton.Enabled = True
        startButton.Enabled = False

        resultLabel.Text = "Game started! Enter your estimates and click 'Reveal Results'"
        resultLabel.ForeColor = Color.LightGreen
    End Sub

    Private Sub RevealButton_Click(sender As Object, e As EventArgs) Handles revealButton.Click
        ' Validate inputs and collect estimates
        Dim validEstimates As Boolean = True
        players.Clear()

        For i As Integer = 0 To playerInputs.Count - 1
            Dim inputBox = playerInputs(i)
            Dim estimate As Integer

            If String.IsNullOrWhiteSpace(inputBox.Text) Then
                inputBox.BackColor = Color.LightPink
                validEstimates = False
                Continue For
            ElseIf Integer.TryParse(inputBox.Text, estimate) Then
                If estimate < 1 OrElse estimate > 1000 Then
                    inputBox.BackColor = Color.LightPink
                    validEstimates = False
                    Continue For
                End If

                inputBox.BackColor = Color.LightGreen
                players.Add(New Player() With {
                    .Name = $"Player {i + 1}",
                    .Estimate = estimate,
                    .Difference = Math.Abs(estimate - targetNumber)
                })
            End If
        Next

        If Not validEstimates OrElse players.Count = 0 Then
            resultLabel.Text = "Please enter valid estimates (1-1000) for all players!"
            resultLabel.ForeColor = Color.LightCoral
            Return
        End If

        ' Find winner(s) - closest estimate
        Dim minDifference = players.Min(Function(p) p.Difference)
        Dim winners = players.Where(Function(p) p.Difference = minDifference).ToList()

        For Each winner In winners
            winner.IsWinner = True
        Next

        ' Update UI with results
        For i As Integer = 0 To players.Count - 1
            Dim player = players(i)
            Dim resultLabel = playerResultLabels(i)
            Dim diffLabel = playerPanels(i).Controls.OfType(Of Label).FirstOrDefault(Function(l) l.Name = $"playerDiff{i}")

            resultLabel.Text = player.Estimate.ToString()
            resultLabel.ForeColor = If(player.IsWinner, Color.Gold, Color.LightGray)

            If diffLabel IsNot Nothing Then
                diffLabel.Text = $"Difference: {player.Difference}"
                diffLabel.ForeColor = If(player.IsWinner, Color.Gold, Color.LightGray)
            End If

            ' Highlight winner panel
            playerPanels(i).BackColor = If(player.IsWinner,
                                          Color.FromArgb(&H2C, &H50, &H2C),
                                          Color.FromArgb(&H32, &H32, &H32))
        Next

        ' Show actual target number
        Dim targetPanel = Me.Controls.OfType(Of Panel).FirstOrDefault(Function(p) p.Controls.ContainsKey("targetValueLabel"))
        If targetPanel IsNot Nothing Then
            Dim targetValueLabel = TryCast(targetPanel.Controls("targetValueLabel"), Label)
            If targetValueLabel IsNot Nothing Then
                targetValueLabel.Text = targetNumber.ToString()
                targetValueLabel.ForeColor = Color.LightGreen
            End If
        End If

        ' Disable inputs
        For Each InputBox2 In playerInputs
            InputBox2.Enabled = False
        Next

        ' Show result message
        If winners.Count = 1 Then
            resultLabel.Text = $"WINNER: {winners(0).Name} with estimate {winners(0).Estimate} " &
                             $"(Target: {targetNumber}, Difference: {winners(0).Difference})"
            resultLabel.ForeColor = Color.Gold
        Else
            resultLabel.Text = $"TIE between {String.Join(", ", winners.Select(Function(w) w.Name))} " &
                             $"(Target: {targetNumber}, Difference: {minDifference})"
            resultLabel.ForeColor = Color.LightBlue
        End If

        revealButton.Enabled = False
        restartButton.Enabled = True
    End Sub

    Private Sub RestartButton_Click(sender As Object, e As EventArgs) Handles restartButton.Click
        InitializeGame()
    End Sub

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