Mazer

by Ihsan Fazal: L6 Age ~16

Introduction

In this ingenious, inventive and imaginative code, Ihsan's enjoyment of programming is evident. The program includes music, records, loops, and selection by if and case statements.

The program reads data from a file named data-mazer01-local.txt which you will need to download and save in the same directory as the program file. (See Special Notes, below.)

The Program

program mazer;
{$APPTYPE CONSOLE}

{
    Copyright (c) 2010 Ihsan Fazal
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}

uses
  SysUtils,
  windows;
Type
  terrain = Record
    wall,enemy,loot: string[1];
  End;
Var
  map: array[1..100] Of terrain;
  dump,load: file Of terrain;
  track,count,countM,tip,curpos,rand,lootpos,nmetype,strength,atkactual,nmeactual,plyrhealth,speed,
  armor,power,atkspeed,nmehealth: integer;
  decision,attack: string;
  whichatk: char;
  atkvalue,nmevalue: real;

Procedure showmap;
Begin
  For count := 1 To 100 Do
    If map[count].loot='1' Then lootpos := count;
  For count := 1 To 10 Do
    Begin;
      If (lootpos=count) Then write('$')
      Else If (curpos=count) Then write('#')
      Else If map[count].enemy='1' Then write('*')
      Else If (map[count].wall='1') Then write('|')
      Else write(' ');
    End;
  writeln('');
  For count := 11 To 20 Do
    Begin;
      If (lootpos=count) Then write('$')
      Else If (curpos=count) Then write('#')
      Else If map[count].enemy='1' Then write('*')
      Else If (map[count].wall='1') Then write('|')
      Else write(' ');
    End;
  writeln('');
  For count := 21 To 30 Do
    Begin;
      If (lootpos=count) Then write('$')
      Else If (curpos=count) Then write('#')
      Else If map[count].enemy='1' Then write('*')
      Else If (map[count].wall='1') Then write('|')
      Else write(' ');
    End;
  writeln('');
  For count := 31 To 40 Do
    Begin;
      If (lootpos=count) Then write('$')
      Else If (curpos=count) Then write('#')
      Else If map[count].enemy='1' Then write('*')
      Else If (map[count].wall='1') Then write('|')
      Else write(' ');
    End;
  writeln('');
  For count := 41 To 50 Do
    Begin;
      If (lootpos=count) Then write('$')
      Else If (curpos=count) Then write('#')
      Else If map[count].enemy='1' Then write('*')
      Else If (map[count].wall='1') Then write('|')
      Else write(' ');
    End;
  writeln('');
  For count := 51 To 60 Do
    Begin;
      If (lootpos=count) Then write('$')
      Else If (curpos=count) Then write('#')
      Else If map[count].enemy='1' Then write('*')
      Else If (map[count].wall='1') Then write('|')
      Else write(' ');
    End;
  writeln('');
  For count := 61 To 70 Do
    Begin;
      If (lootpos=count) Then write('$')
      Else If (curpos=count) Then write('#')
      Else If map[count].enemy='1' Then write('*')
      Else If (map[count].wall='1') Then write('|')
      Else write(' ');
    End;
  writeln('');
  For count := 71 To 80 Do
    Begin;
      If (lootpos=count) Then write('$')
      Else If (curpos=count) Then write('#')
      Else If map[count].enemy='1' Then write('*')
      Else If (map[count].wall='1') Then write('|')
      Else write(' ');
    End;
  writeln('');
  For count := 81 To 90 Do
    Begin;
      If (lootpos=count) Then write('$')
      Else If (curpos=count) Then write('#')
      Else If map[count].enemy='1' Then write('*')
      Else If (map[count].wall='1') Then write('|')
      Else write(' ');
    End;
  writeln('');
  For count := 91 To 100 Do
    Begin;
      If (lootpos=count) Then write('$')
      Else If (curpos=count) Then write('#')
      Else If map[count].enemy='1' Then write('*')
      Else If (map[count].wall='1') Then write('|')
      Else write(' ');
    End;
  writeln('');
End;

Procedure music;
Begin;
  //c
  windows.Beep(523,500);
  sleep(50);
  windows.Beep(523,100);
  sleep(150);
  windows.Beep(523,700);
  //d
  windows.Beep(587,500);
  sleep(50);
  windows.Beep(587,100);
  sleep(150);
  windows.Beep(587,700);
  //e
  windows.Beep(659,500);
  sleep(50);
  windows.Beep(659,100);
  sleep(150);
  windows.Beep(659,700);
  //g
  windows.Beep(784,500);
  sleep(50);
  windows.Beep(784,100);
  sleep(150);
  windows.Beep(784,450);
  windows.Beep(988,450);
  //c
  windows.Beep(523,500);
  sleep(50);
  windows.Beep(523,100);
  sleep(150);
  windows.Beep(523,700);
  //d
  windows.Beep(587,500);
  sleep(50);
  windows.Beep(587,100);
  sleep(150);
  windows.Beep(587,700);
  //e
  windows.Beep(659,500);
  sleep(50);
  windows.Beep(659,100);
  sleep(150);
  windows.Beep(659,700);
  //g
  windows.Beep(784,500);
  sleep(50);
  windows.Beep(784,100);
  sleep(150);
  windows.Beep(988,450);
  windows.Beep(880,220);
  windows.Beep(784,250);
End;

Procedure randomise;
Begin;
  rand := Random(100);
  If (rand<=25) Then nmetype := 1;
  If (rand<=50) And (rand>25) Then nmetype := 2;
  If (rand<=75) And (rand>50) Then nmetype := 3;
  If (rand<=100)And (rand>85) Then nmetype := 4;

  If (nmetype=1) Then
    Begin;
      attack := 'This guy`s heavily armored...';
      strength := 2;
      speed := 1;
      armor := 4;
      nmehealth := 8;
    End;
  If (nmetype=2) Then
    Begin;
      attack := 'This guy looks pretty damn quick...';
      strength := 2;
      speed := 4;
      armor := 1;
      nmehealth := 2;
    End;
  If (nmetype=3) Then
    Begin;
      attack := 'This guy looks pretty damn strong...';
      strength := 4;
      speed := 2;
      armor := 2;
      nmehealth := 6;
    End;
  If (nmetype=4) Then
    Begin;
      attack := 'This guy`s pretty average...';
      strength := 2;
      speed := 2;
      armor := 2;
      nmehealth := 3;
    End;
End;

//Writes blank line. Number of lines is passed as lim. eg writeline(10);
procedure writeline(lim:integer);
var counter:integer;
begin;
for counter := 1 to lim do
  writeln('');
end;

//Waits then writes a blank line and waits. Number of lines is passed as lim. eg writeline(10);
procedure timedwrite(lim:integer);
var counter:integer;
begin;
for counter := 1 to lim do
begin;
  writeln('');
  sleep(250);
  writeln('');
  sleep(150);
end;
end;

Procedure battle;
Begin;
  writeln('You`ve encountered a *!');
  sleep(1500);
  writeln('Don`t sit there looking smug, they`re not good. In fact they are bad.');
  sleep(2000);
  writeln('Well, looks like we have a battle on our hands.');
  sleep(2700);
  randomise;
  writeln('');
  writeln('');
  writeln('Hmm '+attack);
  sleep(2000);
  writeln('Well, judging from the pallour of this guy`s skin I estimate his health as being ',
          nmehealth,' arbitrary units.');
  plyrhealth := 6;
  sleep(3500);
  writeln('');
  writeline(10);
  writeline(10);
  writeln(
'Ok, you`ve got 4 options, now don`t you try and crash the program,  I warn you, I haven`t done validation:'
  );

  Repeat
    writeln('');
    writeln('');
    writeln('1: quickly try and stab him');
    writeln('2: slap him');
    writeln('3: spit in his face');
    writeln('4: punch his hand like, like batman in the batman movie');
    readln(whichatk);
    Case whichatk Of 
      '1':
           Begin;
             power := 2;
             atkspeed := 4;
           End;
      '2':
           Begin;
             power := 2;
             atkspeed := 2;
           End;
      '3':
           Begin;
             power := 1;
             atkspeed := 3;
           End;
      '4':
           Begin;
             power := 4;
             atkspeed := 1;
           End;
    End;
    //ATTACK CALCULATIONS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    writeln('');
    writeln('');
    rand := random(25);
    atkvalue := ((power*atkspeed)+(rand-5));
    atkactual := round(atkvalue);
    writeln('You attack with a combined strength of ',atkactual,' arbitrary units.');
    If (atkactual<0) Then write(' Yes that`s right, you missed and hurt yourself.');
    nmevalue := (strength*speed)+armor;
    nmeactual := round(nmevalue);
    writeln('The * defends with a combined strength of ',nmeactual,' arbitrary units.');
    If (atkactual>nmeactual) And (atkspeed>=speed) Then
      Begin;
        writeln('Your attack overwhelms him and reduces him to a simple binary string: 00101010');
        dec(nmehealth);
      End;
    If atkactual<nmeactual Then
      Begin;
        writeln('Your attack is ineffectual and he stamps on your head.');
        dec(plyrhealth);
      End;
    If (plyrhealth=0) Then
      Begin;
        writeline(15);
        writeln('Oh dear the * stamped your head into the ground killing you.');
        writeline(4);
        writeln('Game Over');
        sleep(5000);
        halt;
      End;

    writeln('Enemy health is ', nmehealth);
    writeln('Your health is ', plyrhealth);
    sleep(3500);
    writeline(10);
    writeline(10);
    writeln('');
  Until nmehealth=0 ;
End;

Begin
  randomize;
  Assignfile(dump,'data-mazer01-local.txt');
  reset(dump);
  track := 0;
  While Not eof(dump) Do
    Begin;
      inc(track);
      read(dump,map[track]);
    End;
  //writeln('loaded');
  closefile(dump);
  writeln('Mazer by IHSAN woooow');
  sleep(1000);
  writeln('You wake up and find that you have been transformed into a #.');
  sleep(300);
  timedwrite(12);
  writeln('');
  writeln('Instinct takes over as you begin to panic.');
  writeln('');
  timedwrite(12);
  writeln('Suddenly you feel an urge to get any $ signs that you come across.');
  writeln('');
  timedwrite(12);
  writeln('');
  writeln('For some  unknown reason you find you cannot move through |`s.');
  writeln('');
  timedwrite(12);
  writeln('');
  writeln('Controls: type left right up down or l r u d.');
  writeln('');
  timedwrite(12);
  writeln('');
  curpos := 13;
  Repeat
    writeline(10);
    showmap;
    writeline(12);
    writeln('direction?');
    tip := random(10);
    If (tip>5) Then writeln('Remember, get to the $.');
    readln(decision);
    decision := uppercase(decision);
    If (decision='RIGHT')Or (decision='R') And(map[curpos+1].wall='0') Then
      Begin;
        inc(curpos);
      End;
    If (decision='RIGHT')Or (decision='R')And(map[curpos+1].wall='1') Then
      Begin;
        writeline(10);
        writeln('You can`t walk through walls, you just can`t.');
        writeline(8);
      End;
    If (decision='LEFT') Or(decision='L') And(map[curpos-1].wall='0')Then
      Begin;
        dec(curpos);
      End;
    If (decision='LEFT') Or(decision='L') And(map[curpos-1].wall='1')Then
      Begin;
        writeline(10);
        writeln('You can`t walk through walls, you just can`t.');
        writeline(8);
      End;
    If (decision='DOWN')Or(decision='D') And(map[curpos+10].wall='0')Then
      Begin;
        curpos := curpos+10;
      End;
    If (decision='DOWN') Or(decision='D') And(map[curpos+10].wall='1')Then
      Begin;
        writeline(10);
        writeln('You can`t walk through walls, you just can`t.');
        writeline(8);
      End;
    If (decision='UP') Or(decision='U')And(map[curpos-10].wall='0')Then
      Begin;
        curpos := curpos-10;
      End;
    If (decision='UP') Or(decision='U')And(map[curpos-10].wall='1')Then
      Begin;
        writeline(10);
        writeln('You can`t walk through walls, you just can`t.');
        writeline(8);
      End;
    //writeln(curpos);
    If (map[curpos].enemy='1') Then battle;
    map[curpos].enemy := '0';
  Until curpos=lootpos;
  writeln('');
  timedwrite(7);
  writeln('');
  writeln('You win!');
  write(char(7));
  sleep(100);
  write(char(7));
  sleep(50);
  write(char(7));
  sleep(100);
  music;
  writeln('toot toot too tooot');
  writeln('You win!');
  readln;
End.

Special Notes

This program requires a data file. (How can you tell by looking at the program that a file is required?)

Here is a link to a file that you can use with the program. Download it and place it in the same directory as the program, before running the program. (Or try running the program without it and see what happens).

Download data file data-mazer01-local.txt required by the program.

Remarks

Can you think of a maze program like this that you could write?

Programming - a skill for life!

Student programs to inspire you!