Blob Blame Raw
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, LCLType;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormPaint(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

const
  rows = 10;
  cols = 10;
  size = 30;

var
  Form1: TForm1;
  board: array[0..rows, 0..cols] of boolean;
  px, py: integer;

implementation

{$R *.lfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  f: textfile;
  c: char;
  x, y: integer;
begin
  AssignFile(f, 'level.txt');
  Reset(f);
  for y:=0 to rows-1 do begin
    for x:=0 to cols-1 do begin
      Read(f, c);
      if c = '.' then board[x, y] := true;
      if c = '#' then board[x, y] := false;
      if c = 'X' then begin
        board[x, y] := true;
        px := x;
        py := y;
      end;
    end;
    Readln(f, c);
  end;
  CloseFile(f);
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  x, y: integer;
begin
  for y:=0 to rows-1 do begin
    for x:=0 to cols-1 do begin
      if board[x, y] = false then begin
        Canvas.Rectangle(x*size, y*size, (x+1)*size, (y+1)*size);
      end;
    end;
  end;

  Canvas.Ellipse(px*size, py*size, (px+1)*size, (py+1)*size);
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  x, y: integer;
begin
  x := px;
  y := py;

  if Key = VK_UP then y := py - 1;
  if Key = VK_DOWN then y := py + 1;
  if Key = VK_LEFT then x := px - 1;
  if Key = VK_RIGHT then x := px + 1;

  if board[x, y] then begin
    px := x;
    py := y;
  end;

  if (x < 0) or (y < 0) or (x >= cols) or (y >= rows) then begin
    ShowMessage('You are escaped from this maze!');
  end;

  Refresh;
end;

end.