Advertisement
Paralogos

tp5

Dec 18th, 2019
2,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.98 KB | None | 0 0
  1. type noeud = {
  2.   mutable nord: bool;
  3.   mutable est: bool;
  4.   mutable sud: bool;
  5.   mutable ouest: bool
  6. }
  7. and graphe_grille = {
  8.   n: int;
  9.   m: int;
  10.   adj: noeud array array
  11. };;
  12.  
  13. let creer_grille l1 l2 =
  14.   let m = Array.make_matrix l1 l2 {nord=false;sud=false;ouest=false;est=false} in
  15.   for x=0 to (l1-1) do
  16.     for y=0 to (l2-1) do
  17.       m.(x).(y)<-{nord=false;sud=false;ouest=false;est=false}
  18.     done
  19.   done;
  20. {n=l1;m=l2;adj=m};;
  21.  
  22. (*
  23. let string_of_grille a =
  24.   let t = a.adj in
  25.   let md = "   |" in
  26.   let vd = "    " in
  27.   let mb = "---x" in
  28.   let vb = "   x" in
  29. print_string "x";
  30. for i = 0 to (a.m-1) do print_string mb done;
  31.   for x = 0 to (a.n-1) do
  32.     for y = 0 to (a.m-1) do
  33.       *)
  34.  
  35. let string_of_grille g =
  36.   let res = ref "" in
  37.   for i = 0 to (g.n - 1) do
  38.  
  39.     for j = 0 to (g.m - 1) do
  40.       res := !res^"x";
  41.       if g.adj.(i).(j).nord then
  42.     res := !res^"   "
  43.       else
  44.     res := !res^"---"
  45.     done;
  46.     res := !res^"x\n";
  47.     for j = 0 to (g.m - 1) do
  48.       if g.adj.(i).(j).ouest then
  49.     res := !res^" "
  50.       else
  51.     res := !res^"|";
  52.       res := !res^"   "
  53.     done;
  54.     if g.adj.(i).(g.m - 1).est then
  55.       res := !res^" \n"
  56.     else
  57.       res := !res^"|\n"
  58.   done;
  59.   for j = 0 to (g.m - 1) do
  60.     res := !res^"x";
  61.     if g.adj.(g.n-1).(j).sud then
  62.       res := !res^"   "
  63.     else
  64.       res := !res^"---"
  65.   done;
  66.   res := !res^"x\n";
  67.   !res
  68. ;;
  69.  
  70. print_string (string_of_grille (creer_grille 24 24));;
  71.  
  72. let ouvrir g (i,j) (a,b) =
  73.    let x=i-a in
  74.    let y=j-b in
  75.    if x=1 && y=0
  76.    then ((g.adj.(i).(j)).nord<-true;(g.adj.(a).(b)).sud<-true)
  77.    else if x=(-1) && y=0
  78.    then ((g.adj.(i).(j)).sud<-true;(g.adj.(a).(b)).nord<-true)
  79.    else if x=0 && y=1
  80.    then ((g.adj.(i).(j)).ouest<-true;(g.adj.(a).(b)).est<-true)
  81.    else if x=0 && y=(-1)
  82.    then ((g.adj.(i).(j)).est<-true;(g.adj.(a).(b)).ouest<-true)
  83.    else failwith "nop";;
  84.  
  85. let test = creer_grille 3 3 in ouvrir test (0,0) (0,1);print_string (string_of_grille test);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement