Jump to content
bellum19

Finding the largest value in an array.

Recommended Posts

Simply put, I would like to know any easy way to find the largest value in an array of integers.

 

More specifically...

I have two arrays, players and enemies. And a third 2d array called threat. When a player attacks an enemy they get threat on the enemy. For example, Let's say player[1] attacks enemy[1] for 27 "threat." the value of threat[1][1] would be 27. Then let's say that player[2] also attacks enemy[1] but only for 20 "threat." Enemy[1] would have the most threat on player[1]. Once all of the players have had their turns, they all have varying threat on varying enemies. I want to be able to find out which player has the most threat for each individual enemy easily. I hope that is clear enough.

Link to comment
Share on other sites

Hmm, I think in your case the fastest way to find it would be just to loop through the array, there isn't really a function in SCAR dedicated to performing this operation. Normally I'd suggest sorting the array, but as I understand it, the order is important in this case...

Link to comment
Share on other sites

Can you show me a code example how this script works?

You don't have to show me full script, but a small bit of the code would be great to see.. (The arrays)

 

Also, do you need to get highest value of only Threat array? Or all arrays? ..and is Threat array a TIntArray/T2DIntArray? (This is why I ask for code example here)

 

I will try my best to help you out. :)

P.S. You can also just Private Message me more info.. If you want to keep your code away from public.

Link to comment
Share on other sites

@Freddy

That's what I was afraid of. I saw lots of examples about sorting arrays low to high, but I can't do that with mine because the order is important. It's difficult because the arrays will always be dynamic and I don't know how long they will be.

 

@Janilabo

I start by asking how many players there are:

SetArrayLength(players):= readln('How many players are there?');

 

then later I ask how many enemies there are:

SetArrayLength(enemies):= readln('How many enemies are there?');

 

Then I create the 2d array threat based on the sizes of the arrays players and enemies. The x length of threat will be equal to players length and threat's y length will be equal to enemys length.

If it were a 3x3 array with enemies being the rows and players being the columns:

 

[06,12,14]

[19,02,03]

[29,07,09]

I would want a result like:

Enemy 1 has the most threat on player 3 with 14 threat.

Enemy 2 has the most threat on player 1 with 19 threat.

Enemy 3 has the most threat on player 1 with 29 threat.

Link to comment
Share on other sites

I wrote a basic version for now..

 

I am sure you will be happy with Freddy's functions, though! :P Worth a wait.

 

Here is my basic version (hay, at least it works :D):

 

[scar]function GetHighestThreatByEnemy(threats: T2DIntArray; enemy: Integer; var player, threat: Integer): Boolean;

var

h, i: Integer;

begin

player := 0;

threat := 0;

h := High(threats);

if h < 0 then

Exit;

if High(threats[0]) < enemy then

Exit;

Result := True;

for i := 0 to h do

if threats[enemy] > threat then

begin

threat := threats[enemy];

player := i;

end;

end;

 

function SetupThreats(var threats: T2DIntArray): Boolean;

var

i, i2, players, enemies: Integer;

begin

players := StrToIntDef(ReadLn('Amount of players? [Only numbers allowed]'), 0);

enemies := StrToIntDef(ReadLn('Amount of enemies? [Only numbers allowed]'), 0);

Result := (players >= 1) and (enemies >= 1);

if not Result then

Exit;

SetLength(threats, players);

for i := 0 to (players - 1) do

begin

SetLength(threats, enemies);

for i2 := 0 to (enemies - 1) do

threats[i2] := StrToIntDef(GetNumbers(ReadLn('Enemy[' + IntToStr(i2) + '] threat on Player[' + IntToStr(i) + ']?')), 0);

end;

end;

 

function GetEnemyCount(threats: T2DIntArray): Integer;

var

h: Integer;

begin

h := High(threats);

if h > -1 then

Result := Length(threats[0]);

end;

 

var

h, i, player, enemies, threat: Integer;

threats: T2DIntArray;

 

begin

ClearDebug;

if not SetupThreats(threats) then

Exit;

enemies := GetEnemyCount(threats);

for i := 0 to (enemies - 1) do

if GetHighestThreatByEnemy(threats, i, player, threat) then

WriteLn('Enemy[' + IntToStr(i + 1) + '] has the most threat on Player[' + IntToStr(player + 1) + '] with ' + IntToStr(threat) + ' threat.');

h := high(threats);

for i := 0 to h do

SetLength(threats, 0);

SetLength(threats, 0);

end. [/scar]

 

Some more stuff I wrote to you (not sure if its really what you need, though..):

 

[scar]function GetHighestThreatByEnemy(threats: T2DIntArray; enemy: Integer; var player, threat: Integer): Boolean;

var

h, i: Integer;

begin

h := High(threats);

if h > -1 then

if High(threats[0]) >= enemy then

begin

player := -99999999999;

threat := -99999999999;

for i := 0 to h do

if threats[enemy] > threat then

begin

threat := threats[enemy];

player := i;

if not Result then

Result := True;

end;

end;

if not Result then

begin

player := -1;

threat := -1;

end;

end;

 

function GetLowestThreatByEnemy(threats: T2DIntArray; enemy: Integer; var player, threat: Integer): Boolean;

var

h, i: Integer;

begin

h := High(threats);

if h > -1 then

if High(threats[0]) >= enemy then

begin

player := 99999999999;

threat := 99999999999;

for i := 0 to h do

if (threats[enemy] < threat) then

begin

threat := threats[enemy];

player := i;

if not Result then

Result := True;

end;

end;

if not Result then

begin

player := -1;

threat := -1;

end;

end;

 

function GetHighestThreatByPlayer(threats: T2DIntArray; player: Integer; var enemy, threat: Integer): Boolean;

var

h, i: Integer;

begin

if High(threats) >= player then

begin

enemy := -99999999999;

threat := -99999999999;

h := High(threats[player]);

for i := 0 to h do

if (threats[player] > threat) then

begin

threat := threats[player];

enemy := i;

if not Result then

Result := True;

end;

end;

if not Result then

begin

enemy := -1;

threat := -1;

end;

end;

 

function GetLowestThreatByPlayer(threats: T2DIntArray; player: Integer; var enemy, threat: Integer): Boolean;

var

h, i: Integer;

begin

if High(threats) >= player then

begin

enemy := 99999999999;

threat := 99999999999;

h := High(threats[player]);

for i := 0 to h do

if (threats[player] < threat) then

begin

threat := threats[player];

enemy := i;

if not Result then

Result := True;

end;

end;

if not Result then

begin

enemy := -1;

threat := -1;

end;

end;

 

function SetThreatsByPlayers(var threats: T2DIntArray; playerArrs: T2DIntArray): Boolean;

var

h, h1, i: Integer;

begin

h := High(threats);

for i := 0 to h do

SetLength(threats, 0);

SetLength(threats, 0);

h := High(playerArrs);

Result := (h > -1);

if not Result then

Exit;

h1 := High(playerArrs[0]);

if h > 0 then

for i := 1 to h do

if h1 <> High(playerArrs) then

Exit;

SetLength(threats, (h + 1));

for i := 0 to h do

threats := playerArrs;

Result := True;

end;

 

function SetThreatsByEnemies(var threats: T2DIntArray; enemyArrs: T2DIntArray): Boolean;

var

h, h1, i, i1: Integer;

begin

h := High(threats);

for i := 0 to h do

SetLength(threats, 0);

SetLength(threats, 0);

h1 := High(enemyArrs);

Result := (h1 > -1);

if not Result then

Exit;

h := High(enemyArrs[0]);

if h1 > 0 then

for i := 1 to h1 do

if h <> High(enemyArrs) then

Exit;

SetLength(threats, (h1 + 1));

for i := 0 to h1 do

begin

SetLength(threats, (h + 1));

for i1 := 0 to h do

threats[i1] := enemyArrs[i1];

end;

Result := True;

end;

 

function GetEnemyCount(threats: T2DIntArray): Integer;

var

h: Integer;

begin

h := High(threats);

if h > -1 then

Result := Length(threats[0]);

end;

 

function GetPlayerCount(threats: T2DIntArray): Integer;

begin

Result := Length(threats);

end;

 

function SetupThreats(var threats: T2DIntArray): Boolean;

var

i, i2, players, enemies: Integer;

begin

players := StrToIntDef(ReadLn('Amount of players? [Only numbers allowed]'), 0);

enemies := StrToIntDef(ReadLn('Amount of enemies? [Only numbers allowed]'), 0);

Result := (players >= 1) and (enemies >= 1);

if not Result then

Exit;

SetLength(threats, players);

for i := 0 to (players - 1) do

begin

SetLength(threats, enemies);

for i2 := 0 to (enemies - 1) do

threats[i2] := StrToIntDef(GetNumbers(ReadLn('Enemy[' + IntToStr(i2) + '] threat on Player[' + IntToStr(i) + ']?')), 0);

end;

end;

 

var

h, i, player, enemy, players, enemies, threat: Integer;

threats, tmp: T2DIntArray;

 

begin

ClearDebug;

ClearReport;

SetLength(tmp, 3);

tmp[0] := [6, 12, 14];

tmp[1] := [19, 2, 3];

tmp[2] := [29, 7, 9];

if SetThreatsByEnemies(threats, tmp) then

begin

players := GetPlayerCount(threats);

WriteLn('THREATS FOR PLAYERS:');

for i := 0 to (players - 1) do

begin

if GetHighestThreatByPlayer(threats, i, enemy, threat) then

WriteLn('Player[' + IntToStr(i) + '] has the most threat with Enemy[' + IntToStr(enemy) + '] on ' + IntToStr(threat) + ' threat.');

if GetLowestThreatByPlayer(threats, i, enemy, threat) then

WriteLn('Player[' + IntToStr(i) + '] has the least threat with Enemy[' + IntToStr(enemy) + '] on ' + IntToStr(threat) + ' threat.');

end;

enemies := GetEnemyCount(threats);

WriteLn('');

WriteLn('THREATS BY ENEMY:');

for i := 0 to (enemies - 1) do

begin

if GetHighestThreatByEnemy(threats, i, player, threat) then

WriteLn('Enemy[' + IntToStr(i) + '] has the most threat on Player[' + IntToStr(player) + '] with ' + IntToStr(threat) + ' threat.');

if GetLowestThreatByEnemy(threats, i, player, threat) then

WriteLn('Enemy[' + IntToStr(i) + '] has the least threat on Player[' + IntToStr(player) + '] with ' + IntToStr(threat) + ' threat.');

end;

h := High(threats);

for i := 0 to h do

SetLength(threats, 0);

SetLength(threats, 0);

end;

SetLength(tmp, 0);

SetLength(tmp, 3);

tmp[0] := [6, 19, 29];

tmp[1] := [12, 2, 7];

tmp[2] := [14, 3, 9];

if SetThreatsByPlayers(threats, tmp) then

begin

players := GetPlayerCount(threats);

AddToReport('THREATS FOR PLAYERS:');

for i := 0 to (players - 1) do

begin

if GetHighestThreatByPlayer(threats, i, enemy, threat) then

AddToReport('Player[' + IntToStr(i) + '] has the most threat with Enemy[' + IntToStr(enemy) + '] on ' + IntToStr(threat) + ' threat.');

if GetLowestThreatByPlayer(threats, i, enemy, threat) then

AddToReport('Player[' + IntToStr(i) + '] has the least threat with Enemy[' + IntToStr(enemy) + '] on ' + IntToStr(threat) + ' threat.');

end;

enemies := GetEnemyCount(threats);

AddToReport('');

AddToReport('THREATS BY ENEMY:');

for i := 0 to (enemies - 1) do

begin

if GetHighestThreatByEnemy(threats, i, player, threat) then

AddToReport('Enemy[' + IntToStr(i) + '] has the most threat on Player[' + IntToStr(player) + '] with ' + IntToStr(threat) + ' threat.');

if GetLowestThreatByEnemy(threats, i, player, threat) then

AddToReport('Enemy[' + IntToStr(i) + '] has the least threat on Player[' + IntToStr(player) + '] with ' + IntToStr(threat) + ' threat.');

end;

h := High(threats);

for i := 0 to h do

SetLength(threats, 0);

SetLength(threats, 0);

end;

SetLength(tmp, 0);

end.[/scar]

 

Got some ideas for 2D stuff from this topic, and so I wrote these functions:

 

[scar]function AMax2D(x: T2DIntArray): Integer;

var

h, i, m: Integer;

begin

h := High(x);

if h < 0 then

Exit;

for i := 0 to h do

begin

m := AMax(x);

if m > Result then

Result := m;

end;

end;

 

function AMin2D(x: T2DIntArray): Integer;

var

h, i, m: Integer;

begin

h := High(x);

if h < 0 then

Exit;

for i := 0 to h do

begin

m := AMin(x);

if m < Result then

Result := m;

end;

end;

 

function Low2D(x: T2DIntArray): Integer;

var

h, i, m: Integer;

begin

h := High(x);

if h < 0 then

begin

Result := -1;

Exit;

end;

for i := 0 to h do

begin

m := Low(x);

if m < Result then

Result := m;

end;

end;

 

function High2D(x: T2DIntArray): Integer;

var

h, i, m: Integer;

begin

Result := -1;

h := High(x);

if h < 0 then

Exit;

for i := 0 to h do

begin

m := High(x);

if m > Result then

Result := m;

end;

end;

 

function AMax2DByColumn(x: T2DIntArray; columnIndex: Integer): Integer;

var

h, i, m: Integer;

begin

h := High(x);

if h < 0 then

Exit;

for i := 0 to h do

if High(x) >= columnIndex then

begin

m := Max(x[columnIndex], Result);

if m > Result then

Result := m;

end;

end;

 

function AMin2DByColumn(x: T2DIntArray; columnIndex: Integer): Integer;

var

h, i, m: Integer;

begin

h := High(x);

if h < 0 then

Exit;

for i := 0 to h do

if High(x) >= columnIndex then

begin

m := Min(x[columnIndex], Result);

if m < Result then

Result := m;

end;

end;

 

function High2DByColumn(x: T2DIntArray; columnIndex: Integer): Integer;

var

h, i: Integer;

begin

Result := -1;

h := High(x);

if h < 0 then

Exit;

for i := 0 to h do

if High(x) >= columnIndex then

Inc(Result);

end;

 

function GetTIAFromT2DIAByColumn(x: T2DIntArray; columnIndex: Integer): TIntArray;

var

h, i, r: Integer;

begin

h := High(x);

if h < 0 then

Exit;

SetLength(Result, (h + 1));

for i := 0 to h do

if High(x) >= columnIndex then

begin

Result[r] := x[columnIndex];

Inc®;

end;

SetLength(Result, r);

end;[/scar]

Edited by Janilabo
Added more stuff in...
Link to comment
Share on other sites

Wow, I'm totally blown away. I didn't expect this much help at all. Thanks so much both of you for being so willing to help me out. Janilabo, I was able to tweak the functions you wrote to fit into my script perfectly. Everything works as intended.

 

@Freddy,

Something like that would be very helpful and I would appreciate it very much.

Link to comment
Share on other sites

Wow, I'm totally blown away. I didn't expect this much help at all. Thanks so much both of you for being so willing to help me out. Janilabo, I was able to tweak the functions you wrote to fit into my script perfectly. Everything works as intended.
Hello bellum19,

Happy to hear you got it all working (and hopefully your project works like charm)!

 

Always willing to help. :)

-Jani

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...