bellum19 Posted April 22, 2012 Share Posted April 22, 2012 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. Quote Link to comment Share on other sites More sharing options...
FHannes Posted April 22, 2012 Share Posted April 22, 2012 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... Quote Link to comment Share on other sites More sharing options...
Janilabo Posted April 22, 2012 Share Posted April 22, 2012 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. Quote Link to comment Share on other sites More sharing options...
bellum19 Posted April 22, 2012 Author Share Posted April 22, 2012 @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. Quote Link to comment Share on other sites More sharing options...
FHannes Posted April 22, 2012 Share Posted April 22, 2012 I can add dedicated functions to do this if you like, but they will first appear in a final build next month, so up until that point you would have to use the alpha/beta version. Quote Link to comment Share on other sites More sharing options...
Janilabo Posted April 22, 2012 Share Posted April 22, 2012 (edited) I wrote a basic version for now.. I am sure you will be happy with Freddy's functions, though! Worth a wait. Here is my basic version (hay, at least it works ): [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 April 23, 2012 by Janilabo Added more stuff in... Quote Link to comment Share on other sites More sharing options...
bellum19 Posted April 23, 2012 Author Share Posted April 23, 2012 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. Quote Link to comment Share on other sites More sharing options...
FHannes Posted April 23, 2012 Share Posted April 23, 2012 I've released a new alpha with the functions [wiki=TIAMinEx]TIAMinEx[/wiki] and [wiki=TIAMaxEx]TIAMaxEx[/wiki]: http://forums.scar-divi.com/showthread.php?1330-SCAR-Divi-3-34-Alpha Quote Link to comment Share on other sites More sharing options...
Janilabo Posted April 23, 2012 Share Posted April 23, 2012 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 Quote Link to comment Share on other sites More sharing options...