1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| Dim rejouer As Integer
Dim quitter As Integer
Dim deviné As Boolean
Dim nbbon, nbplace As Integer
Dim i, j, c, k As Integer
Dim mystere(4) As Integer
Dim prop(39, 4) As Integer
Dim stockprop(4) As Integer
Dim stockmyst(4) As Integer
rejouer = 6
quitter = 6
MsgBox("Règles du jeu Mastermind : le but est de déchiffrer la combinaison mystère à 5 chiffres compris entre 0 et 4. Pour cela vous ferez des propositions qui seront analysées : pour chaque proposition vous aurez le nombre de chiffres bien placés et le nombre de chiffres mal placés. A partir de là, à vous de déchiffrer la combinaison en un nombre d'essais minimum. Bonne chance !", , "Règles du jeu")
Do While rejouer = 6
' --- Initialisation des variables ---
deviné = False
nbbon = 0
nbplace = 0
i = 0
j = 0
c = 0
k = 0
For i = 0 To 4
mystere(i) = 8
Next
For i = 0 To 4
For j = 0 To 4
prop(i, j) = 8
Next
Next
For i = 0 To 4
stockmyst(i) = 8
Next
For i = 0 To 4
stockprop(i) = 8
Next
' --- Combinaison mystère ---
For i = 0 To 4
Randomize()
mystere(i) = Int(Rnd() * 5)
Next
MsgBox(mystere(0))
MsgBox(mystere(1))
MsgBox(mystere(2))
MsgBox(mystere(3))
MsgBox(mystere(4))
'--- Lancement du jeu ---
Do While deviné = False
nbbon = 0
nbplace = 0
' --- Copie du tableu mystere (pour manipuler) ---
For i = 0 To 4
stockmyst(i) = mystere(i)
Next
' --- Remplir le tableau prop ---
For i = 0 To 4
prop(c, i) = InputBox("Proposition pour la case " & i + 1 & " de la combinaison", "Proposition de combinaison")
Next
' --- Copie du tableau prop (pour manipuler) ---
For i = 0 To 4
stockprop(i) = prop(c, i)
Next
' --- Comparer la combinaison mystere et la proposition ---
For i = 0 To 4
If stockprop(i) = stockmyst(i) Then
nbbon = nbbon + 1
stockprop(i) = stockmyst(i) = 8
Else : For j = 0 To 4
If stockprop(i) <> 8 Then
If stockprop(i) = stockmyst(j) Then
If stockmyst(j) <> stockprop(j) Then
nbplace = nbplace + 1
stockprop(i) = stockmyst(j) = 8
End If
End If
End If
Next
End If
Next
' --- Résultats ---
If nbbon < 5 Then
MsgBox("Vous avez " & nbbon & " chiffre(s) bien placé(s) et " & nbplace & " chiffre(s) mal placé(s). Courage !", , "Résultats")
' ---Affichage des propositions ---
For k = 0 To c
MsgBox("Proposition " & k + 1 & " : " & prop(k, 0) & prop(k, 1) & prop(k, 2) & prop(k, 3) & prop(k, 4), , "Combinaisons proposées")
Next
c = c + 1
If c > 6 Then
quitter = MsgBox( MsgBoxStyle.YesNo, "Continuer ?")
If quitter <> 6 Then
MsgBox("La combinaison était : " & mystere(0) & mystere(1) & mystere(2) & mystere(3) & mystere(4) & ".", , "Solution")
Exit Sub
End If
End If
Else
deviné = True
MsgBox("Bravo !!")
rejouer = MsgBox("Rejouer?", MsgBoxStyle.YesNo, "Rejouer ?")
End If
Loop
Loop |
Partager