ScoreBoard

Started by ATLUS, March 29, 2012, 07:44:16 AM

Previous topic - Next topic

ATLUS

 ;)

PlayBASIC Code: [Select]
; PROJECT : main
; AUTHOR : ATLUS
; CREATED : 29.03.2012
; EDITED : 29.03.2012
; --------------------------------------------------------------------
LoadFont "Arial",2,24,0
SetFont 2

type player
name as String // nick on forum http://www.underwaredesign.com/forums/
score as Integer // how many posts in http://www.underwaredesign.com/forums/ on date 29.03.2012 14:06 +3gtm
endType


dim players(11) as player

players(1).name = "Kevin"
players(1).score = 10453
players(2).name = "matty47 "
players(2).score = 1
players(3).name = "LemonWizard"
players(3).score = 269
players(4).name = "micky4fun"
players(4).score = 538
players(5).name = "monkeybot"
players(5).score = 288
players(6).name = "XpMe_v1.2"
players(6).score = 200
players(7).name = "Laskiapina"
players(7).score = 103
players(8).name = "basil99"
players(8).score = 113
players(9).name = "balaporte"
players(9).score = 51
players(10).name = "BlinkOk"
players(10).score = 353
players(11).name = "ATLUS"
players(11).score = 400

do
cls rgb(0,0,0)
sorted(array)
printed(array)
sync
loop


function sorted(array)
temp_name$ = ""
temp_score = 0

for t=11 to 1 step -1
for i=10 to 1 step -1
if(players(i).score<players(i+1).score)
temp_score = players(i+1).score
temp_name$ = players(i+1).name

players(i+1).score = players(i).score
players(i+1).name = players(i).name

players(i).score = temp_score
players(i).name = temp_name$
endif
next i
next t
endfunction

function printed(array)
borderx=GetScreenWidth()/2 - 120
bordery=GetScreenHeight()/2 - 120
for k=1 to 11
text borderx, bordery, k
text borderx+40, bordery, players(k).name
text borderx+180, bordery, players(k).score
bordery += 24
next k
endfunction

kevin

#1
 Very good,  there's a few high score style table routines posted..  Anyway, here's a slight tweak, to make the code a little more generic.  


PlayBASIC Code: [Select]
LoadFont "Arial",2,24,0
SetFont 2

type Player_High_Scores
name as String // nick on forum http://www.underwaredesign.com/forums/
score as Integer // how many posts in http://www.underwaredesign.com/forums/ on date 29.03.2012 14:06 +3gtm
endType

dim players(0) as Player_High_Scores

repeat
Name$=ReadData$()
Score=ReadData()
if Name$<>"END"
Index=GetFreeCell(PLayers())
Players(index).Name$=Name$
Players(index).Score=Score
endif
until Name$="END"


do
cls rgb(0,0,0)
sorted()
printed()
sync
loop



function sorted()

Count=GetArrayElements(Players())
for t=Count to 1 step -1
for i=Count-1 to 1 step -1
if(players(i).score<players(i+1).score)
; here we're using Index zero in the Players() array as the temp
; when swapping the items

; copy the current item to temp (index 0)
players(0).Player_High_Scores =PLayers(i).Player_High_Scores

; copy the next item over this item
players(i).Player_High_Scores =PLayers(i+1).Player_High_Scores

; copy the temp item over the next item
players(i+1).PLayer_high_Scores =players(0).Player_High_Scores

endif
next i
next

; free any temp data that might be here dure to swapping
players(0)= null

endfunction


function printed()
borderx=GetScreenWidth()/2 - 120
bordery=GetScreenHeight()/2 - 120
for k=1 to GetArrayElements(players())
text borderx, bordery, k
text borderx+40, bordery, players(k).name
text borderx+180, bordery, players(k).score
bordery += 24
next k
endfunction


;------------------------------------
;Initial Name + Score Values
;------------------------------------

data "Kevin",10453
data "matty47 ", 1
data "LemonWizard", 269
data "micky4fun", 538
data "monkeybot", 288
data "XpMe_v1.2", 200
data "Laskiapina", 103
data "basil99", 113
data "balaporte", 51
data "BlinkOk", 353
data "ATLUS", 400
data "END", 0





ATLUS

#2
Kevin your code better and tweak, so easy add new players and write this data in txt

this my code update, add new player "Freddy"

PlayBASIC Code: [Select]
; PROJECT : main
; AUTHOR : ATLUS
; CREATED : 29.03.2012
; EDITED : 29.03.2012
; --------------------------------------------------------------------
LoadFont "Arial",2,24,0
SetFont 2
setfps 60
type player
name as String // nick on forum http://www.underwaredesign.com/forums/
score as Integer // how many posts in http://www.underwaredesign.com/forums/ on date 29.03.2012 14:06 +3gtm
endType


newname$ = "Freddy" // new player nick
newscore = 134 // new player score

tempscore = 0
tempname$ = ""


dim players(11) as player

players(1).name = "Kevin"
players(1).score = 10453
players(2).name = "matty47 "
players(2).score = 1
players(3).name = "LemonWizard"
players(3).score = 269
players(4).name = "micky4fun"
players(4).score = 538
players(5).name = "monkeybot"
players(5).score = 288
players(6).name = "XpMe_v1.2"
players(6).score = 200
players(7).name = "Laskiapina"
players(7).score = 103
players(8).name = "basil99"
players(8).score = 113
players(9).name = "balaporte"
players(9).score = 51
players(10).name = "BlinkOk"
players(10).score = 353
players(11).name = "ATLUS"
players(11).score = 400

do
cls rgb(0,0,0)
sorted(array)
printed(array)

// add_new player "Freddy"
for i=1 to 11
if(newscore>players(i).score)
tempscore = players(i).score
tempname$ = players(i).name

players(i).score = newscore
players(i).name = newname$

newscore = tempscore
newname$ = tempname$
endif
next i

sync
loop


function sorted(array)
temp_name$ = ""
temp_score = 0

for t=11 to 1 step -1
for i=10 to 1 step -1
if(players(i).score<players(i+1).score)
temp_score = players(i+1).score
temp_name$ = players(i+1).name

players(i+1).score = players(i).score
players(i+1).name = players(i).name

players(i).score = temp_score
players(i).name = temp_name$
endif
next i
next t
endfunction



function printed(array)
borderx=GetScreenWidth()/2 - 120
bordery=GetScreenHeight()/2 - 120
for k=1 to 11
text borderx, bordery, k
text borderx+40, bordery, players(k).name
text borderx+180, bordery, players(k).score
bordery += 24
next k
endfunction




kevin

#3
 When adding  new scores to the table, it's generally a two step process.  First we scan the existing table looking to see if the score is within the range,  Since it may not be.   If a position is found, then we push all the scores bellow this point down one and write our score directly into the table array.  So we don't need to sort the list and we can use the insertion index for display purposes.. Like high lighting the current/last high score in the list.    


 Here's a version that uses insertion for the sorting, rather than the bubble sort.  The only thing it doesn't do,  is bother to limit the size of the table to the top ten scores or something.


PlayBASIC Code: [Select]
LoadFont "Arial",2,24,0
SetFont 2

type Player_High_Scores
name as String // nick on forum http://www.underwaredesign.com/forums/
score as Integer // how many posts in http://www.underwaredesign.com/forums/ on date 29.03.2012 14:06 +3gtm
endType

dim players(0) as Player_High_Scores

repeat
Name$=ReadData$()
Score=ReadData()
if Name$<>"END"

Index=Insert_New_Score(Score)
if Index
Players(index).Name$=Name$
Players(index).Score=Score
endif
endif
until Name$="END"


; -------------------------------------
; MAIN LOOP
; -------------------------------------
do

cls rgb(0,0,0)

; render the entier player scores array
printed()


; -------------------------------------
; check for a space key, then add a random player called dude
; -------------------------------------
if SpaceKey()
flushkeys

Score=Rnd(10000)
Name$="Dude"+str$(Counter)
Counter++

Index=Insert_New_Score(Score)
if Index

Players(index).Name$=Name$
Players(index).Score=Score

endif


EndIF


sync
loop




function printed()
borderx=GetScreenWidth()/2 - 120
bordery=GetScreenHeight()/2 - 120
for k=1 to GetArrayElements(players())
text borderx, bordery, k
text borderx+40, bordery, players(k).name
text borderx+180, bordery, players(k).score
bordery += 24
next k
endfunction



Function Insert_New_Score(Score)

; Get the size of the players high scores table
Size=GetArrayElements(PLayers())

; look for a place to stick this score
For lp=1 to size
if Score>PLayers(lp).score
index =lp
exitfor
endif
next

; if no insert position was found, just tack it on the bottom :)
if Index=0 then INdex =Size+1


; bump the size
Size++

; resize the array by 1
Redim Players(Size) as Player_High_Scores

; copy the old cells down one
copyarraycells PLayers(),Size-1,-1,players(),Size,-1, Size-Index

; clear the type at the insertion position
Players(index) = null


EndFunction Index




;------------------------------------
;Initial Name + Score Values
;------------------------------------

data "Kevin",10453
data "matty47 ", 1
data "LemonWizard", 269
data "micky4fun", 538
data "monkeybot", 288
data "XpMe_v1.2", 200
data "Laskiapina", 103
data "basil99", 113
data "balaporte", 51
data "BlinkOk", 353
data "ATLUS", 400
data "END", 0