Log in

View Full Version : Returning an array of an array



kitchenrange
12-27-2010, 06:57 AM
So I have a function that returns an array of an array of integers. I want to do things based off of those integers, but I'm having a hard time.

I have tried declaring a multidimensional array of the correct size and setting it equal to the function(we'll say Values). Then accessed the parts through the variable name (Values[0][0] etc.) but that didn't work. This does, however, compile.

I have tried

if FunctionName[0][0] then

but that doesn't work either. It returns a compilation error.

Has anyone ever done something similar to this?

I'm guessing I'll need the code up here for simplicity.


This is the function that returns the multidimensional array.
function CheckCopies : array[0..2] of array[0..1] of Integer;
var
i, p, y, u, s, SetOne, SetTwo, SetThree : integer;
Copies, CopiesTwo, CopiesThree : array of integer;
HandRanks : array[1..7] of Integer;
x : longint;
begin
for i := 1 to 7 do HandRanks[i] := MyHand[i].rank;
for i := 1 to 7 do
begin
if HandRanks[i] = 0 then
begin
x := i - 1;
exit;
end;
i := i + 1;
end;
SetLength(HandRanks, x);
for i := low(HandRanks) to high(HandRanks) do
begin
for p := low(HandRanks) to high(HandRanks) do
begin
if p = i then continue;
if HandRanks[i] = HandRanks[p] then
begin
if SetOne = 0 then
begin
SetOne := HandRanks[i];
SetLength(Copies, 1);
Copies[y] := HandRanks[i];
y := y + 1;
SetLength(Copies, Length(Copies) + 1);
Copies[y] := HandRanks[p];
y := y + 1;
p := p + 1;
Continue;
end else
begin
if SetOne = HandRanks[i] then
begin
Copies[y] := HandRanks[i];
y := y + 1;
p := p + 1;
Continue;
end else
begin
if SetTwo = 0 then
begin
SetTwo := HandRanks[i];
SetLength(CopiesTwo, 1);
CopiesTwo[u] := HandRanks[i];
u := u + 1;
SetLength(CopiesTwo, Length(CopiesTwo) + 1);
CopiesTwo[u] := HandRanks[p];
u := u + 1;
p := p + 1;
Continue;
end else
begin
if SetTwo = HandRanks[i] then
begin
SetLength(CopiesTwo, Length(CopiesTwo) + 1);
CopiesTwo[u] := HandRanks[i];
u := u + 1;
p := p + 1;
Continue;
end else
begin
if SetThree = 0 then
begin
SetThree := HandRanks[i];
SetLength(CopiesThree, 1);
CopiesThree[s] := HandRanks[i];
SetLength(CopiesThree, Length(CopiesThree) + 1);
s := s + 1;
CopiesThree[s] := HandRanks[p];
s := s + 1;
p := p + 1;
Continue;
end else
begin
WriteLn('What the fuck four pairs with seven cards.');
TerminateScript;
end;
end;
end;
end;
end;
end;
p := p + 1;
end;
i := i + 1;
end;
Result[0][0] := SetOne;
Result[0][1] := Length(Copies);
Result[1][0] := SetTwo;
Result[1][1] := Length(CopiesTwo);
Result[2][0] := SetThree;
Result[2][1] := Length(CopiesThree);
end;


This is the functionthat needs to use the results. This is after trying to use the FunctionName[0][0] method.

function RankHand : boolean;
begin
Result := False;
if (CheckFlush = 2) and (FlopCards[1].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because of chance of flush');
end;
if (CheckFlush = 4) and (FlopCards[5].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because of chance of flush');
end;

if (CheckStraight = 4) and (FlopCards[5].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because of chance of straight');
end;
if (CheckCopies[0][0] > 0) and (FlopCards[1].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because I have at least a pair with no flop yet');
end;
if (CheckCopies[1][0] > 0) then
begin
Result := True;
WriteLn('I have two pairs');
end;
if (CheckCopies[0][1] > 2) then
begin
Result := True;
WriteLn('I have three of a kind');
end;
if (CheckFlush = 5) then
begin
Result := True;
WriteLn('I have a flush');
end;
if (CheckStraight = 5) then
begin
Result := True;
WriteLn('I have a straight');
end;
end;

Frement
12-27-2010, 07:00 AM
function Test: T2DIntegerArray;
begin
Result[0][0] := 1;
end;

Blumblebee
12-27-2010, 07:04 AM
hope this helps

program new;

function example(): array of tIntegerArray;
begin
setarraylength(result, 1);
setarraylength(result[0], 1);
result[0][0] := 10;
end;

var
accessor: array of tIntegerArray;

begin
accessor := example();
writeLn('Length of External Array = '+IntToStr(Length(accessor))
+', Length of Internal Array = '+IntToStr(Length(accessor[0])));
writeLn(IntToStr(accessor[0][0]));
end.

Frement
12-27-2010, 07:12 AM
function CheckCopies : array[0..2] of array[0..1] of Integer;
var
i, p, y, u, s, SetOne, SetTwo, SetThree : integer;
Copies, CopiesTwo, CopiesThree : array of integer;
HandRanks : array[1..7] of Integer;
x : longint;
begin
for i := 1 to 7 do HandRanks[i] := MyHand[i].rank;
for i := 1 to 7 do
begin
if HandRanks[i] = 0 then
begin
x := i - 1;
exit;
end;
i := i + 1;
end;
SetLength(HandRanks, x);
for i := low(HandRanks) to high(HandRanks) do
begin
for p := low(HandRanks) to high(HandRanks) do
begin
if p = i then continue;
if HandRanks[i] = HandRanks[p] then
begin
if SetOne = 0 then
begin
SetOne := HandRanks[i];
SetLength(Copies, 1);
Copies[y] := HandRanks[i];
y := y + 1;
SetLength(Copies, Length(Copies) + 1);
Copies[y] := HandRanks[p];
y := y + 1;
p := p + 1;
Continue;
end else
begin
if SetOne = HandRanks[i] then
begin
Copies[y] := HandRanks[i];
y := y + 1;
p := p + 1;
Continue;
end else
begin
if SetTwo = 0 then
begin
SetTwo := HandRanks[i];
SetLength(CopiesTwo, 1);
CopiesTwo[u] := HandRanks[i];
u := u + 1;
SetLength(CopiesTwo, Length(CopiesTwo) + 1);
CopiesTwo[u] := HandRanks[p];
u := u + 1;
p := p + 1;
Continue;
end else
begin
if SetTwo = HandRanks[i] then
begin
SetLength(CopiesTwo, Length(CopiesTwo) + 1);
CopiesTwo[u] := HandRanks[i];
u := u + 1;
p := p + 1;
Continue;
end else
begin
if SetThree = 0 then
begin
SetThree := HandRanks[i];
SetLength(CopiesThree, 1);
CopiesThree[s] := HandRanks[i];
SetLength(CopiesThree, Length(CopiesThree) + 1);
s := s + 1;
CopiesThree[s] := HandRanks[p];
s := s + 1;
p := p + 1;
Continue;
end else
begin
WriteLn('What the fuck four pairs with seven cards.');
TerminateScript;
end;
end;
end;
end;
end;
end;
p := p + 1;
end;
i := i + 1;
end;
Result[0][0] := SetOne;
Result[0][1] := Length(Copies);
Result[1][0] := SetTwo;
Result[1][1] := Length(CopiesTwo);
Result[2][0] := SetThree;
Result[2][1] := Length(CopiesThree);
end;

function RankHand : boolean;
var Copies: T2DIntegerArray;
begin
Result := False;

if (CheckFlush = 2) and (FlopCards[1].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because of chance of flush');
end;
if (CheckFlush = 4) and (FlopCards[5].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because of chance of flush');
end;

if (CheckStraight = 4) and (FlopCards[5].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because of chance of straight');
end;
if (Copies[0][0] > 0) and (FlopCards[1].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because I have at least a pair with no flop yet');
end;
if (Copies[1][0] > 0) then
begin
Result := True;
WriteLn('I have two pairs');
end;
if (Copies[0][1] > 2) then
begin
Result := True;
WriteLn('I have three of a kind');
end;
if (CheckFlush = 5) then
begin
Result := True;
WriteLn('I have a flush');
end;
if (CheckStraight = 5) then
begin
Result := True;
WriteLn('I have a straight');
end;
end;

kitchenrange
12-27-2010, 05:04 PM
Ok, all that looks good and I see the changes, but where does "Copies" assume the identity of the results of the first function?

I'm very appreciative of the help, but if I just copy/paste the code without knowing exactly whats going on, i may have trouble implementing it later in other cases.

Frement
12-27-2010, 05:22 PM
Oops, I forgot to add that to the start of the function, just add it after "Result := False;"

Like this:
Result := False;
Copies := CheckCopies;

kitchenrange
12-27-2010, 07:35 PM
Frement, after trying what you have suggested, it turns out its the equivalent to the method I was using before trying the RankHand posted above.

It is the one I mentioned that compiles, but produces a type mismatch when

Copies := CheckCopies;

is declared. It seems that is also what you are suggesting Blumblebee?

I tried using array of tIntegerArray and T2DIntegerArray.

E: \/\/\/ Regarding the post below let me try that really quickly.

Blumblebee
12-27-2010, 07:39 PM
Frement, after trying what you have suggested, it turns out its the equivalent to the method I was using before trying the RankHand posted above.

It is the one I mentioned that compiles, but produces a type mismatch when

Copies := CheckCopies;

is declared.

the problem occurs when you use a T2DIntegerArray mixed with a array [0..1] of array [0..1] of integer. The issue is that the only way for both of them to be compatible is if they are identical types (I think I had an error like this and tried to fix it this way). What I would do is...

function CheckCopies: T2DintegerArray;

and in the beginning of the function set the lengths. Hopefully then it'll work out together. Who knows though

edit:

let me know if this works.

function CheckCopies : T2DIntegerArray;
var
i, p, y, u, s, SetOne, SetTwo, SetThree : integer;
Copies, CopiesTwo, CopiesThree : array of integer;
HandRanks : array[1..7] of Integer;
x : longint;
begin
SetArrayLength(result, 3);
for i := low(result) to high(result) do
SetArrayLength(result[i], 2);
for i := 1 to 7 do HandRanks[i] := MyHand[i].rank;
for i := 1 to 7 do
begin
if HandRanks[i] = 0 then
begin
x := i - 1;
exit;
end;
i := i + 1;
end;
SetLength(HandRanks, x);
for i := low(HandRanks) to high(HandRanks) do
begin
for p := low(HandRanks) to high(HandRanks) do
begin
if p = i then continue;
if HandRanks[i] = HandRanks[p] then
begin
if SetOne = 0 then
begin
SetOne := HandRanks[i];
SetLength(Copies, 1);
Copies[y] := HandRanks[i];
y := y + 1;
SetLength(Copies, Length(Copies) + 1);
Copies[y] := HandRanks[p];
y := y + 1;
p := p + 1;
Continue;
end else
begin
if SetOne = HandRanks[i] then
begin
Copies[y] := HandRanks[i];
y := y + 1;
p := p + 1;
Continue;
end else
begin
if SetTwo = 0 then
begin
SetTwo := HandRanks[i];
SetLength(CopiesTwo, 1);
CopiesTwo[u] := HandRanks[i];
u := u + 1;
SetLength(CopiesTwo, Length(CopiesTwo) + 1);
CopiesTwo[u] := HandRanks[p];
u := u + 1;
p := p + 1;
Continue;
end else
begin
if SetTwo = HandRanks[i] then
begin
SetLength(CopiesTwo, Length(CopiesTwo) + 1);
CopiesTwo[u] := HandRanks[i];
u := u + 1;
p := p + 1;
Continue;
end else
begin
if SetThree = 0 then
begin
SetThree := HandRanks[i];
SetLength(CopiesThree, 1);
CopiesThree[s] := HandRanks[i];
SetLength(CopiesThree, Length(CopiesThree) + 1);
s := s + 1;
CopiesThree[s] := HandRanks[p];
s := s + 1;
p := p + 1;
Continue;
end else
begin
WriteLn('What the fuck four pairs with seven cards.');
TerminateScript;
end;
end;
end;
end;
end;
end;
p := p + 1;
end;
i := i + 1;
end;
Result[0][0] := SetOne;
Result[0][1] := Length(Copies);
Result[1][0] := SetTwo;
Result[1][1] := Length(CopiesTwo);
Result[2][0] := SetThree;
Result[2][1] := Length(CopiesThree);
end;

function RankHand : boolean;
var Copies: T2DIntegerArray;
begin
Result := False;

if (CheckFlush = 2) and (FlopCards[1].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because of chance of flush');
end;
if (CheckFlush = 4) and (FlopCards[5].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because of chance of flush');
end;

if (CheckStraight = 4) and (FlopCards[5].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because of chance of straight');
end;
if (Copies[0][0] > 0) and (FlopCards[1].rank = 0) then
begin
Result := True;
WriteLn('Hand is good because I have at least a pair with no flop yet');
end;
if (Copies[1][0] > 0) then
begin
Result := True;
WriteLn('I have two pairs');
end;
if (Copies[0][1] > 2) then
begin
Result := True;
WriteLn('I have three of a kind');
end;
if (CheckFlush = 5) then
begin
Result := True;
WriteLn('I have a flush');
end;
if (CheckStraight = 5) then
begin
Result := True;
WriteLn('I have a straight');
end;
end;

kitchenrange
12-27-2010, 08:25 PM
That will compile and run, but the value of Copies is never assigned. If I add the assignment, it returns a type error.

Frement
12-27-2010, 08:39 PM
That will compile and run, but the value of Copies is never assigned. If I add the assignment, it returns a type error.

You have to do the Copies := YourFunctionNameHere;

EDIT: I thought I posted the function CheckCopies: T2DIntegerArray; fix, but apparently not :)

kitchenrange
12-27-2010, 08:42 PM
That is what I mean when I say the assignment. Assigning the value to Copies.
When i add

Copies := MyFunctionNameHere;

the script compiles but when that line is called, it returns a type error.

Frement
12-27-2010, 08:44 PM
Can you please paste whole script? Or PM if you dont want to publicly paste.

kitchenrange
12-27-2010, 09:43 PM
I believe I've solved the problem by creating a new type and using its fields. If you are just interested in the script though, pm me I dont mind sharing my work so far as long as its not completely public.

E : Not sure if its recognizing the pairs correctly yet just no errors I may get back to you soon.