Thread: File I/O with arrays

  1. #31
    Registered User
    Join Date
    Sep 2006
    Posts
    27
    ...well, in just a few short lines you've pretty much done what it's taken me nearly 500 to do. Currently, all my location-relevant things are tied up in one class - like so

    Code:
    class location
    {
    public:
    location();                  //constructor
    ~location();                 //deconstructor
    void set_terrain(int x, int y, int terrtype);  //sets the terrain type at location x,y to type terrtype
    void set_itemno(int x, int y, int number);   //sets the number of items at location x,y
    void set_item(int x, int y, int stack); //sets the item type at location x,y in part 'stack' of the pile there
    void set_monst(int x, int y);   //sets any monsters at location  x,y
    void set_build(int x, int y);   //sets any buildings at location x,y
    void set_char(int x, int y);    //sets the character at position x,y
    void printloc(int x1, int y1, int x, int y);  //prints location x1,y1 at cursor location x,y
    int checkpass(int x, int y);    //checks if there is a wall/door at location x,y
    void set_move(char x);          //moves the character in direction x (78946123 inputs)
    int set_closedoor(char x);      //closes a door in direction x (78946123 inputs)
    int fileread(char filename[50]); //reads a file from file 'filename'
    int terrain[600][600];
    int itemno[600][600];
    int item[600][600][5];
    int monst[600][600];
    int build[600][600];
    };
    At the moment, there's an overlay of 4 other maps containing information about items, monsters, buildings (I'm not quite sure where I'm going with that one), etc. I'm thinking of making a global array and defining types via enum - something like

    Code:
    enum{ TERRAIN,
                ITEM,
                MONST,
                TYPES}
    
    int map [600][600][TYPES];
    Which would make things a lot easier.... Your movement code is so simple. Mine involves checking for keypress, then turning the keypress into a direction, then checking if there's anything in that direction, then moving or not moving. However, what makes it a lot more complex is that not all of the map is displayed on the screen - as you can see, there's the possibility for a map of up to 600x600 tiles (which I'll probably figure out a little better in the future when I get an idea for what a standard map size should be), but the map display will only be 100x30 (if I can ever figure out how to lock the console into those proportions), so I have to deal with four variables, the displayed x and y, and the actual x and y. Still, at the moment I can read a map and run around bumping into things and opening and closing doors. No time, monsters, or objects yet, but first I need to figure out a scrolling method and a look command, and maybe a save command too.

  2. #32
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    The thing about using a vector of strings is that while your method can have a map of 600x600 tiles mine can have a map of ... what's the value of n_pos again ... it's pretty big regardless? And what's even better about it is that the whole file io is so easy with strings. You don't have to set the max of anything! You could also pretty easily make it so that (with the strings), that the print() function will print only a grid of a custom display size, copying a section of the map to a temp vector of strings section, then printing that instead of the full one. Also, it should be pretty easy to change that display size every x movements accross, say 10 or 20 from the center so that the map isn't moving all the time, up down, or the other across. Aside - If you want the ability to go diagonally, I'd recommend using the num pad as opposed to the direction one.

    I like the look of this map project. I may do something further with the code that I wrote last night (surprisingly productive for 4 AM!), but not for a day or so.

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    27
    Hm, dwks told me about vectors in another thread, but I haven't seriously looked into them yet. Perhaps that would be a good idea.... My code is going to get very gluggy for multiple checks. I've just implemented a system that scrolls the map when the character gets within a certain distance of the edge of the screen (or not, if the map is too small or if it's already *at* the edge of the map), but since at the moment it reruns the mapprint() function every single time it scrolls up or down, it's S-L-O-W. Ideally, it'd be best to do something similar to what you say about printing in blocks - make the move without changing the display, then check each tile inside the screen block against its new value after the move to see if it's changed. If it has, *then* reprint that location, if not, don't.

    Currently, though, I'm working on implementing an 'examine' command... maybe once that's done I'll look into vectors.

    It is kind of addictive.... At the moment I have a larger map with a house-type thing with two doors that the character can (slowly, if any scrolling is required) wander about. Not exactly the next installment of Half-Life, and yet strangely addicting all the same. Maybe because it's different when you have an idea of just how difficult it is to make these things

    EDIT: Hitting some severe difficulties with my map refreshing function....

    Code:
    void location::refresh_temp()
    {
         for (int y = ydisp; y<31; y++) {
               for (int x = xdisp; x<101; x++){
                     for (int z = 0; z < TYPE ; z++){
                           mapt[x][y][z]=map[x][y][z];
                           }
                           }
                           }
    }
    
    void location::refresh_print()
    {
         int x1;
         int y1;
         x1 = 0;
         y1 = 0;
         for (int y = ydisp; y < (ydisp+10); y++){
               for (int x = xdisp; x < (xdisp+50); x++){
                     for (int z = 0; z<TYPE; z++){
                           if (mapt[x][y][z] != map[x][y][z]){
                                             locate.printloc(x,y,x1,y1);
                                             }
                                             }
                           x1++;
                                             }
               y1++;
                                             }
    }
    At the moment, it calls refresh_temp() before moving or changing anything, and refresh_print() after the move/change has been made. ydisp and xdisp are the top left corner actual x and y coordinates, and the printloc function variables are, in order, actual x, actual y, display x, display y.

    At the moment, the display doesn't change at all. I'm not sure if this is due to problems in the comparison or in the values passed to printloc or what, but it's really bugging me. If anyone can spot what I'm doing wrong here, I'd really appreciate it.
    Last edited by 20,000leeks; 09-18-2006 at 06:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM