Example #1
0
        private readonly int _KeyLength; // readonly will mean that the value for this field must be either set here (where it is declared) or in the constructor. After that, the value cannot be changed.

        public LockDriver()
        {
            // Set up the fields/properties with values
            // A standard KeyLock
            // 1) Build a key pattern for the lock
            int[] keySequence = { 5, 7, 2, 3, 5 };
            _KeyLength = keySequence.Length;

            List <KeyPin> key = new List <KeyPin>();

            key.Add(new KeyPin(5));
            key.Add(new KeyPin(7));
            key.Add(new KeyPin(2));
            key.Add(new KeyPin(3));
            key.Add(new KeyPin(5));

            _TheLock = new KeyLock(key);
            // Test of my lock
            _TheLock.Lock();
            if (!_TheLock.IsLocked)
            {
                throw new System.Exception("ERROR - Faulty lock");
            }
            _TheLock.Unlock(keySequence);
            if (_TheLock.IsLocked)
            {
                throw new System.Exception("ERROR - Can't unlock with correct key sequence");
            }
            // Display the key sequence for this lock
            DisplayKeySequence(keySequence);
        }
Example #2
0
        public void PickLock()
        {
            _TheLock.Lock(); // Start out by locking the lock
            int count = 0;   // to track the number of attempts

            int[] keySequence = new int[_KeyLength];
            while (_TheLock.IsLocked)
            {
                // attempt to pick the lock
                count++;
                IncrementKeySequence(keySequence);
                DisplayKeySequence(keySequence);
                _TheLock.Unlock(keySequence);
            }
            // Output how many tries it took to pick the lock
            Console.WriteLine($"The computer picked the lock after {count} attempts.");
            Console.WriteLine($"The key sequence is {string.Join(", ", keySequence)}");
        }