
-----------------------------------
TheLastFall
Tue Oct 17, 2006 8:34 am

Saving and Loading files.
-----------------------------------
Ok, I want to have a Score list but I don't know how to import a file and save the scores. I'd also like to know how to save initials of the person to the scores list also but I pretty sure I can do that if I can just figure out how to save the scores. Can anyone please help me? It would be much appreciated. Is it anything like:
import: 'scores'
I actually have no clue how it would look, that is kind of a guess.[/code]

-----------------------------------
Dan
Tue Oct 17, 2006 11:08 am


-----------------------------------
We have serval tutorals on using files in turing. Here are a few:


File I/O: http://www.compsci.ca/v2/viewtopic.php?t=12972

High Score List: http://www.compsci.ca/v2/viewtopic.php?t=5340

Learn how to use File IO - easy!: http://www.compsci.ca/v2/viewtopic.php?t=6818


I sugest using the sreach fuction of the site if you need help on a topic that could have a tutoral for it. Also the import fuction is for importing code not noraml files.

-----------------------------------
TheLastFall
Wed Oct 18, 2006 7:33 am


-----------------------------------
Yea, about that. I am what a lot of people call, even myself an idiot. I do not understand what I'm doing with the 'Hi Scores' link, or the other two. All I want to do is just save a score with the persons name, like so:
Score:      Name:
      
         1000        Ass

That is all. It doesn't seem hard at all but I just don't understand how to save to the file and load it at the end of the program. So everyone could see the past scores that were made. A pretty simple procedure for those who have done importing and exporting in the past. It's just I kind of need things described to me specifically. I'm a moron so please can you make it clear.[/code]

-----------------------------------
TheLastFall
Wed Oct 18, 2006 7:36 am


-----------------------------------
I also forgot to mention, there are tons of spelling mistakes in the 'Hi Score' link. I don't know about other people but I found that really distracting.

-----------------------------------
Tony
Wed Oct 18, 2006 8:03 am


-----------------------------------
alright, lets take it one step at a time. Are you able to follow the File I/O tutorial and read/write _any_ text from/to a file? Doesn't have to be highscore, just a name.

If not, what part are you having problems understanding?

-----------------------------------
TheLastFall
Wed Oct 18, 2006 11:23 am


-----------------------------------
I can't say I do, it's really confusing.

-----------------------------------
TheLastFall
Wed Oct 18, 2006 11:25 am


-----------------------------------
Well I understand the loading the text file part but then the rest just kind of doesn't make any sense.

-----------------------------------
Tony
Wed Oct 18, 2006 11:58 am


-----------------------------------
ok, so you've loaded a file as stream

now it's the same as console based put/get

put : stream, "write to file"
get : stream, readVariable


does that make sense so far?

-----------------------------------
TheLastFall
Wed Oct 18, 2006 5:07 pm


-----------------------------------
Yes, that makes a lot of sense. Also my score interger is tm is it any problem because it's an interger?

-----------------------------------
TheLastFall
Thu Oct 19, 2006 11:46 am


-----------------------------------
Ok, I have to score being saved to txt. Just theres a little problem, ever time I enter my intials and new score it replaces the last one. Is there anything I can do? Also, I want it to say Enter Your Initials:  and have it visible at a certain point and a certain size. This is what I have 
drawfillbox (0, 0, maxx, maxy, 255)
Font.Draw ("Game Over", maxx div 2 - 400, maxy div 2 - 25, font, 119)
Font.Draw ("Enter Your Initials ", maxx div 2 - 200, maxy div 2 - 75, font1, 122)
colorback (255)
color (122)
get name
put : stream, name, "    ", tm

I would like to know how to make it so they can't type past three characters. I can get the name with no problem, but I can't see what I typed because its black and the size of what I typed is about the 12 sized font on MSoft Word. I there any way to have it so that the entering intials will be the same size as the  Enter Your Initials font and have it produced right after the text? I know there is a simple way but I'm just blinded right now.
Basically my txt file looks like this: 

Ass           283

But I want it more like:

Initials:     Score:
Ass            283
Rox            592

and so on.
I want the intial part to look like:

Game Over
Enter Your Initials {Ass}

I can easily figure out how to load up the screen of the scores.

-----------------------------------
neufelni
Thu Oct 19, 2006 4:15 pm


-----------------------------------
Ok, I have to score being saved to txt. Just theres a little problem, ever time I enter my intials and new score it replaces the last one. Is there anything I can do?
You need to use seek in order to add the new scores to the end of the file. Just add the word seek to your open file statement. Like this:
open : stream, "highscores.txt", get, put, seek
And then when you want to put your highscores to the end of the file, add this line before you you use the put statement to put the scores in the file:
seek : stream, *
The seek command makes it so that the next output will begin at the specified position in the file. The * makes it so that position is at the end of the file.

Also, I want it to say Enter Your Initials:  and have it visible at a certain point and a certain size. This is what I have 
drawfillbox (0, 0, maxx, maxy, 255)
Font.Draw ("Game Over", maxx div 2 - 400, maxy div 2 - 25, font, 119)
Font.Draw ("Enter Your Initials ", maxx div 2 - 200, maxy div 2 - 75, font1, 122)
colorback (255)
color (122)
get name
put : stream, name, "    ", tm

I would like to know how to make it so they can't type past three characters. I can get the name with no problem, but I can't see what I typed because its black and the size of what I typed is about the 12 sized font on MSoft Word. I there any way to have it so that the entering intials will be the same size as the  Enter Your Initials font and have it produced right after the text? I know there is a simple way but I'm just blinded right now.
I explained how to get input from the user with Font.Draw in another thread. Here it is:

If you want to get input using Font.Draw, it is a bit more complicated. You have to get one letter at a time using a loop.


View.Set ("noecho, nocursor") % this makes it so that you can't see the user's input in the normal font.

var letter : string (1)
var fullword : string := ""
var font : int := Font.New ("Arial:12")

loop
    getch (letter)
    cls
    exit when letter = (KEY_ENTER)
    if letter = (KEY_BACKSPACE) and length (fullword) > 0 then
        fullword := fullword (1 .. length (fullword) - 1)
    else
        fullword := fullword + letter
    end if
    Font.Draw (fullword, 10, 10, font, 7)
end loop
And to get it so that the user can only enter three characters, just add an if statement to my program above so that length(initials) has to be   0 then
        fullword := fullword (1 .. length (fullword) - 1)
    else
        fullword := fullword + letter
    end if
    Font.Draw (fullword, maxx div 2 + 75, maxy div 2 - 125, font4, 117)
end for
That is what I have. I want the name/initials to be visable while being inputed while the Enter your initials is also on the screen. It just won't appear. I'd have to enter my intials and then the Enter your initials would appear.

-----------------------------------
neufelni
Fri Oct 20, 2006 10:07 am


-----------------------------------
Sorry about that. I figured out what's wrong. All you have to do is add the word mod to the end of your open stream statement, along with put, get, and seek.

And for the Font.Draw, you don't want to make the loop a for loop from 1 - 3. If you do this them the user won't be able to backspace and change it if he makes a mistake. All that you want to do is add another if statement in the else branch of the already existing if statement. Here is the code, and this time I believe everything is working fine:
View.Set ("noecho, nocursor") % this makes it so that you can't see the user's input in the normal font.

var stream : int
var letter : string (1)
var fullword : string := ""
var font : int := Font.New ("Arial:12")
var tm : int := 3

open : stream, "score.txt", get, put, seek, mod

Font.Draw ("Enter your Initials: ", 10, 10, font, 7)

loop
    getch (letter)
    cls
    exit when letter = (KEY_ENTER)
    if letter = (KEY_BACKSPACE) and length (fullword) > 0 then
        fullword := fullword (1 .. length (fullword) - 1)
    else
        if length (fullword) < 3 then
            fullword := fullword + letter
        end if
    end if
    Font.Draw ("Enter your Initials: ", 10, 10, font, 7)
    Font.Draw (fullword, 140, 10, font, 7)
end loop

seek : stream, *
put : stream, fullword, " ------ ", tm 
Freakman just posted a function for getting input from the user with Font.Draw. You should check it out, as it is better than mine.
http://www.compsci.ca/v2/viewtopic.php?t=13777

-----------------------------------
Clayton
Fri Oct 20, 2006 12:53 pm


-----------------------------------
Did you really read all of those tutorials thouroghly? All of the questions you have asked so far have been in there (except for the mod question). Read more carefully next time ;)

Another thing you can do to get and display in font is [url=http://compsci.ca/v2/viewtopic.php?t=13777]use the function i published to do that (its a better idea to compartmentalize your code) and is done almost exactly the same way as your method above.

-----------------------------------
TheLastFall
Fri Oct 20, 2006 1:44 pm


-----------------------------------
Yea I read them just nothing processes. I do have a problem still with the Draw.Font the text 

Font.Draw ("Enter your initials: ", 10, 10, font, 7)

Will not display until after the intials have been typed and it will not show the initials while the player is typing it. Saving the scores isn't hard at all, thats done and out of the way. Just it will be waiting for input once the game ends and you have to type in the initials then while nothing is visable on the screen. The Enter your initials:  will not be displayed while you type in your initials.  I looked at the link I tested out the code, it did the exact same thing.

-----------------------------------
neufelni
Fri Oct 20, 2006 1:59 pm


-----------------------------------
Did you read my last post on the previous page. The code I posted displays Enter your initials: before and while the user inputs their initials. If you want to use Freakman's function then you just have to add this line:
Font.Draw("Enter your name: ", 0, 0, font, 7)
into the function before the loop and right before the Font.Draw statement in the loop.

-----------------------------------
TheLastFall
Fri Oct 20, 2006 2:07 pm


-----------------------------------
Ok, it does work I just didn't read it correctly but there is one thing about Freakmans code, there is no backspace.

function gets (xpos, ypos, font, clr : int) : string
    var a : string (1)
    var word : string := ""
    loop
        getch (a)
        exit when a = '\n'
        if word ~= "" and ord (a) < 3 then
            word := word (1 .. * -1)
            Font.Draw (a + "" + a, xpos, ypos, font, clr)
        else
            if a = (KEY_BACKSPACE) and length (word) > 0 then            
                word := word (1 .. length (word) - 1)
                a := a (1 .. length (a) - 1)
            end if
            if a ~= "\b" then
                word += a
            end if
        end if
        Font.Draw (word, xpos, ypos, font, clr)
    end loop
    result word
end gets

var name : string := ""
put "Please enter your name"
name := gets (maxx div 2, maxy div 2, Font.New ("serif:30"), red)

That works perfectly it shows up and everything, just one slight problem, I can't erase if I mess up while typing the program. I tried to take the formula that had been posted before but it doesn't work. All it does is just goes back to the position that it was before but it doesn't delete the previously typed letter. I'm quiet sure if I add a cls into the code it will also get rid of the previous letters typed. Would I have to have it a  drawfillbox to erase it? Thats the only way I actually see it able to work.

-----------------------------------
TheLastFall
Fri Oct 20, 2006 2:08 pm


-----------------------------------
Sorry I messed up entering some of the code on the last post, here it is:

var chars : array char of boolean

Input.KeyDown (chars)
function gets (xpos, ypos, font, clr : int) : string
    var a : string (1)
    var word : string := ""
    loop
        getch (a)
        exit when a = '\n'
        if word ~= "" and ord (a) < 3 then
            word := word (1 .. * -1)
            Font.Draw (a + "" + a, xpos, ypos, font, clr)
        else
            if a = (KEY_BACKSPACE) and length (word) > 0 then            
                word := word (1 .. length (word) - 1)
                a := a (1 .. length (a) - 1)
            end if
            if a ~= "\b" then
                word += a
            end if
            if chars (KEY_BACKSPACE) then
                word += a
            end if
        end if
        Font.Draw (word, xpos, ypos, font, clr)
    end loop
    result word
end gets


-----------------------------------
neufelni
Fri Oct 20, 2006 2:17 pm


-----------------------------------
Yes, Freakman's function does have backspace. This part of Freakman's function:
if word ~= "" and ord (a) < 3 then
            word := word (1 .. * -1) 
does the same thing as this from my code:
if letter = (KEY_BACKSPACE) and length (fullword) > 0 then
        fullword := fullword (1 .. length (fullword) - 1) 
So you don't need to add my if statement for backspace into his function.

-----------------------------------
TheLastFall
Fri Oct 20, 2006 2:30 pm


-----------------------------------
This probably will be the last question. I tried the code to only have three letters but it didn't work. Where would I have to add it into this code?

setscreen ("graphics:max;max,Title:Font Test")

View.Set ("noecho, nocursor")
var chars : array char of boolean
var font1 : int := Font.New ("serif:30")
Input.KeyDown (chars)
drawfillbox (0, 0, maxx, maxy, 255)
function gets (xpos, ypos, font, clr : int) : string
    var a : string (1)
    var word : string := ""
    loop
        View.Update
        getch (a)
        exit when a = '\n'
        if a = (KEY_BACKSPACE) and length (word) > 0 then
            word := word (1 .. length (word) - 1)
            a := a (1 .. length (a) - 1)
            cls
            drawfillbox (0, 0, maxx, maxy, 255)
            Font.Draw ("Enter your initials: ", maxx div 2 - 200, maxy div 2 - 75, font1, 117)
            Font.Draw (a + "" + a, xpos, ypos, font, clr)
        else
            if a ~= "\b" then
                word += a
            end if
            if length (word) < 3 then
                word := word
            end if
        end if
        Font.Draw (word, xpos, ypos, font, clr)
        setscreen ("offscreenonly")
    end loop
    result word
end gets
var name : string := ""
Font.Draw ("Enter your initials: ", maxx div 2 - 200, maxy div 2 - 75, font1, 117)
name := gets (maxx div 2 + 125, maxy div 2 - 75, font1, 117)


-----------------------------------
neufelni
Fri Oct 20, 2006 3:31 pm


-----------------------------------
Rather than putting it in an if statement all by itself, just add it to the if a ~= "\b" statement using and. Here is the fixed code. I also changed some other things. You don't need to draw a black fill box to make the background black. Rather you can use the colorback command. Also you can put all of your setscreen and View.Set stuff into 1 View.Set statement. Here is your code with all those changes.
View.Set ("graphics:max;max,Title:Font Test, noecho, nocursor, offscreenonly")
colorback(black)
cls

var chars : array char of boolean
var font1 : int := Font.New ("serif:30")
Input.KeyDown (chars)
function gets (xpos, ypos, font, clr : int) : string
    var a : string (1)
    var word : string := ""
    loop
        View.Update
        getch (a)
        exit when a = '\n'
        if a = (KEY_BACKSPACE) and length (word) > 0 then
            word := word (1 .. length (word) - 1)
            a := a (1 .. length (a) - 1)
            cls
            Font.Draw ("Enter your initials: ", maxx div 2 - 200, maxy div 2 - 75, font1, 117)
            Font.Draw (a + "" + a, xpos, ypos, font, clr)
        else
            if a ~= "\b" and length(word) < 3 then
                word += a
            end if
        end if
        Font.Draw (word, xpos, ypos, font, clr)
    end loop
    result word
end gets
var name : string := ""
Font.Draw ("Enter your initials: ", maxx div 2 - 200, maxy div 2 - 75, font1, 117)
name := gets (maxx div 2 + 125, maxy div 2 - 75, font1, 117)



-----------------------------------
TheLastFall
Fri Oct 20, 2006 4:47 pm


-----------------------------------
Ok, this will be the last question, I've got everything down. Is there anyway to save the text to a certain file, no matter where the file is located? I'm planning to put it in a folder at school so everyone can access the game, but I'd like all the scores to be situated in one folder, the folder is linked to everyones file. Is there a way they can play the game from their computers and the scores be all saved to one location?

-----------------------------------
TheLastFall
Fri Oct 20, 2006 4:58 pm


-----------------------------------
Ok, I'm pretty sure I figured out how to do it.  :D I just had the dashes going the wrong way. Ironically I can't wait till monday. Thank you guys a lot for all the help. The only things I'm going to do now is just make the game a bit harder, maybe have a retry option. Well thanks again for all your help, if you want to see what the game looks like (It's really corny) just message me or something. Well thanks for your help.

-----------------------------------
neufelni
Fri Oct 20, 2006 5:33 pm


-----------------------------------
Why not post it in[url=http://www.compsci.ca/v2/viewforum.php?f=48&sid=48a6546f245c855df3e036a81f6ed77a]Turing Source Code so that everyone can see.

-----------------------------------
TheLastFall
Fri Oct 20, 2006 5:47 pm


-----------------------------------
I'll do that... later.

-----------------------------------
Clayton
Fri Oct 20, 2006 5:55 pm


-----------------------------------
how about instead of modifying my function, you actually modify the code outside of it. when you modify a function like that, you narrow its scope dramatically. just have some sort of segment of code in your main line (or wherever you happen to "get" the three letter string from) instead of modifying the function. That way, you can use gets for all sorts of things instead of just getting a three letter string. the whole point i am trying to make is that you want to make a program as dynamic as possible, and by limiting what can be entered can sometimes lessen that ability for dynamic functionality ;)

-----------------------------------
TheLastFall
Fri Oct 20, 2006 6:31 pm


-----------------------------------
Ok well, quick question I'm making another program so I can access the scores file, its all good just something about it annoys me.
The code is:

setscreen ("graphics:max,max;Title: Scores")
drawfillbox (0, 0, maxx, maxy, 255)
colourback (255)
color (119)
var stream : int
var total : string := ""
open : stream, "score.txt", read
read : stream, total
put total

Just I'm getting a box at the end of each line or string.
the out come looks like this:

Name:           Score:
Is there any way of getting rid of the boxes?
I'm also going to want the scores reapeat once it gets the the bottom of the screen. I mean well after a whole bunch of people put in their scores the top ones won't be visable. I'd really like to have scroll bars but it seems way to complex or I'd have it go to the bottom of the screen and just appear at the top a few spaces over.

-----------------------------------
TheLastFall
Mon Oct 23, 2006 7:43 am


-----------------------------------
Is there any possible way that I can make the file so that they can only write the score to it and not be able to change the scores? I know about hidden files and everything but that function is disabled on the school computers. Is there any programming way to hide the file?

-----------------------------------
Clayton
Mon Oct 23, 2006 12:18 pm


-----------------------------------
your best bet is to encrypt your file and keep your encryption method to yourself...

google encryption to see what you come up with, and search the forums, there have been many questions about encryption and you should be able to glean enough information to get you started if you research it enough.
