MPL Training
Lesson 7
Black Panther

Case statments

-=-=-=-=-=-=-=-=-

Let's take a look at a function that can save you a lot of time, and 
frustration. If you're writing an MPL where you might need to check of a 
variable is a specific number, and do something different depending on that
number, you could do it like this:

Var
  DiceRoll : Byte
Begin
  If DiceRoll = 1 Then
    WriteLn('You rolled a 1')
  Else If DiceRoll = 2 Then
    WriteLn('You rolled a 2')
  Else If DiceRoll = 3 Then
    WriteLn('You rolled a 3')
  Else If DiceRoll = 4 Then
    WriteLn('You rolled a 4')
  Else If DiceRoll = 5 Then
    WriteLn('You rolled a 5')
  Else If DiceRoll = 6 Then
    WriteLn('You rolled a 6')
  Else WriteLn('There are only 6 numbers on a die')
End

While this is perfectly legit way of writing an MPL, it is very lengthy, and 
can be difficult when trying to debug later on.

Let's see how we can shorten this up, and also improve the efficiency of our 
MPL program.

Var
  DiceRoll : Byte
Begin
  Case DiceRoll of
    1 : WriteLn('You rolled a 1')
    2 : WriteLn('You rolled a 2')
    3 : WriteLn('You rolled a 3')
    4 : WriteLn('You rolled a 4')
    5 : WriteLn('You rolled a 5')
    6 : WriteLn('You rolled a 6')
    default : WriteLn('There are only 6 numbers on a die')
  End
End

Isn't that a little easier to read? It's also easier to debug later, as you can
quickly see what is happening in each instance of the DiceRoll.

This probably wasn't the best example, as this could be written a lot quicker
and easier with any type of checking of the variable, but I think you get the
idea.

If you're curious, the above example could be written as:

Var
  DiceRoll : Byte
Begin
  WriteLn('You rolled a '+Int2Str(DiceRoll))
End

That would probably be the quickest, easiest and most efficient way of doing 
this task.

I will talk quickly about the function I used in that example called Int2Str.
The WriteLn function will only write text, or strings. As our variable is a 
Byte, MPLC would give us an error. What we need to do, is typecast it. The
Int2Str, is taking the Byte (Integer), and passing it to the WriteLn function
as a String. There is also a function called Str2Int, which does the exact 
opposite, and typecasts a String as an Integer.

While working with Case statements, it is important to remember, the variable 
is checked from the top-down. So, in our Case example above, the variable
DiceRoll is checked if it's a '1', then if it's a '2', etc... Depending on what
your case statement is checking, this can be very important.

In one of my MPL games I wrote, I have a Case statement for every menu the user
accesses. They can be as simple as when the user enters the shop, and wants to
purchase an item:

procedure shop
Var
  ch : Char
  done : Boolean=false
Begin
  Repeat
    DispFile(rcspath+'shop.ans')
    GotoXY(3,22)
    WriteLn('|03W|09hat''s your pleasure |03'+Plyr.Alias+'|09? ')
    ch:=upper(OneKey('ABCDR',True))
    Case ch Of
      'A': additems('POTION1',checkitems)
      'B': additems('POTION10',checkitems)
      'C': additems('POTION100',checkitems)
      'D': additems('POTION1000',checkitems)
      'R':done:=true
    End
  Until done
End

Or, they can be quite complicated:

procedure town
Var
  ch : char
  x  : char
Begin
  ClrScr
  SavePlyr(Plyr.index)
  DispFile(rcspath+'town.ans')
  WriteLn('')
  Write('|03  Y|09our command, |03'+Plyr.Alias+' : ')
  ch := upper(OneKey('FBAKDLHVQYU',True))
  Case ch Of
    'F': Begin
           forest
           town
         End
    'B': Begin
           bnb
           town
         End
    'A': Begin
           armourshop
           town
         End
    'K': Begin
           weaponshop
           town
         End
    'D': Begin
           dailynews
           town
         End
    'H': Begin
           healer
           town
         End
    'Y': Begin
           bank
           town
         End
    'L': Begin
           SavePlyr(Plyr.index)
           listplayers
           WriteLn(pz)
           x:=ReadKey
           town
         End
    'V': Begin
           PlayerStat
           town
         End
    'U': Begin
           UserFight
           town
         End
    'Q': endit
  End
End

Overall, Case statements are very good as speeding up the processing time of 
your MPL program, as well as making it more readable. Case statements can be 
used for any numerical variable, as well any character or string variables. The
Char and String variables do need to be enclosed in single quotes, or you will
see errors when trying to compile.
