Example #1
0
        /// <summary>
        /// Deserialize the specified serializedLockGrid: see Serialize.
        /// </summary>
        /// <param name="serializedLockGrid">Serialized lock grid.</param>
        public static Models.LockGrid Deserialize(string serializedLockGrid)
        {
            Csv csv = new Csv (serializedLockGrid);
            int[] values = csv.ToIntArray ();
            int valueIndex = 0;

            // Deserialize the lock grid properties
            int rowCount = values [valueIndex++];
            int colCount = values [valueIndex++];
            int buttonCount = values [valueIndex++];
            int maxMovesFor2Stars = values [valueIndex++];
            int maxMovesFor3Stars = values [valueIndex++];
            Models.LockGrid lockGrid = new Models.LockGrid (rowCount, colCount, buttonCount, maxMovesFor2Stars, maxMovesFor3Stars);

            // Deserialize the locks and buttons
            for (int row=0; row<rowCount; row++) {
                for (int col=0; col<colCount; col++) {
                    Models.Lock currentLock = lockGrid.Locks [row, col];
                    // Deserialize the lock properties
                    //currentLock.UnlockedPosition = values [valueIndex++];
                    currentLock.CurrentPosition = values [valueIndex++];
                    // Deserialize the buttons
                    for (int buttonIndex=0; buttonIndex<buttonCount; buttonIndex++) {
                        Models.LockButton button = currentLock.Buttons [buttonIndex];
                        button.IsOn = (values [valueIndex++] == 1);
                        int linkedButtonRow = values[valueIndex++];
                        int linkedButtonCol = values [valueIndex++];
                        int linkedButtonIndex = values [valueIndex++];
                        if (button.IsUnlinked ()) {
                            button.LinkWithButton (lockGrid.GetButtonAt (linkedButtonRow, linkedButtonCol, linkedButtonIndex));
                        }
                    }
                }
            }

            return lockGrid;
        }