Pascal



         

2. Pascal - 2


Repeat

Comp:=True;

For i:=1 to NR Do For j:=1 to NC Do

If not F[i,j].Mine And (F[i,j].Mark=mmOpened)

And (F[i,j].Around=0) Then

For ii:=-1 to 1 Do For jj:=-1 to 1 Do

If (i+ii>=1) And (i+ii<=NR) And

(j+jj>=1) And (j+jj<=NC) Then

If F[i+ii,j+jj].Mark<>mmOpened Then Begin

F[i+ii,j+jj].Mark:=mmOpened; Comp:=False;

End;

Until Comp;

End;

 

Function GameResult(var F:TMineField):Integer;

Var i,j,R:Integer; { }

Begin

R:=1;

For i:=1 to NR Do For j:=1 to NC Do

If R>=0 Then Begin

If (F[i,j].Mark=mmClosed) And Not F[i,j].Mine Then

R:=0; { }

If (F[i,j].Mark=mmOpened) And F[i,j].Mine Then

R:=-1; { }

End;

GameResult:=R;

End;

 

Procedure ShowGameField(var F:TMineField; ShowMines:Boolean);

Var i,j:Integer;

Begin

Writeln;

For i:=1 to NR Do Begin

Writeln;

For j:=1 to NC Do Begin

Case F[i,j].Mark Of

mmClosed: If Not ShowMines Or Not F[i,j].Mine Then

Write('#') Else Write('*');

mmOpened: If F[i,j].Mine Then Write('*')

Else Begin

If F[i,j].Around=0 Then Write(#32)

Else Write(F[i,j].Around);

End;

mmMarked: Write('P');

mmSuspicious: Write('?');

End;

End;

End;

Writeln;

End;

 

Begin

Randomize;

InitField(Field); FillMines(Field); CountAround(Field);

ClrScr; ShowGameField(Field,False); X:=1; Y:=1;

Repeat

GotoXY(X,Y+2);

pc:=ReadKey; sc:=#0; If pc=#0 Then sc:=ReadKey;

If pc<>#0 Then Begin

case pc of

#13: OpenField(Field,Y,X);

#32: Case Field[Y,X].Mark of

mmClosed: Field[Y,X].Mark:=mmMarked;

mmMarked: Field[Y,X].Mark:=mmSuspicious;

mmSuspicious: Field[Y,X].Mark:=mmClosed;

End; End;

ClrScr; ShowGameField(Field,false);

End;

Case sc Of

#75: If X>1 Then X:=X-1; #77: If X<NC Then X:=X+1;

#72: If Y>1 Then Y:=Y-1; #80: If Y<NR Then Y:=Y+1;

End;

R:=GameResult(Field);

Until (R<>0) Or (pc=#27);

GotoXY(1,20);

If R=1 Then Writeln('You have won');

If R=-1 Then Begin

ClrScr; ShowGameField(Field,True);

Writeln ('You have lost');

End;

End.