@AmeliaBlackwell/

Multiplayer Mastermind Code Guesser

Python

Play this python version of a board game I used to play when I was younger! Pretty colours even if you don't like codes :) hope you enjoy

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import replit
import random as rand
from termcolor import cprint, colored
colours = ["red","yellow","green","cyan","blue","magenta","white"]
guessnos = 10
won = False
def game_info(colours=colours):
    print(f"welcome to amelia's multiplayer mastermind code guesser game. player 1 is the coder; player 2 is the guesser. player 2 has {guessnos} attempts to guess the code. the code is made of 4 coloured pins, chosen by player 1, from:")
    for colour in colours:
      cprint(colour, colour, end=" ")
    input("\na colour cannot be duplicated within the code. after each guess made by player 2, 2 numbers are displayed. the red shows the number of pins in your guess whose colour matches the colour and position of one of the pins. the white shows the number of pins in player 2's guess whose colour features in player 1's code, but the position is different.\npress enter to play. good luck to both players!")
    replit.clear()
def choosecode(colours=colours):
    print(f'colours available: {" ".join([colored(el, el) for el in colours])}')
    print("player 2, look away! player 1, enter the code. seperate each colour by a space. enter random for a randomly generately code:")
    codedone = False
    while not codedone:
        ans=input()
        if ans=="random":
            guesslist=[rand.choice(colours) for x in range(4)]
            codedone=True
            if input("code has been generated. enter yes to see the code, or just press enter if you don't want to see: ")=="yes":
                print(f'{" ".join([colored("O",el) for el in guesslist])}')
                input("press enter to continue")
        else:
            fewerthan4, boollist = False, []
            guesslist = ans.split(" ")
            if len(guesslist) != 4:
                    print("please enter 4 colours. please reenter the code:")
                    fewerthan4 = True
            if not(fewerthan4):
                for guessedcolour in guesslist:
                    if guessedcolour not in colours:
                        print(f"the colour {guessedcolour} is not available. please reenter the code:")
                        boollist.append(False)
                    else:
                        boollist.append(True)
                if boollist[0] == True and boollist[1] == True and boollist[2] == True and boollist[3] == True:
                    print(f'is this your code? {" ".join([colored("O",el) for el in guesslist])}')
                    ans=input()
                    if ans=="yes":
                        codedone=True
                    else:
                        print("please reenter the code:")
    replit.clear()
    return guesslist
def guess(guessno, code, colours=colours, guessdone=False):
    while not guessdone:
        boollist = []
        print(f"player 2, please enter guess number {guessno+1}. seperate colours with a space:")
        guesslist = input().split(" ")
        if len(guesslist) != 4:
            print("please enter 4 colours. please reenter your guess:")
        else:
            for guessedcolour in guesslist:
                if guessedcolour not in colours:
                    print(f"the colour {guessedcolour} is not available. please reenter your guess:")
                    boollist.append(False)
                else:
                    boollist.append(True)
            if boollist[0] == True and boollist[1] == True and boollist[2] == True and boollist[3] == True:
                guessdone = True
    reds, red, white = [], 0, 0
    for index, pin in enumerate(code):
        if pin == guesslist[index]:
            red += 1
            reds.append(True)
        else:
            for indexx, boolx in enumerate(reds):
                if pin == guesslist[indexx] and not boolx:
                    white+=1
                    break
            reds.append(False)
    sentence = f'{colored(str(red),"red")} {colored("|","grey")} {" ".join([colored("O",el) for el in guesslist])} {colored("|","grey")} {colored(str(white),"white")}'
    return sentence, red
def print_board(guessno, guesses, guessnos=guessnos, colours=colours):
    print(f'colours available: {" ".join([colored(el, el) for el in colours])}')
    for guessnonotdone in range(guessnos - (guessno)):
        cprint("  | X X X X |","grey")
    for guesssentence in reversed(guesses):
        print(guesssentence)
replit.clear()
game_info()
code = choosecode()
guesses = []
print_board(0, guesses)
for guessno in range(guessnos):
    sentence, red = guess(guessno, code)
    replit.clear()
    guesses.append(sentence)
    print_board(guessno+1, guesses)
    if red == 4:
        won = True
        break
if won:
    if guessno+1 == 1:
        guessword = "guess"
    else:
        guessword = "guesses"
    print(f"player 2 is the winner! player 2 guessed the code in {guessno+1} {guessword}")
else:
    print("player 1 is the winner! player 2 wasn't able to crack the code.\nenter yes if you want to reveal the code.")
    ans = input()
    if ans == "yes":
        print("player 1's code was: {}".format(" ".join([colored("O", pin) for pin in code])))
print("thanks for playing")