PDA

Click to See Complete Forum and Search --> : Sorting


omnitech
Apr 17th, 2002, 10:13 PM
I'm making a game called The Weakest Link. If anyone is unfamiliar with it, 8 players answer questions and after each round of questioning, one player is voted off. The voting part is what I have the most trouble with. First, I need to find out the statistical Weakest Link and Strongest Link. This means the person who answered the least and most questions correct, respectively. So...say a person has played a couple rounds, voted off a few players, and there are only 4 Players left. After another round of questions, I need to be able to sort these 4 Players by how many questions they got right. So, say P1 answered 1 question right, P2 answered 1 question right, P3 answered 1 question right, and P4 answered 3 questions right. Since P4 answered the most questions right, he would go at the top of the list. Since there is a tie with the other three, I would need to sort those three by themselves, based on the amount of time they spent answering their questions. Say P1 used 10 seconds, P2 used 15, and P3 also used 15 seconds. Since P1 tied P2 and P3 in the questions category but used less time, P1 would be second place. Then I would need to sort P2 and P3 by the amount of money earned in the round. Say P2 earns $500 and P3 earns $100. Then P2 would go next in the list, and P3 would be the weakest link. This makes it so that P4 is first place with 3 questions right (time and money earned unnecessary since questions correct is top priority); P1 second place with 1 right, 10 seconds used (money earned unnecessary since time used more important than money earned); P2 third place with 1 right, 15 seconds used, and $500 earned; and finally P3 is the Weakest Link with 1 question right, 15 seconds of time used, and only $100 earned. I would then need the program to return which player is the Strongest Link and which is the Weakest Link. I would also need this to work with 8,7,6,5,4,and 3 players, depending on the current round. If a player is voted out already then the statistical Weakest Link is the lowest person in the list that is still in (since a person eliminated would have no questions answered correctly, that would usually make them last place, so I would need the Weakest Link that is still in the game). How could I go about sorting people like this using Visual Basic 6?

/\/\isanThr0p
Apr 18th, 2002, 02:45 AM
well there is a bunch of sorting algorithms. (look for quicksort or bubble sort on this forum). Quicksort and bubble sort are both pretty simple, (Bubble sort might be slow but for only 5 values it really does not matter...) but I think for you it would be enough to add the players to a listbox that is sorted...
just add them like


for i = 0 to playercount
List1.AddItem Str(player(i).Time) + player(i).Name
next
List1.Sorted = True


now the list box will sort them for you...

/\/\isanThr0p
Apr 18th, 2002, 02:52 AM
well I forgot: you need to set the sorted property at design time..


'to get the first player:

List1.ListIndex = 0
MsgBox List1.Text

'to get the last player:

List1.ListIndex = List1.ListCount - 1
msgbox List1.Text

Ambivalentiowa
Apr 18th, 2002, 05:38 AM
Did you know there is already a licensed legal version of this game on the market?

omnitech
Apr 18th, 2002, 10:34 AM
I'm doing this for school. My teacher's out helping his sick mother for the last 2 weeks or so and our substitutes know nothing. This is the only way I can get help.

SLH
Apr 18th, 2002, 11:55 AM
I strongly recommend using an array to store details about each of the contestants, then sorting them is a lot simpler, as when you sort 1 person, all their details move together...

First create a Player type and set up the variables


Private Type typPlayer
Name as string 'Players Name
Correct as Integer 'Questions they got correct
TimeTaken as Long 'Time they took to answer
End Type

Dim Player(1 To 8) as typPlayer 'For each contestant
Dim Temp as typPlayer
Dim Flag as Boolean
Dim Count as Integer
Dim N as Integer


to enter their name do this


Player(1).Name = "Tim"
Player(2).Name = "SLH"


To sort them, having got how long they took, and the number they got correct do this:


N = Ubound(Player)
Do While flag = False And n > 0
flag = False
For Count = LBound(Player) To n
If Player(Count).Correct < Player(Count + 1).Correct Then
Temp = Player(Count)
Player(Count) = Player(Count + 1)
Player(Count + 1) = Temp
flag = True
End If

If Player(Count).Correct = Player(Count + 1).Correct Then
If Player(Count).TimeTaken > Player(Count+1).TimeTaken
Temp = Player(Count)
Player(Count) = Player(Count + 1)
Player(Count + 1) = Temp
flag = True
End If
End If
Next Count
n = n - 1
Loop


That should sort them by answers correct, then timetaken