* Randomizes the brick input. * @return {Brick} Fluent interface
()
| 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. |
nothing calls this directly
no test coverage detected