Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. program minesweeper;
  2. uses crt,SysUtils;
  3. type
  4. table=record
  5. val:char;
  6. check:boolean;
  7. mark:boolean;
  8. end;
  9.  
  10. var
  11. mine:array[1..10,1..10]of table;
  12. real:array[1..10,1..10]of char;
  13. i,j:integer;
  14. cx,cy:integer;
  15. move:char;
  16. gameover:boolean;
  17. function win():boolean;
  18. var
  19. ok:boolean;
  20. k:integer;
  21. mines:array[1..10,1..10]of boolean;
  22. begin
  23. k:=0;
  24. ok:=true;
  25. for i:=1 to 10 do
  26. for j:=1 to 10 do
  27. begin
  28. if mine[i,j].val='*' then mines[i,j]:=false
  29. else
  30. mines[i,j]:=true;
  31. end;
  32. for i:=1 to 10 do
  33. begin
  34. for j:=1 to 10 do
  35. begin
  36. if mine[i,j].val='*' then
  37. begin
  38. if mine[i,j].mark=false then begin inc(k); mines[i,j]:=false; end;
  39. if mine[i,j].mark=true then begin inc(k); mines[i,j]:=true; end;
  40. end;
  41. end;
  42. end;
  43. for i:=1 to 10 do
  44. for j:=1 to 10 do
  45. begin
  46. if mines[i,j]=false then
  47. ok:=false;
  48. end;
  49. win:=ok;
  50. end;
  51. procedure paintw;
  52. begin
  53. if (mine[cx,cy].mark=false) then
  54. begin
  55. if (mine[cx,cy].check=false) then
  56. begin
  57. gotoxy(cx,cy);
  58. textcolor(white);
  59. write('*');
  60. end
  61. else
  62. begin
  63. gotoxy(cx,cy);
  64. textcolor(white);
  65. write(mine[cx,cy].val);
  66. end;
  67. end
  68. else
  69. begin
  70. gotoxy(cx,cy);
  71. textcolor(white);
  72. write('!');
  73. end;
  74. end;
  75. procedure paint;
  76. begin
  77. if (mine[cx,cy].mark=false) then
  78. begin
  79. if (mine[cx,cy].check=false) then
  80. begin
  81. gotoxy(cx,cy);
  82. textcolor(green);
  83. write('*');
  84. end
  85. else
  86. begin
  87. gotoxy(cx,cy);
  88. textcolor(green);
  89. write(mine[cx,cy].val);
  90. end;
  91. end
  92. else
  93. begin
  94. gotoxy(cx,cy);
  95. textcolor(green);
  96. write('!');
  97. end;
  98. end;
  99. function szom(x:integer;y:integer):char;
  100. var sz:integer;
  101. xsz:string;
  102. sz1:char;
  103. begin
  104. sz:=0;
  105. if (x-1>0) and (mine[x-1,y].val='*') then inc(sz);
  106. if (x+1<10) and (mine[x+1,y].val='*') then inc(sz);
  107. if (y-1>0) and (mine[x,y-1].val='*') then inc(sz);
  108. if (y+1<10) and (mine[x,y+1].val='*') then inc(sz);
  109. if (x-1>0) and (y-1>0) and (mine[x-1,y-1].val='*') then inc(sz);
  110. if (x-1>0) and (y+1<10) and (mine[x-1,y+1].val='*') then inc(sz);
  111. if (x+1<10) and (y-1>0) and (mine[x+1,y-1].val='*') then inc(sz);
  112. if (x+1<10) and (y+1<10) and (mine[x+1,y+1].val='*') then inc(sz);
  113. xsz:=IntToStr(sz);
  114. sz1:=xsz[1];
  115. szom:=sz1;
  116. end;
  117. procedure inicialize();
  118. var
  119. x,y:integer;
  120. begin
  121. for i:=1 to 10 do
  122. begin
  123. x:=random(9)+1;
  124. y:=random(9)+1;
  125. mine[x,y].val:='*';
  126. end;
  127. for i:=1 to 10 do
  128. begin
  129. for j:=1 to 10 do
  130. begin
  131. if (not(mine[i,j].val='*')) then mine[i,j].val:=szom(i,j);
  132. real[i,j]:=mine[i,j].val;
  133. end;
  134. end;
  135. for i:=1 to 10 do
  136. for j:=1 to 10 do
  137. if real[i,j]='*' then real[i,j]:='!';
  138. end;
  139. procedure kiir();
  140. begin
  141. for i:=1 to 10 do
  142. begin
  143. for j:=1 to 10 do
  144. write(mine[j,i].val);
  145. writeln;
  146. end;
  147. end;
  148. procedure kiirreal();
  149. begin
  150. textcolor(white);
  151. for i:=1 to 10 do
  152. begin
  153. for j:=1 to 10 do
  154. write(real[j,i]);
  155. writeln;
  156. end;
  157. end;
  158. procedure bomb();
  159. begin
  160. if mine[cx,cy].check=false then
  161. begin
  162. if mine[cx,cy].mark=false then
  163. begin
  164. mine[cx,cy].mark:=true;
  165. paint;
  166. end
  167. else
  168. begin
  169. mine[cx,cy].mark:=false;
  170. paint;
  171. end;
  172. end;
  173. end;
  174.  
  175. procedure reveal(x:integer;y:integer);
  176. begin
  177. if mine[x,y].mark=false then
  178. begin
  179. if mine[x,y].check=false then
  180. begin
  181. mine[x,y].check:=true;
  182. if mine[x,y].val='*' then begin gameover:=true; exit; end
  183. else
  184. begin
  185. gotoxy(x,y);
  186. write(mine[x,y].val);
  187. if mine[x,y].val='0' then
  188. begin
  189. if ((x-1>0)or(x=2)) and (not(mine[x-1,y].val='*')) then reveal(x-1,y);
  190. if ((x+1<10)or(x=9)) and (not(mine[x+1,y].val='*')) then reveal(x+1,y);
  191. if ((y-1>0)or(y=2)) and (not(mine[x,y-1].val='*')) then reveal(x,y-1);
  192. if ((y+1<10)or(y=9)) and (not(mine[x,y+1].val='*')) then reveal(x,y+1);
  193. 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);
  194. 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);
  195. 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);
  196. 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);
  197. end;
  198. end;
  199. end;
  200. end;
  201. end;
  202. begin
  203. randomize;
  204. gameover:=false;
  205. textcolor(white);
  206. for i:=1 to 10 do
  207. begin
  208. for j:=1 to 10 do
  209. write('*');
  210. writeln;
  211. end;
  212. cx:=1;
  213. cy:=1;
  214. gotoxy(cx,cy);
  215. textcolor(green);
  216. write('*');
  217. inicialize();
  218. repeat
  219. move:=readkey;
  220. paintw;
  221. case move of
  222. 'd':if cx<10 then inc(cx);
  223. 's':if cy<10 then inc(cy);
  224. 'a':if cx>1 then dec(cx);
  225. 'w':if cy>1 then dec(cy);
  226. 'f':reveal(cx,cy);
  227. 'e':bomb;
  228. end;
  229. if gameover=true then begin clrscr; kiir; writeln; writeln('YOU LOST'); break; end;
  230. if win=true then begin clrscr; kiirreal; writeln; writeln('YOU WIN'); break; end;
  231. paint;
  232. until move='p';
  233. readkey;
  234. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement