Java: Notepad save game map

Source: Internet
Author: User





At first see this, what is your idea, what does this kid want to do???



This method is a one months ago to do a multi-threaded game when thought of, was originally a friend of South China's proposed. The recent blog is always in the "Fried leftovers", some new things are always too late to make a summary, but feel that this idea is really good, so also just take out and then discuss with you, maybe there is a better way.



We play some of the scenes do not move 2D games, often need to build some maps, such as Bubble Hall, tank wars and so on.






In the map construction, we often use an array to save the required map, but to do so, the need for more maps, save the efficiency is often low, but also consumes most of the code to fill in the array.



Save the map you want to do in a notepad and look more intuitive and concise. Using IO streams for byte reads is a relatively low error rate, so we can theoretically have 256 elements in our map (one byte for 8 bits). Of course, we may not need so much, a-Z plus 1 to 9 can fully meet our daily needs.



At the very beginning, someone asked, since it is OOP, why not use objects to save our map elements? The reason is simple: we operate byte data more efficiently than the object itself.



OK, the gossip is not much said, the method is as follows:



First, we need a ArrayList to keep the data, what's the use? Keep looking, you'll know.



Create an array of maps to hold data



Arraylist<byte> maplist=new arraylist<byte> ();



See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/



Java code


/ **
 * Read data from Notepad into an array
 * @param mapName The file name of the notebook
 * @return byte array
* /
public int [] [] createMap (String mapName) {
    File file = new File (mapName);
    // Create map array
    int map [] [] = null;
    if (file.exists ()) {
        try {
        InputStream in = new FileInputStream (file);
        // Create a byte array of the same size as the file
        byte content [] = new byte [in.available ()];
        // Read byte array
        in.read (content);
        // Add content to the queue
        for (int i = 0; i <content.length; i ++) {
            MapList.add (content [i]);
        }
        // Remove carriage return
        for (int i = 0; i <MapList.size (); i ++) {
            if (MapList.get (i) == 10) {
                       MapList.remove (i);
            }
        }
        // Remove newlines
        for (int i = 0; i <MapList.size (); i ++) {
            if (MapList.get (i) == 13) {
                       MapList.remove (i);
            }
        }
        // package byte content
        String string = new String (content);
        String arr [] = string.split ("\ r \ n");
        // Save in array
        int index = 0;
        map = new int [arr.length] [arr [0] .length ()];
        for (int i = 0; i <map.length; i ++) {
            for (int j = 0; j <map [i] .length; j ++ {
                    map [i] [j] = MapList.get (index) -48;
                    index ++;
            }
        }
                 in.close ();
        } catch (Exception e) {
                e.printStackTrace ();
        }
    } else {
                    System.out.println ("File does not exist");
    }
    return map;
} 


The more critical step is to remove the carriage return newline character, which we can't see, and only find them when we print.



Inexplicably more than 10 and 13, at first I also feel puzzled, any method of course to test, make a notepad map to try it.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.