RETROGICIEL
MASTERMIND
RETROGICIEL
MASTERMIND
⇩. Présentation
Le jeu de Mastermind est maintenant bien connu des grands et des petits. l'ordinateur va créer aléatoirement une combinaison de 4 couleurs différentes parmi 6 possibles. Le joueur devra découvrir cette combinaison en un maximum de 12 essais. A chaque tentative, la machine lui indiquera combien de couleurs sont bien placées, sans préciser lesquelles, et combien des autres couleurs sont présentes mais mal placées.
Au fil des années, de nombreuses variantes sont apparues : plus de couleurs, possibilité de choisir plusieurs fois la même couleur, ... Une fois ce défi terminé, vous pourrez programmer une de ces variantes, ou en inventer de nouvelles.
⇩. Consignes
Le programme doit :
choisir aléatoirement 4 couleurs parmi 6 disponibles ;
permettre au joueur de choisir ses 4 couleurs avec la souris ;
analyser la proposition de code du joueur et lui indiquer ;
combien de couleurs sont bien placée ;
combien de couleurs sont présentes mais mal placées ;
combien de couleurs de sa proposition sont absentes ;
continuer la partie, et :
annoncer la victoire si la combinaison est trouvée en moins de 13 essais ;
demander une nouvelle proposition s’il reste des essais ;
annoncer la défaite si tous les essais sont épuisés ;
montrer la combinaison secrète en fin de partie ;
proposer de faire une autre partie ou de quitter le programme.
D'abord, faite-le fonctionner. Ensuite, faite-le beau. Enfin, faite-le performant.
Amusez-vous bien !
Télécharger RETROGICIEL - Mastermind.
Cette présentation nécessite que Python 3 soit installé sur votre machine.
Après avoir téléchargé le fichier Mastermind.7z, décompressez-le dans le répertoire de votre choix.
Ouvrez le répertoire qui vient d'être créé.
Lancez le fichier Retrogiciel.py.
Cliquez sur l'onglet RUN et testez le programme.
Créez votre propre script dans un des langages proposés ou un autre de votre choix.
Le répertoire contient aussi :
- Python.py, l'exemple en PYTHON sans tkinter ;
- Tkinter.py, l'exemple en PYTHON avec tkinter ;
- Qb84.bas, l'exemple en QBASIC avec qb64 ;
- Bbc.bas, l'exemple en BBC BASIC avec bbc sdl.
# --- Origine Nerd ---
# --- RETROGICIEL - PYTHON ---
# --- Mastermind ---
# -*- coding: utf-8 -*-
# --- IMPORTATION DES MODULES ---
import random
# --- INITIALISATION GÉNÉRALE ---
LST_Couleurs = list ( "ABCDEF" )
LST_Combinaison = [ ]
# --- DEFINITION DES FONCTIONS PERSONNALISEES ---
# --- Analyse de la proposition de l'utilsateur ---
def FNC_Analyse ( Q ) :
# --- Recherche des lettres bien placées ---
kproposition = Q .upper( )
ktrouves = 0
kpresents = 0
kabsents = 0
for kplace in range ( 4 ) :
if kproposition [ kplace ] == LST_Combinaison [ kplace ] : ktrouves += 1
elif kproposition [ kplace ] in LST_Combinaison : kpresents += 1
else : kabsents+= 1
# --- Affichage du résultat de l'analyse ---
print ( f"Bien placé(s) : { ktrouves }." )
print ( f"Présent(s)... : { kpresents }." )
print ( f"Absent(s).... : { kabsents }." )
return ktrouves
# --- PRESENTATION ---
print ( "Faisons une partie de MASTERMIND !" )
print ( "\nLa machine va choisir 4 lettres parmi ABCDEF." )
print ( "Puis elle va les mélanger aléatoirement." )
print ( "Vous avez 12 coups pour trouver sa combinaison." )
print ( "Pour chacune de vos tentatives, elle vous indiquera" )
print ( "combien vous avez de lettres bien placées, mais pas" )
print ( "leurs positions, et combien d'autres lettres de votre" )
print ( "proposition sont présentes aussi dans la combinaison." )
# --- BOUCLE PRINCIPALE ---
while True :
# --- Nouvelle partie ---
random.shuffle ( LST_Couleurs )
LST_Combinaison [ : ] = LST_Couleurs [ 0 : 4 ]
# --- Saisie et analyse des propositions de l'utilsateur ---
for ktour in range ( 1 , 13 ) :
kchoix = input ( f"\nTentative n° { ktour } / 12 : " )
ktrouves = FNC_Analyse ( kchoix + "????" )
if ktrouves == 4 : break
# --- Fin de la partie ---
kcombinaison = "".join ( LST_Combinaison )
print ( f"\nLa bonne combinaison était : { kcombinaison }." )
if ktrouves == 4 : print ( f"Félicitation vous avez gagné en { ktour } coups." )
else : print ( "Désolé, la bonne. C'est perdu." )
# --- Choix du bouclage du programme ---
kchoix = input ( "Voulez-vous faire une autre partie (O ou N) ?" )
if kchoix.upper ( ) == "N" : break
# --- FIN DU PROGRAMME ---
print ( "Au revoir" )
# --- Programme : JFB ---
# --- Mars 2023 ---
# --- Fin ---
Pour mieux comprendre l'exemple en PYTHON sans tkinter.
# --- Origine Nerd ---
# --- RETROGICIEL - TKINTER ---
# --- Mastermind ---
# -*- coding: utf-8 -*-
# --- IMPORTATION DES MODULES ---
# --- Modules de la bibliothèque standard ---
import tkinter
import random
# --- INITIALISATION GÉNÉRALE ---
LST_Couleurs = [ "red" , "lime" , "yellow" , "blue" , "magenta" , "aqua" ]
LST_Places = [ ]
DCT_Donnees = { "tour" : 0 , "place" : 0 , "combinaison" : [ ] }
# --- DÉFINITION DES FONCTIONS PERSONNALISEES ---
# --- Analyse d'une proposition de l'utilisateur ---
def FNC_Analyse ( ) :
# --- Mise en liste de la combinaison du joueur ---
kcombinaison = [ ]
ktour = DCT_Donnees [ "tour" ]
for kplace in range ( 1 , 5 ) : kcombinaison.append ( LST_Places [ ktour ] [ kplace ] [ "background" ] )
# --- Recherche des couleurs bien placées ---
ktrouves = 0
kpresents = 0
kabsents = 0
for kplace in range ( 4 ) :
if kcombinaison [ kplace ] == DCT_Donnees [ "combinaison" ] [ kplace ] : ktrouves += 1
elif kcombinaison [ kplace ] in DCT_Donnees [ "combinaison" ] : kpresents += 1
else : kabsents += 1
# --- Affichage et fin de l'analyse ---
kanalyse = f"{ ktrouves }{ kpresents }{ kabsents }"
LST_Places [ DCT_Donnees [ "tour" ] ] [ 0 ] [ "text" ] = kanalyse
if ktrouves == 4 :
FNC_Gagne ( )
return
else :
DCT_Donnees [ "tour" ] += 1
if DCT_Donnees [ "tour" ] == 12 :
FNC_Perdu ( )
return
LAB_Message [ "text" ] = f"TOUR : { DCT_Donnees [ 'tour' ] + 1 } / 12"
LAB_Tour [ "text" ] = f"tour : { DCT_Donnees [ 'tour' ] + 1 }"
# --- Proposition des couleur par l'utilisateur ---
def FNC_Choix ( Q ) :
if BUT_Jouer [ "state" ] == "normal" : return
ktour = DCT_Donnees [ "tour" ]
kplace = DCT_Donnees [ "place" ] + 1
LST_Places [ ktour ] [ kplace ] [ "background" ] = Q
DCT_Donnees [ "place" ] = kplace if kplace < 4 else 0
if DCT_Donnees [ "place" ] == 0 : FNC_Analyse ( )
# --- Partie gagnée ---
def FNC_Gagne ( ) :
FNC_Montrer ( "fin" )
LAB_Message [ "background" ] = "yellow"
LAB_Message [ "foreground" ] = "red"
LAB_Message [ "text" ] = "GAGNÉ !"
# --- Montrer/effecer le bon code ---
def FNC_Montrer ( Q ) :
BUT_Jouer [ "state" ] = "disabled" if Q == "debut" else "normal"
LAB_Code1 [ "background" ] = "white" if Q == "debut" else DCT_Donnees [ "combinaison" ] [ 0 ]
LAB_Code2 [ "background" ] = "white" if Q == "debut" else DCT_Donnees [ "combinaison" ] [ 1 ]
LAB_Code3 [ "background" ] = "white" if Q == "debut" else DCT_Donnees [ "combinaison" ] [ 2 ]
LAB_Code4 [ "background" ] = "white" if Q == "debut" else DCT_Donnees [ "combinaison" ] [ 3 ]
# --- Proposition des couleur par l'utilisateur ---
def FNC_Nouveau ( ) :
random.shuffle ( LST_Couleurs )
DCT_Donnees [ "combinaison" ] [ : ] = LST_Couleurs [ 0 : 4 ]
for ktour in range ( 12 ) :
LST_Places [ ktour ] [ 0 ] [ "text" ] = "TPA"
for kvaleur in range ( 1 , 5 ) : LST_Places [ ktour ] [ kvaleur ] [ "background" ] = "beige"
FNC_Montrer ( "debut" )
LAB_Message [ "background" ] = "#d9d9d9"
LAB_Message [ "foreground" ] = "black"
LAB_Message [ "text" ] = f"TOUR : 1 / 12"
LAB_Tour [ "text" ] = f"Tour : 1 "
DCT_Donnees [ "tour" ] = 0
# --- Partie perdu ---
def FNC_Perdu ( ) :
FNC_Montrer ( "fin" )
LAB_Message [ "background" ] = "red"
LAB_Message [ "foreground" ] = "yellow"
LAB_Message [ "text" ] = "PERDU."
# --- CREATION DE L'INTERFACE GRAPHIQUE ---
# --- Création de la fenêtre principale ---
TKI_Principal = tkinter.Tk ( )
TKI_Principal.title ( "RETROGICIEL - Mastermind" )
# --- Création des controles nommées ---
BUT_Jouer = tkinter.Button ( TKI_Principal , text = "Jouer" , command = FNC_Nouveau )
LAB_Message = tkinter.Label ( TKI_Principal , font = ( None , 18 , "bold" ) , width = 11 )
FRM_Code = tkinter.LabelFrame ( TKI_Principal , bg = "white" , bd = 1 , relief = "solid" )
LAB_Code1 = tkinter.Label ( FRM_Code , text = "1" , bg = "white" , fg = "black" , bd = 1 , relief = "solid" , width = 4 )
LAB_Code2 = tkinter.Label ( FRM_Code , text = "2" , bg = "white" , fg = "black" , bd = 1 , relief = "solid" , width = 4 )
LAB_Code3 = tkinter.Label ( FRM_Code , text = "3" , bg = "white" , fg = "black" , bd = 1 , relief = "solid" , width = 4 )
LAB_Code4 = tkinter.Label ( FRM_Code , text = "4" , bg = "white" , fg = "black" , bd = 1 , relief = "solid" , width = 4 )
tkinter.Label ( FRM_Code , text = "T pour TROUVÉ\nP = présent et A = absent" , bg = "white" ).pack ( side = "left" , padx = 4 , pady = 3 , fill = "both" )
tkinter.Label ( FRM_Code , bg = "white" , width = 2 , height = 2 ).pack ( side = "right" )
LAB_Code4.pack ( side = "right" , padx = 4 , pady = 3 , fill = "both" )
LAB_Code3.pack ( side = "right" , padx = 4 , pady = 3 , fill = "both" )
LAB_Code2.pack ( side = "right" , padx = 4 , pady = 3 , fill = "both" )
LAB_Code1.pack ( side = "right" , padx = 4 , pady = 3 , fill = "both" )
FRM_Plateau = tkinter.LabelFrame ( TKI_Principal , bd = 1 , relief = "solid" )
for ktour in range ( 12 ) :
kconteneur = tkinter.LabelFrame ( FRM_Plateau , relief = "groove" )
kanalyse = tkinter.Label ( kconteneur , bg = "white" , bd = 1 , relief = "solid" , width = 4 )
kanalyse.pack ( side = "left" , padx = 2 )
kplaces = [ kanalyse ]
for kvaleur in range ( 4 ) :
kplace = tkinter.Label ( kconteneur , bd = 2 , relief = "solid" , width = 1 )
kplace.pack ( side = "left" , padx = 2 , pady = 2 )
kplaces.append ( kplace )
LST_Places.append ( kplaces )
kconteneur.grid ( row = ( ktour % 4 ) , column = ( ktour // 4 ) , padx = 2 , pady = 4 )
FRM_Saisie = tkinter.LabelFrame ( TKI_Principal , bg = "white" , bd = 1 , relief = "solid" )
LAB_Tour = tkinter.Label ( FRM_Saisie , text = "Tour : " , bg = "white" , fg = "black" )
LAB_Tour.grid ( row = 0 , column = 0 , sticky = "w" )
tkinter.Label ( FRM_Saisie , text = "Votre choix ... " , bg = "white" , fg = "black" ).grid ( row = 1 , column = 0 , sticky = "w" )
for kindex in range ( 6 ) :
kcouleur = LST_Couleurs [ kindex ]
kbouton = tkinter.Button ( FRM_Saisie , bg = kcouleur , bd = 2 , relief = "solid" , width = 1 )
kbouton [ "command" ] = lambda k = kcouleur : FNC_Choix ( k )
kbouton.grid ( row = 0 , column = ( kindex + 1 ) , rowspan = 2 , padx = 2 , pady = 2 , sticky = "nesw" )
# --- Mise en place des controles (anonymes et nommés) dans la fenêtre principale ---
FRM_Code.grid ( row = 0 , column = 0 , columnspan = 3 , padx = 5 , pady = 5 , sticky = "nesw" )
FRM_Plateau.grid ( row = 1 , column = 0 , columnspan = 3 , padx = 5 , pady = 5 , sticky = "nesw" )
FRM_Saisie.grid ( row = 2 , column = 0 , columnspan = 3 , padx = 5 , pady = 5 , sticky = "nesw" )
BUT_Jouer.grid ( row = 3 , column = 0 , sticky = "nesw" )
LAB_Message.grid ( row = 3 , column = 1 , padx = 3 , pady = 3 , sticky = "nesw" )
tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy ).grid ( row = 3 , column = 2 , sticky = "nesw" )
# --- Mise à jour de controle ---
FNC_Nouveau ( )
# --- DEBUT DU PROGRAMME ---
TKI_Principal.mainloop ( )
# --- Programme : JFB ---
# --- Mars 2023 ---
# --- Fin ---
Pour mieux comprendre l'exemple en PYTHON avec tkinter.
' --- Origine Nerd propose pour ---
' --- RETROGICIEL - QB64 ---
' --- Mastermind ---
' --- INITIALISATION GENERALE ---
SCREEN 12
RANDOMIZE TIMER
DIM SHARED LST_Couleurs(5) AS INTEGER
FOR kindex = 0 TO 5: LST_Couleurs(kindex) = kindex + 9: NEXT kindex
DIM SHARED LST_Choix(3) AS INTEGER
' --- BOUCLE PRINCIPALE ---
DO
' --- Preparation d'une nouvelle partie ---
FNC_Melange
FNC_Preparation
FOR kindex = 0 TO 3: LST_Choix(kindex) = 0: NEXT kindex
DO: ksouris = _MOUSEINPUT: LOOP UNTIL ksouris = 0
FOR ktour = 1 TO 12
' --- Choix d'une combinaison de 4 couleurs par l'utilsateur ---
LOCATE 13, 48: PRINT "Essais :"; STR$(ktour); "/12"
kplace = 0
DO
kcouleur = 0
ksouris = _MOUSEINPUT
IF _MOUSEBUTTON(1) = -1 THEN FNC_Couleur kcouleur
IF kcouleur <> 0 THEN
IF kplace = 0 THEN FNC_Actualise ktour
LST_Choix(kplace) = kcouleur
FNC_Placement ktour, kplace
kplace = kplace + 1
END IF
LOOP UNTIL kplace = 4
' --- Analyse de la combinason de l'utilisateur ---
FNC_Analyse ktour, ktrouves
IF ktrouves = 4 THEN EXIT FOR
NEXT ktour
' --- Fin de la partie ... ---
FNC_Resultat ktrouves
COLOR 15: LOCATE 13, 41: PRINT "Voulez-vous recommencer [O/N] "
_KEYCLEAR: DO: kchoix$ = INKEY$: LOOP UNTIL kchoix$ <> ""
LOOP UNTIL UCASE$(kchoix$) = "N"
' --- FIN DU PROGRAMME ---
LOCATE 12, 25: PRINT "Au revoir."
END
' --- DEFINITION DES FONCTIONS PERSONNALISEES ---
' --- Actualisation de l'ecran pour la combinaison suivante ---
SUB FNC_Actualise (Q)
LOCATE 2 + (2 * Q), 2: PRINT USING "##"; Q
LINE (360, 245)-(520, 275), 0, BF
khaut = 12 + (32 * Q): kbas = khaut + 20
FOR kcase = 0 TO 3
kdebut = 360 + (kcase * 40): kfin = kdebut + 30
LINE (kdebut, 245)-(kfin, 275), 15, B
kgauche = 30 + (15 * kcase): kdroite = kgauche + 10
LINE (kgauche, khaut)-(kdroite, kbas), 15, B
NEXT kcase
LOCATE 19, 43: PRINT "Couleurs bien placees : "
LOCATE 20, 43: PRINT "Couleurs presentes... : "
LOCATE 21, 43: PRINT "Couleurs absentes.... : "
END SUB
' --- Analyse d'une combinaison de l'utilisateur ---
SUB FNC_Analyse (Qtour, QTrouves)
' --- Recherche des couleurs trouvees ---
QTrouves = 0
FOR kindex = 0 TO 3
IF LST_Choix(kindex) = LST_Couleurs(kindex) THEN
QTrouves = QTrouves + 1
LST_Choix(kindex) = 0
END IF
NEXT kindex
' --- Recherche des couleurs presentes ---
kpresents = 0
FOR kindex = 0 TO 3
IF LST_Couleurs(kindex) = LST_Choix(0) THEN kpresents = kpresents + 1
IF LST_Couleurs(kindex) = LST_Choix(1) THEN kpresents = kpresents + 1
IF LST_Couleurs(kindex) = LST_Choix(2) THEN kpresents = kpresents + 1
IF LST_Couleurs(kindex) = LST_Choix(3) THEN kpresents = kpresents + 1
NEXT kindex
' --- Affichage de l'analyse ---
kabsentes = 4 - QTrouves - kpresents
kanalyse$ = STR$(QTrouves) + STR$(kpresents) + STR$(kabsentes)
LOCATE 2 + (2 * Qtour), 12: PRINT kanalyse$
LOCATE 19, 43: PRINT "Couleurs bien placees : "; QTrouves
LOCATE 20, 43: PRINT "Couleurs presentes... : "; kpresents
LOCATE 21, 43: PRINT "Couleurs absentes.... : "; kabsentes
END SUB
' --- Saisie d'une combinaison par l'utiliateur ---
SUB FNC_Couleur (Qcouleur)
DO: ksouris = _MOUSEINPUT: LOOP UNTIL _MOUSEBUTTON(1) = 0
IF _MOUSEY > 350 AND _MOUSEY < 380 THEN Qcouleur = POINT(_MOUSEX, _MOUSEY)
END SUB
' --- Brassage aleatoire des couleurs pour le code a trouver ---
SUB FNC_Melange
FOR ktour = 1 TO 100
ka = INT(RND * 6)
kb = INT(RND * 6)
IF ka <> kb THEN SWAP LST_Couleurs(ka), LST_Couleurs(kb)
NEXT ktour
END SUB
' --- Affichage de la couleur choisie dans sa case ---
SUB FNC_Placement (Qtour, Qplace)
kcouleur = LST_Choix(Qplace)
kdebut = 360 + (Qplace * 40)
kfin = kdebut + 30
LINE (kdebut, 245)-(kfin, 275), kcouleur, BF: ' - placement dans les grandes cases de droite
kgauche = 30 + (15 * Qplace)
kdroite = kgauche + 10
khaut = 12 + (32 * Qtour)
kbas = khaut + 20
LINE (kgauche, khaut)-(kdroite, kbas), kcouleur, BF: ' - placement dans les petites cases de gauche
END SUB
' --- Actualisation de l'ecran pour une nouvelle partie ---
SUB FNC_Preparation
CLS
LOCATE 2, 35: COLOR 15: PRINT "MASTERMIND"
kcouleur = 8
FOR kdebut = 320 TO 530 STEP 40
kfin = kdebut + 30
kcouleur = kcouleur + 1
LINE (kdebut, 350)-(kfin, 380), kcouleur, BF
IF kdebut > 320 AND kdebut < 500 THEN
LINE (kdebut, 90)-(kfin, 120), 15, B
LINE (kdebut, 245)-(kfin, 275), 15, B
END IF
NEXT kdebut
LOCATE 5, 47: PRINT "CODE A DECOUVRIR."
LOCATE 15, 47: PRINT "VOTRE PROPOSTION."
LOCATE 25, 41: PRINT "Cliquez sur les couleurs pour"
LOCATE 26, 41: PRINT "constituer votre combinaison."
END SUB
' --- Fin de la partie - affichage du resultat ---
SUB FNC_Resultat (Qtrouves)
' --- Affichage de la combinaison a trouver ---
FOR kcase = 0 TO 3
kcouleur = LST_Couleurs(kcase)
kdebut = 360 + (kcase * 40)
kfin = kdebut + 30
LINE (kdebut, 90)-(kfin, 120), kcouleur, BF
NEXT kcase
' --- Affichage du resultat (gagne ou perdu) ---
IF Qtrouves = 4 THEN
COLOR 14
LOCATE 10, 42: PRINT "FELICITATION ! C'est gagnee."
LOCATE 11, 41: PRINT "Vous avez trouvez le bon code."
ELSE
COLOR 12
LOCATE 10, 41: PRINT "Vous n'avez pas trouve le code."
LOCATE 11, 42: PRINT "Vous avez perdu cette partie."
END IF
END SUB
' --- Programme : JFB ---
' --- Mars 2023 ---
' --- Fin ---
Pour mieux comprendre l'exemple en QB64.
REM --- Origine Nerd propose pour ---
REM --- RETROGICIEL - BBC BASIC ---
REM --- Mastermind ---
REM --- INITIALISATION GENERALE ---
MODE 8
OFF
DIM LST_Couleurs( 5 ) : LST_Couleurs() = 9 , 10 , 11 , 12 , 13 , 14
DIM LST_Choix( 3 )
REM --- BOUCLE PRINCIPALE ---
REPEAT
REM --- Preparation d'une nouvelle partie ---
PROC_Melange
PROC_Preparation
LST_Choix() = 0 , 0 , 0 , 0
FOR ktour = 1 TO 12
REM --- Choix d'une combinaison de 4 couleurs par l'utilsateur ---
PRINT TAB( 47 , 14 )"Essais : "; STR$(ktour); "/12"
kplace = 0
REPEAT
kcouleur = 0
REPEAT : MOUSE kabscisse , kordonnee , kbouton : UNTIL kbouton = 4
PROC_Couleur( kcouleur )
IF kcouleur <> 0 THEN
IF kplace = 0 THEN PROC_Actualise( ktour )
LST_Choix( kplace ) = kcouleur
PROC_Placement( ktour , kplace )
kplace += 1
ENDIF
UNTIL kplace = 4
REM --- Analyse de la combinason de l'utilisateur ---
PROC_Analyse( ktour, ktrouves )
IF ktrouves = 4 THEN EXIT FOR
NEXT ktour
REM --- Fin de la partie ---
PROC_Resultat( ktrouves )
COLOUR 15
PRINT TAB( 40 , 12 ) "Voulez-vous recommencer [ O ou N ] "
kchoix$ = GET$
UNTIL kchoix$ = "N" OR kchoix$ = "n"
REM --- FIN DU PROGRAMME ---
PROC_Preparation
PRINT TAB( 50 , 11 ) "Au revoir."
END
REM --- DEFINITION DES FONCTIONS PERSONNALISEES ---
REM --- Actualisation de l'ecran pour la combinaison suivante ---
DEF PROC_Actualise( Q )
IF Q < 10 THEN kcolonne = 2 ELSE kcolonne = 1
PRINT TAB( kcolonne , 2 + ( 2 * Q ) ) STR$( Q )
GCOL 0 : RECTANGLE FILL 720 , 410 , 320 , 60
kordonnee = 932 - ( 64 * Q )
GCOL 15
FOR kcase = 0 TO 3
kabscisse = 720 + ( 80 * kcase )
RECTANGLE kabscisse , 410 , 60 , 60
kabscisse = 60 + ( 30 * kcase )
RECTANGLE kabscisse , kordonnee , 20 , 30
NEXT kcase
PRINT TAB( 42 , 20 ) "Couleurs bien placees : "
PRINT TAB( 42 , 21 ) "Couleurs presentes... : "
PRINT TAB( 42 , 22 ) "Couleurs absentes.... : "
ENDPROC
REM --- Analyse d'une combinaison de l'utilisateur ---
DEF PROC_Analyse(Qtour, RETURN QTrouves)
REM --- Recherche des couleurs trouvees ---
QTrouves = 0
FOR kindex = 0 TO 3
IF LST_Choix( kindex ) = LST_Couleurs( kindex ) THEN
QTrouves += 1
LST_Choix( kindex ) = 0
ENDIF
NEXT kindex
REM --- Recherche des couleurs presentes ---
kpresents = 0
FOR kindex = 0 TO 3
IF LST_Couleurs( kindex ) = LST_Choix( 0 ) THEN kpresents += 1
IF LST_Couleurs( kindex ) = LST_Choix( 1 ) THEN kpresents += 1
IF LST_Couleurs( kindex ) = LST_Choix( 2 ) THEN kpresents += 1
IF LST_Couleurs( kindex ) = LST_Choix( 3 ) THEN kpresents += 1
NEXT kindex
REM --- Affichage de l'analyse ---
kabsentes = 4 - QTrouves - kpresents
kanalyse$ = STR$( QTrouves ) + STR$( kpresents ) + STR$( kabsentes )
PRINT TAB( 12 , 2 + ( 2 * Qtour ) ) kanalyse$
PRINT TAB( 66 , 20 ) STR$( QTrouves )
PRINT TAB( 66 , 21 ) STR$( kpresents )
PRINT TAB( 66 , 22 ) STR$( kabsentes )
ENDPROC
REM --- Saisie d'une combinaison par l'utiliateur ---
DEF PROC_Couleur( RETURN Qcouleur )
REPEAT : MOUSE kabscisse , kordonnee , kbouton : UNTIL kbouton = 0
IF kordonnee > 180 AND kordonnee < 240 THEN Qcouleur = POINT( kabscisse , kordonnee )
ENDPROC
REM --- Affichage de la couleur choisie dans sa case ---
DEF PROC_Placement( Qtour , Qplace )
GCOL LST_Choix( Qplace )
kabscisse = 720 + ( Qplace * 80 )
RECTANGLE FILL kabscisse , 410 , 60 , 60
kabscisse = 60 + ( 30 * Qplace )
kordonnee = 932 - ( 64 * Qtour )
RECTANGLE FILL kabscisse , kordonnee , 20 , 30
ENDPROC
REM --- Actualisation de l'ecran pour une nouvelle partie ---
DEF PROC_Preparation
CLS : CLG
COLOUR 15: PRINT TAB( 35 , 1 ) "MASTERMIND"
kcouleur = 8
FOR kdebut = 640 TO 1060 STEP 80
kcouleur = kcouleur + 1
GCOL kcouleur : RECTANGLE FILL kdebut , 180 , 60 , 60
IF kdebut > 700 AND kdebut < 1000 THEN
GCOL 15
RECTANGLE kdebut , 785 , 60 , 60
RECTANGLE kdebut , 410 , 60 , 60
ENDIF
NEXT kdebut
PRINT TAB( 46 , 4 ) "CODE A DECOUVRIR."
PRINT TAB( 46 , 16 ) "VOTRE PROPOSTION."
PRINT TAB( 40 , 27 ) "Cliquez sur les couleurs pour"
PRINT TAB( 40 , 28 ) "constituer votre combinaison."
ENDPROC
REM --- Brassage aleatoire des couleurs pour le code a trouver ---
DEF PROC_Melange
FOR ktour = 1 TO 100
ka = RND( 6 ) - 1 : kb = RND( 6 ) - 1
IF ka <> kb THEN SWAP LST_Couleurs(ka), LST_Couleurs(kb)
NEXT ktour
ENDPROC
REM --- Fin de la partie - affichage du resultat ---
DEF PROC_Resultat( Qtrouves )
REM --- Affichage de la combinaison a trouver ---
FOR kcase = 0 TO 3
GCOL LST_Couleurs( kcase )
kabsbisse = 720 + ( 80 * kcase )
RECTANGLE FILL kabsbisse , 785 , 60 , 60
NEXT kcase
REM --- Affichage du resultat (gagne ou perdu) ---
IF Qtrouves = 4 THEN
COLOUR 11
PRINT TAB( 42 , 9 ) "FELICITATION ! C'est gagnee."
PRINT TAB( 41 , 10 ) "Vous avez trouvez le bon code."
ELSE
COLOUR 6
PRINT TAB( 41 , 9 ) "Vous n'avez pas trouve le code."
PRINT TAB( 42 , 10 ) "Vous avez perdu cette partie."
ENDIF
ENDPROC
REM --- Programme : JFB ---
REM --- Mars 2023 ---
REM --- Fin ---
Pour mieux comprendre l'exemple en BBC BASIC.