Log in

View Full Version : Different number of Cases



Imagine
05-26-2012, 09:35 PM
This is sorta hard to explain, but I'm pretty much wondering if you're able to set a variable number of cases.

This gets my point across, but there are easier ways to do it and is not what I am trying to accomplish:

Say I'm writing a flax picking bot, so I write my TPA finder and all, and I'm left with X TPA's. (X being a variable). Would there be a way to construct a Switch statement such that it would have X different outcomes? (X would be different each time).

Again, I know there are many easier ways to do this specific task (Flax picking), but I'm asking in general if you're allowed or if it's possible to make a switch statement with different amount of cases.

Runaway
05-26-2012, 10:09 PM
Well I don't really understand this at all, but I'll take a shot at it >.<


var
i, hPoints, hColors: Integer;
aPoints: array of TPointArray;
TPA: TPointArray;
Colors: TIntegerArray;
begin
Colors := [1875487, 2686219, {etc.}];
hColors := High(Colors);
for i := 0 to hColors do
begin
FindColorxxx(TPA, Colors[i], {etc.});
aPoints[i] := TPA;
//...
end;
hPoints := High(aPoints);
for i := 0 to hPoints do
begin
MMouse(MiddleTPA(aPoints[i]).x, MiddleTPA(aPoints[i]).y, {etc.});
//...
end;
//...
end;


Lemme know if that's anything like what you were looking for haha :p

Imagine
05-26-2012, 10:21 PM
I don't need anything to do with finding the flax, that's just an example.

Say for example that I wanted to have a lottery, alright, but I didn't know how many people would enter. Person '1' is a, person '2' is called b, and I wanted to print out a customized message for each person.

So I'm asking how could I have an undefined number of cases in a switch statement?

Imagine
05-26-2012, 10:22 PM
And would something like:

Switch(random(A)) of:
1..x : ------
x..y : ------
y..z : ------
z..A : ------

work? And how would I be able to input more options?

honeyhoney
05-26-2012, 10:33 PM
Set a variable (X in your case) to the length of the TPA?

Runaway
05-26-2012, 10:43 PM
Yeah you lost me. Good luck ;)

Abu
05-26-2012, 10:49 PM
I'm confused, do you mean something like?

(run this)
program New;
{$i srl\srl.simba}

procedure NameEm;
var
name: Integer;
begin
for name := 0 to 5 do
begin
Writeln('The value is : ' + IntToStr(name));
Wait(500);
end;
end;

begin
ClearDebug;
SetUpSRL;
NameEm;
end.

bg5
05-27-2012, 12:31 AM
This ?
program new;

var
a ,b ,c : integer;
begin

a := 10;
b := 5;
c := 15;

case a of
0: writeln('df');
b..c : writeln('d');
end;
end.


And how would I be able to input more options?

You can't add options to case statement dynamically inside program. But you can do the same in few other ways. It would be simplier if you say exactly what you want to do.

J J
05-27-2012, 09:58 AM
This is sorta hard to explain, but I'm pretty much wondering if you're able to set a variable number of cases.

This gets my point across, but there are easier ways to do it and is not what I am trying to accomplish:

Say I'm writing a flax picking bot, so I write my TPA finder and all, and I'm left with X TPA's. (X being a variable). Would there be a way to construct a Switch statement such that it would have X different outcomes? (X would be different each time).

Again, I know there are many easier ways to do this specific task (Flax picking), but I'm asking in general if you're allowed or if it's possible to make a switch statement with different amount of cases.
You don't have an X amount of TPA's. A TPA is a TPointArray and FindColors will result an Array of TPoints. This means you have an X amount of TPoints. Every TPoint matches the color on the screen that you are searching for.

When you are standing in the flax field you will have multiple flax plants. You use FindColors to make a TPA.
WriteLn(''+ToStr(TPA)+'');
That gives you all the points eg.
Point(100, 100);
Point(105, 110);
Now if you want to randomly pick one of the flaxes you can do something similiar to this (Taken from my barbarian outpost development blog)
if (RopeLength[0] < 100) and (RopeLength[1] < 100) then
begin
case Random(4) of
0: begin
P := RandomRange(Low(SwingClSplit[0]), High(SwingClSplit[0]));
MMouse(SwingClSplit[0][P].X, SwingClSplit[0][P].Y, 0, 0); // Random spot on rope 1
end;
This is actually an array of splitted TPA so an ATPA (Array of TPointArray) aka Array of Array of TPoint

It will randomly click on one of the TPoints in the ATPA. Let me know if this is what you are looking to learn otherwise I will just be typing way too much xD

Chris
05-27-2012, 10:28 AM
I don't need anything to do with finding the flax, that's just an example.

Say for example that I wanted to have a lottery, alright, but I didn't know how many people would enter. Person '1' is a, person '2' is called b, and I wanted to print out a customized message for each person.

So I'm asking how could I have an undefined number of cases in a switch statement?

That would be done like:


var Participants: TStringArray;
i: integer;
begin
for i :=0 to high(Participants) do
case lowercase(Participants[i]) of
'bart': MessageHim('hey Bart, ')
'susan': MessageHim('hi mom..')
'lois': MessageHim('Hey <3')
'mark': MessageHim('yo mark, you joint anyways?')
'noob': MessageHim('wow your name is noob? awesome.')
end;

If for example Bart didnt join, he wont get the message.
These are customized messages for each person, I hope this explains something you want to know because Im not sure if this is what your asking for

putonajonny
05-27-2012, 10:56 AM
And would something like:

Switch(random(A)) of:
1..x : ------
x..y : ------
y..z : ------
z..A : ------

work? And how would I be able to input more options?

Yes that would work, let's take your lottery idea, you would have everybody pick 5 numbers, at the end of it you want to email everybody saying how many numbers they got right:

program new;
Var
NumberCorrect : array of integer;
Messages : array of String;
i : integer;
begin
//The first person got 4 right, the next got 0 etc...
NumberCorrect := [4, 0, 3, 2, 2, 5, 3];//This array could be any length (dependant on the number of people in your lottery)
Messages := ['Sorry you didn''t get any right, better luck next time', //your messages dependant on the number correct
'You only got 1 number correct, sorry',
'You managed to get 2 correct, well done',
'You got 3 correct and won £20, well done',
'You got 4 numbers correct and won £200, well done',
'You won the jackpot!!!!'];
for i := 0 to high(NumberCorrect) do //for each person it prints out their result
WriteLn('Person '+IntToStr(i)+' - '+Messages[NumberCorrect[i]]);

end.


Compiled successfully in 31 ms.
Person 0 - You got 4 numbers correct and won £200, well done
Person 1 - Sorry you didn't get any right, better luck next time
Person 2 - You got 3 correct and won £20, well done
Person 3 - You managed to get 2 correct, well done
Person 4 - You managed to get 2 correct, well done
Person 5 - You won the jackpot!!!!
Person 6 - You got 3 correct and won £20, well done
Successfully executed.

Imagine
05-27-2012, 12:50 PM
This ?
program new;

var
a ,b ,c : integer;
begin

a := 10;
b := 5;
c := 15;

case a of
0: writeln('df');
b..c : writeln('d');
end;
end.



You can't add options to case statement dynamically inside program. But you can do the same in few other ways. It would be simplier if you say exactly what you want to do.

Yeah, that was pretty much what I meant, you can't have a dynamic case statement can you?


You don't have an X amount of TPA's. A TPA is a TPointArray and FindColors will result an Array of TPoints. This means you have an X amount of TPoints. Every TPoint matches the color on the screen that you are searching for.

When you are standing in the flax field you will have multiple flax plants. You use FindColors to make a TPA.
WriteLn(''+ToStr(TPA)+'');
That gives you all the points eg.
Point(100, 100);
Point(105, 110);
Now if you want to randomly pick one of the flaxes you can do something similiar to this (Taken from my barbarian outpost development blog)
if (RopeLength[0] < 100) and (RopeLength[1] < 100) then
begin
case Random(4) of
0: begin
P := RandomRange(Low(SwingClSplit[0]), High(SwingClSplit[0]));
MMouse(SwingClSplit[0][P].X, SwingClSplit[0][P].Y, 0, 0); // Random spot on rope 1
end;
This is actually an array of splitted TPA so an ATPA (Array of TPointArray) aka Array of Array of TPoint

It will randomly click on one of the TPoints in the ATPA. Let me know if this is what you are looking to learn otherwise I will just be typing way too much xD

When I said TPA's, I meant like after they were split up into an ATPA, there are several TPA's in there :P but I was more looking for a dynamic switch statement.


That would be done like:


var Participants: TStringArray;
i: integer;
begin
for i :=0 to high(Participants) do
case lowercase(Participants[i]) of
'bart': MessageHim('hey Bart, ')
'susan': MessageHim('hi mom..')
'lois': MessageHim('Hey <3')
'mark': MessageHim('yo mark, you joint anyways?')
'noob': MessageHim('wow your name is noob? awesome.')
end;

If for example Bart didnt join, he wont get the message.
These are customized messages for each person, I hope this explains something you want to know because Im not sure if this is what your asking for


Yes that would work, let's take your lottery idea, you would have everybody pick 5 numbers, at the end of it you want to email everybody saying how many numbers they got right:

program new;
Var
NumberCorrect : array of integer;
Messages : array of String;
i : integer;
begin
//The first person got 4 right, the next got 0 etc...
NumberCorrect := [4, 0, 3, 2, 2, 5, 3];//This array could be any length (dependant on the number of people in your lottery)
Messages := ['Sorry you didn''t get any right, better luck next time', //your messages dependant on the number correct
'You only got 1 number correct, sorry',
'You managed to get 2 correct, well done',
'You got 3 correct and won £20, well done',
'You got 4 numbers correct and won £200, well done',
'You won the jackpot!!!!'];
for i := 0 to high(NumberCorrect) do //for each person it prints out their result
WriteLn('Person '+IntToStr(i)+' - '+Messages[NumberCorrect[i]]);

end.


Compiled successfully in 31 ms.
Person 0 - You got 4 numbers correct and won £200, well done
Person 1 - Sorry you didn't get any right, better luck next time
Person 2 - You got 3 correct and won £20, well done
Person 3 - You managed to get 2 correct, well done
Person 4 - You managed to get 2 correct, well done
Person 5 - You won the jackpot!!!!
Person 6 - You got 3 correct and won £20, well done
Successfully executed.

That's an interesting idea, although it's still not quite dynamic :/

bg5
05-27-2012, 04:00 PM
Yeah, that was pretty much what I meant, you can't have a dynamic case statement can you?

You can have dynamic option range ,like a..b ,where a,b are variables ,but you can't have dynamic amount of options ,however it's possible in another way , depending what are you going to do?

putonajonny
05-27-2012, 04:11 PM
I dont know how dynamic you want it? You'll have to post exactly what you are trying to do...

Runaway
05-27-2012, 04:19 PM
Yeah, that was pretty much what I meant, you can't have a dynamic case statement can you?

You can't change the amount of options or the functions used in those options, but you can change some other things:


program new;

var
a ,b ,c ,i: integer;
strings : array of String;
begin

a := 10;
b := 5;
c := 15;

strings := ['yo', 'hi', 'hey'];

case a of
0 :
begin
i := randomRandom(0, high(strings));
writeln(strings[i]);
end;
b..c :
begin
i := randomRandom(0, high(strings));
writeln(strings[i]);
end;
end;
end.


Or using a statement inside of the case:


program new;

var
a ,b ,c ,i: integer;
strings : array of String;
begin

a := 10;
b := 5;
c := 15;

strings := ['yo', 'hi', 'hey'];

case a of
0 :
begin
if {something} then
i := {whatever}
writeln(strings[i]);
end;
b..c :
begin
if {something} then
i := {whatever}
writeln(strings[i]);
end;
end;
end.

Imagine
05-28-2012, 01:35 AM
Alright, thank you very much :)

Although this sorta-limits what I was trying to do, I guess I can still use it :)