Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mastermind game
#6
Here is an independent script with prints to show what's going on
t = [1, 2, 3, 4]
tableauEntree = [5, 1, 1, 4]
tableauPlacement = [0, 0]

def placement():
    print('t:', t)
    print('tableauEntree:', tableauEntree)
    paires = list(zip(t, tableauEntree))
    print('paires (1):', paires)
    n = len(paires)
    paires = [(u, v) for u, v in paires if u != v]
    print('paires (2):', paires)
    compteurBienPlaces = n - len(paires)
    print('bien places: {} = {} - {}'.format(compteurBienPlaces, n, len(paires)))
    if paires:
        a, b = zip(*paires)
        print('a:', a)
        print('b:', b)
        compteurMalPlaces = sum(1 for x in b if x in a)
    else:
        compteurMalPlaces = 0
    print('mal places:', compteurMalPlaces)
    tableauPlacement[:] = [compteurBienPlaces, compteurMalPlaces]
    
placement()
The result is this
Output:
t: [1, 2, 3, 4] tableauEntree: [5, 1, 1, 4] paires (1): [(1, 5), (2, 1), (3, 1), (4, 4)] paires (2): [(1, 5), (2, 1), (3, 1)] bien places: 1 = 4 - 3 a: (1, 2, 3) b: (5, 1, 1) mal places: 2
The first zip groups corresponding numbers in t and tableauEntree. See the paires (1) print. Then well placed pairs are removed from the list. It remains only the pairs that don't match (paires (2)). The second zip separates the pairs again, see lists a and b above. It remains the slots where the computer's choice and the player's choice don't match. Then we sum 1 for every number in the player's list that appears in the computer's list. This is the count of misplaced numbers.
(Feb-03-2018, 02:00 PM)Staren Wrote: for TableauPlacement, it is because I "declared" the variable accidentaly ?
Indeed there is no formal variable declaration in python but a variable becomes a local variable in a function if it is on the left hand side of an assignment statement. So a statement foo = 3 in a function's body declares foo as a local variable, but foo[1] = 3 doesn't, nor does x = foo.
Reply


Messages In This Thread
Mastermind game - by Staren - Feb-03-2018, 11:39 AM
RE: Mastermind game - by Gribouillis - Feb-03-2018, 11:59 AM
RE: Mastermind game - by Staren - Feb-03-2018, 12:25 PM
RE: Mastermind game - by Gribouillis - Feb-03-2018, 01:22 PM
RE: Mastermind game - by Staren - Feb-03-2018, 02:00 PM
RE: Mastermind game - by Gribouillis - Feb-03-2018, 02:49 PM
RE: Mastermind game - by Staren - Feb-03-2018, 03:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help (mastermind game) sseansssean 2 2,995 Jul-06-2020, 12:47 PM
Last Post: deanhystad
  Mastermind Davidlit95 2 3,465 Oct-16-2017, 10:17 PM
Last Post: Davidlit95

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020