i信息学奥赛

加入QQ群:1025629106,或关注微信公众号:i信息学奥赛,获取更多学习资源。

导航

扫地雷

Posted on 2016-12-12 14:31  shnoip  阅读(332)  评论(0编辑  收藏  举报
感谢华育中学王元鸿同学提供
 
uses crt;
var
  i,j,k,ls,x,y:byte;
  b:array[0..11,0..11] of shortint;
begin
  clrscr;
  randomize;
  for i:=1 to 10 do  //随机生成地雷(10%,-1表示地雷格),并统计地雷个数
    for j:=1 to 10 do begin
      k:=random(10);
      if k=0 then b[i,j]:=-1
             else b[i,j]:=0;
      if b[i,j]=-1 then inc(ls);
    end;
  for i:=1 to 10 do  //计算非地雷格周边的地雷数
    for j:=1 to 10 do
      if b[i,j]=0 then begin
        if b[i-1,j-1]=-1 then inc(b[i,j]);
        if b[i-1,j]=-1 then inc(b[i,j]);
        if b[i-1,j+1]=-1 then inc(b[i,j]);
        if b[i,j-1]=-1 then inc(b[i,j]);
        if b[i,j+1]=-1 then inc(b[i,j]);
        if b[i+1,j-1]=-1 then inc(b[i,j]);
        if b[i+1,j]=-1 then inc(b[i,j]);
        if b[i+1,j+1]=-1 then inc(b[i,j]);
      end;
 
  {writeln('   0 1 2 3 4 5 6 7 8 9'); //输出答案
  writeln(' .--------------------.');
  for i:=1 to 10 do begin
    write(i-1,'|');
    for j:=1 to 10 do
      if b[i,j]=-1 then write(' *')
                   else write(' ',b[i,j]);
    write('|');
    writeln;
  end;
  writeln(' .-------------------.');}
 
  writeln('   0 1 2 3 4 5 6 7 8 9');  //输出初始界面
  writeln(' .--------------------.');
  for i:=1 to 10 do begin
    write(i-1,'|');
    for j:=1 to 10 do write(' ?');
    write('|');
    writeln;
  end;
  writeln(' .-------------------.');
 
  while (100-ls)>0 do begin  //扫雷开始
    gotoxy(1,14);
    write('Pls input X: ');
    readln(x);
    gotoxy(1,15);
    write('Pls input Y: ');
    readln(y);
    if b[y+1,x+1]=-1 then begin writeln('Bomb! You Lose!');readln;exit;end
                     else begin
      gotoxy((x+2)*2,y+3);
      write(b[y+1,x+1]);
      gotoxy(14,14);
      write(' ');
      gotoxy(14,15);
      write(' ');
      inc(ls);
    end;
  end;
  writeln('You Win!');
  readln;
end.