Program JeuLabyrinthe ;
Uses
Crt ;
Const
HAUTEUR = 10 ;
LARGEUR = 20 ;
SORTIE_X = LARGEUR – 1 ;
SORTIE_Y = HAUTEUR – 1 ;
PERSONNAGE = ‘P’ ;
MUR = ‘#’ ;
SORTIE = ‘S’ ;
VIDE = ‘ ‘ ;
Type
Labyrinthe = array[1..HAUTEUR, 1..LARGEUR] of Char ;
Var
Lab : Labyrinthe ;
PosX, PosY : Integer ;
Key : Char ;
Procedure InitialiserLabyrinthe(var Lab : Labyrinthe) ;
Var
I, j : Integer ;
Begin
For i := 1 to HAUTEUR do
For j := 1 to LARGEUR do
Lab[i, j] := VIDE ;
// Création des murs
For i := 1 to HAUTEUR do
Begin
Lab[i, 1] := MUR ;
Lab[i, LARGEUR] := MUR ;
End ;
For j := 1 to LARGEUR do
Begin
Lab[1, j] := MUR ;
Lab[HAUTEUR, j] := MUR ;
End ;
// Positionner la sortie
Lab[SORTIE_Y, SORTIE_X] := SORTIE ;
End ;
Procedure AfficherLabyrinthe(Lab : Labyrinthe) ;
Var
I, j : Integer ;
Begin
Clrscr ;
For i := 1 to HAUTEUR do
Begin
For j := 1 to LARGEUR do
Write(Lab[i, j]) ;
Writeln ;
End ;
End ;
Procedure DeplacerPersonnage(var Lab : Labyrinthe ; var PosX, PosY : Integer ; DeplX,
DeplY : Integer) ;
Begin
If (Lab[PosY + DeplY, PosX + DeplX] <> MUR) then
Begin
Lab[PosY, PosX] := VIDE ;
PosX := PosX + DeplX ;
PosY := PosY + DeplY ;
Lab[PosY, PosX] := PERSONNAGE ;
End ;
End ;
Procedure JeuLabyrinthe() ;
Begin
InitialiserLabyrinthe(Lab) ;
PosX := 2 ;
PosY := 2 ;
Lab[PosY, PosX] := PERSONNAGE ;
Repeat
AfficherLabyrinthe(Lab) ;
Key := ReadKey ;
Case Key of
#72 : DeplacerPersonnage(Lab, PosX, PosY, 0, -1) ; // Haut
#80 : DeplacerPersonnage(Lab, PosX, PosY, 0, 1) ; // Bas
#75 : DeplacerPersonnage(Lab, PosX, PosY, -1, 0) ; // Gauche
#77 : DeplacerPersonnage(Lab, PosX, PosY, 1, 0) ; // Droite
End ;
Until (Lab[PosY, PosX] = SORTIE) ;
AfficherLabyrinthe(Lab) ;
Writeln(‘Félicitations, vous avez trouvé la sortie !’) ;
ReadKey ;
End ;
Begin
Clrscr ; // Efface l’écran
JeuLabyrinthe() ;
End.