Create a simple version of the board game:   Mastermind.

It must be possible to:

   choose the number of colors will be used in the game (2 - 20)
   choose the color code length (4 - 10)
   choose the maximum number of guesses the player has (7 - 20)
   choose whether or not colors may be repeated in the code


The (computer program) game should display all the player guesses and the results of that guess.

Display (just an idea):



 Feature 
 Graphic Version 
 Text Version


 Player guess

 Colored circles

 Alphabet letters


Correct color & position

Black circle

X


Correct color

White circle

O


None

Gray circle

 -




A text version example:        1.   ADEF   -   XXO- 

Translates to:

the first guess;

the four colors (ADEF);

result:

 two correct colors and spot, 
 one correct color/wrong spot, one color isn't in the code.
Happy coding!



Related tasks

   Bulls and cows
   Bulls and cows/Player
   Guess the number
   Guess the number/With Feedback


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()