----------------------------------- Dan Sat Jun 05, 2004 3:40 pm [Tutorial]High Score List ----------------------------------- I made this tutorial awhile ago and when i made it, i did it kind of fast b/c it was to help some one with there code, so hopfully there are not any logic errors in it. This method of doing a highscore list is one of the more simple ones and there are ways to make it more advaced and work faster but i was going for somting poleop whould understand. So here it is: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %How to make a high sorce system for a game %-Hacker Dan %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %This asumes u have a file made up like this: % %Dan 10000 %Tony 100 %Rei 10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%% %Some vars we need %%%%%%%%%%%%%%%%%%%%%%%% var numOfPeople : int := 3 var names : array 1 .. numOfPeople of string %place to hold the names var scorse : array 1 .. numOfPeople of int %place to hold the scorse var fileName : string := "top3.txt" %name of the file where the top sorces will be placed and read from %%%%%%%%%%%%%%%%%%%%%%%%% %LOAD THE HGIH SCORE FILE %%%%%%%%%%%%%%%%%%%%%%%%% var fileIn : int %the file stream var i : int := 0 %this is a counter to tell what name we are on open : fileIn, fileName, get loop exit when eof (fileIn) %exit when end of file i += 1 %add one to the counter get: fileIn, names (i) %gets the name of the person at line i get: fileIn, scorse (i) %gets the sorcer of the person at line i end loop %now the file data is loaded in to the vars names and scorse %%%%%%%%%%%%%%%%%%%%%%%% %Sorting the list %%%%%%%%%%%%%%%%%%%%%%%% %sort the high score list to put the person %with the highest sorce 1st and lowest last %bouble sort procedure %puts the arrays in order of bigested to smallest procedure sort for ii : 1 .. numOfPeople for iii : 2 .. numOfPeople if scorse (iii - 1) < scorse (iii) then %switchs the values var temp : int := scorse (iii - 1) scorse (iii - 1) := scorse (iii) scorse (iii) := temp %also have to swtich the names so they %keep lined up with the scorse var temp2 : string := names (iii - 1) names (iii - 1) := names (iii) names (iii) := temp2 end if end for end for end sort %%%%%%%%%%%%%%%%%%%%%%%% %Updating the list %%%%%%%%%%%%%%%%%%%%%%%% var newScore : int := 123 %the curent players sorce var newName : string := "Lain" %should sort to list to start with sort %now we need to check if the newScore beats any of the old ones %NOTE: this could be done throw any sreaching alrgithem but %i am using this one b/c it is easy rather then fast var newIndex : int := 0 %the place to put this new sorce %if the newScore is better then all of them if newScore > scorse (1) then newIndex := 1 %else sreach for the spot where the new score fits else for k : 1 .. (numOfPeople - 1) if newScore < scorse (k) and newScore > scorse (k + 1) then newIndex := (k + 1) end if %chechtch any of thous pesky entrys with the same score if newScore > scorse(k) then newIndex := k end if end for end if %if newIndex = 0 then newScore dose not get on the list if newIndex not= 0 then %now we need to put the newScore and newName in the list in the right place, but 1st we have %to shift all the ones below it down. for decreasing j : numOfPeople .. (newIndex + 1) names (j) := names (j - 1) scorse (j) := scorse (j - 1) end for %now we simply place the new entry in names (newIndex) := newName scorse (newIndex) := newScore end if %%%%%%%%%%%%%%%%%%%%%%%%%%%% %Displaying the list %%%%%%%%%%%%%%%%%%%%%%%%%%%% %this is the easy part, all we have to do is print each array on to the %screen in order. %we porby do not need to sort this if it was just affter updating %but just to be safe we will sort %now we need a for loop to go throw each array and put that data on the screen for d : 1..numOfPeople put d, ". ", names (d), " - ", scorse (d) end for ----------------------------------- s_climax Sat Jun 05, 2004 5:26 pm ----------------------------------- What about re-writing the high-score list. Why doesn't this work? close : fileIn open : fileIn, fileName, put loop exit when eof (fileIn) %exit when end of file i += 1 %add one to the counter put : fileIn, names (i) %gets the name of the person at line i put : fileIn, scores (i) %gets the sorcer of the person at line i end loop ----------------------------------- [Gandalf] Thu Jan 13, 2005 8:12 pm ----------------------------------- I tried experimenting with this, but it didn't work. If a high score did show up, it did not save to the file... When I try to incorporate it into my game, it says that scorse has no value when the time to sort the list comes. Can you please rewrite this for everyone? I would greatly appreciate that. I, in the meantime will try to experiment some more. ----------------------------------- jr.ranger.33 Mon Sep 18, 2006 3:52 pm Confused ----------------------------------- I'm new to programming and in the midst of designing a classic arcade games program... i'd like to add a simple highscores feature to it. Instead of takeing ur program and adding it to mine i'd like to try and understand it so i can program one of my own...so i'm probably going to be asking a few questions over the next few days (hope u don't mind). First question is: What is the numOfPeolpe variable representing? (It may seem like a retarded question...it is, but for some reason i can't figure out what it is representing lol) ----------------------------------- Clayton Mon Sep 18, 2006 4:53 pm ----------------------------------- first of all, questions should be posted in [Turing Help] so please post there in the future :D right now though, numOfPeople is representing the number of people there are to be in the high score list, you should also look into [url=http://www.compsci.ca/v2/viewtopic.php?t=12972]File I/O and sorting methods available. ----------------------------------- Decadence666 Sun Oct 29, 2006 6:20 pm ----------------------------------- Thanks this is exactly what i needed for an assignment. For anybody who wants to reverse the order of the scores ( from lowest to greatest ) its really easy. Took me a bit to figure it out, and then it was really obvious lol. Here we go: Heres Hacker Dans Version %%%%%%%%%%%%%%%%%%%%%%%% %Sorting the list %%%%%%%%%%%%%%%%%%%%%%%% %sort the high score list to put the person %with the highest sorce 1st and lowest last %bouble sort procedure %puts the arrays in order of bigested to smallest procedure sort for ii : 1 .. numOfPeople for iii : 2 .. numOfPeople if scorse (iii - 1) < scorse (iii) then %switchs the values var temp : int := scorse (iii - 1) scorse (iii - 1) := scorse (iii) scorse (iii) := temp %also have to swtich the names so they %keep lined up with the scorse var temp2 : string := names (iii - 1) names (iii - 1) := names (iii) names (iii) := temp2 end if end for end for end sort Just change this line: if scorse (iii - 1) < scorse (iii) then To: if scorse (iii - 1) > scorse (iii) then All it takes is to change the less than < to a greater than > Just thought that might be helpful to somebody... ----------------------------------- razrdude Mon Oct 30, 2006 1:07 am ----------------------------------- lol thanks, nice tut overall im gonna try to implement it in my program, although i gotta make a scoring system first.