MCPcopy Create free account
hub / github.com/cryptii/cryptii / randomize

Method randomize

src/Encoder/Enigma.js:868–905  ·  view source on GitHub ↗

* Randomizes the brick input. * @return {Brick} Fluent interface

()

Source from the content-addressed store, hash-verified

866 * @return {Brick} Fluent interface
867 */
868 randomize () {
869 const model = EnigmaEncoder.getModel(this.getSettingValue('model'))
870 let i, index, rotor
871
872 // Gather rotor collection available to current model
873 let rotors = model.reflectorRotors
874 for (i = 0; i < model.slots.length; i++) {
875 rotors = rotors.concat(model.slots[i].rotors)
876 }
877
878 // Usually one set of rotors only contain a single rotor for each type
879 rotors = ArrayUtil.unique(rotors)
880
881 // Randomize rotor collection
882 rotors = ArrayUtil.shuffle(rotors)
883
884 // Pick a reflector
885 index = rotors.findIndex(rotor =>
886 model.reflectorRotors.indexOf(rotor) !== -1)
887 if (index !== -1) {
888 rotor = rotors.splice(index, 1)[0]
889 this.setSettingValue('reflector', rotor)
890 }
891
892 // Pick a rotor for each slot
893 for (i = 0; i < model.slots.length; i++) {
894 index = rotors.findIndex(rotor =>
895 model.slots[i].rotors.indexOf(rotor) !== -1)
896 if (index !== -1) {
897 rotor = rotors.splice(index, 1)[0]
898 this.setSettingValue(`rotor${i + 1}`, rotor)
899 }
900 }
901
902 // Randomize remainding settings
903 super.randomize()
904 return this
905 }
906
907 /**
908 * Generates a random plugboard setting value.

Callers

nothing calls this directly

Calls 6

getSettingValueMethod · 0.80
uniqueMethod · 0.80
shuffleMethod · 0.80
indexOfMethod · 0.80
setSettingValueMethod · 0.80
getModelMethod · 0.45

Tested by

no test coverage detected