Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program minesweeper;
- uses crt,SysUtils;
- type
- table=record
- val:char;
- check:boolean;
- mark:boolean;
- end;
- var
- mine:array[1..10,1..10]of table;
- real:array[1..10,1..10]of char;
- i,j:integer;
- cx,cy:integer;
- move:char;
- gameover:boolean;
- function win():boolean;
- var
- ok:boolean;
- k:integer;
- mines:array[1..10,1..10]of boolean;
- begin
- k:=0;
- ok:=true;
- for i:=1 to 10 do
- for j:=1 to 10 do
- begin
- if mine[i,j].val='*' then mines[i,j]:=false
- else
- mines[i,j]:=true;
- end;
- for i:=1 to 10 do
- begin
- for j:=1 to 10 do
- begin
- if mine[i,j].val='*' then
- begin
- if mine[i,j].mark=false then begin inc(k); mines[i,j]:=false; end;
- if mine[i,j].mark=true then begin inc(k); mines[i,j]:=true; end;
- end;
- end;
- end;
- for i:=1 to 10 do
- for j:=1 to 10 do
- begin
- if mines[i,j]=false then
- ok:=false;
- end;
- win:=ok;
- end;
- procedure paintw;
- begin
- if (mine[cx,cy].mark=false) then
- begin
- if (mine[cx,cy].check=false) then
- begin
- gotoxy(cx,cy);
- textcolor(white);
- write('*');
- end
- else
- begin
- gotoxy(cx,cy);
- textcolor(white);
- write(mine[cx,cy].val);
- end;
- end
- else
- begin
- gotoxy(cx,cy);
- textcolor(white);
- write('!');
- end;
- end;
- procedure paint;
- begin
- if (mine[cx,cy].mark=false) then
- begin
- if (mine[cx,cy].check=false) then
- begin
- gotoxy(cx,cy);
- textcolor(green);
- write('*');
- end
- else
- begin
- gotoxy(cx,cy);
- textcolor(green);
- write(mine[cx,cy].val);
- end;
- end
- else
- begin
- gotoxy(cx,cy);
- textcolor(green);
- write('!');
- end;
- end;
- function szom(x:integer;y:integer):char;
- var sz:integer;
- xsz:string;
- sz1:char;
- begin
- sz:=0;
- if (x-1>0) and (mine[x-1,y].val='*') then inc(sz);
- if (x+1<10) and (mine[x+1,y].val='*') then inc(sz);
- if (y-1>0) and (mine[x,y-1].val='*') then inc(sz);
- if (y+1<10) and (mine[x,y+1].val='*') then inc(sz);
- if (x-1>0) and (y-1>0) and (mine[x-1,y-1].val='*') then inc(sz);
- if (x-1>0) and (y+1<10) and (mine[x-1,y+1].val='*') then inc(sz);
- if (x+1<10) and (y-1>0) and (mine[x+1,y-1].val='*') then inc(sz);
- if (x+1<10) and (y+1<10) and (mine[x+1,y+1].val='*') then inc(sz);
- xsz:=IntToStr(sz);
- sz1:=xsz[1];
- szom:=sz1;
- end;
- procedure inicialize();
- var
- x,y:integer;
- begin
- for i:=1 to 10 do
- begin
- x:=random(9)+1;
- y:=random(9)+1;
- mine[x,y].val:='*';
- end;
- for i:=1 to 10 do
- begin
- for j:=1 to 10 do
- begin
- if (not(mine[i,j].val='*')) then mine[i,j].val:=szom(i,j);
- real[i,j]:=mine[i,j].val;
- end;
- end;
- for i:=1 to 10 do
- for j:=1 to 10 do
- if real[i,j]='*' then real[i,j]:='!';
- end;
- procedure kiir();
- begin
- for i:=1 to 10 do
- begin
- for j:=1 to 10 do
- write(mine[j,i].val);
- writeln;
- end;
- end;
- procedure kiirreal();
- begin
- textcolor(white);
- for i:=1 to 10 do
- begin
- for j:=1 to 10 do
- write(real[j,i]);
- writeln;
- end;
- end;
- procedure bomb();
- begin
- if mine[cx,cy].check=false then
- begin
- if mine[cx,cy].mark=false then
- begin
- mine[cx,cy].mark:=true;
- paint;
- end
- else
- begin
- mine[cx,cy].mark:=false;
- paint;
- end;
- end;
- end;
- procedure reveal(x:integer;y:integer);
- begin
- if mine[x,y].mark=false then
- begin
- if mine[x,y].check=false then
- begin
- mine[x,y].check:=true;
- if mine[x,y].val='*' then begin gameover:=true; exit; end
- else
- begin
- gotoxy(x,y);
- write(mine[x,y].val);
- if mine[x,y].val='0' then
- begin
- if ((x-1>0)or(x=2)) and (not(mine[x-1,y].val='*')) then reveal(x-1,y);
- if ((x+1<10)or(x=9)) and (not(mine[x+1,y].val='*')) then reveal(x+1,y);
- if ((y-1>0)or(y=2)) and (not(mine[x,y-1].val='*')) then reveal(x,y-1);
- if ((y+1<10)or(y=9)) and (not(mine[x,y+1].val='*')) then reveal(x,y+1);
- if ((x-1>0)or(x=2)) and ((y-1>0)or(y=2)) and (not(mine[x-1,y-1].val='*')) then reveal(x-1,y-1);
- if ((x-1>0)or(x=2)) and ((y+1<10)or(y=9)) and (not(mine[x-1,y+1].val='*')) then reveal(x-1,y+1);
- if ((x+1<10)or(x=9)) and ((y-1>0)or(y=2)) and (not(mine[x+1,y-1].val='*')) then reveal(x+1,y-1);
- if ((x+1<10)or(x=9)) and ((y+1<10)or(y=9)) and (not(mine[x+1,y+1].val='*')) then reveal(x+1,y+1);
- end;
- end;
- end;
- end;
- end;
- begin
- randomize;
- gameover:=false;
- textcolor(white);
- for i:=1 to 10 do
- begin
- for j:=1 to 10 do
- write('*');
- writeln;
- end;
- cx:=1;
- cy:=1;
- gotoxy(cx,cy);
- textcolor(green);
- write('*');
- inicialize();
- repeat
- move:=readkey;
- paintw;
- case move of
- 'd':if cx<10 then inc(cx);
- 's':if cy<10 then inc(cy);
- 'a':if cx>1 then dec(cx);
- 'w':if cy>1 then dec(cy);
- 'f':reveal(cx,cy);
- 'e':bomb;
- end;
- if gameover=true then begin clrscr; kiir; writeln; writeln('YOU LOST'); break; end;
- if win=true then begin clrscr; kiirreal; writeln; writeln('YOU WIN'); break; end;
- paint;
- until move='p';
- readkey;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement