Mastermind: Difference between revisions

507 bytes removed ,  4 years ago
→‎{{header|Python}}: Edited Python example to match rules as discussed under the section about the Julia implementation
(→‎{{header|Python}}: Edited Python example to match rules as discussed under the section about the Julia implementation)
Line 2,167:
def encode(correct, guess):
output_arr = [''] * len(correct)
correct_list = list(correct)
 
for i, (correct_char, guess_char) in enumerate(zip(correct, guess)):
output_arr[i] = 'X' if correct_charguess_char == correct_char else 'O' if guess_char: in correct else '-'
output_arr[i] = 'X'
correct_list.remove(guess_char)
 
for i, guess_char in enumerate(guess):
if output_arr[i] != '':
continue
elif guess_char in correct_list:
output_arr[i] = 'O'
correct_list.remove(guess_char)
else:
output_arr[i] = '-'
 
return ''.join(output_arr)
Line 2,203 ⟶ 2,191:
print("You will need to guess a random code.")
print("For each guess, you will receive a hint.")
print("In this hint, X denotes a correct letter, and O a letter in the original string but in a different position.")
print('')
print("X's and O's will only be given for a letter as many times as that letter occurs in the code.")
print('')
 
number_of_letters = safe_int_input("Select a number of possible letters for the code (2-20): ", 2, 20)
Line 2,215 ⟶ 2,202:
 
while True:
print('')
guess = input(f"Enter a guess of length {code_length} ({letters}): ").upper().strip()
 
Line 2,242 ⟶ 2,229:
For each guess, you will receive a hint.
In this hint, X denotes a correct letter, and O a letter in the original string but a different position.
X's and O's will only be given for a letter as many times as that letter occurs in the code.
 
Select a number of possible letters for the code (2-20): 4
Line 2,249 ⟶ 2,235:
*omitted first guesses*
 
Enter a guess of length 4 (ABCD): ccddcdaa
------------------------------------
1: A A B B => O -O - -
------------------------------------
2: C C D DA A => O X X - XO
------------------------------------
 
Enter a guess of length 4 (ABCD): ccadddac
 
Your guess CCADDDAC was correct!
</pre>