├── README.md └── MASTERMIND └── game.py /README.md: -------------------------------------------------------------------------------- 1 | # Mastermind Game Python 2 | 3 | The Mastermind game is a code-breaking game where the player tries to guess the secret code created by another player or the computer. The secret code consists of a combination of colors, and the player has to guess the correct sequence of colors. After each guess, the player receives feedback in the form of black and white pegs. A black peg indicates that one of the colors in the guess is correct and in the correct position, while a white peg indicates that one of the colors in the guess is correct but in the wrong position. The player continues to guess the code until the correct sequence is found. 4 | 5 | This game can be implemented in Python using various programming concepts such as loops, conditional statements, and arrays. The code logic can be simple or complex, depending on the features and complexity desired for the game. 6 | -------------------------------------------------------------------------------- /MASTERMIND/game.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | COLORS = ["R", "G", "B", "Y", "W", "O"] 4 | TRIES = 10 5 | CODE_LENGTH = 4 6 | 7 | def generate_code(): 8 | code = [] 9 | 10 | for _ in range(CODE_LENGTH): 11 | color = random.choice(COLORS) 12 | 13 | code.append(color) 14 | 15 | return code 16 | 17 | def guess_code(): 18 | while True: 19 | guess = input("Guess: ").upper().split(" ") 20 | 21 | if len(guess) != CODE_LENGTH: 22 | print(f"You must guess {CODE_LENGTH} colors!") 23 | continue 24 | 25 | for color in guess: 26 | if color not in COLORS: 27 | print(f"Invalid color: {color}. Try again!") 28 | break 29 | else: 30 | break 31 | 32 | return guess 33 | 34 | def check_code(guess, real_code): 35 | color_counts = {} 36 | correct_pos = 0 37 | incorrect_pos = 0 38 | 39 | for color in real_code: 40 | if color not in color_counts: 41 | color_counts[color] = 0 42 | color_counts[color] += 1 43 | 44 | for guess_color, real_color in zip(guess, real_code): 45 | if guess_color == real_color: 46 | correct_pos += 1 47 | color_counts[guess_color] -= 1 48 | 49 | for guess_color, real_color in zip(guess, real_code): 50 | if guess_color in color_counts and color_counts[guess_color] > 0: 51 | incorrect_pos += 1 52 | color_counts[guess_color] -= 1 53 | 54 | return correct_pos, incorrect_pos 55 | 56 | def game(): 57 | print(f"Welcome to MASTERMIND game, you have {TRIES} to guess the code....") 58 | print("The valid colors are", *COLORS) 59 | 60 | code = generate_code() 61 | for attempts in range(1, TRIES + 1): 62 | guess = guess_code() 63 | correct_pos, incorrect_pos = check_code(guess, code) 64 | 65 | if correct_pos == CODE_LENGTH: 66 | print("Congratulations 🎉 you won the game!") 67 | print(f"You guessed the code in {attempts} tries!") 68 | break 69 | 70 | print(f"Correct Positions: {correct_pos} | Incorrect Positions: {incorrect_pos}") 71 | 72 | else: 73 | print("You ran out of tries, the code was:", *code) 74 | 75 | if __name__ == "__main__": 76 | game() --------------------------------------------------------------------------------