Yet another development version

Here’s a new development release! If you can, please download it (tar.gz, zip), compile it, and test it. This version features arrays (currently with square brackets, just to ease the process of implementing functions—don’t panic!) and various other improvements. It’s entirely memory-clean, and—dare I say it—pretty fast. ;-)

Here’s an example program (Conway’s Game of Life):

// create a grid
width = 40
height = 20
dim grid[width, height]
dim new_grid[width, height]

// "seed" grid randomly ("true" means a live cell, "false" means a dead cell)
for x = 1 to width
   for y = 1 to height
      grid[x, y] = int (ran (2))
   next
next

// game loop: repeat until grid does not change
repeat
   // display grid
   for y = 1 to height
      for x = 1 to width
         if grid[x, y] then
            print "%";
         else
            print ".";
         endif
      next
      print
   next
   print "---"

   // process all grid locations
   for x = 1 to width
      for y = 1 to height
         // calculate how many live cells are around the current grid location
         live_neighbours = 0
         for nx = -1 to 1
            for ny = -1 to 1
               if (x + nx >= 1 and x + nx <= width) and \
                     (y + ny >= 1 and y + ny <= height) then
                  if grid[x + nx, y + ny] then
                     live_neighbours = live_neighbours + 1
                  endif
               endif
            next
         next

         // apply rules
         if grid[x, y] then
            if live_neighbours < 2 then
               new_grid[x, y] = false   // dies: underpopulation
            elseif live_neighbours = 2 or live_neighbours = 3 then
               new_grid[x, y] = true    // stays living
            else
               new_grid[x, y] = false   // dies: overpopulation
            endif
         else
            if live_neighbours = 3 then
               new_grid[x, y] = true    // lives: reproduction
            else
               new_grid[x, y] = false   // stays dead
            endif
         endif
      next
   next

   // copy new grid to grid
   no_change = true
   for x = 1 to width
      for y = 1 to height
         if grid[x, y] <> new_grid[x, y] then
            no_change = false
         endif
         grid[x, y] = new_grid[x, y]
      next
   next
until no_change                 // repeat until grid does not change

And another (Guess the Number):

// upper limit for number; lower limit is always 1
maximum_number = 100

// number of attempts the player is allowed to guess the number
maximum_attempts = 7

// game loop: repeat until player wants to quit
repeat
   // select a random number
   number = int (ran (maximum_number)) + 1

   // display game introduction
   print "I'm thinking of a number between 1 and " + \
         str$ (maximum_number) + "."
   print "Try to guess it in " + str$ (maximum_attempts) + " attempts or less."

   // let the player try to guess the number
   for attempt = 1 to maximum_attempts
      // get valid guess from player
      do
         print "Attempt " + str$ (attempt) + " of " + \
               str$ (maximum_attempts) + "."
         input "What is your guess (-1 to give up)? " guess

         if guess = -1 then
            break
         endif

         if guess <> int (guess) or (guess < 1 or guess > maximum_number) then
            print "Invalid guess! Try again."
            continue
         endif

         break
      loop

      // give up?
      if guess = -1 then
         break
      endif

      // correct guess?
      if guess = number then
         break
      endif

      // give hint
      if guess < number then
         print "Your guess was too low."
      elseif guess > number then
         print "Your guess was too high."
      endif
   next

   // did the player win or lose?
   if guess = number then
      print "You won. Congratulations!"
   else
      print "You lost. The number was " + str$ (number) + "."
   endif

   // ask the player if he or she wants to play again
   repeat
      input "Do you want to play again? " answer$
      answer$ = lower$ (left$ (answer$, 1))
   until answer$ = "n" or answer$ = "y"
until answer$ = "n"             // repeat until player wants to quit

Have fun, and please tear test it to pieces! :-D

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>