Advertisement
jacknpoe

Classe para Campo Minado

Jul 11th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.10 KB | None | 0 0
  1. unit UCampo;
  2.  
  3. interface
  4.  
  5. type
  6.  
  7.   TCampo = class
  8.   private
  9.     cvisivel: array of boolean;
  10.     cbandeira: array of boolean;
  11.     cmina: array of boolean;
  12.     cbusca: array of boolean;
  13.     x_max, y_max, n_minas: integer;
  14.     morreu: boolean;
  15.     function  GetBusca( x, y: integer): boolean;
  16.     procedure SetBusca( x, y: integer; valor: boolean);
  17.     procedure AbreRedor( x, y: integer);
  18.   public
  19.     constructor Cria( x, y, minas: integer);
  20.     procedure Novo( x, y, minas: integer);
  21.     function  GetVisivel( x, y: integer): boolean;
  22.     function  GetBandeira( x, y: integer): boolean;
  23.     function  GetMina( x, y: integer): boolean;
  24.     function  GetNMinas( x, y: integer): integer;
  25.     function  GetBitMap( x, y: integer): integer;
  26.     function  FaltaMina: integer;
  27.     function  LimpaBandeiras: integer;
  28.     procedure Morre;
  29.     procedure AbreAoRedor( x, y: integer);
  30.     procedure SetVisivel( x, y: integer; valor: boolean);
  31.     procedure SetBandeira( x, y: integer; valor: boolean);
  32.     procedure SetMina( x, y: integer; valor: boolean);
  33.     destructor Destroi;
  34.   published
  35.     property X: integer read x_max write x_max;
  36.     property Y: integer read y_max write y_max;
  37.     property Minas: integer read n_minas write n_minas;
  38.   end;
  39.  
  40. implementation
  41.  
  42. {uses System;}
  43.  
  44. { TCampo }
  45.  
  46. procedure TCampo.AbreAoRedor(x, y: integer);
  47. var
  48.   tx, ty: integer;
  49. begin
  50.   for tx := 0 to x_max-1 do
  51.     for ty := 0 to y_max-1 do
  52.       SetBusca( tx, ty, False);
  53.   AbreRedor( x, y);
  54. end;
  55.  
  56. procedure TCampo.AbreRedor(x, y: integer);
  57. var
  58.   tx, ty: integer;
  59. begin
  60.   SetBusca( x, y, True);
  61.   for tx := x-1 to x+1 do
  62.   begin
  63.     for ty := y-1 to y+1 do
  64.     begin
  65.       if ( tx >= 0) AND ( ty >= 0) AND ( tx < x_max) AND ( ty < y_max) then
  66.       begin
  67.         SetVisivel( tx, ty, True);
  68.         if ( GetBusca( tx, ty) = False) AND ( GetNMinas( tx, ty) = 0) then AbreRedor( tx, ty);
  69.       end;
  70.     end;
  71.   end;
  72. end;
  73.  
  74. constructor TCampo.Cria(x, y, minas: integer);
  75. begin
  76.   Novo( x, y, minas);
  77. end;
  78.  
  79. destructor TCampo.Destroi;
  80. begin
  81.   SetLength( cvisivel, 0);
  82.   SetLength( cbandeira, 0);
  83.   SetLength( cmina, 0);
  84.   SetLength( cbusca, 0);
  85. end;
  86.  
  87. function TCampo.FaltaMina: integer;
  88. var
  89.   tx, ty, total: integer;
  90. begin
  91.   total := 0;
  92.   for tx := 0 to x_max - 1 do
  93.     for ty := 0 to y_max - 1 do
  94.       if GetVisivel( tx, ty) then Inc( total);
  95.   Result := x_max * y_max - total - n_minas;
  96. end;
  97.  
  98. function TCampo.GetBandeira(x, y: integer): boolean;
  99. begin
  100.   Result := cbandeira[ x + y * x_max];
  101. end;
  102.  
  103. function TCampo.GetBitMap(x, y: integer): integer;
  104. var
  105.   temp: integer;
  106. begin
  107.   if morreu AND GetMina( x,y) then
  108.   begin
  109.     Result := 3;
  110.     exit;
  111.   end;
  112.   if GetVisivel( x, y) then
  113.   begin
  114.     temp := GetNMinas( x, y) + 4;
  115.   end else begin
  116.     if GetBandeira( x, y) then temp := 2
  117.       else temp := 1;
  118.   end;
  119.   Result := temp;
  120. end;
  121.  
  122. function TCampo.GetBusca(x, y: integer): boolean;
  123. begin
  124.   Result := cbusca[ x + y * x_max];
  125. end;
  126.  
  127. function TCampo.GetMina(x, y: integer): boolean;
  128. begin
  129.   Result := cmina[ x + y * x_max];
  130. end;
  131.  
  132. function TCampo.GetNMinas(x, y: integer): integer;
  133. var
  134.   tx, ty, temp: integer;
  135. begin
  136.   temp := 0;
  137.   for tx := x-1 to x+1 do
  138.   begin
  139.     for ty := y-1 to y+1 do
  140.     begin
  141.       if ( tx >= 0) AND ( ty >= 0) AND ( tx < x_max) AND ( ty < y_max) then
  142.         if GetMina( tx, ty) then Inc(temp);
  143.     end;
  144.   end;
  145.   Result := temp;
  146. end;
  147.  
  148. function TCampo.GetVisivel(x, y: integer): boolean;
  149. begin
  150.   Result := cvisivel[ x + y * x_max];
  151. end;
  152.  
  153. function TCampo.LimpaBandeiras: integer;
  154. var
  155.   tx, ty, total: integer;
  156. begin
  157.   total := 0;
  158.   for tx := 0 to x_max - 1 do
  159.     for ty := 0 to y_max - 1 do
  160.       if GetBandeira( tx, ty) AND GetVisivel( tx, ty) then
  161.       begin
  162.         Inc( total);
  163.         SetBandeira( tx, ty, False);
  164.       end;
  165.   Result := total;
  166. end;
  167.  
  168. procedure TCampo.Morre;
  169. begin
  170.   morreu := True;
  171. end;
  172.  
  173. procedure TCampo.Novo(x, y, minas: integer);
  174. var
  175.   tx, ty, temp: integer;
  176. begin
  177.   if x < 1 then x := 1;
  178.   if y < 1 then y := 1;
  179.   if minas > x * y div 3 then minas := x * y div 3;
  180.   SetLength( cvisivel, x*y);
  181.   SetLength( cbandeira, x*y);
  182.   SetLength( cmina, x*y);
  183.   SetLength( cbusca, x*y);
  184.   x_max := x;
  185.   y_max := y;
  186.   n_minas := minas;
  187.   morreu := false;
  188.   for tx := 0 to x_max-1 do
  189.   begin
  190.     for ty := 0 to y_max-1 do
  191.     begin
  192.       SetVisivel( tx, ty, False);
  193.       SetBandeira( tx, ty, False);
  194.       SetMina( tx, ty, False);
  195.     end;
  196.   end;
  197.   temp := minas;
  198.   while temp > 0 do
  199.   begin
  200.     tx := Random( x_max);
  201.     ty := Random( y_max);
  202.     if not GetMina( tx, ty) then
  203.     begin
  204.       SetMina( tx, ty, True);
  205.       Dec( temp);
  206.     end;
  207.   end;
  208. end;
  209.  
  210. procedure TCampo.SetBandeira(x, y: integer; valor: boolean);
  211. begin
  212.   cbandeira[ x + y * x_max] := valor;
  213. end;
  214.  
  215. procedure TCampo.SetBusca(x, y: integer; valor: boolean);
  216. begin
  217.   cbusca[ x + y * x_max] := valor;
  218. end;
  219.  
  220. procedure TCampo.SetMina(x, y: integer; valor: boolean);
  221. begin
  222.   cmina[ x + y * x_max] := valor;
  223. end;
  224.  
  225. procedure TCampo.SetVisivel(x, y: integer; valor: boolean);
  226. begin
  227.   cvisivel[ x + y * x_max] := valor;
  228. end;
  229.  
  230. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement