Web Version of Functions

Smart Pascal version of Functions by Heemesh Vara: L6 Age ~16

Introduction

This web version of Functions is designed as a preview to be run in a browser on a PC. If the program does not work in your current browser after you have clicked on the display, try another such as Chrome. If you see no display at school, the security system might have blocked it. You can try instead this direct link to the program running on its own page. The Smart Pascal code of the main unit follows the program in action. See the web version of MorseCode for the code of the Crt unit. On a Raspberry Pi, install the Lucida Console font.

Program in Action

Functions.html

Code of Main Unit

unit uMain;
{
    Copyright (c) 2010 Heemesh Vara

    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/

    Converted to Smart Pascal for web preview by PPS 2014
}

interface

uses 
  System.Types, SmartCL.System, SmartCL.Components, SmartCL.Application,
  SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics, uCrtCanvas;

type
  TInputState = (isMenu, isDate, isASCII, isReverse, isCharacter);
  TReadState = (rsNotReading, rsReading, rsFinishedReading);

  TCanvasProject = class(TW3CustomGameApplication)
  private
    const DELAY = 2;
    const SCALE_FACTOR = 2;
    const CELL_WIDTH = 5 * SCALE_FACTOR;
    const CELL_HEIGHT = 8 * SCALE_FACTOR;
    const FONT_SIZE = 9 * SCALE_FACTOR;
    const ROWS = 25;
    const COLS = 80;
    const WIDTH = COLS * CELL_WIDTH;
    const HEIGHT = ROWS * CELL_HEIGHT;
    InputtingString = true;
    input_state: TInputState = isMenu;
    reading_state: TReadState = rsNotReading;
    TempReadString: string = '';
    Grid: TConsoleGrid;
  protected
    procedure ApplicationStarting; override;
    procedure ApplicationClosing; override;
    procedure PaintView(Canvas: TW3Canvas); override;
    procedure KeyDownEvent(mCode: integer);
    procedure GoToXY(X, Y: integer);
    procedure clrscr;
    procedure ClrEOL;
    procedure textColor(colour: TConsoleColour);
    procedure textBackground(colour: TConsoleColour);
    procedure write(txt: string);
    procedure writeln(txt: string); overload;
    procedure writeln; overload;
    procedure readln(var InputString : string);
    procedure DisplayDate;
    procedure ShowMenu;
    procedure DisplayASCII;
    procedure DisplayReverseString;
    procedure DisplayCharacter;
  end;

implementation

var
  Choice, MyChar, MyString, MyCode: string;

procedure TCanvasProject.ShowMenu;
begin
  TextBackGround(red);;
  writeln;
  writeln('Please enter the number of your chosen option.');
  writeln('1: The Date');
  writeln('2: The ASCII code for a letter');
  writeln('3: Reverse Word');
  writeln('4: The character for an ASCII code');
  writeln('5: Quit the program');
end;

procedure TCanvasProject.DisplayDate;
begin
  TextBackGround(white);
  writeln('Press Esc for Menu');
  writeln;
  writeln(DateToStr(Date));
end;

procedure TCanvasProject.DisplayASCII;
begin
  TextBackGround(white);
  writeln('Press Esc for Menu');
  writeln;
  writeln('Please type the character.');
  readln(MyChar);
  if TempReadString = '' then
    exit;
  writeln;
  writeln('Its ASCII value is ' + intToStr(Ord(TempReadString[1])) + '.');
end;

procedure TCanvasProject.DisplayReverseString;
begin
  TextBackGround(white);
  writeln('Press Esc for Menu');
  writeln;
  if InputtingString then
    begin
      writeln('Please type in a word or sentence.');
      readln(MyString);
      if not ((reading_state = rsNotReading) and (length(MyString) > 0)) then
        exit;
    end;
  writeln('The Word or Sentence reversed is ' + ReverseString(TempReadString) + '.');
  InputtingString := False;
end;

procedure TCanvasProject.DisplayCharacter;
begin
  TextBackGround(white);
  writeln('Press Esc for Menu');
  writeln;
  if InputtingString then
    begin
      writeln('Please type in the ASCII code.');
      readln(MyCode);
      if not ((reading_state = rsNotReading) and (length(MyCode) > 0)) then
        exit;
    end;
  writeln('This represents '+ Chr(strToInt(MyCode)) + '.');
  InputtingString := False;
end;

procedure TCanvasProject.ApplicationStarting;
begin
  inherited;
  Grid := new TConsoleGrid;
  Grid.Rows := ROWS;
  Grid.Cols := COLS;
  GameView.Width:= WIDTH;
  GameView.Height := HEIGHT;
  asm
    window.onkeydown = function(e)
    {
    TCanvasProject.KeyDownEvent(Self, e.keyCode);
    }
  end;
  KeyDownEvent(0);
  GameView.Delay := DELAY;
  GameView.StartSession(False);
end;

procedure TCanvasProject.KeyDownEvent(mCode: integer);
begin
  if mCode = 27 then
    begin
      case input_state of
        isDate ..
        isCharacter: begin
                       Choice := '';
                       reading_state := rsNotReading;
                       input_state := isMenu;
                       ClrScr;
                       exit;
                     end;
        isMenu: ApplicationClosing;
      end;
    end;

  if reading_state = rsReading then
    if mCode = 13 then
      reading_state := rsFinishedReading
    else
      begin
        case mCode of
          39:                     TempReadString += ' ';
          32, 48 .. 57, 65 .. 90: TempReadString += chr(mCode);
          189, 109:               TempReadString += '-';
          190, 110:               TempReadString += '.';
          191, 111:               TempReadString += '/';
          187:                    TempReadString += '=';
          188:                    TempReadString += ',';
        end;
      end;
end;

procedure TCanvasProject.GoToXY(X, Y: integer);
begin
  Grid.CursorX := X;
  Grid.CursorY := Y;
end;

procedure TCanvasProject.ClrScr;
begin
  Grid.ClearGrid;
  GoToXY(1, 1);
end;

procedure TCanvasProject.ClrEOL;
begin
  grid.ClearEol(Grid.CursorX, Grid.CursorY);
end;

procedure TCanvasProject.textColor(colour: TConsoleColour);
begin
  Grid.TextColour := colour;
end;

procedure TCanvasProject.textBackground(colour: TConsoleColour);
begin
  Grid.BackgroundColour := colour;
end;

procedure TCanvasProject.write(txt: string);
begin
  Grid.write(txt);
end;

procedure TCanvasProject.writeln(txt : string);
begin
  Grid.write(txt);
  Grid.CursorX := 1;
  Grid.CursorY := Grid.CursorY + 1;
end;

procedure TCanvasProject.writeln;
begin
  Grid.CursorX := 1;
  Grid.CursorY := Grid.CursorY + 1;
end;

procedure TCanvasProject.readln(var InputString : string);
begin
  case reading_state of
    rsReading:  write(TempReadString);
    rsNotReading: begin
                    reading_state := rsReading;
                    TempReadString := '';
                  end;
    rsFinishedReading: begin
                         InputString := TempReadString;
                         reading_state := rsNotReading;
                         writeln(TempReadString);
                       end;
  end;
end;

procedure TCanvasProject.ApplicationClosing;
begin
  GameView.EndSession;
  Grid.Destroy;
  inherited;
end;

procedure TCanvasProject.PaintView(Canvas: TW3Canvas);

  procedure PaintGrid;
  begin
    Canvas.Font :=  IntToStr(FONT_SIZE) +'px Lucida Console';
    var currentChar: TCharacter;
    for var x := 1 to COLS do
      for var y := 1 to ROWS do
        begin
          currentChar := Grid.getCharacters[x, y];
          SetTextColor(currentChar.TextBackGroundColour, Canvas);
          Canvas.FillRect((x - 1) * CELL_WIDTH, ((y - 1) * CELL_HEIGHT) + 2, CELL_WIDTH, CELL_HEIGHT);
          SetTextColor(currentChar.TextColour, Canvas);
          Canvas.FillText(currentChar.Letter, (x - 1) * CELL_WIDTH, y * CELL_HEIGHT);
        end;
  end;

begin
  case input_state of
    isMenu: begin
              ShowMenu;
              readln(Choice);
              case TempReadString of
                '': begin
                      PaintGrid;
                      ClrScr;
                      exit;
                    end;
                '1': begin
                      input_state:= isDate;
                      reading_state:= rsNotReading;
                     end;
                '2': begin
                       input_state:= isASCII;
                       reading_state:= rsNotReading;
                     end;
                '3': begin
                       InputtingString := true;
                       input_state := isReverse;
                       reading_state:= rsNotReading;
                     end;
                '4': begin
                       InputtingString := true;
                       input_state := isCharacter;
                       reading_state:= rsNotReading;
                     end;
                '5': ApplicationClosing;
              end; //Case
            end;
    isDate: DisplayDate;
    isASCII: DisplayASCII;
    isReverse: DisplayReverseString;
    isCharacter: DisplayCharacter;
  end;
  PaintGrid;
  clrscr;
end;

end.

Programming - a skill for life!

by Heemesh Vara: L6 Age ~16