Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # basic_6_peg_mastermind_game.py
- import random
- colors=('r','g','b','y')
- class Cv: 0
- cv = Cv()
- def match_guess(guess):
- if len(guess)!=len(cv.to_guess) or [g for g in guess if g not in colors]:
- return
- b,w=0,0
- usedindexes=[]
- for i,g in enumerate(guess):
- if g==cv.to_guess[i]:
- b+=1
- usedindexes.append(i)
- for i,g in enumerate(guess):
- if i in usedindexes: continue
- for j,c in enumerate(cv.to_guess):
- if c==g and j not in usedindexes:
- w+=1
- usedindexes.append(j)
- return b,w
- def make_move():
- guess=raw_input("Guess: ")
- return list(guess.lower())
- def main():
- while True:
- print("Possible Peg Colors (Enter First Letter): [r]ed [g]reen [b]lue [y]ellow")
- print("White Pegs = Correct Colors, Wrong Place")
- print("Black Pegs = Correct Colors, Correct Place")
- print("Enter Your 6-Peg Guess Like... rgbyyr")
- count=0
- cv.to_guess=[random.choice(colors) for i in range(6)]
- print cv.to_guess
- while count != 'restart':
- guess=make_move()
- try:
- bp,wp=match_guess(guess)
- except:
- print("Invalid Entry... Please Try Again")
- continue
- print("Black Pegs %s"%bp)
- print("White Pegs %s"%wp)
- if bp==6:
- print("You Won In %s Guesses!"%count)
- print("Game Restart...")
- count='restart'
- else:
- count+=1
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement