*create implicit_control_flow true *create right_pos 0 *create wrong_pos 0 *create password "" *create attempts 0 // Array will unroll into a sequence of *create statements. *array combos 10000 0 *array used_guess 5 false *array used_combo 5 false *image mastermind.jpg *image mastertitle.png *temp my_guess "" // Fill all combinations. Probably redundant as combo #1234 is equal to "1234", // but it's more flexible if one wants to change number of 'colors' or remove repetitions. *temp counter 0 // The variable @i will be replaced with a unique symbol to avoid conflicts. *for @i 0 10 *for @j 0 10 *for @k 0 10 *for @l 0 10 *set combos[counter] ((@i&@j)&(@k&@l)) *set counter +1 I'll try to guess your code. Choose 4 digits (0-9), possibly with repetitions. Examples: 0000, 5678, 9966, 3455 To save you from giving me hints, enter it here. I'm not programmed to cheat! [i]The first guess will take a few seconds. Please, wait.[/i] *input_text password *label guess_loop Your secret code is ${password}. I will try to guess... // Pick first available password (not blanked out). *gosub find_valid My guess is ${my_guess}. *set attempts +1 *gosub compare password my_guess *if right_pos = 4 *goto done I have guessed ${right_pos} number(s) in the right position and ${wrong_pos} number(s) in the wrong position. // Remove all combos that wouldn't match. *gosub exclude_combos right_pos wrong_pos *page_break *goto guess_loop *label done I guessed your code with ${attempts} attempts. *ending *finish // Blank out all combos that can't be a match. // Note that *function parameters will be replaced with unique symbols. *function exclude_combos expected_right expected_wrong *temp still_valid 0 *for @i 0 10000 *if combos[@i] != "" *gosub compare my_guess combos[@i] *if (expected_right != right_pos) or (expected_wrong != wrong_pos) *set combos[@i] "" *else *set still_valid +1 ${still_valid} combinations are still good candidates. *function find_valid *set my_guess "" *temp offset 0 *rand offset 0 9999 *temp adjusted *for @j 0 10000 *set adjusted ((@j + offset) modulo 10000) *if combos[adjusted] != "" *set my_guess combos[adjusted] *return *return // Tests. *gosub test "0000" "5555" 0 0 *gosub test "1234" "1234" 4 0 *gosub test "1234" "4321" 0 4 *gosub test "1134" "4311" 0 4 *gosub test "1567" "4311" 0 1 *gosub test "1167" "4301" 0 1 *gosub test "1166" "6661" 1 2 *gosub test "1166" "1661" 2 2 *gosub test "6666" "1661" 2 0 *gosub test "1616" "6661" 1 2 *gosub test "1666" "1661" 3 0 *gosub test "6611" "1661" 2 2 *function test guess combo expected_right expected_wrong *gosub compare guess combo *if (expected_right != right_pos) or (expected_wrong != wrong_pos) FAILED ${combo} with ${guess}. (${expected_right} != ${right_pos}) or (${expected_wrong} != ${wrong_pos}) *else PASSED TEST ${combo} with ${guess}. (${expected_right} (${expected_wrong} *function compare @guess @combo // Count all pins in right position. *set right_pos 0 *set wrong_pos 0 *for @i 1 5 *set used_guess[@i] false *set used_combo[@i] false *if (@combo#@i) = (@guess#@i) *set right_pos +1 *set used_guess[@i] true *set used_combo[@i] true // Find pins in the wrong position (excluding those already counted). *for @i 1 5 *if not(used_guess[@i]) *for @j 1 5 *if (not(used_combo[@j])) *if (@guess#@i) = (@combo#@j) *set wrong_pos +1 *set used_combo[@j] true // Next "i" *set @j 5