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

Username:   Password: 
 RegisterRegister   
 Reading Maze from File, Output is Backwards
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
vahnx




PostPosted: Wed Jan 04, 2012 12:21 pm   Post subject: Reading Maze from File, Output is Backwards

I'm working on a program to import mazes from text files. I notice that when I draw my array inside the GUI window, it prints it from bottom to top when I need to print it from top to bottom. I tried modifying my for loop but it just makes it even worse. When you click a level you can see the text output in the output window fine.


Maze.zip
 Description:

Download
 Filename:  Maze.zip
 Filesize:  6.96 KB
 Downloaded:  148 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Wed Jan 04, 2012 12:34 pm   Post subject: RE:Reading Maze from File, Output is Backwards

You probably read the file assuming the bottom left corner is your origin. That doesn't work in text files. Remember, in there, your origin is the top right corner. If you read it with your y variable starting at 0, it's going to read and save it upside down. Start your y variable from the highest point ex if your text file was 20 lines long, you'd start the count at 20 and work your way down.
vahnx




PostPosted: Wed Jan 04, 2012 3:11 pm   Post subject: Re: Reading Maze from File, Output is Backwards

Darn can't edit my post at all...

Well I've updated my code for the .t file it's much better! I had to detect carriage returns so stuffs alinging proper, but I'm still upside down! I think it's a for loop somewhere but having trouble finding it.
Edit: Added maxy + 200 - the Y axis on the drawing font for the walls to fix it.

Sorry for the long post below, spoiler doesn't seem to be hiding with a button for "Show"

Spoiler:

Turing:

var debug : boolean := false

/* Counts the number of mazes */
var dir := Dir.Current
var fileName : string
var stream : int := Dir.Open (dir)
var counter : int := 0
loop
    fileName := Dir.Get (stream)
    exit when fileName = ""
    if length (fileName) > 4 then
        if Str.Lower (fileName (length (fileName) - 3 .. length (fileName))) = ".txt" then
            counter += 1
        end if
    end if
end loop
Dir.Close (stream)

/* Adds the maze names into an array */
var file : array 1 .. counter of string
counter := 0
stream := Dir.Open (dir)
loop
    fileName := Dir.Get (stream)
    exit when fileName = ""
    if length (fileName) > 4 then
        if Str.Lower (fileName (length (fileName) - 3 .. length (fileName))) = ".txt" then
            counter += 1
            file (counter) := fileName
        end if
    end if
end loop


/* Load datafrom all mazes into arrays */
View.Set ("text")
var ch : char := ' '
var fileID : int
var row : int := 0
var column : int := 0

var numOfRows, numOfColumns : array 1 .. counter of int
for i : 1 .. counter                 % Go through each file
    open : fileID, file (i), get
    loop
        exit when eof (fileID)
        get : fileID, ch
        column += 1
        if ch = '\n' then
            column := 0
            row += 1
        end if
    end loop
    close : fileID
    row += 1
    %put file (i), "\n", column, " x ", row, "\n"
    %put "Putting ", row, " into numOfRows (", i, ")"
    numOfRows (i) := row
    %put "Putting ", column, " into numOfColumns (", i, ")"
    numOfColumns (i) := column
    column := 0
    row := 0
end for

for i : 1 .. counter
    %put numOfRows (i), 'x', numOfColumns (i)
end for

var maze : array 1 .. counter, 1 .. 99, 1 .. 99 of char
for i : 1 .. counter
    for y : 1 .. numOfRows (i)
        for x : 1 .. numOfColumns (i)
            maze (i, x, y) := '*' % Empties array
        end for
    end for
end for


var charX : int := 0
var charY : int := 1

for i : 1 .. counter    % go through each file
    open : fileID, file (i), get
    loop
        exit when eof (fileID)
        get : fileID, ch

        /*
         charX += 1

         if charX = numOfColumns (i) then
         charX := 1
         if charY < numOfRows (i) then
         charY += 1
         end if
         end if

         if not ch = '\n' then

         %put charX, " ", charY, " ", maze (i, charX, charY), " is now ", ch
         maze (i, charX, charY) := ch
         end if
         */



        charX += 1
        if ch = '\n' then
            charY += 1
            charX := 0
        else
            maze (i, charX, charY) := ch
            %put charX, "/", charY, "=", maze (i, charX, charY), " " ..
        end if



    end loop
    %put " "
    charX := 0
    charY := 1
    close : fileID
end for

for i : 1 .. 3
    for c : 1 .. 3
        %put maze (1, c, i) ..
    end for
    %put ""
end for
%Input.Pause


View.Set ("graphics;offscreenonly")
var mouseX, mouseY, button, updown : int
var boxX, boxY : int := 0
var boxWidth, boxHeight : int := 5
var col : int := white
var fontSize : int := 7
var charFont : int := Font.New ("Sans Serif:" + intstr (fontSize))
var nameFont : int := Font.New ("Sans Serif:14")
var levelFontSize : int := 20
var levelFont : int := Font.New ("Sans Serif:" + intstr (levelFontSize))
var heightY : int := 60
var topText : string := ""

var firstTime : boolean := true
var outputID : int

for i : 1 .. 3
    for s : 1 .. 3
        %put maze (1, i, s) ..
    end for
end for


loop
    Mouse.Where (mouseX, mouseY, button)
    Draw.FillBox (0, 0, maxx, maxy, black)
    Font.Draw (topText, maxx div 2 - 40, maxy - 50, nameFont, white)
    topText := "Select a Level"

    for i : 1 .. counter
        heightY := 60
        boxX := i * 50 - 30

        if boxX > maxx - 50 then
            boxX -= maxx - 39
            heightY -= 35
            %levelFontSize -= 12
            %Font.Free(levelFont)
            %levelFont := Font.New ("Serif:" + intstr(levelFontSize))
        end if


        if mouseX >= 20 + boxX and mouseX <= 50 + boxX and mouseY >= heightY - 10 and mouseY <= heightY + 20 then
            col := yellow
            % DRAW PREVIEW
            topText := file (i)
            for y : 1 ..numOfRows (i)
                for x : 1 .. numOfColumns (i)

                    Font.Draw (maze (i, x, y), x * fontSize + (maxx div 6), (maxy - y * fontSize + (maxy div 3))-250, charFont, white)
                   
                    %Font.Draw (intstr (x) + "x" + intstr (y), x * 40 + (maxx div 6), y * 40 + (maxy div 2), defFontID, white)
                end for
            end for

            if button = 1 then
                if not firstTime then
                    Window.Close (outputID)
                end if
                firstTime := false
                Mouse.ButtonWait ("up", mouseX, mouseY, button, updown)
                outputID := Window.Open ("text,position:" + intstr (maxx * 2 - 30) + "," + intstr (maxy - 300))

                for y : 1 .. numOfRows (i)
                    for x : 1 .. numOfColumns (i)
                        put maze (i, x, y) ..
                    end for
                    put ""
                end for


                open : fileID, file (i), get
                loop
                    exit when eof (fileID)
                    get : fileID, ch
                    charX += 1

                    if charX = numOfColumns (i) then
                        charX := 1
                        if charY < numOfRows (i) then
                            charY += 1
                        end if
                    end if

                    %put ch ..
                end loop
                close : fileID

                if debug then
                    put "DONE"
                end if


                Window.SetActive (defWinID)
            end if
        else
            col := white
        end if

        %Draw.FillBox (20 + boxX, heightY - 10, 50 + boxX, heightY + 20, blue)
        %Draw.Box (20 + boxX, heightY - 10, 50 + boxX, heightY + 20, brightred)
        Font.Draw (intstr (i), boxX + 20, heightY - 5, levelFont, col)
    end for

    View.Update
    cls
end loop

dffhkhksg




PostPosted: Wed Jan 04, 2012 11:51 pm   Post subject: Re: Reading Maze from File, Output is Backwards

I did not read your full code, but I assume you are using a for loop to read the columns/rows of the maze, correct? To "flip" the map upside down, either you must flip the source data (meta file, w/e) or flip the way it is read. To do this you simply reverse the for loop for the rows.

Psuedo-code:

for row : 1..maxrows

is changed to

for decreasing row : maxrows..1

Hope I helped Very Happy
chipanpriest




PostPosted: Fri Jan 06, 2012 11:48 pm   Post subject: Re: RE:Reading Maze from File, Output is Backwards

Raknarg @ Wed Jan 04, 2012 12:34 pm wrote:
Remember, in there, your origin is the top right corner.


Wouldnt it be the top left corner?
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  [ 5 Posts ]
Jump to:   


Style:  
Search: