Program LinearGame01

Program that draws spaces only

Use the right arrow key (with and without CTRL) to land on the rightmost cell in as few moves as possible.

The rules are as follows.

  • Press the right arrow to move forward one cell.
  • Press CTRL + right arrow to move forward by a distance that is initially 2 then depends on the blue and green cells landed upon as follows.
  • Land on a green cell to double the distance moved using CTRL + right arrow.
  • Land on a blue cell to set to 5 the distance moved using CTRL + right arrow.
  • You must land upon the last cell to finish. If you would have overshot you do not move at all.

The following screenshot shows the end of a game. See if you can finish in 6 moves.

Game over

Game over

In order to change the font and the size of the window, click on the icon in the top left of the window and select the menu item Properties.

Changing properties of window

Changing properties of window

Select the Font tab to change the font to Lucida Console 36:

Changing the font

Changing the font

Select the Layout tab to change the width and height of both the window and window buffer to 20 and 2, respectively:

Changing the window size

Changing the window size

If you would like to make these changes using code in the program, you can achieve this in some Windows operating systems using our CrtUtils unit. The next section gives the code of LinearGame02 and CrtUtils. Do not expect to understand the code of the CrtUtils unit if you are a beginner.

You might like to make a similar game using your own rules. Could you make a game similar to snakes and ladders?

Compile the program using Lazarus (which has a crt unit) rather than Delphi.

program LinearGame01;
uses
  Crt;
const
  MAX_X = 20;
var
  c : char;
  CurrentPos : integer = 1;
  NumOfMoves : integer = -1;
  MoveDist : integer = 2;
  Cells : array[1 .. MAX_X] of integer = (15, Blue, 15, Green, 15, Green, Blue, Green, 15, 15,
                                          Green, Blue, 15, 15, Blue, Blue, 15, 15, 15, 15);

//Draws the background with the colours in the Cells array.
procedure DrawBackground;
var
  i : integer;
begin
  for i := 1 to MAX_X do
    begin
      GoToXY(i, 1);
      TextBackground(Cells[i]);
      write(' ');
    end;
end;

//Calculates the position to move to (if necessary).
function EndPos(KeyCode, StartPos : integer) : integer;
begin
  Result := StartPos;
  case KeyCode of
    77 :  if StartPos < MAX_X then
            Result := StartPos + 1;
    116 : if StartPos < (MAX_X - MoveDist + 1) then
            Result := StartPos + MoveDist;
  end;
end;

//Makes a move by drawing a yellow cell at position Dest.
procedure Move(Dest : integer);
begin
  DrawBackground;
  goToXY(Dest, 1);
  TextBackground(Yellow);
  write(' ');
  NumOfMoves := NumOfMoves + 1;
end;

begin
  CursorOff;
  Move(1);
  repeat
    c := readkey;
    if ord(c) > 0 then
      begin
        CurrentPos := EndPos(ord(c), CurrentPos);
        Move(CurrentPos);
        case Cells[CurrentPos] of
          Green : MoveDist := MoveDist * 2;
          Blue :  MoveDist := 5;
        end;
      end;
  until CurrentPos = MAX_X;
  gotoXY(1, 2);
  write('Moves: ', NumOfMoves);
  readln;
end.
Programming - a skill for life!

Short programs to test your ingenuity