Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Winforms VB.Net: Best way to code rules for german cards game "Schafkopf"?

Winforms VB.Net: Best way to code rules for german cards game "Schafkopf"?

Scheduled Pinned Locked Moved Visual Basic
csharphelpwinformsgame-dev
19 Posts 2 Posters 59 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jo_vb net
    wrote on last edited by
    #1

    Since some weeks i'm working on a german cards game "Schafkopf" [private] demo. The demo is not bad but somehow my coding drifted away and appears like "Spaghetti code". The game has 32 cards and 4 players. For a "SOLO" or "WENZ" the declarer plays against the 3 other players. A standard game is that the declarer plays with one "Ace" (owned by another player) => 2 play against the other 2. The computer controls 3 players, one player is the user who can click on one of his cards (which are presented on a dgv). The problem is that there are many, many loops when checking a players cards and endless "rules" which now are done with "If, Then, ElseIf ..." statements. So one of the related functions is > 500 code lines ... I think I need a better concept, because the existing one is hard to handle, problems are not easy to fix. Any ideas? Example for those loops:

       Try
                If MyForm.GameOver = True Then Exit Function
                If GameStatus.ToString = "SpielAus" Then MyForm.GameOver = True
                If GameStatus.ToString = "SpielAus" Then Exit Function
    
                If sHandCards Is Nothing Then
                    ' => LeadSuit n.a.
                    Debug.Print("ACP 516 sHandCards: Is Nothing")
                    Debug.Print("ACP 517 LeadSuitID: '" & LeadSuitID & "'; TrumpCardID: '" & TrumpCardID & "'")
                    sHandCards = sTrumpList
                Else
                    'sHandCards <> Nothing
                    'MessageBox.Show("ACP 525 sHandCards: " & sHandCards.ToString)
                    If ContainsHandCardsStandardTrumps(sHandCards) = False Then
                        'MessageBox.Show("ACP 528 sHandCards: " & sHandCards.ToString)
                        If TrumpCardID <> 4 Then sHandCards = sTrumpList
                        If TrumpCardID = 4 Then
                            If sHandCards.ToString.Contains("O") Then
                            Else
                                sHandCards = sTrumpList
                            End If
                        End If
                    End If
    
    
                    If dgv.CurrentCell.Value.ToString.Contains("♠") Or dgv.CurrentCell.Value.ToString.Contains("♥") Or 
                       dgv.CurrentCell.Value.ToString.Contains("Ⴖ") Or dgv.CurrentCell.Value.ToString.Contains("Ꚛ") Or 
                       dgv.CurrentCell.Value.ToString = "" Or dgv.CurrentCell.Value Is Nothing Or 
                       dgv.CurrentCell.Value.ToString = String.Empt
    
    L 2 Replies Last reply
    0
    • J Jo_vb net

      Since some weeks i'm working on a german cards game "Schafkopf" [private] demo. The demo is not bad but somehow my coding drifted away and appears like "Spaghetti code". The game has 32 cards and 4 players. For a "SOLO" or "WENZ" the declarer plays against the 3 other players. A standard game is that the declarer plays with one "Ace" (owned by another player) => 2 play against the other 2. The computer controls 3 players, one player is the user who can click on one of his cards (which are presented on a dgv). The problem is that there are many, many loops when checking a players cards and endless "rules" which now are done with "If, Then, ElseIf ..." statements. So one of the related functions is > 500 code lines ... I think I need a better concept, because the existing one is hard to handle, problems are not easy to fix. Any ideas? Example for those loops:

         Try
                  If MyForm.GameOver = True Then Exit Function
                  If GameStatus.ToString = "SpielAus" Then MyForm.GameOver = True
                  If GameStatus.ToString = "SpielAus" Then Exit Function
      
                  If sHandCards Is Nothing Then
                      ' => LeadSuit n.a.
                      Debug.Print("ACP 516 sHandCards: Is Nothing")
                      Debug.Print("ACP 517 LeadSuitID: '" & LeadSuitID & "'; TrumpCardID: '" & TrumpCardID & "'")
                      sHandCards = sTrumpList
                  Else
                      'sHandCards <> Nothing
                      'MessageBox.Show("ACP 525 sHandCards: " & sHandCards.ToString)
                      If ContainsHandCardsStandardTrumps(sHandCards) = False Then
                          'MessageBox.Show("ACP 528 sHandCards: " & sHandCards.ToString)
                          If TrumpCardID <> 4 Then sHandCards = sTrumpList
                          If TrumpCardID = 4 Then
                              If sHandCards.ToString.Contains("O") Then
                              Else
                                  sHandCards = sTrumpList
                              End If
                          End If
                      End If
      
      
                      If dgv.CurrentCell.Value.ToString.Contains("♠") Or dgv.CurrentCell.Value.ToString.Contains("♥") Or 
                         dgv.CurrentCell.Value.ToString.Contains("Ⴖ") Or dgv.CurrentCell.Value.ToString.Contains("Ꚛ") Or 
                         dgv.CurrentCell.Value.ToString = "" Or dgv.CurrentCell.Value Is Nothing Or 
                         dgv.CurrentCell.Value.ToString = String.Empt
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Simple thing like:

      string s = dgv.CurrentCell.Value.ToString

      then

      If s.Conttains("x") or s.Contains...etc

      makes the thing more palatable. Then you look at it again; repeat.

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      J 1 Reply Last reply
      0
      • L Lost User

        Simple thing like:

        string s = dgv.CurrentCell.Value.ToString

        then

        If s.Conttains("x") or s.Contains...etc

        makes the thing more palatable. Then you look at it again; repeat.

        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

        J Offline
        J Offline
        Jo_vb net
        wrote on last edited by
        #3

        Thank you. Will use string s = dgv.CurrentCell.Value.ToString and remove redundant code lines. But the main question is if it makes sense using If - Then statements for game rules or if there is a better way to do this.

        L 1 Reply Last reply
        0
        • J Jo_vb net

          Thank you. Will use string s = dgv.CurrentCell.Value.ToString and remove redundant code lines. But the main question is if it makes sense using If - Then statements for game rules or if there is a better way to do this.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Rules come in a form best suited for a given situation; be it "if's", tables or whatever. Your "if's" depend on the length of your "cell value". If it's only 1 (char) long, instead of multiple if's, you can code:

          string s = "x"

          if "xwz".Contains(s) etc.

          if the cell value's length is greater than one, then iterate each character:

          For Each ch As Char in s
          if "xyx".Contains(ch) etc.
          Next
          }

          "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

          J 1 Reply Last reply
          0
          • J Jo_vb net

            Since some weeks i'm working on a german cards game "Schafkopf" [private] demo. The demo is not bad but somehow my coding drifted away and appears like "Spaghetti code". The game has 32 cards and 4 players. For a "SOLO" or "WENZ" the declarer plays against the 3 other players. A standard game is that the declarer plays with one "Ace" (owned by another player) => 2 play against the other 2. The computer controls 3 players, one player is the user who can click on one of his cards (which are presented on a dgv). The problem is that there are many, many loops when checking a players cards and endless "rules" which now are done with "If, Then, ElseIf ..." statements. So one of the related functions is > 500 code lines ... I think I need a better concept, because the existing one is hard to handle, problems are not easy to fix. Any ideas? Example for those loops:

               Try
                        If MyForm.GameOver = True Then Exit Function
                        If GameStatus.ToString = "SpielAus" Then MyForm.GameOver = True
                        If GameStatus.ToString = "SpielAus" Then Exit Function
            
                        If sHandCards Is Nothing Then
                            ' => LeadSuit n.a.
                            Debug.Print("ACP 516 sHandCards: Is Nothing")
                            Debug.Print("ACP 517 LeadSuitID: '" & LeadSuitID & "'; TrumpCardID: '" & TrumpCardID & "'")
                            sHandCards = sTrumpList
                        Else
                            'sHandCards <> Nothing
                            'MessageBox.Show("ACP 525 sHandCards: " & sHandCards.ToString)
                            If ContainsHandCardsStandardTrumps(sHandCards) = False Then
                                'MessageBox.Show("ACP 528 sHandCards: " & sHandCards.ToString)
                                If TrumpCardID <> 4 Then sHandCards = sTrumpList
                                If TrumpCardID = 4 Then
                                    If sHandCards.ToString.Contains("O") Then
                                    Else
                                        sHandCards = sTrumpList
                                    End If
                                End If
                            End If
            
            
                            If dgv.CurrentCell.Value.ToString.Contains("♠") Or dgv.CurrentCell.Value.ToString.Contains("♥") Or 
                               dgv.CurrentCell.Value.ToString.Contains("Ⴖ") Or dgv.CurrentCell.Value.ToString.Contains("Ꚛ") Or 
                               dgv.CurrentCell.Value.ToString = "" Or dgv.CurrentCell.Value Is Nothing Or 
                               dgv.CurrentCell.Value.ToString = String.Empt
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Instead of hard coding the rules, can you think of a way to load some rules from a list and check those?

            Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

            J 1 Reply Last reply
            0
            • L Lost User

              Instead of hard coding the rules, can you think of a way to load some rules from a list and check those?

              Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              J Offline
              J Offline
              Jo_vb net
              wrote on last edited by
              #6

              I had some ideas during the last days. It is not easy to find and fix issues with my Spaghetti Code - but the computer is already playing better than a human with beginner level. If I only could fix 4 or 5 important remaining issues the pc would reach medium playing level. One idea is to replace each loop with a Sub or Function (what is possible but I'm not sure it this makes sense). A list or a xml file came also to my mind - I searched for CP articles about rules engine or business rules. But from my findings there was nothing really easy - something that you can use easily and when you look at it some weeks later it should still look easy and allow changes without any problems. The last thing would be to have classes for card, hand cards, cards deck, current trick and so on. But I do not know how to bring together the properties of the classes and my existing rules. Any link to an article which may help me would be great. Note: My Schafkopf demo is based on A Bridge Card Game and Display Card Presentation[^]

              L 2 Replies Last reply
              0
              • J Jo_vb net

                I had some ideas during the last days. It is not easy to find and fix issues with my Spaghetti Code - but the computer is already playing better than a human with beginner level. If I only could fix 4 or 5 important remaining issues the pc would reach medium playing level. One idea is to replace each loop with a Sub or Function (what is possible but I'm not sure it this makes sense). A list or a xml file came also to my mind - I searched for CP articles about rules engine or business rules. But from my findings there was nothing really easy - something that you can use easily and when you look at it some weeks later it should still look easy and allow changes without any problems. The last thing would be to have classes for card, hand cards, cards deck, current trick and so on. But I do not know how to bring together the properties of the classes and my existing rules. Any link to an article which may help me would be great. Note: My Schafkopf demo is based on A Bridge Card Game and Display Card Presentation[^]

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I think you could have found more current examples. [How to Build a Multiplayer Card Game with Unity 2D and Mirror (UPDATED)](https://www.freecodecamp.org/news/how-to-build-a-multiplayer-card-game-with-unity-2d-and-mirror/) [card-game · GitHub Topics · GitHub](https://github.com/topics/card-game?l=c%23) etc.

                "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                L J 3 Replies Last reply
                0
                • J Jo_vb net

                  I had some ideas during the last days. It is not easy to find and fix issues with my Spaghetti Code - but the computer is already playing better than a human with beginner level. If I only could fix 4 or 5 important remaining issues the pc would reach medium playing level. One idea is to replace each loop with a Sub or Function (what is possible but I'm not sure it this makes sense). A list or a xml file came also to my mind - I searched for CP articles about rules engine or business rules. But from my findings there was nothing really easy - something that you can use easily and when you look at it some weeks later it should still look easy and allow changes without any problems. The last thing would be to have classes for card, hand cards, cards deck, current trick and so on. But I do not know how to bring together the properties of the classes and my existing rules. Any link to an article which may help me would be great. Note: My Schafkopf demo is based on A Bridge Card Game and Display Card Presentation[^]

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Jo_vb.net wrote:

                  but the computer is already playing better than a human with beginner level. If I only could fix 4 or 5 important remaining issues the pc would reach medium playing level.

                  You programmed it like that! Look into the "Random" object and force it to play a dumb card now and then (once out of five?).

                  Jo_vb.net wrote:

                  A list or a xml file came also to my mind - I searched for CP articles about rules engine or business rules.

                  Hehe, good idea; those tend to generalize to fit a lot of rules though. You want to keep it simpler; understand the concept enough to build a simpler version of your own. Remember that "xyz".Contains("c")? Imagine xyz and c coming from a file? It wouldn't be rules based, but halfway. If you can do that, we'll go from there to make actual rules :)

                  Jo_vb.net wrote:

                  The last thing would be to have classes for card, hand cards, cards deck, current trick and so on. But I do not know how to bring together the properties of the classes and my existing rules.

                  You could write multiple classes for a "card"; I'd talk about interfaces at this point if we'd be in class :D

                  Jo_vb.net wrote:

                  Any link to an article which may help me would be great.

                  I did not help in any way, and any suggestion may have sounded like homework for school. There's never a library that does exactly what you need. I can only point to CodeProject; they'd help. Conquer the rules; if you can do that, I'll write an article about depending on classes and having variants :thumbsup:

                  Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                  J 1 Reply Last reply
                  0
                  • L Lost User

                    I think you could have found more current examples. [How to Build a Multiplayer Card Game with Unity 2D and Mirror (UPDATED)](https://www.freecodecamp.org/news/how-to-build-a-multiplayer-card-game-with-unity-2d-and-mirror/) [card-game · GitHub Topics · GitHub](https://github.com/topics/card-game?l=c%23) etc.

                    "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Are we a programming forum, or a Google service? :confused:

                    Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      I think you could have found more current examples. [How to Build a Multiplayer Card Game with Unity 2D and Mirror (UPDATED)](https://www.freecodecamp.org/news/how-to-build-a-multiplayer-card-game-with-unity-2d-and-mirror/) [card-game · GitHub Topics · GitHub](https://github.com/topics/card-game?l=c%23) etc.

                      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                      J Offline
                      J Offline
                      Jo_vb net
                      wrote on last edited by
                      #10

                      Thank you for the links.

                      1 Reply Last reply
                      0
                      • L Lost User

                        Jo_vb.net wrote:

                        but the computer is already playing better than a human with beginner level. If I only could fix 4 or 5 important remaining issues the pc would reach medium playing level.

                        You programmed it like that! Look into the "Random" object and force it to play a dumb card now and then (once out of five?).

                        Jo_vb.net wrote:

                        A list or a xml file came also to my mind - I searched for CP articles about rules engine or business rules.

                        Hehe, good idea; those tend to generalize to fit a lot of rules though. You want to keep it simpler; understand the concept enough to build a simpler version of your own. Remember that "xyz".Contains("c")? Imagine xyz and c coming from a file? It wouldn't be rules based, but halfway. If you can do that, we'll go from there to make actual rules :)

                        Jo_vb.net wrote:

                        The last thing would be to have classes for card, hand cards, cards deck, current trick and so on. But I do not know how to bring together the properties of the classes and my existing rules.

                        You could write multiple classes for a "card"; I'd talk about interfaces at this point if we'd be in class :D

                        Jo_vb.net wrote:

                        Any link to an article which may help me would be great.

                        I did not help in any way, and any suggestion may have sounded like homework for school. There's never a library that does exactly what you need. I can only point to CodeProject; they'd help. Conquer the rules; if you can do that, I'll write an article about depending on classes and having variants :thumbsup:

                        Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                        J Offline
                        J Offline
                        Jo_vb net
                        wrote on last edited by
                        #11

                        Thanks a lot for this detailed answer. But I'm only on a better beginner level in coding... I use already subs in my loops and I really don't understand how

                        Quote:

                        Remember that "xyz".Contains("c")? Imagine xyz and c coming from a file? It wouldn't be rules based, but halfway. If you can do that, we'll go from there to make actual rules

                        could be used for a loop like the following:

                                   If MyForm.RufAs.MeAlreadyPlayed = True Or MyForm.iCoSpieler = -1 Then
                                        For Each row As DataGridViewRow In dgv.Rows
                                            For n As Integer = 1 To dgv.Columns.Count - 1
                                                If MyForm.RufAs.MeAlreadyPlayed = True Or MyForm.iCoSpieler = -1 Then
                        
                                                    If MyForm.Trick\_Content.CurrentTrickWinner <> MyForm.iCoSpieler AndAlso MyForm.Trick\_Content.CurrentTrickWinner <> DeclarerID Then
                                                        If n > 0 AndAlso PlayerID <> DeclarerID AndAlso PlayerID <> MyForm.iCoSpieler Then
                                                            If TrumpCardID <> 4 Then
                        
                                                                If PlayTogether\_WenzUsage(dgvCell, PlayerID, DeclarerID, GameStatus, sTrumpList, TrumpCardID, "RufAs", MyForm, LeadSuitID) = True Then
                        
                                                                    If row.Cells(n).Value.ToString.Contains("U") Then
                                                                        If ContainsHandCardsStandardTrumps(CStr(sHandCards)) = False Then ' Or sHandCards Is Nothing Then ' Or MyForm.cardLessThan(MyForm.Trick\_Content.PossibleWinnerCardString, row.Cells(n).Value.ToString & row.Cells(0).Value.ToString) Then
                                                                            If LeadSuitID = TrumpCardID AndAlso row.Cells(n).Value.ToString <> String.Empty Then
                                                                                SetCurrentCell(dgvCell, dgv, row.Cells(n), row.Cells(0), " ~ " & (String.Format("Line # {0}", (New StackTrace(New StackFrame(True))).GetFrame(0).GetFileLineNumber())) & " ~ ")
                                                                            End If
                                                                        End If
                                                                    End If
                                                                End If
                                                            End If
                        
                                                        End If
                                                    End If
                                                End If
                                            Next
                                        Next
                        
                        L 1 Reply Last reply
                        0
                        • J Jo_vb net

                          Thanks a lot for this detailed answer. But I'm only on a better beginner level in coding... I use already subs in my loops and I really don't understand how

                          Quote:

                          Remember that "xyz".Contains("c")? Imagine xyz and c coming from a file? It wouldn't be rules based, but halfway. If you can do that, we'll go from there to make actual rules

                          could be used for a loop like the following:

                                     If MyForm.RufAs.MeAlreadyPlayed = True Or MyForm.iCoSpieler = -1 Then
                                          For Each row As DataGridViewRow In dgv.Rows
                                              For n As Integer = 1 To dgv.Columns.Count - 1
                                                  If MyForm.RufAs.MeAlreadyPlayed = True Or MyForm.iCoSpieler = -1 Then
                          
                                                      If MyForm.Trick\_Content.CurrentTrickWinner <> MyForm.iCoSpieler AndAlso MyForm.Trick\_Content.CurrentTrickWinner <> DeclarerID Then
                                                          If n > 0 AndAlso PlayerID <> DeclarerID AndAlso PlayerID <> MyForm.iCoSpieler Then
                                                              If TrumpCardID <> 4 Then
                          
                                                                  If PlayTogether\_WenzUsage(dgvCell, PlayerID, DeclarerID, GameStatus, sTrumpList, TrumpCardID, "RufAs", MyForm, LeadSuitID) = True Then
                          
                                                                      If row.Cells(n).Value.ToString.Contains("U") Then
                                                                          If ContainsHandCardsStandardTrumps(CStr(sHandCards)) = False Then ' Or sHandCards Is Nothing Then ' Or MyForm.cardLessThan(MyForm.Trick\_Content.PossibleWinnerCardString, row.Cells(n).Value.ToString & row.Cells(0).Value.ToString) Then
                                                                              If LeadSuitID = TrumpCardID AndAlso row.Cells(n).Value.ToString <> String.Empty Then
                                                                                  SetCurrentCell(dgvCell, dgv, row.Cells(n), row.Cells(0), " ~ " & (String.Format("Line # {0}", (New StackTrace(New StackFrame(True))).GetFrame(0).GetFileLineNumber())) & " ~ ")
                                                                              End If
                                                                          End If
                                                                      End If
                                                                  End If
                                                              End If
                          
                                                          End If
                                                      End If
                                                  End If
                                              Next
                                          Next
                          
                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Jo_vb.net wrote:

                          But I'm only on a better beginner level in coding...

                          Lots of beginners here; most looking for templates to do their work.

                          Jo_vb.net wrote:

                          could be used for a loop like the following:

                          Don't worry about the big picture; understand the concept first, then you integrate it into your 'engine'. Someone hit me if I wrong, but string s = File.ReadAllText(path, appendText, Encoding.UTF8); would load the data of that file into the string called s? "xyz".Contains("c") becomes s.Contains("c") Where s is then loaded from a file (specified in "path"). This way, the "xyz" isn't hardcoded, but just data in a file. Requires you to read a string from a file. Could you achieve that?

                          Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                          J 1 Reply Last reply
                          0
                          • L Lost User

                            Jo_vb.net wrote:

                            But I'm only on a better beginner level in coding...

                            Lots of beginners here; most looking for templates to do their work.

                            Jo_vb.net wrote:

                            could be used for a loop like the following:

                            Don't worry about the big picture; understand the concept first, then you integrate it into your 'engine'. Someone hit me if I wrong, but string s = File.ReadAllText(path, appendText, Encoding.UTF8); would load the data of that file into the string called s? "xyz".Contains("c") becomes s.Contains("c") Where s is then loaded from a file (specified in "path"). This way, the "xyz" isn't hardcoded, but just data in a file. Requires you to read a string from a file. Could you achieve that?

                            Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                            J Offline
                            J Offline
                            Jo_vb net
                            wrote on last edited by
                            #13

                            I'm in doubt about this approach [the .Contains(s) method is only used in < 10% of the "if - then" statements/rules], but reading a text file line after line is possible.

                            L 1 Reply Last reply
                            0
                            • J Jo_vb net

                              I'm in doubt about this approach [the .Contains(s) method is only used in < 10% of the "if - then" statements/rules], but reading a text file line after line is possible.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              The idea is not limited to that method :)

                              Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                              1 Reply Last reply
                              0
                              • L Lost User

                                Are we a programming forum, or a Google service? :confused:

                                Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #15

                                I could have just said "There are better examples". If someone said that to me, I would have responded: "And...?" So, I saw no point in responding as you might suggest. There, I wasted a message after all.

                                "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                                1 Reply Last reply
                                0
                                • L Lost User

                                  Rules come in a form best suited for a given situation; be it "if's", tables or whatever. Your "if's" depend on the length of your "cell value". If it's only 1 (char) long, instead of multiple if's, you can code:

                                  string s = "x"

                                  if "xwz".Contains(s) etc.

                                  if the cell value's length is greater than one, then iterate each character:

                                  For Each ch As Char in s
                                  if "xyx".Contains(ch) etc.
                                  Next
                                  }

                                  "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                                  J Offline
                                  J Offline
                                  Jo_vb net
                                  wrote on last edited by
                                  #16

                                  Well this leads me to another improvement. Instead of many times using If statements like that

                                  If PlayerID <> DeclarerID And PlayerID <> MyForm.iCoSpieler Then ...
                                  or
                                  If PlayerID = DeclarerID Or PlayerID = MyForm.iCoSpieler Then ...

                                  I will use variables like

                                  Dim bTeamDeclarer As Boolean
                                  Dim bTeamOpponent As Boolean

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

                                  Then i can combine 2 or 3 lines of my rules in the loops like

                                  If n > 0 AndAlso bTeamDeclarer= True AndAlso row.Index = TrumpCardID Then
                                  ...

                                  This should make code easier to read AND shorter.

                                  L 1 Reply Last reply
                                  0
                                  • J Jo_vb net

                                    Well this leads me to another improvement. Instead of many times using If statements like that

                                    If PlayerID <> DeclarerID And PlayerID <> MyForm.iCoSpieler Then ...
                                    or
                                    If PlayerID = DeclarerID Or PlayerID = MyForm.iCoSpieler Then ...

                                    I will use variables like

                                    Dim bTeamDeclarer As Boolean
                                    Dim bTeamOpponent As Boolean

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

                                    Then i can combine 2 or 3 lines of my rules in the loops like

                                    If n > 0 AndAlso bTeamDeclarer= True AndAlso row.Index = TrumpCardID Then
                                    ...

                                    This should make code easier to read AND shorter.

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #17

                                    In c# I'd code:

                                    // As a "property"
                                    bool bTeamComponent => PlayerID != DeclarerID && PlayerID != MyForm.iCoSpieler;
                                    ...
                                    // Then in a method:
                                    if ( bTeamComponent ) ...

                                    Note how bTeamComponent is "dynamic" for every access. It obviously has to be scoped properly.

                                    "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      I think you could have found more current examples. [How to Build a Multiplayer Card Game with Unity 2D and Mirror (UPDATED)](https://www.freecodecamp.org/news/how-to-build-a-multiplayer-card-game-with-unity-2d-and-mirror/) [card-game · GitHub Topics · GitHub](https://github.com/topics/card-game?l=c%23) etc.

                                      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                                      J Offline
                                      J Offline
                                      Jo_vb net
                                      wrote on last edited by
                                      #18

                                      I do not want to learn Unity or Python. But I found a WPF project in a CP article with a cards deck where each card is a class object (which shows a card image) called PlayingCard. I've added some properties which I need for my game rules and migrated all I already had to WPF. All I have to do now is to re-write > 1000 code lines with the game rules. But the UI is much better then the old one.

                                      L 1 Reply Last reply
                                      0
                                      • J Jo_vb net

                                        I do not want to learn Unity or Python. But I found a WPF project in a CP article with a cards deck where each card is a class object (which shows a card image) called PlayingCard. I've added some properties which I need for my game rules and migrated all I already had to WPF. All I have to do now is to re-write > 1000 code lines with the game rules. But the UI is much better then the old one.

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #19

                                        They were there for "design" ideas. You were asking for "best ways to code" (i.e. "patterns"). (I personally have never written a lick of VB.NET). But I see you get the idea: find a mouse trap (sample app) and build a better one.

                                        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups