Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
53
Task/Mastermind/11l/mastermind.11l
Normal file
53
Task/Mastermind/11l/mastermind.11l
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue