BAIT Community Project (thread 2)

Miscellaneous Forums/Blitz Showcase/BAIT Community Project (thread 2)

League Table code:

; Data Declarations

Const TopTextY   		= 100
Const PositionX  		= 100
Const NameX      		= 250
Const PointsX	 		= 450
Const TextHeight 		= 30
Const BreakBetweenLines = 20
Const NumberOfBots      = 6

Dim Names$    (NumberOfBots - 1)
Dim Points    (NumberOfBots - 1)

Function InitialiseArrays()

	; Reads in the names and the points of the 
	; competitors into the corresponding arrays

	Restore RobotData
	
	For Index = 0 To NumberOfBots - 1
		
		Read Names  (Index)
		Read Points (Index)
	
	Next

End Function

Function DisplayLeagueTable()

	; Displays the league table in descending order of points
	; Tied scores are not treated differently 

	Local TextY 

	Text PositionX, TopTextY, "POSITION"
	Text NameX,     TopTextY, "NAME"
	Text PointsX,   TopTextY, "POINTS"
	
	For Index = 0 To NumberOfBots - 1
	
		TextY = TopTextY + (Index + 1) * (TextHeight + BreakBetweenLines)
		
		Text PositionX, TextY, (Index + 1) + Suffix (Index + 1)
		Text NameX,     TextY, Names  (Index)
		Text PointsX,   TextY, Points (Index)
		
	Next 	 

End Function

Function Suffix$( Number )

	; Returns the suffix for a given position number 

	Select Number
	
		Case 1  Return "st"
		Case 2  Return "nd"
		Case 3  Return "rd"
		Default Return "th"
	
	End Select	

End Function

Function SortLeagueTable()

	; Sorts the competitors scores into descending order of points
	; Uses a bubble sort to achieve this

	Local TempName$
	Local TempPoints

	For PassNumber = 0 To NumberOfBots - 2

		For PairNumber = 0 To NumberOfBots - 2

			If Points (PairNumber) < Points (PairNumber + 1)
			
				TempName   = Names  (PairNumber + 1)
				TempPoints = Points (PairNumber + 1)
				
				Names  (PairNumber + 1) = Names  (PairNumber)
				Points (PairNumber + 1) = Points (PairNumber)
				
				Names  (PairNumber) = TempName
				Points (PairNumber)	= TempPoints
			
			EndIf

		Next
	
	Next

End Function

; Test Harness

InitialiseArrays()
SortLeagueTable()

Graphics 800,600

DisplayLeagueTable()

WaitKey()

End

.RobotData
Data "BotA", 200
Data "BotB", 400
Data "BotC", 100
Data "BotD", 500
Data "BotE", 300
Data "BotF", 600


I expect to see lots of things ticked off our ToDo list now WolRon!

Ryan