Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I started a redesign of my Schafkopf (german) cards game.

While I'm implementing large parts of Graeme_Grant's cards framework
Cards game - data binding issue[^]
I'm also looking for ideas how my game rules could be changed to OOP conform game rules.

In my game the user plays against the pc which controls the other 3 players.
The rules I have now are
if - then - elseif
based and ended up in > 1000 lines which look like Spaghetti code.

What I have tried:

Found only a python game with rules for automatic game mode - but this is too complex for me to understand.
https://github.com/tobiasemrich/SchafkopfRL

UPDATE 1:

Now I've implemented parts of Graeme_Grant's cards framework and use the HandExtensions Module (which works together with the HandCards class) as a place where game rules are placed.
In this module LinQ queries can run which is much better than if - then statements.
Examples: GetHighestCard or GetLowestCard

Here is an example function of the remaing code behind the MainWindow:

Private Function SelectBestFirstCard(ByVal CardsPanel As Object, PlayerID As Integer, DeclarerID As Integer, GameStatus As Object, sHandCards As Object, TrumpCardID As Integer, CorrectCards As Object, MyForm As MainWindow, LeadSuitID As Integer) As PlayingCard 'DataGridViewCell

           Dim sk As New Schafkopf
           Dim CurrentCard As PlayingCard
           Dim bTeamDeclarer As Boolean
           Dim bTeamOpponent As Boolean

           If MyForm.GameOver = True Then Exit Function
           If GameStatus.ToString = "SpielAus" Then MyForm.GameOver = True
           If GameStatus.ToString = "SpielAus" Then Exit Function

           iRufAsOwner = -1
           If MyForm.RufAs.CardOwner = CardOwner.North Then iRufAsOwner = 0
           If MyForm.RufAs.CardOwner = CardOwner.East Then iRufAsOwner = 1
           If MyForm.RufAs.CardOwner = CardOwner.South Then iRufAsOwner = 2
           If MyForm.RufAs.CardOwner = CardOwner.West Then iRufAsOwner = 3
           If MyForm.GameModus = GameMode.Solo Then iRufAsOwner = -1
           If MyForm.GameModus = GameMode.Wenz Then iRufAsOwner = -1

           If PlayerID <> DeclarerID AndAlso PlayerID <> MyForm.iCoSpieler AndAlso PlayerID <> iRufAsOwner Then bTeamOpponent = True
           If iRufAsOwner = PlayerID Or PlayerID = DeclarerID Or PlayerID = MyForm.iCoSpieler Then bTeamDeclarer = True

           Debug.Print("ACP 323 bTeamDeclarer = " & bTeamDeclarer)

           With CardsPanel.Items

               'PlayerID = DeclarerID Or PlayerID = MyForm.iCoSpieler
               If .Count > 0 AndAlso bTeamDeclarer = True Then
                   For n = 0 To .Count - 1
                       If sHandCards.ToString.Contains("A") = True AndAlso DeclarerID = PlayerID Then
                           If MyForm.iTricks = 6 Or MyForm.iTricks = 7 Or MyForm.iTricks = 8 Then
                               If SearchInCardsPanel(11, CardsPanel).CardType <> TrumpCardID Then
                                   CurrentCard = SearchInCardsPanel(11, CardsPanel)
                               End If
                           End If
                       Else
                           If hc.GetHighestCard(LeadSuitID, TrumpCardID) IsNot Nothing Then
                               If .Item(n).ToString = hc.GetHighestCard(LeadSuitID, TrumpCardID).ToString Then
                                   CurrentCard = CardsPanel.Items.Item(n)
                                   Debug.Print("ACP 362 CurrentCard: " & CurrentCard.CardShortName & " " & CurrentCard.CardSymbol)
                               End If
                           End If
                       End If
                   Next
               End If

               If .Count > 0 AndAlso bTeamDeclarer = True Then
                   For n = 0 To .Count - 1

                       Dim sO As String
                       If TrumpCardID = 4 Then sO = "X"

                       If TrumpCardID <> 4 Then
                           sO = "O"
                       Else
                           sO = "X"
                       End If

                       If .Item(n).CardShortName.Contains(sO) Or .Item(n).CardShortName.Contains("U") Then

                           If MyForm.bPlayHighestTrump = True Then
                               MyForm.bPlayHighestTrump = False
                               CurrentCard = .Item(n)
                               LeadSuitID = TrumpCardID
                               sk.leadSuit = TrumpCardID
                               Return CurrentCard
                           Else
                               CurrentCard = .Item(n)
                               LeadSuitID = TrumpCardID
                               sk.leadSuit = TrumpCardID
                               MyForm.bPlayHighestTrump = True
                           End If

                       End If
                   Next
               End If

               ' RufAs Suchen (wenn nicht CoSpieler)
               If bTeamOpponent = True Then

                   If .Count > 0 Then
                       For n = 0 To .Count - 1

                           ' 8th July
                           If .Item(n).CardType = LeadSuitID AndAlso .Item(n).CardType <> TrumpCardID Then
                               'If .Item(n).CardType = LeadSuitID Then

                               Dim sO As String
                               If TrumpCardID = 4 Then sO = "X"
                               If TrumpCardID <> 4 Then sO = "O"
                               If .Item(n).CardShortName.Contains(sO) = False AndAlso
                                   .Item(n).CardShortName.Contains("U") = False Then
                                   CurrentCard = .Item(n)
                               End If
                               'End If
                           End If
                       Next
                   End If

                   If .Count > 0 AndAlso MyForm.RufAs.IsAlreadyPlayed = False AndAlso MyForm.iCoSpieler > -1 Then
                       For n = 0 To .Count - 1
                           If .Item(n).CardType <> TrumpCardID AndAlso .Item(n).CardType = MyForm.RufAs.CardType Then
                               Dim sO As String
                               If TrumpCardID = 4 Then sO = "X"
                               If TrumpCardID <> 4 Then sO = "O"
                               If .Item(n).CardShortName.Contains(sO) = False AndAlso
                                   .Item(n).CardShortName.Contains("U") = False Then
                                   CurrentCard = .Item(n)
                                   MyForm.RufAs.IsAlreadyPlayed = True
                               End If
                           End If
                       Next
                   End If

               End If

               ' CurrentCard Is Nothing

               If .Count > 0 AndAlso CurrentCard Is Nothing Then
                   For n = 0 To .Count - 1
                       If CurrentCard Is Nothing Then CurrentCard = .Item(n)
                       If .Count > 0 AndAlso .Item(n).CardType <> TrumpCardID Then
                           CurrentCard = .Item(n)
                       End If
                   Next
               End If

           End With

           Debug.Print("ACP 444 CurrentCard: " & CurrentCard.CardShortName & " " & CurrentCard.CardSymbol)
           Return CurrentCard

       End Function
Posted
Updated 15-Jan-23 4:44am
v4
Comments
Ralf Meier 12-Jan-23 11:46am    
I don't see a question ... or is it the ask for "do the remaining work for me" ?
Jo_vb.net 12-Jan-23 11:56am    
The question is "How to code game rules for cards game in VB.NET?"

I'm happy to keep the remaining work at my end, no problem.

What I need is a concept or structure for card game rules.
Ralf Meier 12-Jan-23 14:15pm    
Basicly I agree with Gerry ...
What we do here is helping those who stuck at a specific point. Perhaps it is possible to help you ... but without some code from you and a idea what you want to have it is impossible (for me) - or in other way : we don't sit in front of your PC and can't see what is happening.
The concept for game rules (generally) are the rules of the game itself - if you want to create a kind of KI it depends primary also on this rules ...
adriancs 13-Jan-23 1:30am    
Maybe CP can come up with a section something similar to freelancer.com/fiverr/upwork that allows the request of "do the remaining work for me" or "do the research for me".
[no name] 12-Jan-23 13:47pm    
Basically, you want someone to Google for "card game examples", and find one that "you will like" and is "not too hard". Sounds like work ... for someone else.

1 solution

Looking at this:
VB
If MyForm.RufAs.CardOwner = CardOwner.North Then iRufAsOwner = 0
If MyForm.RufAs.CardOwner = CardOwner.East Then iRufAsOwner = 1
If MyForm.RufAs.CardOwner = CardOwner.South Then iRufAsOwner = 2
If MyForm.RufAs.CardOwner = CardOwner.West Then iRufAsOwner = 3
If MyForm.GameModus = GameMode.Solo Then iRufAsOwner = -1
If MyForm.GameModus = GameMode.Wenz Then iRufAsOwner = -1

If CardOwner.North is an enum value, then you can shortcut with:
VB
iRufAsOwner = If(MyForm.GameModus = GameMode.Solo OrElse _
                 MyForm.GameModus = GameMode.Wenz, _
                 -1, _
                 MyForm.RufAs.CardOwner)

Or ...
VB
iRufAsOwner = If(MyForm.GameModus = GameMode.Solo OrElse _
                 MyForm.GameModus = GameMode.Wenz, _
                 CardOwnerType.Solo, _
                 MyForm.RufAs.CardOwner)

Where:
VB
Public Enum CardOwnerType
    Solo = -1
    North
    East
    South
    West
End Enum

Then...
VB
Dim iRufAsOwner as CardOwnerType

iRufAsOwner is then self-describing, rather than integer values. You can use them as integers if needed for calculations.

As for the game logic, that is your fun, not ours. Enjoy!
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900