# Introduction


Mastermind or Master Mind is a code-breaking game for two players. The modern game with pegs was invented in 1970 by Mordecai Meirowitz, an Israeli postmaster and telecommunications expert. It resembles an earlier pencil and paper game called Bulls and Cows that may date back a century or more. (Source Wikipedia)



# Rules


1. The Mastermind (computer) will select 4 colours. The colours are randomly selected from ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"]. Colours can be duplicated but there will always be exactly 4.

2. The Mastermind will return an array back to you. For every correctly positioned colour in the array an element of “Black” is returned. For every correct colour but in the wrong position an element of “White” will be returned.

3. Passing the correct array will pass the Kata test and return "WON!".

4. Passing an invalid colour will fail the test with the error "Error: you have given an invalid colour!"

5. Passing an invalid array length will fail the test with the error "Error: you must pass 4 colours!"

6. Guessing more than 60 times will fail the test with the error "Error: you have had more than 60 tries!"

7. All colours are capitalised

8. The return array will be shuffled!


# Task


Your task is to create a method called mastermind() that will take an object called  game. The object has already been preloaded so you do not need to worry about it.

Within your method you must pass an array into the game object method .check(). This will evoke the object to check your array to see if it is correct.


# Example

If the Mastermind selected the following colours

Then the array you are trying to solve is `["Red", "Blue", "Green", "Yellow"]`
So you guess with 

`["Red", "Orange", "Yellow", "Orange"]`

Your method would look like this.
```python
def mastermind(game):
  answer = game.check(["Red", "Orange", "Yellow", "Orange"])
```
The element `0 => Red` is at the correct index so `Black` is added to the return array. Element `2 => Yellow` is in the array but at the wrong index possition so `White` is added to the return array.

The Mastermind would then return `["Black", "White"]` (But not necessarily in that order as the return array is shuffled my the Mastermind).

Keep guessing until you pass the correct solution which will pass the Kata.

# Check result
To check the Masterminds return value
```python
  answer = game.check(["Red", "Orange", "Yellow", "Orange"])
  print (answer)
```

Good luck and enjoy!

# Kata Series
If you enjoyed this, then please try one of my other Katas. Any feedback, translations and grading of beta Katas are greatly appreciated. Thank you.

 Maze Runner
 Scooby Doo Puzzle
 Driving License
 Connect 4
 Vending Machine
 Snakes and Ladders
 Mastermind
 Guess Who?
 Am I safe to drive?
 Mexican Wave
 Pigs in a Pen
 Hungry Hippos
 Plenty of Fish in the Pond
 Fruit Machine
 Car Park Escape

Difficulty: introductory

Starter code:

def mastermind(game):
	

Solutions:
import itertools

def mastermind(game):
    colours = "Red Blue Green Orange Purple Yellow".split()
    def check(solution, guess):
        black = sum(a == b for a, b in zip(solution, guess))
        white = sum(max(0, min(solution.count(c), guess.count(c))) for c in colours) - black
        return ["Black"] * black + ["White"] * white
    candidates = list(itertools.product(colours, repeat=4))
    while candidates:
        candidate = candidates.pop()
        score = sorted(game.check(list(candidate)))
        candidates = [c for c in candidates if check(candidate, c) == score]

def mastermind(game):
    cols = {"Red", "Blue", "Green", "Orange", "Purple", "Yellow"}
    used = cols - set([c for c in cols if game.check([c]*4)])
    duff = (cols - used).pop()

    return [c for c in used for i in range(4) if game.check([duff] * i + [c] + [duff] *(3-i)) == ['Black']]

mastermind = lambda g: map(g.check, map(list, __import__('itertools').permutations(c for c in ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"] for _ in range(g.check([c] * 4).count('Black')))))

from itertools import permutations
def mastermind(game):
    colors = ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"]
    counts = []
    good = []
    for color in colors:
        attempt = [color, color, color, color]
        answer = game.check(attempt)
        if answer == 'WON!': 
            return
        for i in range(answer.count('Black')):
            good.append(color)
    for p in permutations(good):
        answer = game.check(p)
        if answer.count('Black') == 4: 
            return        

from random import choice
def mastermind(game):
    colors = ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"]
    answer = ''
    for i in range(60):
        my_choice = [choice(colors) for i in range(4)]
        answer = game.check(my_choice)

def mastermind(game):
    colors = ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"]
    answers = []
    confirmed = ["", "", "", ""]
    possible = ["", "", "", ""]
    not_a_color = 0
    
    for color in colors:
        answers.append(game.check([color, color, color, color]))
    
    i = 0
    while i < len(answers):
        if answers[i] == []:
            not_a_color = colors[i]
            i = len(answers)
        i += 1

    for c, possible in enumerate(answers):
        if possible:
            possible_color = colors[c]
            not_possible_color = not_a_color
            i = 0
            remaining = len(possible)
            while i < 4 and remaining > 0:
                possible = [possible_color if i == j else not_possible_color for j in range(4)]
                possible_answer = game.check(possible)
                if possible_answer[0] == "Black":
                    confirmed[i] = possible_color
                    remaining -= 1
                i += 1

    game.check(confirmed)

def mastermind(game):
    colors = ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"]
    answers = []
    confirmed = ["", "", "", ""]
    possible = ["", "", "", ""]
    not_a_color = 0
    
    for color in colors:
        answers.append(game.check([color, color, color, color]))

    i = 0
    while i < len(answers):
        if answers[i] == []:
            not_a_color = i
            i = len(answers)
        i += 1

    for c, possible in enumerate(answers):
        if possible:
            possible_color = colors[c]
            not_possible_color = colors[not_a_color]
            i = 0
            while i < 4:
                possible = [possible_color if i == j else not_possible_color for j in range(4)]
                possible_answer = game.check(possible)
                if possible_answer[0] == "Black":
                    confirmed[i] = possible_color
                i += 1

    game.check(confirmed)

from itertools import chain, permutations
colours = ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"]

def mastermind(game):
    possible = list(chain.from_iterable([c]*len(game.check([c]*4)) for c in colours))
    for p in permutations(possible):
        game.check(p)

def mastermind(game):
    colors = ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"]
    my_choice = [random.choice(colors), random.choice(colors), random.choice(colors), random.choice(colors)]
    answer = game.check(my_choice)
    if answer == ['Black','Black','Black','Black']:
        count_won += 1
        return 'WON'
    else:
        return   


def update(c):
    if c=="Red":return "Blue"
    if c=="Blue":return "Green"
    if c=="Green":return "Orange"
    if c=="Orange":return "Purple"
    if c=="Purple":return "Yellow"

def mastermind(game):
    guess,I=["Red","Red","Red","Red"],0
    while I<4:
        answer=game.check(guess)
        if len(answer)==I:
            guess[I]=update(guess[I])
        else:I+=1