Advertisement
here2share

# basic_6_peg_mastermind_game.py

Apr 17th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. # basic_6_peg_mastermind_game.py
  2.  
  3. import random
  4.  
  5. colors=('r','g','b','y')
  6. class Cv: 0
  7. cv = Cv()
  8.  
  9. def match_guess(guess):
  10.     if len(guess)!=len(cv.to_guess) or [g for g in guess if g not in colors]:
  11.         return
  12.     b,w=0,0
  13.     usedindexes=[]
  14.     for i,g in enumerate(guess):
  15.         if g==cv.to_guess[i]:
  16.             b+=1
  17.             usedindexes.append(i)
  18.     for i,g in enumerate(guess):
  19.         if i in usedindexes: continue
  20.         for j,c in enumerate(cv.to_guess):
  21.             if c==g and j not in usedindexes:
  22.                 w+=1
  23.                 usedindexes.append(j)
  24.     return b,w         
  25.    
  26. def make_move():
  27.     guess=raw_input("Guess: ")
  28.     return list(guess.lower())
  29.  
  30. def main():
  31.     while True:
  32.         print("Possible Peg Colors (Enter First Letter): [r]ed [g]reen [b]lue [y]ellow")
  33.         print("White Pegs = Correct Colors, Wrong Place")
  34.         print("Black Pegs = Correct Colors, Correct Place")
  35.         print("Enter Your 6-Peg Guess Like... rgbyyr")
  36.         count=0
  37.         cv.to_guess=[random.choice(colors) for i in range(6)]
  38.         print cv.to_guess
  39.         while count != 'restart':
  40.             guess=make_move()
  41.             try:
  42.                 bp,wp=match_guess(guess)
  43.             except:
  44.                 print("Invalid Entry... Please Try Again")
  45.                 continue
  46.             print("Black Pegs %s"%bp)
  47.             print("White Pegs %s"%wp)
  48.             if bp==6:
  49.                 print("You Won In %s Guesses!"%count)
  50.                 print("Game Restart...")
  51.                 count='restart'
  52.             else:
  53.                 count+=1
  54.  
  55. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement