Mastermind Python coding -


ok have feeling simple simple issue have been staring @ code 10 hours now. issue having in mastermind once recognize have correct colors in right spot can display right spots x , wrong spots o. need able convert instead of x , o need tell user he/she has 2 blacks , 1 white example: secret code rgyb user enters rgoy python relays "you have 2 blacks(the r , g spots) , 1 one white (the y because it's right color in wrong index) of right got display x right color in right spot , else o post have been working today @ wit's end https://pastebin.com/hkk0t7bq

    if correctcolor != "xxxx":     in range(4):         if guess[i] == tempcode[i]:            correctcolor += "x"         if  guess[i] != tempcode[i] in tempcode:            correctcolor += "o"     print (correctcolor + "\n")      if correctcolor == "xxxx":     if attempts == 1:         print ("you think sweet because got right on first try? play me again!")     else:         print ("well done... needed " + str(attempts) + " attempts guess.")     game = false 

a few comments

x , o

you use x , 0 denote success, easier , faster use list or tuple or booleans this, way can use sum() count how many colors , locations correct. whether represent x , o or red , white pins matter later

compartmentalization

your game logic (guess input, input validation, want continue, etc) mixed comparison logic, best separate different functions of program different methods.

this fineexample introduce object oriented programming, simple doesn't need oo, can help. need method takes series of colours , compares series of colours

standard library

python has extended standard library, lot of stuff want exists

correct colours

to count number of letters occur in 2 strings, can use collections.counter

guess = "rgoy " solution = "rgyb" = collections.counter(guess) b = collections.counter(solution)  & b 
counter({'g': 1, 'r': 1, 'y': 1}) 
correct_colours = sum((a & b).values()) 
3 

so user guessed 3 colours correctly

correct locations

can solved easy list comprehension

[g == s g, s in zip(guess, solution)] 
[true, true, false, false] 
sum(g == s g, s in zip(guess, solution)) 
2 

so used put 2 colours on correct location


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -