Jump to content

Mastermind: Difference between revisions

3,106 bytes added ,  4 years ago
Added Python example
(Added Python example)
Line 2,159:
end procedure
main()</lang>
 
=={{header|Python}}==
{{works with|cpython|3.7.3}}
Tested in Python 3.7.3. Includes input verification.
<lang python>
import random
 
 
def encode(correct, guess):
output_arr = [''] * len(correct)
correct_list = list(correct)
 
for i, (correct_char, guess_char) in enumerate(zip(correct, guess)):
if correct_char == guess_char:
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)
 
 
def safe_int_input(prompt, min_val, max_val):
while True:
user_input = input(prompt)
 
try:
user_input = int(user_input)
except ValueError:
continue
 
if min_val <= user_input <= max_val:
return user_input
 
 
def play_game():
print("Welcome to Mastermind.")
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 a different position.")
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)
code_length = safe_int_input("Select a length for the code (4-10): ", 4, 10)
 
letters = 'ABCDEFGHIJKLMNOPQRST'[:number_of_letters]
code = ''.join(random.choices(letters, k=code_length))
guesses = []
 
while True:
print('')
guess = input(f"Enter a guess of length {code_length} ({letters}): ").upper().strip()
 
if len(guess) != code_length or any([char not in letters for char in guess]):
continue
elif guess == code:
print(f"\nYour guess {guess} was correct!")
break
else:
guesses.append(f"{len(guesses)+1}: {' '.join(guess)} => {' '.join(encode(code, guess))}")
 
for i_guess in guesses:
print("------------------------------------")
print(i_guess)
print("------------------------------------")
 
 
if __name__ == '__main__':
play_game()
 
</lang>
{{out}}
<pre>
Welcome to Mastermind.
You will need to guess a random code.
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
Select a length for the code (4-10): 4
 
*omitted first guesses*
 
Enter a guess of length 4 (ABCD): ccdd
------------------------------------
1: A A B B => O - - -
------------------------------------
2: C C D D => X X - X
------------------------------------
 
Enter a guess of length 4 (ABCD): ccad
 
Your guess CCAD was correct!
</pre>
 
=={{header|REXX}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.