Help rewriting sketch: keypad-lock

to unlock, just press 123456

So, call checkPin() when z has the right value.

to lock, just press a random button on keypad

How would you distinguish "a random button" from entering a character in the password?

You could have an if test in the default case. If the door is unlocked, lock it and otherwise ignore the key press. Else, the key is to be added to the array.