Il s'agit du jeu bien connu ou l'un de 2 participants pense à un chiffre entre 1 et 1000 et l'autre doit le deviner par touches successives. Le premier doit répondre pour chaque essai "Trop" grand ou "Trop petit".
NbATrouver = Hasard (1 à 1000)
FlagTrouve = FAUX
TANT QUE FlagTrouve = FAUX
AFFICHE "Entre un nombre : "
ACCEPTE Essai
SI Essai = Nombre ALORS
FlagTrouve = VRAI
FIN SI
FIN TANT QUE
AFFICHE "BRAVO. Tu as trouvé"
AMELIORATIONS :
1. Il faudrait pouvoir choisir le nombre maximum au début de la partie
2. Il faut prévoir au début de chaque partie le nombre maximum d'essais dont on dispose, et indiquer le nombre d'essais restant à chaque essai
3. Il faut indiquer à la fin de chaque partie si on à gagné (on a découvert le nombre en moins de coups que le maximum), et si oui, indiquer le nombre d'essais effectués
4. Suivant le nombre d'essais effectués, afficher un petit commentaire adapté ("Quel champion", "Pas mal", Peut faire mieux")
5. Il faut demander à l'utilisateur à la fin de chaque partie s'il veut recommencer
Génération d'un nombre aléatoire entre 1 et 100 : Utilisation de la fonction RND
Sub NombreAleatoire()
NombreAuHasard = Rnd ' Nombre entre 0 et 1 (0.7894654)
MsgBox NombreAuHasard
NombreAuHasard = NombreAuHasard * 100 ' Nombre entre 0 et 99, avec des
décimales (78.94654)
MsgBox NombreAuHasard
NombreAuHasard = Int(NombreAuHasard) ' Nombre entre 0 et 99 sans décimales
(78)
MsgBox NombreAuHasard
NombreAuHasard = NombreAuHasard + 1 ' Nombre entre 1 et 100 sans décimales
(79)
MsgBox NombreAuHasard
La même chose en une seule ligne
Sub NombreAleatoireV2
NombreAuHasard = Int((100 * Rnd) + 1) ' (79)
End Sub
Utilisation de & (Concaténation)
Sub UtilisationEtCommercial()
Dim Nom As String, Prenom As String, Age As Integer
Nom = InputBox("Entrez votre nom")
Prenom = InputBox("Entrez votre Prénom")
Age = InputBox("Entrez votre age")
MsgBox "Vous vous appelez " & Prenom & " " & Nom & ".
Vous avez " & Age & " ans"
End Sub
Utilisation du if
Sub TropGrandTropPetitV1()
NombreATrouver = Int((1000 * Rnd) + 1)
Proposition = InputBox("Faites une propostion")
If Proposition = NombreATrouver Then
MsgBox "Vous avez trouvé le bon nombre"
End If
End Sub
Utilisation du if ... Else
Sub TropGrandTropPetitV2()
Dim NombreATrouver As Integer
NombreATrouver = Int((1000 * Rnd) + 1)
Proposition = InputBox("Faites une propostion (" & NombreATrouver
& ")")
If Proposition > NombreATrouver Then
MsgBox "Nombre trop grand"
Else:
MsgBox "Nombre trop petit"
End If
End Sub
Utilisation de Select Case
Sub SelectCase()
Dim Nombre
Nombre = 8
Select Case Nombre
Case Is <= 5
MsgBox "Entre 1 et 5"
Case 6, 7, 8
MsgBox "Entre 6 et 8"
Case 9 To 10
MsgBox "9 ou 10"
Case Else
MsgBox "Plus grand que 10"
End Select
End Sub
Utilisation du Select ... Case dans le jeu du plus grand plus petit
Sub TropGrandTropPetitV3()
Dim NombreATrouver As Integer
NombreATrouver = Int((1000 * Rnd) + 1)
Proposition = InputBox("Faites une propostion (" & NombreATrouver
& ")")
Select Case Proposition
Case Is < NombreATrouver
MsgBox "trop petit"
Case Is > NombreATrouver
MsgBox "trop grand"
Case NombreATrouver
MsgBox "GAGNE !!!"
End Select
End Sub
On recommence jusqu'à ce que le nombre soit trouvé
Sub TropGrandTropPetitV4()
Dim NombreATrouver As Integer, Proposition As Integer
NombreATrouver = Int((1000 * Rnd) + 1)
MsgBox NombreATrouver
Proposition = 0
Do Until Proposition = NombreATrouver
Proposition = InputBox("Faites une propostion (" & NombreATrouver
& ")")
Select Case Proposition
Case Is < NombreATrouver
MsgBox "trop petit"
Case Is > NombreATrouver
MsgBox "trop grand"
Case NombreATrouver
MsgBox "GAGNE !!!"
End Select
Loop
End Sub
On compte le nombre d'essais nécessaires :
Sub TropGrandTropPetitV5()
Dim NombreATrouver As Integer, Proposition As Integer, NombreEssais As
Integer
NombreATrouver = Int((1000 * Rnd) + 1)
Proposition = 0
NombreEssais = 0
Do Until Proposition = NombreATrouver
Proposition = InputBox(NombreEssais & " : Faites une
propostion (" & NombreATrouver & ")")
NombreEssais = NombreEssais + 1
Select Case Proposition
Case Is < NombreATrouver
MsgBox "trop petit"
Case Is > NombreATrouver
MsgBox "trop grand"
Case NombreATrouver
MsgBox "GAGNE en " &
NombreEssais & " essais !!!"
End Select
Loop
End Sub
On peut rejouer autant de fois qu'on veut :
Sub TropGrandTropPetitV6()
Dim NombreATrouver As Integer, Proposition As Integer, NombreEssais As
Integer, Recommencer As String
Recommencer = "oui"
Do While Recommencer = "oui"
Proposition = 0
NombreEssais = 0
NombreATrouver = Int((1000 * Rnd) + 1)
Do Until Proposition = NombreATrouver
NombreEssais = NombreEssais + 1
Proposition = InputBox(NombreEssais & " :
Faites une propostion (" & NombreATrouver & ")")
Select Case Proposition
Case Is < NombreATrouver
MsgBox "trop petit"
Case Is > NombreATrouver
MsgBox "trop grand"
Case NombreATrouver
MsgBox "GAGNE en " &
NombreEssais & " essais !!!"
End Select
Loop ' Entrer une autre proposition
Recommencer = InputBox("Voulez-vous recommencer ?")
Loop ' Recommencer le jeu
End Sub