Wyświetlenie losowych 30 pól tablicy dwuwymiarowej

0

mam stale w tablicy na [1..9,1..9] i chcialbym aby losowo wyswietlic 30 roznych pol.. jak to zrobic? prosze o pomoc

0
wyzeruj licznik;
wyzeruj wylosowane;
powtarzaj 
{
      wylosuj x z zakresu 1..9;
      wylosuj y z zakresu 1..9;
      jesli nie ma w wylosowanych punktu [x,y]
     {
          wyswietl pole[x,y];
          dodaj do listy wylosowanych punktow punkt[x,y];
          zwieksz licznik o jeden; 
      }
} dopóki licznik <30

Albo jakoś tak podobnie...

0

Np. tak:

const
  ARR_SIZE_LOW  = Integer(1);
  ARR_SIZE_HIGH = Integer(9);

type
  T2DArray = array [ARR_SIZE_LOW .. ARR_SIZE_HIGH, ARR_SIZE_LOW .. ARR_SIZE_HIGH] of Integer;

  procedure FillArray(var AArray: T2DArray);
  var
    intX, intY: Integer;
  begin
    for intX := ARR_SIZE_LOW to ARR_SIZE_HIGH do
      for intY := ARR_SIZE_LOW to ARR_SIZE_HIGH do
        AArray[intX, intY] := Random(High(Byte));
  end;

  procedure ShowArrayContent(AArray: T2DArray);
  var
    intX, intY: Integer;
  begin
    for intX := ARR_SIZE_LOW to ARR_SIZE_HIGH do
    begin
      for intY := ARR_SIZE_LOW to ARR_SIZE_HIGH - 1 do
        Write(AArray[intY, intX]:3, '  ');

      WriteLn(AArray[ARR_SIZE_HIGH, intX]);
    end;

    WriteLn;
  end;

  procedure ShowRandomArrayFields(AArray: T2DArray);
  const
    RANDOM_FIELDS_LOW  = Integer(1);
    RANDOM_FIELDS_HIGH = Integer(30);
  type
    TRandomFieldRec = record
      X, Y: Byte;
    end;
  type
    TRandom2DArrayFields = array [RANDOM_FIELDS_LOW .. RANDOM_FIELDS_HIGH] of TRandomFieldRec;
  var
    rafNumbers: TRandom2DArrayFields;

    function FieldHasNotBeenDrawn(AField: TRandomFieldRec; ACurrIndex: Integer): Boolean;
    var
      intToken: Integer;
    begin
      for intToken := RANDOM_FIELDS_LOW to ACurrIndex - 1 do
        if (rafNumbers[intToken].X = AField.X) and (rafNumbers[intToken].Y = AField.Y) then
        begin
          Result := False;
          Exit;
        end;

      Result := True;
    end;

  var
    intToken: Integer;
    rfrNumber: TRandomFieldRec;
  begin
    FillChar(rafNumbers[RANDOM_FIELDS_LOW], RANDOM_FIELDS_HIGH * 2, 0);

    for intToken := RANDOM_FIELDS_LOW to RANDOM_FIELDS_HIGH do
    begin
      repeat
        rfrNumber.X := Random(ARR_SIZE_HIGH) + 1;
        rfrNumber.Y := Random(ARR_SIZE_HIGH) + 1;
      until FieldHasNotBeenDrawn(rfrNumber, intToken);

      rafNumbers[intToken].X := rfrNumber.X;
      rafNumbers[intToken].Y := rfrNumber.Y;

      WriteLn(Format('[%d,%d] = %d', [rfrNumber.X, rfrNumber.Y, AArray[rfrNumber.X, rfrNumber.Y]]));
    end;
  end;

var
  arrNumbers: T2DArray;
begin
  Randomize();
  FillArray(arrNumbers);

  ShowArrayContent(arrNumbers);
  ShowRandomArrayFields(arrNumbers);

  ReadLn;
end.

Wyjście:

187   20  131    1  212  198  202   61  209
 63   93  171  153  148  138   96   31  56
 30   44  199  237  234   50   51  251  88
 23   94   29  239  246  129   59  116  242
180  203  166  125  176   22  131   25  112
237  183  199  102   74  149   98  138  191
 24    6  147   14  171  115  109   60  202
221   95  179  229  177  232  127   28  194
152   33  199   50  213  116   38  175  219

[4,9] = 50
[4,2] = 153
[1,6] = 237
[9,2] = 56
[5,7] = 171
[7,1] = 202
[8,8] = 28
[6,9] = 116
[2,8] = 95
[8,6] = 138
[6,7] = 115
[2,2] = 93
[8,4] = 116
[1,1] = 187
[9,9] = 219
[1,7] = 24
[3,4] = 29
[1,4] = 23
[4,6] = 102
[1,9] = 152
[3,1] = 131
[5,8] = 177
[2,3] = 44
[9,5] = 112
[7,8] = 127
[7,4] = 59
[9,7] = 202
[2,1] = 20
[1,2] = 63
[4,4] = 239
1

@furious programming zbyt skomplikowałeś:

program ideone;

type TData=array[0..8,0..8] of Integer;
type TArr=array[0..29] of Integer;

const data:TData=
(
  (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)
);

procedure rand30(var arr:TArr;const data:TData);
var copy:TData;
var I,P,swap:Integer;
begin
  copy:=data;
  for I:=0 to high(TArr) do
  begin
  	P:=I+Random(9*9-I);
  	swap:=copy[0,P];
  	copy[0,P]:=copy[0,I];
  	copy[0,I]:=swap;
  	arr[I]:=swap;
  end;
end;

procedure show(const arr:TArr);
var I:Integer;
begin
  for I:=0 to high(TArr) do Write(' ',arr[I]);
  WriteLn;
end;

var arr:TArr;
begin
  Randomize;
  rand30(arr,data);
  show(arr);
  rand30(arr,data);
  show(arr);
  rand30(arr,data);
  show(arr);
  rand30(arr,data);
  show(arr);
end.

http://ideone.com/6FEeLF

1

Na wszelki wypadek jeżeli chodzi o 30 wartości na swoich miejscach:

program ideone;

type TData=array[0..8,0..8] of Integer;
type TArr=array[0..8,0..8] of Boolean;

const data:TData=
(
  (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)
);

procedure randShow(var arr:TArr;ToShow:Integer);
var I,P:Integer;
var swap:Boolean;
begin
  for I:=0 to ToShow-1 do arr[0,I]:=true;
  for I:=ToShow to 9*9-1 do arr[0,I]:=false;
  for I:=1 to 9*9-1 do
  begin
  	P:=Random(I+1);
  	swap:=arr[0,P];
  	arr[0,P]:=arr[0,I];
  	arr[0,I]:=swap;
  end;
end;

procedure show(const data:TData;const arr:TArr);
var Y,X:Integer;
begin
  for Y:=0 to 9-1 do
  begin
    for X:=0 to 9-1 do
    begin
      if arr[Y,X] then Write(' ',data[Y,X]) else Write(' --');
    end;
    WriteLn;
  end;
  WriteLn;
end;

var arr:TArr;
begin
  Randomize;
  randShow(arr,10);
  show(data,arr);
  randShow(arr,20);
  show(data,arr);
  randShow(arr,30);
  show(data,arr);
  randShow(arr,40);
  show(data,arr);
end.

http://ideone.com/lHHq1W

1 użytkowników online, w tym zalogowanych: 0, gości: 1