Mastermind: Difference between revisions
Content added Content deleted
(Added Wren) |
Alextretyak (talk | contribs) (Added 11l) |
||
Line 54: | Line 54: | ||
* [[Guess the number/With Feedback]] |
* [[Guess the number/With Feedback]] |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<lang 11l>F encode(correct, guess) |
|||
[String] output_arr |
|||
L(i) 0 .< correct.len |
|||
output_arr [+]= I guess[i] == correct[i] {‘X’} E I guess[i] C correct {‘O’} E ‘-’ |
|||
R output_arr |
|||
F safe_int_input(prompt, min_val, max_val) |
|||
L |
|||
V user_input_str = input(prompt) |
|||
X.try |
|||
V user_input = Int(user_input_str) |
|||
I user_input C min_val .. max_val |
|||
R user_input |
|||
X.catch ValueError |
|||
L.continue |
|||
F 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 in a different position.’) |
|||
print() |
|||
V number_of_letters = safe_int_input(‘Select a number of possible letters for the code (2-20): ’, 2, 20) |
|||
V code_length = safe_int_input(‘Select a length for the code (4-10): ’, 4, 10) |
|||
V letters = ‘ABCDEFGHIJKLMNOPQRST’[0 .< number_of_letters] |
|||
V code = ‘’ |
|||
L 0 .< code_length |
|||
code ‘’= random:choice(letters) |
|||
[String] guesses |
|||
L |
|||
print() |
|||
V guess = input(‘Enter a guess of length #. (#.): ’.format(code_length, letters)).uppercase() |
|||
I guess.len != code_length | any(guess.map(char -> char !C @letters)) |
|||
L.continue |
|||
E I guess == code |
|||
print("\nYour guess "guess‘ was correct!’) |
|||
L.break |
|||
E |
|||
guesses.append(‘#.: #. => #.’.format(guesses.len + 1, Array(guess).join(‘ ’), encode(code, guess).join(‘ ’))) |
|||
L(i_guess) guesses |
|||
print(‘------------------------------------’) |
|||
print(i_guess) |
|||
print(‘------------------------------------’) |
|||
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 in a different position. |
|||
Select a number of possible letters for the code (2-20): 4 |
|||
Select a length for the code (4-10): 4 |
|||
Enter a guess of length 4 (ABCD): abcd |
|||
------------------------------------ |
|||
1: A B C D => X X O O |
|||
------------------------------------ |
|||
Enter a guess of length 4 (ABCD): abcc |
|||
------------------------------------ |
|||
1: A B C D => X X O O |
|||
------------------------------------ |
|||
2: A B C C => X X O X |
|||
------------------------------------ |
|||
Enter a guess of length 4 (ABCD): abdc |
|||
Your guess ABDC was correct! |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |