simple mastermind programpython

Solutions on MaxInterview for simple mastermind programpython by the best coders in the world

showing results for - "simple mastermind programpython"
Lilly
30 Apr 2017
1import random
2import os
3 
4def clear():
5    os.system("clear")
6 
7# Function to print the mastermind board
8def print_mastermind_board(passcode, guess_codes, guess_flags):
9 
10 
11    print("-----------------------------------------")
12    print("\t      MASTERMIND")
13    print("-----------------------------------------")
14 
15    print("    |", end="")
16    for x in passcode:
17        print("\t" + x[:3], end="")
18    print() 
19 
20    for i in reversed(range(len(guess_codes))):
21        print("-----------------------------------------")
22        print(guess_flags[i][0], guess_flags[i][1], "|")
23         
24 
25        print(guess_flags[i][2], guess_flags[i][3], end=" |")
26        for x in guess_codes[i]:
27            print("\t" + x[:3], end="")
28 
29        print() 
30    print("-----------------------------------------")
31 
32# The Main function
33if __name__ == '__main__':
34 
35    # List of colors
36    colors = ["RED", "GREEN", "YELLOW", "BLUE", "BLACK", "ORANGE"]
37 
38    # Mapping of colors to numbers  
39    colors_map = {1:"RED", 2:"GREEN", 3:"YELLOW", 4:"BLUE", 5:"BLACK", 6:"ORANGE"}
40 
41    # Randomly selecting a passcode
42    random.shuffle(colors)
43    passcode = colors[:4]
44     
45    # Number of chances for the player
46    chances = 8
47 
48    # The passcode to be shown to the user
49    show_passcode = ['UNK', 'UNK', 'UNK', 'UNK']
50 
51    # The codes guessed by the player each turn
52    guess_codes = [['-', '-', '-', '-'] for x in range(chances)]
53 
54    # The clues provided to the player each turn
55    guess_flags = [['-', '-', '-', '-'] for x in range(chances)]
56     
57    clear()
58 
59    # The current turn
60    turn = 0
61 
62    # The GAME LOOP
63    while turn < chances:
64         
65        print("-----------------------------------------")
66        print("\t\tMenu")
67        print("-----------------------------------------")
68        print("Enter code using numbers.")
69        print("1 - RED, 2 - GREEN, 3 - YELLOW, 4 - BLUE, 5 - BLACK, 6 - ORANGE")
70        print("Example: RED YELLOW ORANGE BLACK ---> 1 3 6 5")
71        print("-----------------------------------------")
72        print_mastermind_board(show_passcode, guess_codes, guess_flags)
73 
74        # Accepting the player input 
75        try:    
76            code = list(map(int, input("Enter your choice = ").split()))
77        except ValueError:
78            clear()
79            print("\tWrong choice!! Try again!!")
80            continue   
81 
82        # Check if the number of colors nunbers are 4
83        if len(code) != 4:
84            clear()
85            print("\tWrong choice!! Try again!!")
86            continue
87 
88        # Check if each number entered corresponds to a number
89        flag = 0
90        for x in code:
91            if x > 6 or x < 1:
92                flag = 1
93 
94        if flag == 1:           
95            clear()
96            print("\tWrong choice!! Try again!!")
97            continue   
98 
99        # Storing the player input
100        for i in range(4):
101            guess_codes[turn][i] = colors_map]  
102 
103        # Process to apply clues according to the player input  
104        dummy_passcode = [x for x in passcode]  
105 
106        pos = 0
107 
108        # Loop to set up clues for the player move
109        for x in code:
110            if colors_map[x] in dummy_passcode:
111                if code.index(x) == passcode.index(colors_map[x]):
112                    guess_flags[turn][pos] = 'R'
113                else:
114                    guess_flags[turn][pos] = 'W'
115                pos += 1
116                dummy_passcode.remove(colors_map[x])
117 
118        random.shuffle(guess_flags[turn])               
119 
120 
121        # Check for win condition
122        if guess_codes[turn] == passcode:
123            clear()
124            print_mastermind_board(passcode, guess_codes, guess_flags)
125            print("Congratulations!! YOU WIN!!!!")
126            break
127 
128        # Update turn   
129        turn += 1          
130        clear()
131 
132# Check for loss condiiton  
133if turn == chances:
134    clear()
135    print_mastermind_board(passcode, guess_codes, guess_flags)
136    print("YOU LOSE!!! Better luck next time!!!")   
137