repl.it
Python

No description

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
import random

c = ["black", "white", "red", "yellow", "blue", "green"]

play = True

while play == True:

  color1 = random.choice(c)
  color2 = random.choice(c)
  color3 = random.choice(c)
  color4 = random.choice(c)


  max_turns = 10
  black_peg = "BP"
  white_peg = "WP"

  current_turn = 0

  print("Hey! Play Mastermind with me. Choose from " + ", ".join(c) + ".")

  while current_turn < max_turns:
    computer_colors = [color1, color2, color3, color4]

    uc1 = input("First color? ").strip().lower();
    uc2 = input("Second color? ").strip().lower();
    uc3 = input("Third color? ").strip().lower();
    uc4 = input("Fourth color? ").strip().lower();

    user = [uc1, uc2, uc3, uc4]

    correct = []

    for i in range(0, len(user)):
      if user[i] == computer_colors[i]:
        print(black_peg)
        correct.append(i)

    if len(correct) == len(user):
      print("You win! The sequence is the following: ", uc1, uc2, uc3, uc4, ". It took you  " + str(current_turn + 1) + " turns.")
      break;

    for i in reversed(correct):
      del computer_colors[i]

    for i in range(0, len(user)):
      if i not in correct:
        if user[i] in computer_colors:
          print(white_peg)
          computer_colors.remove(user[i])

    current_turn = current_turn + 1
    remainingTurns = max_turns - current_turn
    print("Try again. You have " + str(remainingTurns) + " turns remaining.")

  if current_turn == max_turns:
    print("You lose!")
    print("The correct order was the following: " + ",".join([color1, color2, color3, color4]) + ".")

  playAgain = input("Do you want to play again? y/n \n")
  if playAgain == "n":
    play = False
  else:
    continue