Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 saving help needed
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
noobprogrammer123




PostPosted: Mon May 03, 2004 6:40 pm   Post subject: saving help needed

how do i make a hall of fame type thing in a program, like when they have completed the game they can enter their name and it will save it
Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Mon May 03, 2004 7:23 pm   Post subject: (No subject)

well what u need to do is 1st make a file with some names and scorces somting like:

Dan 1000000000
Tony 10000
Randa 1000
Martin 100
Asok 10
AsianSensation 1
JSBN 0

and load this file in to an array of strings and an array of ints witchs indexs corasponed. (this loading should be done at the start of the progame or any time b4 u need to save the high scorce file)

then u sort the array of sorces and make shure to chage the array of names in the same way so they do not get mixed up.

now u check to see if the curent players scorce is higher then any of the ones in the array, if it is delete the lowest enerty and move all the ones below the new higher score down one and put the new high scorce in that place.

lastly to dum this data back in to a file in the same foramt u read it in.

i know what i side sounds kind of cofisuing but the main things u need to know are:

arrays
sorting
loops
ifs

all of witch u can lrean about in the tutorial section of the site. if u have more question about this i will be glad to help. (i know my explation is kind of confusing there)
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
noobprogrammer123




PostPosted: Tue May 04, 2004 1:36 pm   Post subject: (No subject)

could you explain that a little using code
Dan




PostPosted: Tue May 04, 2004 6:45 pm   Post subject: (No subject)

i just worte up this tutoiral on this, i made it up very fast so the spelling is bad and hopfully there are not big bugs or misatakes in it

code:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%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


if any ones sees anything wrong with it (other then spelling) or ways i could fix it up, plz let me know b4 i post this as a realy tutorial

P.S. in case this dose not look good in just code tags i upload it as well



highscoretut.t
 Description:
what u think?

Download
 Filename:  highscoretut.t
 Filesize:  3.61 KB
 Downloaded:  135 Time(s)

Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
noobprogrammer123




PostPosted: Wed May 05, 2004 4:31 pm   Post subject: (No subject)

when i put this into my program, for:
prodedure sort

it says, "procedure's may only be declared at the program, module, or monitor level"

what does that mean?
anywho_ya




PostPosted: Wed May 05, 2004 4:40 pm   Post subject: problem

when i ran this their was a problem with one of the for statements with all the i's something like (iii - 1) it said it had no value
noobprogrammer123




PostPosted: Wed May 05, 2004 6:24 pm   Post subject: (No subject)

I didn't have a problem with the i
anywho_ya




PostPosted: Wed May 05, 2004 8:35 pm   Post subject: Problem

Ya i definately have a problem with this:

if scorse (iii - 1) < scorse (iii) then

it says: Varible has no value
Sponsor
Sponsor
Sponsor
sponsor
anywho_ya




PostPosted: Wed May 05, 2004 8:58 pm   Post subject: problem solved

K i found the problem ... numOfPeople shoud be 3 not 13
Dan




PostPosted: Wed May 05, 2004 9:18 pm   Post subject: (No subject)

you got to chage num of poleop for how many poleop u using in your file



EDIT: OOOOOOOO opps thats a type-o in my file, thanks for catching that. alought u should chage it to w/e num of polep u using.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Dan




PostPosted: Wed May 05, 2004 9:20 pm   Post subject: (No subject)

noobprogrammer123 wrote:
when i put this into my program, for:
prodedure sort

it says, "procedure's may only be declared at the program, module, or monitor level"

what does that mean?


thats odd, trying puting the sort procedure at the top of your code. are u using any modules or class in it? the sort thing should not be in a procedure or anything but by it's self
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
anywho_ya




PostPosted: Mon May 10, 2004 8:13 am   Post subject: (No subject)

noobprogrammer123 wrote:
when i put this into my program, for:
prodedure sort

it says, "procedure's may only be declared at the program, module, or monitor level"


This is prolly b/c u have another procedure. Meaning u have put this procedure inside ur other one.
white_dragon




PostPosted: Sat May 15, 2004 4:21 pm   Post subject: (No subject)

[quote="Hacker Dan"]you got to chage num of poleop for how many poleop u using in your file[/quote]


wat do u mean by that???????? like does it mean the maximum amount of ppl able to input their name into it?
SuperGenius




PostPosted: Sat May 15, 2004 10:19 pm   Post subject: (No subject)

if you are using inflexible arrays to handle this task, you need to know how many names are on the list and put that number into the code or else you will get an error
Dan




PostPosted: Sat May 15, 2004 11:09 pm   Post subject: (No subject)

SuperGenius wrote:
if you are using inflexible arrays to handle this task, you need to know how many names are on the list and put that number into the code or else you will get an error


yep, see the line "var numOfPeople : int := 3 " you can chage it there in my verson of the code.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: