Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Private WithEvents btnNewGame As Button
    Private WithEvents btnDrawCard As Button
    Private WithEvents btnQuit As Button
    Private lblStatus As Label
    Private lblDeckCount As Label
    Private lblDiscardCount As Label
    Private pnlPlayerHand As Panel
    Private pnlDiscardPile As Panel
    Private pnlDeck As Panel

    Private game As BurroGame
    Private playerCards As New List(Of CardControl)()
    Private discardCard As CardControl
    Private deckCard As CardControl

    Private Const CARD_WIDTH As Integer = 70
    Private Const CARD_HEIGHT As Integer = 100
    Private Const CARD_SPACING As Integer = 20

    Public Sub New()
        InitializeComponent()
        InitializeGameControls()
        StartNewGame()
    End Sub

    Private Sub InitializeGameControls()
        ' Set up form
        Me.Text = "Burro Card Game"
        Me.Size = New Size(900, 700)
        Me.BackColor = Color.DarkGreen

        ' Status label
        lblStatus = New Label()
        lblStatus.Location = New Point(20, 20)
        lblStatus.Size = New Size(400, 30)
        lblStatus.Font = New Font("Arial", 14, FontStyle.Bold)
        lblStatus.ForeColor = Color.White
        lblStatus.Text = "Burro Card Game"
        Me.Controls.Add(lblStatus)

        ' Deck count label
        lblDeckCount = New Label()
        lblDeckCount.Location = New Point(20, 60)
        lblDeckCount.Size = New Size(200, 20)
        lblDeckCount.Font = New Font("Arial", 10)
        lblDeckCount.ForeColor = Color.White
        Me.Controls.Add(lblDeckCount)

        ' Discard count label
        lblDiscardCount = New Label()
        lblDiscardCount.Location = New Point(20, 85)
        lblDiscardCount.Size = New Size(200, 20)
        lblDiscardCount.Font = New Font("Arial", 10)
        lblDiscardCount.ForeColor = Color.White
        Me.Controls.Add(lblDiscardCount)

        ' Deck panel
        pnlDeck = New Panel()
        pnlDeck.Location = New Point(50, 120)
        pnlDeck.Size = New Size(CARD_WIDTH, CARD_HEIGHT)
        pnlDeck.BackColor = Color.DarkBlue
        pnlDeck.BorderStyle = BorderStyle.FixedSingle
        Me.Controls.Add(pnlDeck)

        ' Discard pile panel
        pnlDiscardPile = New Panel()
        pnlDiscardPile.Location = New Point(200, 120)
        pnlDiscardPile.Size = New Size(CARD_WIDTH, CARD_HEIGHT)
        pnlDiscardPile.BackColor = Color.DarkRed
        pnlDiscardPile.BorderStyle = BorderStyle.FixedSingle
        Me.Controls.Add(pnlDiscardPile)

        ' Player hand panel
        pnlPlayerHand = New Panel()
        pnlPlayerHand.Location = New Point(50, 250)
        pnlPlayerHand.Size = New Size(800, 150)
        pnlPlayerHand.BackColor = Color.FromArgb(0, 100, 0)
        pnlPlayerHand.BorderStyle = BorderStyle.FixedSingle
        Me.Controls.Add(pnlPlayerHand)

        ' New Game button
        btnNewGame = New Button()
        btnNewGame.Location = New Point(400, 20)
        btnNewGame.Size = New Size(100, 30)
        btnNewGame.Text = "New Game"
        btnNewGame.BackColor = Color.LightBlue
        Me.Controls.Add(btnNewGame)

        ' Draw Card button
        btnDrawCard = New Button()
        btnDrawCard.Location = New Point(400, 60)
        btnDrawCard.Size = New Size(100, 30)
        btnDrawCard.Text = "Draw Card"
        btnDrawCard.BackColor = Color.LightGreen
        Me.Controls.Add(btnDrawCard)

        ' Quit button
        btnQuit = New Button()
        btnQuit.Location = New Point(400, 100)
        btnQuit.Size = New Size(100, 30)
        btnQuit.Text = "Quit"
        btnQuit.BackColor = Color.LightCoral
        Me.Controls.Add(btnQuit)
    End Sub

    Private Sub StartNewGame()
        game = New BurroGame()
        UpdateDisplay()
    End Sub

    Private Sub UpdateDisplay()
        ' Clear existing card controls
        For Each cardCtrl In playerCards
            pnlPlayerHand.Controls.Remove(cardCtrl)
        Next
        playerCards.Clear()

        If discardCard IsNot Nothing Then
            pnlDiscardPile.Controls.Remove(discardCard)
            discardCard = Nothing
        End If

        If deckCard IsNot Nothing Then
            pnlDeck.Controls.Remove(deckCard)
            deckCard = Nothing
        End If

        ' Update labels
        lblDeckCount.Text = $"Deck: {game.DeckCount} cards"
        lblDiscardCount.Text = $"Discard: {game.DiscardCount} cards"
        lblStatus.Text = game.StatusMessage

        ' Create deck card (face down)
        deckCard = New CardControl(Nothing, Nothing)
        deckCard.Location = New Point(0, 0)
        deckCard.Size = New Size(CARD_WIDTH, CARD_HEIGHT)
        deckCard.IsFaceDown = True
        AddHandler deckCard.Click, AddressOf DeckCard_Click
        pnlDeck.Controls.Add(deckCard)

        ' Create discard pile card
        If game.TopDiscardCard IsNot Nothing Then
            discardCard = New CardControl(game.TopDiscardCard.Suit, game.TopDiscardCard.Rank)
            discardCard.Location = New Point(0, 0)
            discardCard.Size = New Size(CARD_WIDTH, CARD_HEIGHT)
            pnlDiscardPile.Controls.Add(discardCard)
        End If

        ' Create player's hand cards
        Dim xPos As Integer = 10
        For i As Integer = 0 To game.PlayerHand.Count - 1
            Dim card = game.PlayerHand(i)
            Dim cardCtrl As New CardControl(card.Suit, card.Rank)
            cardCtrl.Location = New Point(xPos, 10)
            cardCtrl.Size = New Size(CARD_WIDTH, CARD_HEIGHT)
            cardCtrl.Tag = i ' Store card index
            AddHandler cardCtrl.Click, AddressOf PlayerCard_Click
            playerCards.Add(cardCtrl)
            pnlPlayerHand.Controls.Add(cardCtrl)
            xPos += CARD_WIDTH + CARD_SPACING
        Next

        ' Enable/disable buttons based on game state
        btnDrawCard.Enabled = game.CanDrawCard
    End Sub

    Private Sub PlayerCard_Click(sender As Object, e As EventArgs)
        If TypeOf sender Is CardControl Then
            Dim cardCtrl As CardControl = DirectCast(sender, CardControl)
            Dim cardIndex As Integer = CInt(cardCtrl.Tag)

            If game.PlayCard(cardIndex) Then
                UpdateDisplay()
                CheckGameEnd()
            Else
                lblStatus.Text = "Invalid move! " & game.StatusMessage
            End If
        End If
    End Sub

    Private Sub DeckCard_Click(sender As Object, e As EventArgs)
        If game.DrawCard() Then
            UpdateDisplay()
        End If
    End Sub

    Private Sub CheckGameEnd()
        If game.IsGameOver Then
            Dim result As DialogResult = MessageBox.Show(
                $"Game Over! {game.StatusMessage}" & vbCrLf & "Start a new game?",
                "Game Finished",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Information)

            If result = DialogResult.Yes Then
                StartNewGame()
            End If
        End If
    End Sub

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

    Private Sub btnDrawCard_Click(sender As Object, e As EventArgs) Handles btnDrawCard.Click
        If game.DrawCard() Then
            UpdateDisplay()
        End If
    End Sub

    Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
        Me.Close()
    End Sub
End Class

Public Class CardControl
    Inherits Control

    Private _suit As String
    Private _rank As String
    Private _isFaceDown As Boolean = False

    Public Property Suit As String
        Get
            Return _suit
        End Get
        Set(value As String)
            _suit = value
            Invalidate()
        End Set
    End Property

    Public Property Rank As String
        Get
            Return _rank
        End Get
        Set(value As String)
            _rank = value
            Invalidate()
        End Set
    End Property

    Public Property IsFaceDown As Boolean
        Get
            Return _isFaceDown
        End Get
        Set(value As Boolean)
            _isFaceDown = value
            Invalidate()
        End Set
    End Property

    Public Sub New(suit As String, rank As String)
        Me.Suit = suit
        Me.Rank = rank
        Me.Size = New Size(70, 100)
        Me.BackColor = Color.White
        Me.Cursor = Cursors.Hand
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)

        If _isFaceDown Then
            ' Draw face down card
            e.Graphics.FillRectangle(Brushes.Blue, 0, 0, Width, Height)
            e.Graphics.DrawRectangle(Pens.White, 0, 0, Width - 1, Height - 1)

            ' Draw card back pattern
            Using font As New Font("Arial", 8)
                e.Graphics.DrawString("BURRO", font, Brushes.Yellow, 5, 40)
            End Using
        Else
            ' Draw face up card
            e.Graphics.FillRectangle(Brushes.White, 0, 0, Width, Height)
            e.Graphics.DrawRectangle(Pens.Black, 0, 0, Width - 1, Height - 1)

            ' Determine card color
            Dim cardColor As Brush
            If _suit = "♥" OrElse _suit = "♦" Then
                cardColor = Brushes.Red
            Else
                cardColor = Brushes.Black
            End If

            ' Draw rank and suit
            Using font As New Font("Arial", 14, FontStyle.Bold)
                e.Graphics.DrawString(_rank, font, cardColor, 5, 5)
                e.Graphics.DrawString(_suit, font, cardColor, 5, 25)
            End Using

            ' Draw larger suit in center
            Using largeFont As New Font("Arial", 24, FontStyle.Bold)
                e.Graphics.DrawString(_suit, largeFont, cardColor, 20, 40)
            End Using
        End If
    End Sub
End Class

Public Class Card
    Public Property Suit As String
    Public Property Rank As String
    Public Property Value As Integer

    Public Sub New(suit As String, rank As String, value As Integer)
        Me.Suit = suit
        Me.Rank = rank
        Me.Value = value
    End Sub

    Public Overrides Function ToString() As String
        Return $"{Rank} of {Suit}"
    End Function
End Class

Public Class BurroGame
    Private deck As New List(Of Card)()
    Private discardPile As New List(Of Card)()
    Private playerHand2 As New List(Of Card)()
    Private currentSuit As String
    Private currentRank As String
    Private gameStatus As String
    Private random As New Random()

    Public ReadOnly Property PlayerHand As List(Of Card)
        Get
            Return playerHand2
        End Get
    End Property

    Public ReadOnly Property TopDiscardCard As Card
        Get
            If discardPile.Count > 0 Then
                Return discardPile(discardPile.Count - 1)
            End If
            Return Nothing
        End Get
    End Property

    Public ReadOnly Property DeckCount As Integer
        Get
            Return deck.Count
        End Get
    End Property

    Public ReadOnly Property DiscardCount As Integer
        Get
            Return discardPile.Count
        End Get
    End Property

    Public ReadOnly Property StatusMessage As String
        Get
            Return gameStatus
        End Get
    End Property

    Public ReadOnly Property CanDrawCard As Boolean
        Get
            Return deck.Count > 0
        End Get
    End Property

    Public ReadOnly Property IsGameOver As Boolean
        Get
            Return playerHand.Count = 0 OrElse deck.Count = 0
        End Get
    End Property

    Public Sub New()
        InitializeDeck()
        ShuffleDeck()
        DealCards()
        StartGame()
    End Sub

    Private Sub InitializeDeck()
        deck.Clear()
        Dim suits() As String = {"♥", "♦", "♣", "♠"}
        Dim ranks() As String = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}

        For Each suit In suits
            For i As Integer = 0 To ranks.Length - 1
                Dim value As Integer = i + 1
                deck.Add(New Card(suit, ranks(i), value))
            Next
        Next
    End Sub

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

    Private Sub DealCards()
        playerHand.Clear()

        ' Deal 7 cards to player
        For i As Integer = 1 To 7
            If deck.Count > 0 Then
                playerHand.Add(deck(0))
                deck.RemoveAt(0)
            End If
        Next
    End Sub

    Private Sub StartGame()
        ' Place first card on discard pile
        If deck.Count > 0 Then
            discardPile.Add(deck(0))
            deck.RemoveAt(0)
            currentSuit = TopDiscardCard.Suit
            currentRank = TopDiscardCard.Rank
        End If

        gameStatus = "Your turn! Play a card or draw from the deck."
    End Sub

    Public Function PlayCard(cardIndex As Integer) As Boolean
        If cardIndex < 0 OrElse cardIndex >= playerHand.Count Then
            gameStatus = "Invalid card selection!"
            Return False
        End If

        Dim card As Card = playerHand(cardIndex)

        ' Check if card can be played (same suit or rank, or is an 8)
        If card.Suit = currentSuit OrElse card.Rank = currentRank OrElse card.Rank = "8" Then
            ' Remove from hand and add to discard pile
            playerHand.RemoveAt(cardIndex)
            discardPile.Add(card)

            ' Update current suit/rank
            currentSuit = card.Suit
            currentRank = card.Rank

            ' If 8 was played, player chooses suit (simplified - auto chooses suit of next card)
            If card.Rank = "8" Then
                ' In a real game, you'd ask the player to choose a suit
                ' For simplicity, we'll use the suit of the first card in hand
                If playerHand.Count > 0 Then
                    currentSuit = playerHand(0).Suit
                End If
                gameStatus = "Eight played! Suit changed to " & currentSuit
            Else
                gameStatus = "Card played: " & card.ToString()
            End If

            Return True
        Else
            gameStatus = "Cannot play " & card.ToString() & ". Must match " & currentSuit & " or " & currentRank & " or play an 8."
            Return False
        End If
    End Function

    Public Function DrawCard() As Boolean
        If deck.Count = 0 Then
            ' Reshuffle discard pile (except top card) into deck
            If discardPile.Count > 1 Then
                Dim topCard As Card = discardPile(discardPile.Count - 1)
                discardPile.RemoveAt(discardPile.Count - 1)

                deck.AddRange(discardPile)
                discardPile.Clear()
                discardPile.Add(topCard)
                ShuffleDeck()
                gameStatus = "Deck reshuffled!"
            Else
                gameStatus = "No cards left to draw!"
                Return False
            End If
        End If

        If deck.Count > 0 Then
            playerHand.Add(deck(0))
            deck.RemoveAt(0)
            gameStatus = "Card drawn. Your turn!"
            Return True
        End If

        Return False
    End Function
End Class

