Beispiel #1
0
        /*
         * Having received the whole information saved in the file as to what
         * elements the game has, now this method is parsing it from string
         * and populating a new game object which could be returned to the main game and
         * take over the flow of information.
         */
        public static Game parseFromFile(String info)
        {
            //define deliminator of new line
            string [] delim = new string[1];
            delim[0]= (string)(System.Environment.NewLine);
            //split the text by new lines
            string[] splitText = info.Split(delim,System.StringSplitOptions.RemoveEmptyEntries);
            //get size from the string
            int size = Convert.ToInt32(splitText[0]);
            //get rate from the string
            int rate = Convert.ToInt32(splitText[1]);
            //create a new game object with the new size and rate
            Game theGame = new Game(size, rate);

            //set new deliminator
            delim[0] = ",";

            //start splitting each row into seperate values - for each value check if it is a zero or one and populate the cell
            for (int x = 2; x < splitText.Length; x++)
            {
                //split row into characters by the comma
                string[] array = splitText[x].Split(delim, System.StringSplitOptions.RemoveEmptyEntries);

                //for each of the characters check one or zero and set the value at row and column index to proper cell value
                for (int y = 0; y < array.Length; y++)
                {
                    //(x-2) is the row index because the 0th and the 1st element in the array for the rows (whose index is x) where size and rate
                    if (array[y] == "1") theGame.setValueAt(x - 2, y, true);
                    else theGame.setValueAt(x - 2, y, false);
                }
            }

            return theGame;
        }