Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    ' Game variables
    Private WithEvents btnNewGame As Button
    Private WithEvents btnDrawCard As Button
    Private WithEvents btnPlayCard As Button
    Private lblPlayerHand As Label
    Private lblDiscardPile As Label
    Private lblDeck As Label
    Private lblStatus As Label

    Private playerHandPanel As Panel
    Private discardPilePanel As Panel
    Private deckPanel As Panel

    Private playerCards As New List(Of Card)
    Private discardPile As New List(Of Card)
    Private deck As New List(Of Card)

    Private selectedCardIndex As Integer = -1

    ' Card suits and values
    Private suits() As String = {"♠", "♥", "♦", "♣"}
    Private values() As String = {"7", "8", "9", "10", "J", "Q", "K", "A"}

    Private Class Card
        Public Property Suit As String
        Public Property Value As String
        Public Property Color As Color
        Public Property Panel As Panel

        Public Overrides Function ToString() As String
            Return Value & Suit
        End Function
    End Class

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

    Private Sub InitializeForm()
        Me.Text = "Mau-Mau Card Game"
        Me.Size = New Size(900, 700)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.BackColor = Color.DarkGreen
    End Sub

    Private Sub CreateControls()
        ' Title label
        Dim titleLabel As New Label With {
            .Text = "♠♥♦♣ MAU-MAU ♣♦♥♠",
            .Font = New Font("Arial", 24, FontStyle.Bold),
            .ForeColor = Color.Gold,
            .Size = New Size(400, 40),
            .Location = New Point(250, 10),
            .TextAlign = ContentAlignment.MiddleCenter
        }
        Me.Controls.Add(titleLabel)

        ' Status label
        lblStatus = New Label With {
            .Text = "Welcome to Mau-Mau! Click 'New Game' to start.",
            .Font = New Font("Arial", 12, FontStyle.Bold),
            .ForeColor = Color.White,
            .Size = New Size(800, 30),
            .Location = New Point(50, 60),
            .TextAlign = ContentAlignment.MiddleCenter
        }
        Me.Controls.Add(lblStatus)

        ' Deck panel and label
        lblDeck = New Label With {
            .Text = "DECK",
            .Font = New Font("Arial", 10, FontStyle.Bold),
            .ForeColor = Color.White,
            .Size = New Size(100, 20),
            .Location = New Point(100, 120),
            .TextAlign = ContentAlignment.MiddleCenter
        }
        Me.Controls.Add(lblDeck)

        deckPanel = New Panel With {
            .Size = New Size(80, 120),
            .Location = New Point(110, 150),
            .BackColor = Color.DarkBlue,
            .BorderStyle = BorderStyle.FixedSingle
        }
        Me.Controls.Add(deckPanel)

        ' Discard pile panel and label
        lblDiscardPile = New Label With {
            .Text = "DISCARD PILE",
            .Font = New Font("Arial", 10, FontStyle.Bold),
            .ForeColor = Color.White,
            .Size = New Size(120, 20),
            .Location = New Point(300, 120),
            .TextAlign = ContentAlignment.MiddleCenter
        }
        Me.Controls.Add(lblDiscardPile)

        discardPilePanel = New Panel With {
            .Size = New Size(80, 120),
            .Location = New Point(320, 150),
            .BackColor = Color.DarkRed,
            .BorderStyle = BorderStyle.FixedSingle
        }
        Me.Controls.Add(discardPilePanel)

        ' Player hand label
        lblPlayerHand = New Label With {
            .Text = "YOUR HAND",
            .Font = New Font("Arial", 12, FontStyle.Bold),
            .ForeColor = Color.White,
            .Size = New Size(200, 30),
            .Location = New Point(350, 350),
            .TextAlign = ContentAlignment.MiddleCenter
        }
        Me.Controls.Add(lblPlayerHand)

        ' Player hand panel
        playerHandPanel = New Panel With {
            .Size = New Size(800, 150),
            .Location = New Point(50, 380),
            .BackColor = Color.FromArgb(0, 50, 0),
            .BorderStyle = BorderStyle.FixedSingle,
            .AutoScroll = True
        }
        Me.Controls.Add(playerHandPanel)

        ' Buttons
        btnNewGame = New Button With {
            .Text = "New Game",
            .Size = New Size(120, 40),
            .Location = New Point(100, 300),
            .Font = New Font("Arial", 10, FontStyle.Bold),
            .BackColor = Color.SteelBlue,
            .ForeColor = Color.White
        }
        Me.Controls.Add(btnNewGame)

        btnDrawCard = New Button With {
            .Text = "Draw Card",
            .Size = New Size(120, 40),
            .Location = New Point(250, 300),
            .Font = New Font("Arial", 10, FontStyle.Bold),
            .BackColor = Color.SteelBlue,
            .ForeColor = Color.White,
            .Enabled = False
        }
        Me.Controls.Add(btnDrawCard)

        btnPlayCard = New Button With {
            .Text = "Play Card",
            .Size = New Size(120, 40),
            .Location = New Point(400, 300),
            .Font = New Font("Arial", 10, FontStyle.Bold),
            .BackColor = Color.SteelBlue,
            .ForeColor = Color.White,
            .Enabled = False
        }
        Me.Controls.Add(btnPlayCard)
    End Sub

    Private Sub InitializeGame()
        ' Create deck
        deck.Clear()
        For Each suit In suits
            Dim cardColor As Color = If(suit = "♥" OrElse suit = "♦", Color.Red, Color.Black)
            For Each value In values
                deck.Add(New Card With {.Suit = suit, .Value = value, .Color = cardColor})
            Next
        Next

        ' Shuffle deck
        ShuffleDeck()

        ' Deal initial cards (5 to player)
        playerCards.Clear()
        For i As Integer = 1 To 5
            If deck.Count > 0 Then
                playerCards.Add(deck(0))
                deck.RemoveAt(0)
            End If
        Next

        ' Start discard pile
        discardPile.Clear()
        If deck.Count > 0 Then
            discardPile.Add(deck(0))
            deck.RemoveAt(0)
        End If

        UpdateDisplay()
        UpdateStatus("Game started! Select a card from your hand.")
    End Sub

    Private Sub ShuffleDeck()
        Dim rnd As New Random()
        For i As Integer = deck.Count - 1 To 1 Step -1
            Dim j As Integer = rnd.Next(0, i + 1)
            Dim temp As Card = deck(i)
            deck(i) = deck(j)
            deck(j) = temp
        Next
    End Sub

    Private Sub UpdateDisplay()
        ' Clear panels
        playerHandPanel.Controls.Clear()
        discardPilePanel.Controls.Clear()
        deckPanel.Controls.Clear()

        ' Display deck count
        Dim deckLabel As New Label With {
            .Text = deck.Count.ToString() & vbCrLf & "Cards",
            .Font = New Font("Arial", 10, FontStyle.Bold),
            .ForeColor = Color.White,
            .Size = New Size(80, 120),
            .TextAlign = ContentAlignment.MiddleCenter
        }
        deckPanel.Controls.Add(deckLabel)

        ' Display top discard card
        If discardPile.Count > 0 Then
            DisplayCard(discardPile.Last(), discardPilePanel, False)
        End If

        ' Display player's hand
        Dim xPos As Integer = 10
        For i As Integer = 0 To playerCards.Count - 1
            Dim cardPanel As Panel = CreateCardPanel(playerCards(i), i)
            cardPanel.Location = New Point(xPos, 10)
            playerHandPanel.Controls.Add(cardPanel)
            xPos += 90
        Next

        ' Update button states
        btnDrawCard.Enabled = (deck.Count > 0)
        btnPlayCard.Enabled = (selectedCardIndex >= 0 AndAlso selectedCardIndex < playerCards.Count)
    End Sub

    Private Function CreateCardPanel(card As Card, index As Integer) As Panel
        Dim panel As New Panel With {
            .Size = New Size(80, 120),
            .BackColor = Color.White,
            .BorderStyle = BorderStyle.FixedSingle,
            .Tag = index
        }

        ' Add suit label
        Dim suitLabel As New Label With {
            .Text = card.Suit,
            .Font = New Font("Arial", 24, FontStyle.Bold),
            .ForeColor = card.Color,
            .Size = New Size(40, 40),
            .Location = New Point(20, 40),
            .TextAlign = ContentAlignment.MiddleCenter
        }
        panel.Controls.Add(suitLabel)

        ' Add value label
        Dim valueLabel As New Label With {
            .Text = card.Value,
            .Font = New Font("Arial", 14, FontStyle.Bold),
            .ForeColor = card.Color,
            .Size = New Size(40, 20),
            .Location = New Point(20, 10),
            .TextAlign = ContentAlignment.MiddleCenter
        }
        panel.Controls.Add(valueLabel)

        ' Add click event
        AddHandler panel.Click, AddressOf CardPanel_Click

        ' Highlight if selected
        If index = selectedCardIndex Then
            panel.BackColor = Color.LightYellow
            panel.BorderStyle = BorderStyle.Fixed3D
        End If

        Return panel
    End Function

    Private Sub DisplayCard(card As Card, container As Panel, interactive As Boolean)
        Dim panel As New Panel With {
            .Size = New Size(80, 120),
            .BackColor = Color.White,
            .BorderStyle = BorderStyle.FixedSingle
        }

        Dim suitLabel As New Label With {
            .Text = card.Suit,
            .Font = New Font("Arial", 24, FontStyle.Bold),
            .ForeColor = card.Color,
            .Size = New Size(40, 40),
            .Location = New Point(20, 40),
            .TextAlign = ContentAlignment.MiddleCenter
        }
        panel.Controls.Add(suitLabel)

        Dim valueLabel As New Label With {
            .Text = card.Value,
            .Font = New Font("Arial", 14, FontStyle.Bold),
            .ForeColor = card.Color,
            .Size = New Size(40, 20),
            .Location = New Point(20, 10),
            .TextAlign = ContentAlignment.MiddleCenter
        }
        panel.Controls.Add(valueLabel)

        container.Controls.Add(panel)
    End Sub

    Private Sub CardPanel_Click(sender As Object, e As EventArgs)
        Dim panel As Panel = DirectCast(sender, Panel)
        selectedCardIndex = CInt(panel.Tag)
        UpdateDisplay()
        UpdateStatus("Card selected: " & playerCards(selectedCardIndex).ToString())
    End Sub

    Private Sub UpdateStatus(message As String)
        lblStatus.Text = message
    End Sub

    Private Function CanPlayCard(card As Card) As Boolean
        If discardPile.Count = 0 Then Return True

        Dim topCard As Card = discardPile.Last()

        ' Mau-Mau rules: Can play if same suit OR same value
        Return card.Suit = topCard.Suit OrElse card.Value = topCard.Value
    End Function

    ' Event Handlers
    Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles btnNewGame.Click
        InitializeGame()
    End Sub

    Private Sub btnDrawCard_Click(sender As Object, e As EventArgs) Handles btnDrawCard.Click
        If deck.Count > 0 Then
            playerCards.Add(deck(0))
            deck.RemoveAt(0)
            selectedCardIndex = playerCards.Count - 1
            UpdateDisplay()
            UpdateStatus("You drew a card. Select it to play.")
        Else
            UpdateStatus("Deck is empty!")
        End If
    End Sub

    Private Sub btnPlayCard_Click(sender As Object, e As EventArgs) Handles btnPlayCard.Click
        If selectedCardIndex >= 0 AndAlso selectedCardIndex < playerCards.Count Then
            Dim cardToPlay As Card = playerCards(selectedCardIndex)

            If CanPlayCard(cardToPlay) Then
                ' Move card to discard pile
                discardPile.Add(cardToPlay)
                playerCards.RemoveAt(selectedCardIndex)
                selectedCardIndex = -1

                ' Check for win
                If playerCards.Count = 0 Then
                    UpdateStatus("Congratulations! You won! Click 'New Game' to play again.")
                    btnPlayCard.Enabled = False
                    btnDrawCard.Enabled = False
                Else
                    UpdateStatus("Card played: " & cardToPlay.ToString())
                End If

                UpdateDisplay()
            Else
                UpdateStatus("Cannot play " & cardToPlay.ToString() & " on " & discardPile.Last().ToString())
            End If
        End If
    End Sub
End Class
