|
-
Jan 15th, 2009, 05:56 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Jan 15th, 2009, 07:20 PM
#2
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:
Public Structure Scores Implements IComparable Public Gross() As Integer Public Player As String Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo Dim other As Scores = DirectCast(obj, Scores) 'Compare the total scores first. Dim result As Integer = Me.Gross.Sum().CompareTo(other.Gross.Sum()) If result = 0 Then 'The total scores are the same so perform a countback. result = Me.GetSum(Me.Gross, 9, 17).CompareTo(Me.GetSum(other.Gross, 9, 17)) If result = 0 Then 'The scores for the last 9 holes are the same so continue the countback '... End If End If Return result End Function Private Function GetSum(ByVal gross As Integer(), _ ByVal startIndex As Integer, _ ByVal endIndex As Integer) As Integer Dim sum As Integer = 0 For index As Integer = startIndex To endIndex sum += gross(index) Next Return sum End Function 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.
-
Jan 15th, 2009, 07:29 PM
#3
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
 
-
Jan 15th, 2009, 07:41 PM
#4
Re: [2008] How would you do it ?
 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:
Private _gross() As Integer
Public ReadOnly Property Gross() As Integer()
Get
If Me._gross Is Nothing Then
Me._gross = New Integer(17) {}
End If
Return Me._gross
End Get
End Property
Another example of where properties are superior to public fields.
-
Jan 15th, 2009, 07:59 PM
#5
Re: [2008] How would you do it ?
 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.
-
Jan 15th, 2009, 08:07 PM
#6
Re: [2008] How would you do it ?
 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.
-
Jan 16th, 2009, 01:03 PM
#7
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|