Hello there,
I have to do a sort-of mastermind game, where you have to guess the numbers in an array.
I have no particular problem except when it comes to count how many numbers are in the array, but at the wrong place.
(Note that there is a rule saying numbers can't go above 5)
For example when the answer is 1234 and I type 1115, I need the game to tell me I have 1 correct number but since the only "1" needed is well placed, I also need the game to tell me there are no numbers at the wrong spot
Here's the code:
Could you help me find how to count correctly ?
Thanks for any help you could bring !
Edit: I'm sorry if you can't understand my code because of the lack of comments, feel free to ask me if there's anything you don't get
I have to do a sort-of mastermind game, where you have to guess the numbers in an array.
I have no particular problem except when it comes to count how many numbers are in the array, but at the wrong place.
(Note that there is a rule saying numbers can't go above 5)
For example when the answer is 1234 and I type 1115, I need the game to tell me I have 1 correct number but since the only "1" needed is well placed, I also need the game to tell me there are no numbers at the wrong spot
Here's the code:
import random as R t = [0, 0, 0, 0] tableauPlacement =[0,0] global joue joue = True; tableauEntree = [0, 0, 0, 0] compteur = 0; compteurLose = 0; choixJoueur = ""; def choix_ordinateur (): for i in range (4): t[i] = R.randint(1, 5) def choix_utilisateur(): entree = 0 print("Entrez un nombre a 4 chiffres svp ? ") entree = int(input("")) while((entree % 10) > 5 or (entree % 100) >= 60 or entree % 1000 > 600 or entree >= 6000): print("n'entrez que des nombres entre 1 et 5 svp: ") entree = int(input("")) for i in range(4): tableauEntree[3-i] = entree % 10 tableauEntree[3-i] = int(tableauEntree[3 - i]) entree = (entree - tableauEntree[3 - i])/10 print("vous avez donc: ", tableauEntree) def placement(): tableauPlacement =[0,0] compteurMalPlaces = 0 compteurBienPlaces = 0 for i in range(4): if (t[i] == tableauEntree[i]): compteurBienPlaces += 1 totalMalPlaces = t.count(i) - tableauEntree.count(i) if(compteurBienPlaces <= totalMalPlaces): tableauPlacement[1] += totalMalPlaces - compteurBienPlaces print(compteurBienPlaces, " ", tableauPlacement[0]) tableauPlacement[0] += compteurBienPlaces print(compteurBienPlaces, " ", tableauPlacement[0]) def jeu(): compteur = 0 reponse = ""; choix_ordinateur() print("la reponse est: ", t, " comme ca plus utile pour faire des tests") while(compteur <12): compteur += 1 choix_utilisateur() placement() if(tableauPlacement == [4, 0]): print("GG ! 4 corrects et 0 mal placés") return True elif(tableauPlacement[0] == 0): print("aucun nombre bien placé", end =" ") elif(tableauPlacement[0] == 1): print("Un nombre bien placé", end = " ") else: print(tableauPlacement[0], " nombres bien placés", end = " ") if(tableauPlacement[1] == 0): print("et aucun mal placé") elif(tableauPlacement[1] == 1): print("et un nombre mal placé") else: print("et ", tableauPlacement[1], " nombres bien placés") return False while(joue): if(jeu() == False): print("Perdu !") reponse = input("Voulez vous rejouer ? o/n") if (reponse == "n"): joue = False;So I think everything is ok in this code but the "Placement" function, however I can't manage to find it.
Could you help me find how to count correctly ?
Thanks for any help you could bring !
Edit: I'm sorry if you can't understand my code because of the lack of comments, feel free to ask me if there's anything you don't get