Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Text file parsing

Text file parsing

Scheduled Pinned Locked Moved C#
csharpgame-devdata-structuresjsonhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zeikcied
    wrote on last edited by
    #1

    I have an unfinished C# puzzle game I wrote, and I want to change it to use a text file to load the levels by parsing the text. I don't know how exactly to do it. I would also like to be able to read a directory and list each text file in an array, so I don't have to alter the code to add new levels. Can someone please help?

    T L 2 Replies Last reply
    0
    • Z Zeikcied

      I have an unfinished C# puzzle game I wrote, and I want to change it to use a text file to load the levels by parsing the text. I don't know how exactly to do it. I would also like to be able to read a directory and list each text file in an array, so I don't have to alter the code to add new levels. Can someone please help?

      T Offline
      T Offline
      tjvictor
      wrote on last edited by
      #2

      you can search some data in msdn about File's read and write.Believe me , it is very easy.you can do that by yourself

      1 Reply Last reply
      0
      • Z Zeikcied

        I have an unfinished C# puzzle game I wrote, and I want to change it to use a text file to load the levels by parsing the text. I don't know how exactly to do it. I would also like to be able to read a directory and list each text file in an array, so I don't have to alter the code to add new levels. Can someone please help?

        L Offline
        L Offline
        LongRange Shooter
        wrote on last edited by
        #3

        There are a few processes you could look into for accomplishing this...but it depends on the design you want to do. First you need to know the exact data you need to save and determine if you want to expose this data in a simple text file. (cheaters can hack a text file to death) Then you need to determine if you can get all of the puzzle data into a single object. This helps drive your design. Finally I find the best way to parse such data is to let the system do as much work as possible. This can easily be accomplished simply by using a binary serializer / deserializer to accomplish the task. So the steps you would then follow -- if this looks good to you -- is as follows: Define your object and all collections within that object. Make certain that every class that makes up that puzzle object has the [Serializable] attribute added to the definition. Also make sure that your puzzle object consists of (hopefully) nothing but private data with getter/setter logic to encapsulate the data and expose the data to the serializer. Next research what you need to do to get a BinarySerializer cooking (which isn't much) and you will also need System.IO to do your file stuff. Then create your stream, create your serializer for your object type, then do a serializer.Serialize( stream, myPuzzleObject ); You will find your object is dumped into the filename the user defines if you use the FileDialog and you will find that when you do a serializer.Deserialize() that you have a fully populated puzzle object loaded. This is how I persist my Sudoku puzzles for my library displays, library adds, and current puzzle state saves.

        Z 1 Reply Last reply
        0
        • L LongRange Shooter

          There are a few processes you could look into for accomplishing this...but it depends on the design you want to do. First you need to know the exact data you need to save and determine if you want to expose this data in a simple text file. (cheaters can hack a text file to death) Then you need to determine if you can get all of the puzzle data into a single object. This helps drive your design. Finally I find the best way to parse such data is to let the system do as much work as possible. This can easily be accomplished simply by using a binary serializer / deserializer to accomplish the task. So the steps you would then follow -- if this looks good to you -- is as follows: Define your object and all collections within that object. Make certain that every class that makes up that puzzle object has the [Serializable] attribute added to the definition. Also make sure that your puzzle object consists of (hopefully) nothing but private data with getter/setter logic to encapsulate the data and expose the data to the serializer. Next research what you need to do to get a BinarySerializer cooking (which isn't much) and you will also need System.IO to do your file stuff. Then create your stream, create your serializer for your object type, then do a serializer.Serialize( stream, myPuzzleObject ); You will find your object is dumped into the filename the user defines if you use the FileDialog and you will find that when you do a serializer.Deserialize() that you have a fully populated puzzle object loaded. This is how I persist my Sudoku puzzles for my library displays, library adds, and current puzzle state saves.

          Z Offline
          Z Offline
          Zeikcied
          wrote on last edited by
          #4

          Ok, well, it's a puzzle game much like PopCap's Mummy Maze. The parts that I would put in a text file is simply the starting point of the player and enemy (or enemies), the exit, and the walls. All of which I currently have hard coded into an array. Since I only have one level, it seems nice enough, but on a 25x25 grid, and a two dimensional array, it takes a lot of lines of manual code to fill it up for just one level. That's why I don't want to have to hard code several levels. It would be so much easier if I could just compile the program once, and just add files as I go along, thus adding more levels. Sure, cheaters could just place themselves next to the exit, but what is the fun in that? As for all this serializing stuff...I don't think I have learned that yet. Also, I work much better with examples. Preferably commented examples. So, could you please point me toward one? I would very much appreciate it. I know how to open files and use the stremreader, but I only know how to use the StreamReader for one line at a time, though I suppose I could use a For loop to go through the characters and then use a Switch...Case statement to parse the text into the correct object for the array. But I was hoping for something easier. Assuming it is possible to make it easier.

          L 1 Reply Last reply
          0
          • Z Zeikcied

            Ok, well, it's a puzzle game much like PopCap's Mummy Maze. The parts that I would put in a text file is simply the starting point of the player and enemy (or enemies), the exit, and the walls. All of which I currently have hard coded into an array. Since I only have one level, it seems nice enough, but on a 25x25 grid, and a two dimensional array, it takes a lot of lines of manual code to fill it up for just one level. That's why I don't want to have to hard code several levels. It would be so much easier if I could just compile the program once, and just add files as I go along, thus adding more levels. Sure, cheaters could just place themselves next to the exit, but what is the fun in that? As for all this serializing stuff...I don't think I have learned that yet. Also, I work much better with examples. Preferably commented examples. So, could you please point me toward one? I would very much appreciate it. I know how to open files and use the stremreader, but I only know how to use the StreamReader for one line at a time, though I suppose I could use a For loop to go through the characters and then use a Switch...Case statement to parse the text into the correct object for the array. But I was hoping for something easier. Assuming it is possible to make it easier.

            L Offline
            L Offline
            LongRange Shooter
            wrote on last edited by
            #5

            Tip: If you want to learn by example, go to MSDN and search for the key items you want to do ... such as Serialize. You can find quite a bit of documentation and usage info out there. But here is something to get you started quickly. I used shorthand for the public accessors...you SHOULD know how to implement those!

             private PuzzleArray level = new PuzleArray();
             ...
             ///
             /// Saves a game in progress to the location used by the FileDialog.
             /// 
             private void SaveLevel ( PuzzleArray currentLevel, string path )
             {
                   // create a stream and use Create (opens if file exists or not)
                   using (FileStream stream = File.Create(path) )
                   {
                        // create the serializer
                        BinarySerializer ser = new BinarySerializer(typeof(PuzzleArray));
                        // save the file
                        ser.Serializer(stream, level);
                   }
             }
            
             /// /// This method is used for loading a new level or loading a saved level
             /// 
             private void LoadLevel ( ref PuzzleArray currentLevel, string path )
             {
                  using (FileStream stream = File.Open(path))
                  {
                       // create serializer
                       BinarySerializer ser = new BinarySerializer(typeof(PuzzleArray));
                       // load the file
                       currentLevel = (PuzzleArray) ser.Deserialize( stream );
                  }
             }
            

            ....
            /// /// VS2005 definition
            ///
            [Serializable]
            public class PuzzleArray : List{}
            [Serializable]
            public class Puzzle
            {
            Point playerPosition;
            public Point PlayerPostition{get; set;}
            Point enemyPosition;
            public Point EnemyPosition{get; set; }
            Point[] walls
            public Point[] Walls{get; set;}
            // An element that will not be serialized because of no public accessor.
            private Point lastMove = new Point();
            }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups