Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Private players As New List(Of Player)()
    Private deck As New List(Of Card)()
    Private currentPlayerIndex As Integer = 0
    Private trickCards As New List(Of Card)()
    Private WithEvents btnStartGame As Button
    Private WithEvents btnDealCards As Button
    Private WithEvents btnPlayCard As Button
    Private WithEvents lblStatus As Label
    Private playerPanels As New List(Of Panel)()
    Private selectedCardIndex As Integer = -1

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

    Private Sub InitializeGame()
        ' Initialize 4 players
        players.Add(New Player("Player 1"))
        players.Add(New Player("Player 2"))
        players.Add(New Player("Player 3"))
        players.Add(New Player("Player 4"))

        ' Create deck of 32 cards (standard Rikken deck)
        Dim suits() As String = {"Hearts", "Diamonds", "Clubs", "Spades"}
        Dim ranks() As String = {"7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}

        For Each suit In suits
            For Each rank In ranks
                deck.Add(New Card(rank, suit))
            Next
        Next
    End Sub

    Private Sub SetupForm()
        Me.Text = "Rikken Dutch Card Game"
        Me.Size = New Size(1200, 800)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.BackColor = Color.DarkGreen
    End Sub

    Private Sub CreateControls()
        ' Status label
        lblStatus = New Label With {
            .Text = "Welcome to Rikken! Click 'Start Game' to begin.",
            .Location = New Point(20, 20),
            .Size = New Size(1140, 30),
            .Font = New Font("Arial", 12, FontStyle.Bold),
            .ForeColor = Color.White,
            .TextAlign = ContentAlignment.MiddleCenter
        }
        Me.Controls.Add(lblStatus)

        ' Start Game button
        btnStartGame = New Button With {
            .Text = "Start Game",
            .Location = New Point(20, 60),
            .Size = New Size(150, 40),
            .Font = New Font("Arial", 10),
            .BackColor = Color.LightBlue
        }
        Me.Controls.Add(btnStartGame)

        ' Deal Cards button
        btnDealCards = New Button With {
            .Text = "Deal Cards",
            .Location = New Point(180, 60),
            .Size = New Size(150, 40),
            .Font = New Font("Arial", 10),
            .BackColor = Color.LightGreen,
            .Enabled = False
        }
        Me.Controls.Add(btnDealCards)

        ' Play Card button
        btnPlayCard = New Button With {
            .Text = "Play Selected Card",
            .Location = New Point(340, 60),
            .Size = New Size(150, 40),
            .Font = New Font("Arial", 10),
            .BackColor = Color.LightCoral,
            .Enabled = False
        }
        Me.Controls.Add(btnPlayCard)

        ' Create player panels
        Dim panelLocations() As Point = {
            New Point(100, 400),   ' Bottom
            New Point(100, 100),   ' Top
            New Point(900, 100),   ' Right
            New Point(900, 400)    ' Left
        }

        Dim panelNames() As String = {"Player 1 (You)", "Player 2", "Player 3", "Player 4"}

        For i As Integer = 0 To 3
            Dim panel As New Panel With {
                .Location = panelLocations(i),
                .Size = New Size(200, 200),
                .BackColor = Color.FromArgb(40, 40, 40),
                .BorderStyle = BorderStyle.FixedSingle
            }

            Dim lbl As New Label With {
                .Text = panelNames(i),
                .Location = New Point(10, 10),
                .Size = New Size(180, 25),
                .Font = New Font("Arial", 10, FontStyle.Bold),
                .ForeColor = Color.White,
                .TextAlign = ContentAlignment.MiddleCenter
            }
            panel.Controls.Add(lbl)

            Dim cardsLbl As New Label With {
                .Name = "cardsLabel",
                .Text = "Cards: 0",
                .Location = New Point(10, 40),
                .Size = New Size(180, 25),
                .Font = New Font("Arial", 9),
                .ForeColor = Color.LightGray,
                .TextAlign = ContentAlignment.MiddleLeft
            }
            panel.Controls.Add(cardsLbl)

            Dim scoreLbl As New Label With {
                .Name = "scoreLabel",
                .Text = "Score: 0",
                .Location = New Point(10, 70),
                .Size = New Size(180, 25),
                .Font = New Font("Arial", 9),
                .ForeColor = Color.LightGray,
                .TextAlign = ContentAlignment.MiddleLeft
            }
            panel.Controls.Add(scoreLbl)

            playerPanels.Add(panel)
            Me.Controls.Add(panel)
        Next

        ' Create trick area panel
        Dim trickPanel As New Panel With {
            .Location = New Point(400, 250),
            .Size = New Size(400, 150),
            .BackColor = Color.FromArgb(60, 60, 60),
            .BorderStyle = BorderStyle.Fixed3D
        }

        Dim trickLbl As New Label With {
            .Text = "Current Trick",
            .Location = New Point(10, 10),
            .Size = New Size(380, 25),
            .Font = New Font("Arial", 11, FontStyle.Bold),
            .ForeColor = Color.White,
            .TextAlign = ContentAlignment.MiddleCenter
        }
        trickPanel.Controls.Add(trickLbl)

        Me.Controls.Add(trickPanel)
    End Sub

    Private Sub btnStartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
        ResetGame()
        btnDealCards.Enabled = True
        lblStatus.Text = "Game started! Click 'Deal Cards' to begin."
    End Sub

    Private Sub btnDealCards_Click(sender As Object, e As EventArgs) Handles btnDealCards.Click
        ShuffleDeck()
        DealCards()
        btnDealCards.Enabled = False
        btnPlayCard.Enabled = True
        lblStatus.Text = "Cards dealt! Player 1's turn. Select a card to play."
        UpdatePlayerDisplays()
    End Sub

    Private Sub btnPlayCard_Click(sender As Object, e As EventArgs) Handles btnPlayCard.Click
        If selectedCardIndex = -1 Then
            MessageBox.Show("Please select a card first!", "No Card Selected", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        PlayCard(selectedCardIndex)
        selectedCardIndex = -1
        UpdatePlayerDisplays()

        ' Check if round is over
        If players.All(Function(p) p.Hand.Count = 0) Then
            CalculateScores()
            lblStatus.Text = "Round over! Click 'Start Game' to play again."
            btnPlayCard.Enabled = False
            btnStartGame.Enabled = True
        Else
            currentPlayerIndex = (currentPlayerIndex + 1) Mod 4
            lblStatus.Text = players(currentPlayerIndex).Name & "'s turn"
        End If
    End Sub

    Private Sub ResetGame()
        ' Clear all hands
        For Each player In players
            player.Hand.Clear()
            player.Score = 0
        Next

        ' Reset deck
        deck.Clear()
        InitializeGame()

        ' Reset trick
        trickCards.Clear()

        ' Reset UI
        currentPlayerIndex = 0
        selectedCardIndex = -1
        UpdatePlayerDisplays()
    End Sub

    Private Sub ShuffleDeck()
        Dim rnd As New Random()
        deck = deck.OrderBy(Function(x) rnd.Next()).ToList()
    End Sub

    Private Sub DealCards()
        ' Deal 8 cards to each player
        For i As Integer = 0 To 7
            For Each player In players
                If deck.Count > 0 Then
                    player.Hand.Add(deck(0))
                    deck.RemoveAt(0)
                End If
            Next
        Next
    End Sub

    Private Sub PlayCard(cardIndex As Integer)
        Dim currentPlayer = players(currentPlayerIndex)

        If cardIndex >= 0 AndAlso cardIndex < currentPlayer.Hand.Count Then
            Dim card = currentPlayer.Hand(cardIndex)
            trickCards.Add(card)
            currentPlayer.Hand.RemoveAt(cardIndex)

            ' Update trick display
            UpdateTrickDisplay()
        End If
    End Sub

    Private Sub UpdateTrickDisplay()
        ' Find trick panel
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is Panel AndAlso ctrl.Location = New Point(400, 250) Then
                Dim trickPanel As Panel = DirectCast(ctrl, Panel)
                trickPanel.Controls.Clear()

                Dim trickLbl As New Label With {
                    .Text = "Current Trick (" & trickCards.Count & " cards)",
                    .Location = New Point(10, 10),
                    .Size = New Size(380, 25),
                    .Font = New Font("Arial", 11, FontStyle.Bold),
                    .ForeColor = Color.White,
                    .TextAlign = ContentAlignment.MiddleCenter
                }
                trickPanel.Controls.Add(trickLbl)

                ' Display cards in trick
                Dim xPos As Integer = 20
                For Each card In trickCards
                    Dim cardLbl As New Label With {
                        .Text = card.ToString(),
                        .Location = New Point(xPos, 50),
                        .Size = New Size(80, 30),
                        .Font = New Font("Arial", 9),
                        .ForeColor = GetCardColor(card),
                        .BackColor = Color.White,
                        .BorderStyle = BorderStyle.FixedSingle,
                        .TextAlign = ContentAlignment.MiddleCenter
                    }
                    trickPanel.Controls.Add(cardLbl)
                    xPos += 85
                Next

                Exit For
            End If
        Next
    End Sub

    Private Sub UpdatePlayerDisplays()
        For i As Integer = 0 To playerPanels.Count - 1
            Dim panel = playerPanels(i)
            Dim player = players(i)

            ' Update cards label
            For Each ctrl In panel.Controls
                If ctrl.Name = "cardsLabel" Then
                    DirectCast(ctrl, Label).Text = "Cards: " & player.Hand.Count
                ElseIf ctrl.Name = "scoreLabel" Then
                    DirectCast(ctrl, Label).Text = "Score: " & player.Score
                End If
            Next

            ' Clear existing card buttons
            Dim buttonsToRemove As New List(Of Control)
            For Each ctrl In panel.Controls
                If TypeOf ctrl Is Button AndAlso ctrl.Name.StartsWith("cardBtn_") Then
                    buttonsToRemove.Add(ctrl)
                End If
            Next

            For Each btn In buttonsToRemove
                panel.Controls.Remove(btn)
                btn.Dispose()
            Next

            ' Add card buttons for player 1 (human player)
            If i = 0 Then
                Dim yPos As Integer = 100
                For cardIndex As Integer = 0 To player.Hand.Count - 1
                    Dim card = player.Hand(cardIndex)
                    Dim cardBtn As New Button With {
                        .Name = "cardBtn_" & cardIndex,
                        .Text = card.ToString(),
                        .Location = New Point(20, yPos),
                        .Size = New Size(160, 25),
                        .Font = New Font("Arial", 9),
                        .ForeColor = GetCardColor(card),
                        .BackColor = Color.White,
                        .Tag = cardIndex
                    }
                    AddHandler cardBtn.Click, AddressOf CardButton_Click
                    panel.Controls.Add(cardBtn)
                    yPos += 30
                Next
            End If
        Next
    End Sub

    Private Sub CardButton_Click(sender As Object, e As EventArgs)
        Dim btn = DirectCast(sender, Button)
        selectedCardIndex = CInt(btn.Tag)

        ' Highlight selected card
        For Each panel In playerPanels
            For Each ctrl In panel.Controls
                If TypeOf ctrl Is Button AndAlso ctrl.Name.StartsWith("cardBtn_") Then
                    Dim button = DirectCast(ctrl, Button)
                    button.BackColor = If(CInt(button.Tag) = selectedCardIndex, Color.Yellow, Color.White)
                End If
            Next
        Next

        lblStatus.Text = "Selected: " & players(0).Hand(selectedCardIndex).ToString() & " - Click 'Play Selected Card'"
    End Sub

    Private Sub CalculateScores()
        ' Simple scoring for demonstration
        For Each player In players
            player.Score += player.Hand.Count * 10 ' Each card = 10 points
        Next
    End Sub

    Private Function GetCardColor(card As Card) As Color
        Return If(card.Suit = "Hearts" Or card.Suit = "Diamonds", Color.Red, Color.Black)
    End Function
End Class

Public Class Player
    Public Property Name As String
    Public Property Hand As New List(Of Card)()
    Public Property Score As Integer

    Public Sub New(name As String)
        Me.Name = name
    End Sub
End Class

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

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

    Public Overrides Function ToString() As String
        Return Rank & " of " & Suit
    End Function
End Class

Module Program
    <STAThread>
    Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New Form1())
    End Sub
End Module
