Ooops
March 01, 2021, 10:27:23 PM

Author Topic: [bb] Hiscore by jfk EO-11110 [ 1+ years ago ]  (Read 525 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Hiscore by jfk EO-11110 [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Hiscore
Author : jfk EO-11110
Posted : 1+ years ago

Description : A simple Example for Hiscore Sorting, Saving and Loading. It's using BubbleSort with a 2-Dimensional String Array. Although it may be kind of uncommon to use Strings for the Score, it makes sorting a little bit easier.

Code :
Code: BlitzBasic
  1. Graphics 800,600,32,2
  2.  
  3.  
  4. ;; a 2-dimansional string-array, containing scores and names
  5.  
  6. Const ARRAYSIZE = 30
  7. Dim SortArray$( ARRAYSIZE ,1)
  8.  
  9. ;; Fill it with pseudo-random values
  10. For N = 0 To ARRAYSIZE
  11.     ; this may be the score
  12.         SortArray( N ,0) = Rand(100,1000)
  13.     ; this may be the player name (creating some random names here)
  14.         SortArray( N ,1) = Chr$(Rand(65,90))+Chr$(Rand(65,90))+Chr$(Rand(65,90))+Chr$(Rand(65,90))
  15. Next
  16.  
  17. ;Sort the list
  18. BubbleSort( ARRAYSIZE )
  19.  
  20. ;this will save the hiscore list
  21. save_score(ARRAYSIZE)
  22. ;and if it's once saved, you can also load it:
  23. load_score(ARRAYSIZE)
  24.  
  25.  
  26. Print "-=Hall of Fame=-"
  27. For i=0 To ARRAYSIZE
  28.  Print SortArray( i ,0) +" Points by: " + SortArray( i ,1)
  29. Next
  30.  
  31.  
  32. WaitKey()
  33. End
  34.  
  35. Function save_score(size)
  36.  wr=WriteFile("myhiscore.txt")
  37.  For i=0 To size
  38.   WriteLine wr,SortArray(i,0)
  39.   WriteLine wr,SortArray(i,1)
  40.  Next
  41.  CloseFile wr
  42. End Function
  43.  
  44. Function load_score(size)
  45.  re=ReadFile("myhiscore.txt")
  46.  For i=0 To size
  47.   SortArray(i,0)=ReadLine(re)
  48.   SortArray(i,1)=ReadLine(re)
  49.  Next
  50.  CloseFile re
  51. End Function
  52.  
  53.  
  54. Function BubbleSort( Size% )
  55.         Local i, j, temp0$,temp1$
  56.        
  57.         For i = Size To 0 Step -1
  58.                 For j = 1 To i
  59.                         If SortArray$( j - 1,0 ) < SortArray$( j ,0) Then
  60.                                 temp0$ = SortArray$( j - 1 ,0)
  61.                                 temp1$ = SortArray$( j - 1 ,1)
  62.                                 SortArray$( j - 1 ,0) = SortArray$( j ,0)
  63.                                 SortArray$( j - 1 ,1) = SortArray$( j ,1)
  64.                                 SortArray$( j ,0) = temp0$
  65.                                 SortArray$( j ,1) = temp1$
  66.                         EndIf
  67.                 Next
  68.         Next
  69. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal