Results 1 to 7 of 7

Thread: [2008] How would you do it ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2003
    Location
    Wigan, UK
    Posts
    291

    [2008] How would you do it ?

    I have been away from Vb for a while but now i am making a golf program and i am wondering how to approach the calculations for the finish of a round of golf. If there is a draw they do a calculation called countback(explained below).

    I have this structure
    Code:
     Private Structure Scores
            Dim Gross() As Integer
            Dim Player As String
     End Structure
    
    Private GrossScores As New List(Of Scores)
    When i add a player i do this
    Code:
    Dim GS As New Scores
           
            GS.Player = "Player" & (GrossScores.Count + 1).ToString
            Array.Resize(GS.Gross, 18)
    
            GrossScores.Add(GS)
    Through the game i put the gross scores for each hole into the elements of the array but when i have filled all 18 holes i need to add the scores to another list in order of who is winning.

    For example if player1 has a complete gross(all 18 holes) of say 90 and player2 has 87, player2 wins because its a lower score. if they both had 90 then this is where the countback comes in.

    If after 18 holes they are the same then you check who had the lowest last 9 holes, if they are the same then you check who has the lowest 6 holes, if they are the same then you check the lowest 3 holes, if they are the same then you check from hole 18 one by one backwards until you find the lowest of the two. the lowest wins therefore needs to added to the list higher.

    Have i explained that good enough ?

    thank you.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] How would you do it ?

    You can implement IComparable in your structure, then a List(Of Score) or a Score() will sort itself.
    vb.net Code:
    1. Public Structure Scores
    2.     Implements IComparable
    3.  
    4.     Public Gross() As Integer
    5.     Public Player As String
    6.  
    7.     Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
    8.         Dim other As Scores = DirectCast(obj, Scores)
    9.  
    10.         'Compare the total scores first.
    11.         Dim result As Integer = Me.Gross.Sum().CompareTo(other.Gross.Sum())
    12.  
    13.         If result = 0 Then
    14.             'The total scores are the same so perform a countback.
    15.             result = Me.GetSum(Me.Gross, 9, 17).CompareTo(Me.GetSum(other.Gross, 9, 17))
    16.  
    17.             If result = 0 Then
    18.                 'The scores for the last 9 holes are the same so continue the countback
    19.                 '...
    20.             End If
    21.         End If
    22.  
    23.         Return result
    24.     End Function
    25.  
    26.     Private Function GetSum(ByVal gross As Integer(), _
    27.                             ByVal startIndex As Integer, _
    28.                             ByVal endIndex As Integer) As Integer
    29.         Dim sum As Integer = 0
    30.  
    31.         For index As Integer = startIndex To endIndex
    32.             sum += gross(index)
    33.         Next
    34.  
    35.         Return sum
    36.     End Function
    37.  
    38. End Structure
    You would continue to nest If statements in that CompareTo method for each step of your countback algorithm. Now you can just call GrossScores.Sort() and everything will be taken care of. That' encapsulation, which is one of the cornerstones of OOP.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2008] How would you do it ?

    By the way, I notice that you have a dynamic array for Gross. Is there any reason why this should not be fixed size (with a size of 18, of course)? Will you be wanting to extend this to tournament play (72 holes) or some such? If so, that will probably factor into the design of the countback.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] How would you do it ?

    Quote Originally Posted by Shaggy Hiker
    By the way, I notice that you have a dynamic array for Gross. Is there any reason why this should not be fixed size (with a size of 18, of course)? Will you be wanting to extend this to tournament play (72 holes) or some such? If so, that will probably factor into the design of the countback.
    I'm guessing that this was a side-effect of having an array field in a structure. The array cannot be initialised in the structure where it's declared and you cannot add a parameterless constructor to a structure. Making the type a class would allow the Gross field to be made ReadOnly and have it initialised where it's declared.

    Another option would be to use a property in the structure and use lazy loading:
    vb.net Code:
    1. Private _gross() As Integer
    2.  
    3. Public ReadOnly Property Gross() As Integer()
    4.     Get
    5.         If Me._gross Is Nothing Then
    6.             Me._gross = New Integer(17) {}
    7.         End If
    8.  
    9.         Return Me._gross
    10.     End Get
    11. End Property
    Another example of where properties are superior to public fields.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] How would you do it ?

    Quote Originally Posted by jmcilhinney
    Another example of where properties are superior to public fields.
    Thats the first 'real world' example of why a property would be better than a public variable that I have ever seen thanks

    I also didnt realise you could define functions and subs etc inside structures! Which brings me to a question... but I'll ask that in another thread.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] How would you do it ?

    Quote Originally Posted by chris128
    Thats the first 'real world' example of why a property would be better than a public variable that I have ever seen
    You've actually seen lots of examples before but you just didn't know it. Consider the Text property of a TextBox. When you type into a TextBox its Text property gets set and a TextChanged event is raised. If Text was a field rather than a property then it would not be possible to raise that event. Any property that causes an event to be raised when set is a real-world example of why properties are superior to public fields.

    Have you ever used data-binding? That's another example. Data-binding only works with properties, not fields. More accurately, data-binding works with property descriptors. Property descriptors exist by default for properties but not for fields. You can create your own property descriptors for a type, which is why you're able to bind a DataTable and get the column values when they are not actually property values, but it's not a trivial thing to do so you wouldn't want to be doing it for every type just so you could use fields instead of properties.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2003
    Location
    Wigan, UK
    Posts
    291

    Re: [2008] How would you do it ?

    jmcilhinney, Thank you for your time. I wont pretend to know exactly whats going on there but i will see to it that i will. I was expecting to do all sorts of loops, if thats all i need then i am a very happy man.

    Thanks again.

    Shaggy Hiker, Thats a good point about tournaments, i spoke to them today and they say they never have more than two rounds in any tournament, usually its only one but if they do have two then they use the countback only on the last round.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width