SRL Archive - OSR Help

doing awkward things with nested for loops.


xdarkshadowx

2012-04-29 13:22:11


for r := ROWS downto 1 do
for c := COLUMNS - 3 downto 1 do

for r := 1 to ROWS do
for c := 1 to COLUMNS - 3 do

for r := 1 to ROWS do
for c := COLUMNS - 3 downto 1 do

for r := ROWS downto 1 do
for c := 1 to COLUMNS - 3 do

I want to be able to alternate between these in 8 of my procedures at the begining of them, randomly. I thought about using a case like

case whichWay:
o: begin
for r := ROWS downto 1 do
for c := COLUMNS - 3 downto 1 do
end;
1: begin
for r := 1 to ROWS do
for c := 1 to COLUMNS - 3 do
end;
ect…
but, there is code after these that the for loops need to run, and I don't know how to get them to run it with that configuration. I guess if there is no sollution I could just post the code that comes after it into each case but that would make my program 4x as large and just ugly looking, so not my first choice. Let me know if you guys have any suggestions? :s thanks

mixster

2012-04-29 13:32:12

const
ROWS = 10;
COLUMNS = 5;

type
TRowColProcedure = procedure(row, col: Integer);

procedure PerformRowColProcedure(num: Integer; proc: TRowColProcedure);
var
r, c: Integer;
begin
case num of
0: begin
for r := ROWS downto 1 do
for c := COLUMNS - 3 downto 1 do
proc(r, c);
end;
1: begin
for r := 1 to ROWS do
for c := 1 to COLUMNS - 3 do
proc(r, c);
end;
end;
end;

procedure PrintRowCol(row, col: Integer);
begin
Writeln(IntToStr(row) + ':' + IntToStr(col));
end;

begin
PerformRowColProcedure(1, @PrintRowCol);
end.

xdarkshadowx

2012-04-29 13:35:59

That's an interesting way to do it. Thanks haha, I think I understand what's happening.