Computer Science Canada

Reading Text from a file

Author:  scottyrush13 [ Mon Jan 10, 2005 9:42 pm ]
Post subject:  Reading Text from a file

ok i know there is a tutorial on how to open a file and get like inforation, but i am lost on how i would open the file and just get the program to read every second line into an array. (BTW this is for my pacman, i have plotted in a file where the dots are going for pacman to eat, and i have 2 arrays (for the x and y location of the dots)). But i dont know how to get the program to read every second line into the y array, and the other lines into the x array.

example

25<--x
25<--y
50<--x
25<--y

how do i get it to read the x values into the x array and same for the y array.

Thank you for your help once again (to whomever decides to help me)

Author:  MysticVegeta [ Tue Jan 11, 2005 7:54 am ]
Post subject: 

Simply create 2 arrays and make them 1..2 of int here is the example i made for you

Author:  scottyrush13 [ Tue Jan 11, 2005 12:24 pm ]
Post subject: 

ok i kinda changed how im doing things, i now have 2 seperate files (1 for the X Co-ordinates, and one for the Y Co-ordinates), after i get the coordinates from the files how do i draw the pac dots? cause i'd have to get an x value from the x array and then the matching y coordinate from the y array...any insight would be greatly appreciated

Author:  MysticVegeta [ Tue Jan 11, 2005 5:23 pm ]
Post subject: 

scottyrush13 wrote:
ok i kinda changed how im doing things, i now have 2 seperate files (1 for the X Co-ordinates, and one for the Y Co-ordinates), after i get the coordinates from the files how do i draw the pac dots? cause i'd have to get an x value from the x array and then the matching y coordinate from the y array...any insight would be greatly appreciated


Do you mean drawovals? cause the x's and y's will make you a box Confused

Author:  scottyrush13 [ Tue Jan 11, 2005 6:06 pm ]
Post subject: 

the x and y coordinates are the locations of where the dots will be....

Author:  Cervantes [ Tue Jan 11, 2005 8:37 pm ]
Post subject: 

You have an x array and a y array, and they both have the same lower and upper bounds (therefore they have the same number of elements.) If you read the information from your text files correctly (and if you wrote the text files correctly Wink) element one of the x array should match with element two of the y array. Element 2 of x array should match element 2 of y array. etc. So, to draw your dots:

code:

for i : 1 .. upper (xArray) %or upper (yArray).  It doesn't matter, since they should be the same
drawdot (xArray (i), yArray (i))
end for

Author:  scottyrush13 [ Tue Jan 11, 2005 10:23 pm ]
Post subject: 

lol i ended up figuring it out on my own...just had to fiddle a bit.

thanks anyways for your help...

one more question...WHO IS CERVANTES? and why does he seem to be the only person willing/able to help me?

oh well, your advice has guided me through my program so far...i thank you Very Happy

Author:  scottyrush13 [ Wed Jan 12, 2005 12:28 pm ]
Post subject: 

*sigh*...well i guess you guys knew this was comming, but once again that little yellow SOB wont work, (actually this has nothing to do with him, more then the dot problem)
this is what i have (im trying to read the X values into an array from a .txt file
code:

var one:int
var pacdotsx:array 1..125 of int
var pacdotsy:array 1..125 of int

open : one, "PacdotsX.txt", get
for s : 1 .. 125
    get : one, pacdotsx (s)
end for
    end loop


anyhelp would be appreciated, and thanks to Cervantes and MysticVegeta for their help thus far

Author:  Cervantes [ Wed Jan 12, 2005 7:27 pm ]
Post subject: 

What error are you getting? I'm going to assume doing this will fix it:
code:

var one:int
var pacdotsx:array 1..125 of int
var pacdotsy:array 1..125 of int

open : one, "PacdotsX.txt", get
for s : 1 .. 125
    exit when eof (one)
    get : one, pacdotsx (s)
end for


You know, another way you could go about this is make a text file with a bunch of numbers in the form of a map. Then use a 2D array to store the information from the file. Then draw the stuff to the screen, based on the value of each element of the array (eg. 0 could be blank, 1 could be a dot, 2 could be pacman) and the location of that element in the array).

scottyrush13 wrote:

WHO IS CERVANTES?

He wrote Don Quixote. Smile http://www.donquixote.com/english.html

Author:  scottyrush13 [ Wed Jan 12, 2005 10:10 pm ]
Post subject: 

once again, i have had a question answered by cervantes...you have an answer to all questions it seems, and for that i am most thankful I am not worthy

Author:  scottyrush13 [ Thu Jan 13, 2005 11:46 am ]
Post subject: 

cervantes, i am stuck again lol sorry!

i just have a simple question, when opening the file i get the error "Eof attempted on incompatible stream number 0."

can someone answer this....(5 bits says that cervantes is the person that'll help, hes the only one who loves me enough to help Sad ....BUT HIS LOVE IS PURE Very Happy )

Author:  Cervantes [ Thu Jan 13, 2005 12:07 pm ]
Post subject: 

I've never had that problem, personally. But I would assume that it means you're trying to do an eof on a filestream that isn't valid.
Make sure that your
code:

exit when eof (filestream)

refers to a valid filestream.
If that's not it... we'll figure it out soon Smile
-Cervantes

Author:  scottyrush13 [ Thu Jan 13, 2005 12:17 pm ]
Post subject: 

it was a simple error that my good buddy nikk..(buen) solved, the problem was that i didnt have streamin equaling anything, so it was 0, and it didnt work

code:

open : streamin, "PacdotsX.txt", get
streamin := 1
for s : 1 .. 125     
exit when eof (streamin)
    get : streamin, pacdotsx (s)


notice the streamin:=1? that was the problem...lol, but thank you for your help a ton cervantes......i love you! Laughing

Author:  scottyrush13 [ Fri Jan 14, 2005 12:13 pm ]
Post subject: 

i have a problem again Sad

its places dots...but not the dots i plotted! and only 5 of them...here is my program so far and please help me if you could!

Author:  Cervantes [ Fri Jan 14, 2005 5:01 pm ]
Post subject: 

The problem now is the same as before. You need to make your open line link to an actual file. If you have the .txt files in the same directory as your .t file, you should be O.K. here. Otherwise, you'll have to change the string. Now, by having streamin := 1 just after you open the second file, you're screwing everything up.
Try taking out both instances of the following line:
code:

streamin := 1

Or, you could change the second instance of steamin := 1 to steamin := 2. But I wouldn't recommend that.

Also, you shouldn't declare variables inside your loop. Nor should you get information from files and store them to the array, inside your loop. The reason being, the information in the file does not change. So, getting "new" information (that's not really new) each time through the loop merely slows your program down.
Lastly, carefully consider the location of the code that draws your dots.

-Cervantes

Author:  scottyrush13 [ Mon Jan 17, 2005 12:09 pm ]
Post subject: 

im still having problems, i cant get the program to draw my dots, its driving me crazy!!!!!

here is my program and can someone please help

Author:  Cervantes [ Mon Jan 17, 2005 3:59 pm ]
Post subject: 

Cervantes wrote:
Also, you shouldn't declare variables inside your loop. Nor should you get information from files and store them to the array, inside your loop.

You've taken care of the variables -- good. Just move the code that gets information from the files to someplace before the loop. Also, just get rid of that steamin := 1 ; streamin2 := 2 stuff. I mostly said that to show you something interesting. You don't need it, and if you were to do something with files before that it'd screw things up.
Lastly, you aren't drawing the dots anywhere in your loop. Your drawing code should look like this:
Turing:

    for i : 1 .. upper (pacdotsx)
        drawfilloval (pacdotsx (i), pacdotsy (i), 3, 3, green)
    end for

Since upper (pacdotsx) and upper (pacdotsy) should return the same value, you shouldn't get any "array subscript out of range" errors. Mind you, if they aren't the same, you might get an error, depending on which is less. In this case, if pacdotsy has less elements then pacdotsx, you'll get an error. To fix this:
Turing:

    for i : 1 .. min (upper (pacdotsx), upper (pacdotsy))
        drawfilloval (pacdotsx (i), pacdotsy (i), 3, 3, green)
    end for

That picks the lower of the two. With this, you won't get an error, you'll just not draw some dots. But that's not the fault of the code, that would be the fault of the file.

Lastly, just make sure to have your drawing code before the View.Update, and after the cls. Wink
-Cervantes

Author:  scottyrush13 [ Mon Jan 17, 2005 4:44 pm ]
Post subject: 

i am most grateful for your explanation cervantes....at the moment i am on a computer that does not have the turing program so i have to wait untill i am at school or at my house before i can try that out and fix up my computer....but what you says makes sense and should fixens my problem right up!

thanks

Author:  r0ssar00 [ Mon Jan 17, 2005 8:58 pm ]
Post subject: 

i have a bit of an idea, it doesn't require two files, i was having this problem mself
code:

const maxDots:int:= 50 % put number of dots in here
var x, y : int
var fileName : string := "./dots.txt"
var fileNum : int
open : fileNum, fileName , get
for tx:1..maxDots
   for ty:1..maxDots
      get : fileNum, x,y
      Draw.Dot (x,y,green)
   end for
end for
i hope this helps, oh, here's some code to put the dots in the file
make sure to put each in their own file, first is for reading, second is for writing
code:

var x,y, butn, button : int
const maxDots : int : = 50
var fileNum:int
var fileName : string := "./dots.txt"
open : fileNum, fileName, put
for tx:1..maxDots
   for ty:1..maxDots
      Mouse.ButtonWait ("down", x, y, butn, button) % ignore the butn and button
      put : fileNum, x, chr (10), y % the chr(1) is an enter
   end for
end for

Author:  scottyrush13 [ Mon Jan 17, 2005 9:37 pm ]
Post subject: 

YES!!! it now displays the dots...ALL of them...exactly where i want them!!!!

now all i have to do is make it so that when pacman runs over the dots he eats them.....if anyone wants to suggest how to do it go right ahead...but im not neccessarily begging you to!

i kinda wanna try and figure this one out on my own!

thank you to everyone who helped me get through the last hurdle in my program!

Author:  scottyrush13 [ Mon Jan 17, 2005 9:58 pm ]
Post subject: 

ok just a question...i was thinking of using whatdotcolour to determine when pacman hit a dot, im almost positive that would work, but how would i make the dot disapear and how would i get the program to exit when the player gets all of the dots?

Author:  scottyrush13 [ Tue Jan 18, 2005 8:19 pm ]
Post subject: 

ok...above question...urgently needs answering if possible....thank you in advance


-scotty

Author:  Cervantes [ Tue Jan 18, 2005 8:32 pm ]
Post subject: 

Since they're small ovals, you could employ Math.Distance. As for making the dot disappear, the simplest way would be to use a flag variable. But don't forget to also wrap your collision detection code in the if flag then statement. As for checking if their all gone, for loop through all the dots and if each and every flag is set to true (or false, but they all have to be false. If your flag variable is called dotEaten, you'd probably be looking for true), then exit.
A better way would be using flexible arrays Smile

Author:  scottyrush13 [ Tue Jan 18, 2005 9:18 pm ]
Post subject: 

thank you cervantes i'll try that out tommorow in computer science class, i cannot right now because i need to finish my english homework...i dont understand all of the things like the flag variable, but im sure i can find a tutorial or something about it elsewhere on the site...thanks a ton man

Author:  scottyrush13 [ Tue Jan 18, 2005 9:59 pm ]
Post subject: 

ok i just tried searching and i have to make a boolean array right...so i made that. But how do i make this new array know the coordinates of all of the dots?

Author:  scottyrush13 [ Wed Jan 19, 2005 12:10 pm ]
Post subject: 

can some one offer assistance on these boolean arrays? and how to read the values into this new array, so it knows which dot is which and the location of the dot?

this is due soon and i really need this...please help!

Author:  scottyrush13 [ Thu Jan 20, 2005 12:00 pm ]
Post subject: 

i just found out that at school we have turing 4.0.4 so i cannot use Math.Distance, what is another way i can do this? can someone respond quickly? thank you a ton in advance

Author:  Bacchus [ Thu Jan 20, 2005 7:21 pm ]
Post subject: 

make a distance function of your own, it isnt to hard (basically pythagorean theorm)

function dist(x,y,x1,y1:int):real
result ((x1-x)**2+(y1-y)**2)**0.5
end dist

Author:  Cervantes [ Thu Jan 20, 2005 8:34 pm ]
Post subject: 

scottyrush13 wrote:
can some one offer assistance on these boolean arrays? and how to read the values into this new array, so it knows which dot is which and the location of the dot?

this is due soon and i really need this...please help!

The boolean array doesn't store the location of the dots. It stores information as to which dots are eaten and which aren't. The thing is, element n in your boolean array corresponds to element n of your x array and your y array as well. You don't really have an object, just a bunch of arrays that work together. If you had a record, it'd be a little better. But it's not necessary.

Author:  scottyrush13 [ Thu Jan 20, 2005 10:53 pm ]
Post subject: 

people im sorry, and im really trying but im just not good at this. I do not understand how this works, for instance, how am i going to get the dot to disapear, how does the boolean array know which dot from the other 2 arrays has been eatin? im pretty sure i can get the thing to quit when all of the dots are gone by using a counter...or something, but i have NO clue about this boolean array thing.

can someone post some examples of something similar to the problem im having so i can see how to make a boolean array? my current program (what i have so far) is attached also.

Author:  Cervantes [ Fri Jan 21, 2005 3:56 pm ]
Post subject: 

scottyrush13 wrote:
how does the boolean array know which dot from the other 2 arrays has been eatin?

By the element in question. If element 4 of the boolean array is false, don't draw the dot located at (dotsx (4), dotsy (4)).
Turing:

var flagArray : 1 .. 10 of boolean
for i : 1 .. ` upper (flagArray)
    flagArray := true
end for
var dotsX : 1 .. 10 of int
var dotsY : 1 .. 10 of int

loop

    %input
    %manipulate variables.  move pacman and the ghosts here, check if pacman has eaten the dots,
    % and if the ghosts have killed pacman.
    for i : 1 .. upper (flagArray)
        if not flagArray (i) and Math.Distance (pacmanX, pacmanY, dotsX (i), dotsY (i)) < pacmanRadius + dotRadius then
            %note that if not flagArray (i) is the same as if flagArray (i) = false.  It's also the same as if ~flagArray (i)
            flagArray (i) := false
        end if
    end for

    cls
    for i : 1 .. upper (flagArray)
        if flagArray (i) then
            drawfilloval (dotsX (i), dotsY (i), 3, 3, green)
        end if
    end for
    View.Update
    delay (7)
end loop

Hope that helps.

Author:  scottyrush13 [ Fri Jan 21, 2005 11:24 pm ]
Post subject:  thank you soooooo much

cervantes...you are a life saver...thank you for your help!!!! pacman now eats the dots!!! the screen flickers a whole bunch but im going to experiment with a few theories i have about the prob, and if that doesnt work im going to search the site....then i'll ask another question if im still having problems

anyways....

so far cervantes has answered AT LEAST 90% of my questions on this forum (and i've asked quite a few questions)

so i would like to thank cervantes for all of the time he has spent answering my questions

Author:  scottyrush13 [ Sat Jan 22, 2005 12:13 am ]
Post subject: 

ok i searched for a bit and couldnt find a solution to my problem... and i have another problem i just noticed...

1) my game still flickers quite a bit, BUT pacman does eat the dots!

and

2) something seems to be interfering with the colision detection for my walls because when pacman goes towards a wall from the right, or from above, he goes straight through the wall....

i seriously have no clue what is going on here

Author:  Cervantes [ Sat Jan 22, 2005 8:15 am ]
Post subject: 

Hard to say without seeing the recent code. Your last update didn't seem to have any problem with either flickering or collision.

Author:  scottyrush13 [ Sat Jan 22, 2005 12:29 pm ]
Post subject: 

here is my code so far....

Author:  scottyrush13 [ Sat Jan 22, 2005 12:30 pm ]
Post subject: 

here is my code so far....


::edit::
how come when i edited my last post (cause i forgot to add the x and y files...it made a new post, thats never happened to me before)

Author:  Cervantes [ Sat Jan 22, 2005 1:46 pm ]
Post subject: 

What in the... You were doing pretty well, and then this! Laughing
The reason it's flashing is because you've got two cls's, two sets of drawing code, and two View.Updates. Merge them.
The reason your collision is going wack-o is because of the following two if statements:
code:

    %controls moving left towards wall
    if whatdotcolour (pacx - pacradius + 1, pacy) = blue then
        pacx += 1
    end if

and
code:

    %controls moving down towards wall
    if whatdotcolour (pacx, pacy - pacradius + 1) = blue then
        pacy += 1
    end if

THINK about what you're doing in those two if statements. Really, it's quite simple.

-Cervantes

Author:  scottyrush13 [ Sun Jan 23, 2005 8:08 pm ]
Post subject: 

ok i made progress...i fixed the flickering...it was due to an extra cls that i didnt need!!! now all i need to do is fix the collision detection for the walls. And if cervantes says its simple im going to trust him...so i might be able to tackle this problem on my own

::edit::
wow that was simple!!! i cannot believe i didnt notice that before! ok i got both things fixed...so im happy!!! thank you cervantes, couldnt have done it without you

Author:  scottyrush13 [ Sun Jan 23, 2005 8:28 pm ]
Post subject: 

Cervantes wrote:
As for checking if their all gone, for loop through all the dots and if each and every flag is set to true (or false, but they all have to be false. If your flag variable is called dotEaten, you'd probably be looking for true), then exit.


ok just another question

right now im not sure, i've tried what i think you mean...but it doesnt work

code:
for i:1..upper (flagarray)
if flagarray(i)=false then
exit
end if
end for


is that kinda what you meant?? my teacher tought us diddly about boolean arrays, so i know nothing of them

Author:  Cervantes [ Sun Jan 23, 2005 8:43 pm ]
Post subject: 

Right, but there's one problem. If flagarray (1) is false, it'll exit. If not, if flagarray (2) is false, it'll exit. If neither of those are false, it'll check if flagarray (3) is false - if it is, it'll exit. You see where I'm going? You're checking if any of the elements of flagarray are false, and if one of them is false, it exits. What you want to do is check if all of them are false, then exit. A function is of great value here, because you can scan through all the elements of flagarray and as soon as one of them is true, you can exit the function and return false. But, if the for loop finishes scanning all the elements of flagarray and none of them are true, it will result true.

Turing:

fcn checkVictory : boolean
  for i : 1 .. upper (flagarray)
    if flagarray (i)  = true then
      result false
    end if
  end for
  result true
end function


Right-o?
-Cervantes

Author:  scottyrush13 [ Sun Jan 23, 2005 9:39 pm ]
Post subject: 

thank you again cervantes

ok i have one FINAL question and then im done my program!!!!!!!

i need it to say how long it took to finish the level, im using Time.Elapsed but it puts it in milliseconds!

how can i make it in seconds? or at least put like a decimal in it so that its 13.50 and not 1350?

Author:  scottyrush13 [ Sun Jan 23, 2005 9:57 pm ]
Post subject: 

nvm...i feel like such an idiot...i dont know why i never thought of it, all i had to do was divide the time by 60 and it would put the decimal in the appropriate place

Author:  Bacchus [ Sun Jan 23, 2005 9:59 pm ]
Post subject: 

hang on a min, you wanted to put milli seconds into seconds so you divided it by 60? ..... srry to ruin the moment but thats not right. 1 second = 1000 milliseconds so divide it by 1000 to put it into seconds

Author:  scottyrush13 [ Sun Jan 23, 2005 10:20 pm ]
Post subject: 

oh my Embarassed

thank you for pointing that out, or else it would have slipped right by me!

im just in a rush to finish this in time

Author:  scottyrush13 [ Sun Jan 23, 2005 10:37 pm ]
Post subject: 

ummm when i divide it by 1000 it says that i did the level in 1.291 seconds...when it took me like 19 seconds...wtf

how do i fix this?

Author:  scottyrush13 [ Sun Jan 23, 2005 10:40 pm ]
Post subject: 

when i divide it by 100 it works fine...so whatever

Author:  scottyrush13 [ Sun Jan 23, 2005 11:12 pm ]
Post subject: 

i found out that i do need to divide it by 1000, but i had
code:
clock (timeBeaten)


it was outside of my loop for my main program and it needed to be inside

i am finally done my program and will post the finished version very shortly


i would like give a huge thanks to cervantes (i couldnt have done this program without you man), da short 1 (for your help with a couple problems), and i would also like to thank everyone else who helped me

its people like you that make this a great site to get help Very Happy

Author:  Leftover [ Sun Jan 23, 2005 11:32 pm ]
Post subject: 

Yeah, so many CS students be lost / screwed without it Very Happy

Author:  scottyrush13 [ Mon Jan 24, 2005 12:17 pm ]
Post subject: 

how can i make a dist function to do the same as math.distance? cause at school i dont have version 4.0.5 so i cant use math.distance

code:
for i : 1 .. upper (flagarray)
        if flagarray (i) and Math.Distance (pacx, pacy, pacdotsx (i), pacdotsy (i)) < pacradius + dotradius then
            flagarray (i) := false
        end if
    end for


pacx and pacy are pacmans position
and pacdotsx and pacdotsy are the arrays with the location of the pacdots

flagarray is the array of boolean values that know whether or not the dots are still there

could someone help me or tell me how to make a funtion to do the same as the above?

Author:  Bacchus [ Mon Jan 24, 2005 4:41 pm ]
Post subject: 

code:
function distance(x,y,x1,y1:real):real
result ((x1-x)**2+(y1-y)**2)**0.5
end distance
change the real crap at the top to suite ur need


: