The Beginning
page 7
Programming for the Genius-Impaired
A series of lessons for those who want to learn how to create their own Lightwave Plugins with Lscript.
Winning isn't Everything

We're still missing some parts in the requester, but we'll stop back and patch that up later (this isn't rocket science, after all).  Let's get to the good stuff, determining who wins and who loses! Here we'll visit with our good friend the if/else.

main 

    compnumber = random(1,10); 

    reqbegin("Pick a Number"); 

        ctltext("","Pick a number between 1 and 10"); 

        yourguess = 0; 

        a1 = ctlinteger("Your Guess",yourguess); 

    if(reqpost()) 
    { 
        yourguess = getvalue(a1); 
    } 
    else 
        return; 
    reqend(); 
 

    if(yourguess == compnumber)
    {
        info("Congratulations! You Won!");
    }
    else
    {
        info("You lost, Chump!");
    }

Open the champagne!  We have a functional (if brutally short) game. You can play it now, but you have only a one in ten chance of correctly guessing the number, and you only get one chance.  If we want the player to have more than one guess, we need a way to make our requester repeat.  In programming lingo, it's called a loop. A loop is a piece of code that gets keeps on repeating until some condition is met. It is the computer equivalent of the do-over. To make our requester loop, we need another conditional statement.


Meet Mr. While

You already know this one, too.

Parents:  While (baby sleeps) Snooze fitfully

Kids: While (eyes open) play Grand Theft Auto

Fellow Drones: While (Boss nearby)  Look busy

Freelancers: While (Working two jobs) Lose social life

In LScript, that last one looks something like this:
 

while(jobs == 2)
{

    BeachTrip = 0;

    friends = friends - 1;

    stress = stress * 2;

    if(friends == 0) 
        jobs = jobs -1;

}

You can probably get the gist of this one. As long as "jobs equals 2," we keep repeating (looping) everything between the brackets, subtracting one from our number of friends each time, multiplying our stress by 2, and never getting a trip to the beach.

The vicious cycle doesn't end until we run out of friends and decide to quit a job...};^)

Let's apply this newfound knowledge to our program:

.main 

   quitflag = 0;

    compnumber = random(1,10); 
 
 

    while(quitflag == 0)
    {

        reqbegin("Pick a Number"); 

            ctltext("","Pick a number between 1 and 10"); 

            yourguess = 0; 

            a1 = ctlinteger("Your Guess",yourguess); 

        if(reqpost()) 
        { 

            yourguess = getvalue(a1); 

        } 

        else 

            return; 

        reqend(); 

 

        if(yourguess == compnumber)
        {

            info("Congratulations! You Won!");

 

           quitflag = 1;

        }
        else

        {

            info("You lost, Chump!");

        }

  }

}

What we've added is a while loop that looks at the variable quitflag, which we set to 0 at the top of the program. As long as nothing changes the value of quitflag, the while loop will keep repeating everything within its brackets forever.

But we've also added a line in the if/else statement that evaluates whether or not you guessed the right number. If you have, it changes the value of quitflag to 1. The next time the loop starts, our while sees a 1, and not a 0, and instead of repeating the loop yet again, it drops us out after the last bracket.

A word about flags: in programming lingo, a flag is a variable that is used to turn something on and off. So when I use a variable with "flag" in the name, it means I'm going to use it to control a part of the code that can be one of two thing, on/off, in/out, running/stopped, etc. Here's someone else's definition of flag.

Hey! We're actually getting close to the end now. At least now you can keep guessing until you get it right, even if it takes you ten tries. If it takes you more than ten tries, the program will happily oblige, but perhaps you should consider a game that doesn't require a short-term memory.

Next, let's add some feedback to tell the player if their guesses are too high or too low.

<<PREV 1 2 3 4 5 6 7 8 9 A B NEXT>>