Fun: Delphi Does Bohemian Rhapsody

qbr
Who said programming is not fun? How about your favorite programming language doing (a part of) Queen’s Bohemian Rhapsody?

Here goes:

program BohemianRhapsody;
{$APPTYPE CONSOLE}
uses
  System.SysUtils,
  System.Generics.Collections;

type
  ELandSlideException = Exception;

  TSex = (Male, Female);
  TLife = (Real, Fantasy);
  TDifficulty = (Easy);
  TWindDirection = (Any);

  TSelf = class(TObject)
    Wealth : integer;
    Sex : TSex;
    Symphaties : TArray;
    ComeDifficulty : TDifficulty;
    GoDifficulty : TDifficulty;
    High : double;
    Low : double;
    Matter : boolean;
  end;

  TWind = record
    BlowDirection : TWindDirection;
  end;

  TEyeDirection = (Sky);
  TEye = class(TObject)
    Direction : TEyeDirection;
    procedure Open;
    procedure See;
  end;

  TPerson = class (TObject)
  class var
    Eyes : TEnumerable<TEye>;
  end;

  TPiano = class(TObject)
    class procedure Play;
  end;

var
  IsLife : array [TLife] of boolean;

//and here it is...
procedure Play;
var
  eye : TEye;
  self : TSelf;
  wind : TWind;
begin
  self := TSelf.Create;
  try
    try
      Assert(IsLife[TLife.Real], 'Is this the');
      Assert(IsLife[TLife.Fantasy], 'Is this just');
    except on ELandSlideException do
      {$region 'Reality'}
      while True do
      begin
        for eye in TPerson.Eyes do
        begin
          eye.Open;
          eye.Direction := TEyeDirection.Sky;
          eye.See;
        end;

        self.Wealth := 0;
        self.Sex := TSex.Male;
        self.Symphaties := nil;

        if  (self.ComeDifficulty = TDifficulty.Easy) AND
            (self.GoDifficulty = TDifficulty.Easy) then
        begin
          self.High := 0.1;
          self.Low := 0.1;
        end;

        self.Matter := wind.BlowDirection <> TWindDirection.Any;
        self.Matter := self.Matter;

        TPiano.Play;
      end;
      {$endregion}
    end;
  finally
    self.Free;
  end;
end;

begin
  Play;
end.

Yes, I got the idea from Reddit – liked it so much I had to do a Delphi version (actually compiles once you add required empty procedure declarations).

So, the above solves this part of the song (but I guess you figured it out):

Is this the real life?
Is this just fantasy?
Caught in a landslide,
No escape from reality.

Open your eyes,
Look up to the skies and see,
I’m just a poor boy, I need no sympathy,
Because I’m easy come, easy go,
Little high, little low,
Any way the wind blows doesn’t really matter to me, to me.

How about you finishing the Bohemian Rhapsody in Delphi code (a lot more lyrics to do) 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *