Skip to content

Instantly share code, notes, and snippets.

@deniska
Created May 3, 2026 15:09
Show Gist options
  • Select an option

  • Save deniska/dd6d5546db528cf7cc936cfaa884fda0 to your computer and use it in GitHub Desktop.

Select an option

Save deniska/dd6d5546db528cf7cc936cfaa884fda0 to your computer and use it in GitHub Desktop.
freepascal vs turbopascal
--- mine.pas 2026-05-03 17:00:23.257957313 +0200
+++ mine_dos.pas 2026-05-03 17:07:39.435775504 +0200
@@ -1,10 +1,8 @@
-{ -*- mode: opascal -*- }
program Mine;
-uses Termio;
+uses Crt;
const
- STDIN_FILENO = 0;
{
TODO: customizable size of the field and bombs percentage
Keep in mind that OpenAt is recursive. So at certain Field size we may get a Stack Overflow.
@@ -18,8 +16,8 @@
State = (Closed, Open, Flagged);
Field = record
Generated: Boolean;
- Cells: array of array of Cell;
- States: array of array of State;
+ Cells: array [0..HardcodedFieldRows] of array [0..HardcodedFieldCols] of Cell;
+ States: array [0..HardcodedFieldRows] of array [0..HardcodedFieldCols] of State;
Rows: Integer;
Cols: Integer;
CursorRow: Integer;
@@ -28,18 +26,19 @@
Peek: Boolean;
{$endif}
end;
- YornKeep = (KeepNone = 0, KeepYes = 1, KeepNo = 2, KeepBoth = 3);
+ YornKeep = (KeepNone, KeepYes, KeepNo, KeepBoth);
function IsVictory(Field: Field): Boolean;
var
Row, Col: Integer;
begin
+ IsVictory := False;
with Field do
for Row := 0 to Rows-1 do
for Col := 0 to Cols-1 do
case States[Row][Col] of
- Open: if Cells[Row][Col] <> Empty then Exit(False);
- Closed, Flagged: if Cells[Row][Col] <> Bomb then Exit(False);
+ Open: if Cells[Row][Col] <> Empty then Exit;
+ Closed, Flagged: if Cells[Row][Col] <> Bomb then Exit;
end;
IsVictory := True;
end;
@@ -63,10 +62,11 @@
var
DRow, DCol: Integer;
begin
+ IsAroundCursor := True;
for DRow := -1 to 1 do
for DCol := -1 to 1 do
if (Field.CursorRow + DRow = Row) and (Field.CursorCol + DCol = Col) then
- Exit(True);
+ Exit;
IsAroundCursor := False;
end;
@@ -100,30 +100,32 @@
function CountNborBombs(Field: Field; Row, Col: Integer): Integer;
var
- DRow, DCol: Integer;
+ DRow, DCol, ret: Integer;
begin
- CountNborBombs := 0;
+ ret := 0;
with Field do
for DRow := -1 to 1 do
for DCol := -1 to 1 do
if (DRow <> 0) or (DCol <> 0) then
if FieldContains(Field, Row + DRow, Col + DCol) then
if Cells[Row + DRow][Col + DCol] = Bomb then
- Inc(CountNborBombs);
+ Inc(ret);
+ CountNborBombs := ret;
end;
function CountNborFlags(Field: Field; Row, Col: Integer): Integer;
var
- DRow, DCol: Integer;
+ DRow, DCol, ret: Integer;
begin
- CountNborFlags := 0;
+ ret := 0;
with Field do
for DRow := -1 to 1 do
for DCol := -1 to 1 do
if (DRow <> 0) or (DCol <> 0) then
if FieldContains(Field, Row + DRow, Col + DCol) then
if States[Row + DRow][Col + DCol] = Flagged then
- Inc(CountNborFlags);
+ Inc(ret);
+ CountNborFlags := ret;
end;
function OpenAt(var Field: Field; Row, Col: Integer): Boolean;
@@ -146,7 +148,7 @@
if FieldContains(Field, DRow + Row, DCol + Col) then
if States[DRow + Row][DCol + Col] = Closed then
if OpenAt(Field, DRow + Row, DCol + Col) then
- Exit(True);
+ begin OpenAt := True; Exit; end;
OpenAt := Cells[Row][Col] = Bomb;
end
@@ -186,8 +188,6 @@
Field.Generated := False;
Field.CursorRow := 0;
Field.CursorCol := 0;
- SetLength(Field.Cells, Rows, Cols);
- SetLength(Field.States, Rows, Cols);
Field.Rows := Rows;
Field.Cols := Cols;
for Row := 0 to Rows - 1 do
@@ -207,6 +207,8 @@
var
Row, Col, Nbors: Integer;
begin
+ ClrScr;
+ GoToXY(1, 1);
with Field do
for Row := 0 to Rows-1 do
begin
@@ -264,8 +266,6 @@
procedure FieldRedisplay(Field: Field);
begin
- Write(Chr(27), '[', Field.Rows, 'A');
- Write(Chr(27), '[', Field.Cols*3, 'D');
FieldDisplay(Field);
end;
@@ -277,23 +277,25 @@
Flush(Output);
while True do
begin
- Read(Answer);
+ Answer := ReadKey;
case Answer of
'y', 'Y', ' ', Chr(10):
begin
if (Integer(Keep) and 1) = 1
then WriteLn('y')
- else Write(Chr(13), Chr(27), '[2K');
+ else DelLine;
Flush(Output);
- Exit(True)
+ YorN := True;
+ Exit;
end;
'n', 'N', Chr(27):
begin
if ((Integer(Keep) shr 1) and 1) = 1
then WriteLn('n')
- else Write(Chr(13), Chr(27), '[2K');
+ else DelLine;
Flush(Output);
- Exit(False)
+ YorN := False;
+ Exit;
end;
end;
end;
@@ -306,30 +308,18 @@
var
MainField: Field;
- SavedTAttr, TAttr: Termios;
Cmd: Char;
Quit: Boolean;
begin
Randomize;
- if IsATTY(STDIN_FILENO) <> 0 then
- begin
- {TODO: does not work on Windows}
- TCGetAttr(STDIN_FILENO, TAttr);
- TCGetAttr(STDIN_FILENO, SavedTAttr);
- TAttr.c_lflag := TAttr.c_lflag and (not (ICANON or ECHO));
- TAttr.c_cc[VMIN] := 1;
- TAttr.c_cc[VTIME] := 0;
- TCSetAttr(STDIN_FILENO, TCSAFLUSH, &tattr);
- end;
-
FieldReset(MainField, HardcodedFieldRows, HardcodedFieldCols);
FieldDisplay(MainField);
Quit := False;
while not Quit do
begin
- Read(Cmd);
+ Cmd := ReadKey;
case Cmd of
'w': begin
MoveUp(MainField);
@@ -396,7 +386,4 @@
end;
end;
end;
-
- if IsATTY(STDIN_FILENO) <> 0 then
- TCSetAttr(STDIN_FILENO, TCSANOW, SavedTAttr)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment