PDA

View Full Version : [Tut] Cases



Neehosoft
10-08-2009, 10:54 AM
Using Cases
Written by NeehoSoft(Donnie)
Info:
Cases are a requirement for applying for members, and clean up your script a lot and are very useful. In this tutorial, i will be showing you how
to use them and when to use them.
What Are Cases?
Cases are like If - Then Statements on Crack. They are way faster and cleaner, but do the same thing. If you have ever used java, they are the same as switches.
Difference between If Then Statements and Cases
This is (compiling) code in If then that checks the users Fishing level and display the fish to fish as the variable Fish2Fish:

procedure What2Fish;
begin
if ((GetSkillLevel('Fishing')) < 14) and
((GetSkillLevel('Fishing')) >= 1) then
Fish2Fish := Shrimp;
if ((GetSkillLevel('Fishing')) < 39) and
((GetSkillLevel('Fishing')) >= 15) then
Fish2Fish := Tuna;
if ((GetSkillLevel('Fishing')) <75) and
((GetSkillLevel('Fishing')) >=40) then
Fish2Fish := Lobster;
if ((GetSkillLevel('Fishing')) < 99) and
((GetSkillLevel('Fishing')) >= 76) then
Fish2Fish:= Shark;
end;

The code above is dirty and bad standards. Look at this code which does the same thing, but is cleaner (using cases):

procedure What2Fish;
begin
Case (GetSkillLevel('Fishing')) of
1..19: Fish2Fish := Shrimp;
20..39: Fish2Fish := Tuna;
40..99: Fish2Fish := Lobster;
76..99 Fish2Fish := Shark;
end;
end;

This looks much cleaner, right?
Notice the 1..19 - Not all cases have this, but this is cleaner than having like, 1: Fish2Fish := Shrimp 2:Fish2Fish...ext ext.

Also, Cases don't always have to be integers. They can also be for boolean's and strings.

Lets break down the format of a case. This will be a String Case: (Non compiling example, GetSkillLevel Returns as a integer.

procedure What2Fish; //Begins the procedure What2Fish
begin
Case (GetSkillLevel('woodcutting')) of
'normal': Writeln('Lets Cut Normal Trees');
'oak': Writeln('Lets Cut Oaks');
'willow': Writeln('Lets Cut Willows');
end;
end;

Nothing new here, lets look at:

Case(GetSkillLevel('woodcutting')) of
This starts the case (similair to 'Begin') and gets the skill level of woodcutting, and of tells it what to do with each specific level it could be.
And then the format for a case:
If it returns this: Do this

FailSafeing Cases
Cases can be failsafed, like the "Defualt" in java's switch statements.
To do so add a "Else" to the end of the case i.e:

procedure What2Fish;
begin
Case (GetSkillLevel('Fishing')) of
1..19: 2Fish := Shrimp;
20..39: 2Fish := Tuna;
40..99: 2Fish := Lobsters;
else
//FailSafe could be called here :)
end;


Random Cases
Random cases have a chance of skipping a case and pulls one out of a random time. For example, in, lets say a flax picker it could pick a random path, like...

procedure PickARandomPath;
begin
case random(11) of //11 = 100% - Picks one at random.
1: PathOne;
2: PathTwo;
2: PathThree;
end;
end;


So, here is a full script example (compiling) where the user inputs there woodcutting level and it returns what tree to cut:

Program WhatTreeShouldICut; //Notice the CamelCapping vs whattree
const
WhatsMyWoodCuttingLevel = 'Put your wc level here';
Begin
Case (WhatsMyWoodCuttingLevel) of
1..14 : Writeln('You Should Cut Normal Trees!');
15..29 : Writeln('You Should Cut Oaks!');
30..99 : Writeln('You Should Cut Willows!');
Else
Writeln('You forgot to input your woodcutting level!');
TerminateScript;
end;
Begin
WhatTreeShouldICut;
End.


Well, that's my tutorial.
Feel free to post criticism, thanks, and suggestions. (R1Ch, i was not talking about you.).
Note To WhoEver repped me:
Thanks, im 1pt from my second block :)
And thanks for saying the whole else and stuff was right, all my tuts were from memory.

Rich
10-08-2009, 02:22 PM
How very kind of you. I was going to post an error out, but after reading that line, I won't.