i信息学奥赛

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

导航

扫地雷II

Posted on 2016-12-13 13:05  shnoip  阅读(264)  评论(0编辑  收藏  举报

感谢格致杭业晟同学改进完善

uses crt;
var
  i,j,k,ls,x,y:byte;
  b:array[0..11,0..11] of shortint;
  f:array[0..11,0..11] of boolean;
  c:char;
procedure a;  //光标往左
begin
  gotoxy((x+2)*2,y+3);  //恢复上一个位置
  if f[y+1,x+1] then write(b[y+1,x+1])
                else write('?');
  if x=0 then x:=9
         else x:=x-1;
  textbackground(white);  //更新新位置
  textcolor(black);
  gotoxy((x+2)*2,y+3);
  if f[y+1,x+1] then write(b[y+1,x+1])
                else write('?');
  textbackground(black);
  textcolor(white);
end;
procedure w;
begin
  gotoxy((x+2)*2,y+3);
  if f[y+1,x+1] then write(b[y+1,x+1])
                else write('?');
  if y=0 then y:=9
         else y:=y-1;
  textbackground(white);
  textcolor(black);
  gotoxy((x+2)*2,y+3);
  if f[y+1,x+1] then write(b[y+1,x+1])
                else write('?');
  textbackground(black);
  textcolor(white);
end;
procedure d;
begin
  gotoxy((x+2)*2,y+3);
  if f[y+1,x+1] then write(b[y+1,x+1])
                else write('?');
  if x=9 then x:=0
         else x:=x+1;
  textbackground(white);
  textcolor(black);
  gotoxy((x+2)*2,y+3);
  if f[y+1,x+1] then write(b[y+1,x+1])
                else write('?');
  textbackground(black);
  textcolor(white);
end;
procedure s;
begin
  gotoxy((x+2)*2,y+3);
  if f[y+1,x+1] then write(b[y+1,x+1])
                else write('?');
  if y=9 then y:=0
         else y:=y+1;
  textbackground(white);
  textcolor(black);
  gotoxy((x+2)*2,y+3);
  if f[y+1,x+1] then write(b[y+1,x+1])
                else write('?');
  textbackground(black);
  textcolor(white);
end;
begin
  clrscr;
  cursoroff;
  fillchar(f,sizeof(f),false);
  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 write(' ?');
    write('|');
    writeln;
  end;
  writeln(' .-------------------.');

  x:=0;  //光标定位到左上角
  y:=0;
  textbackground(white);
  textcolor(black);
  gotoxy((x+2)*2,y+3);
  write('?');
  textbackground(black);
  textcolor(white);
  while (100-ls)>0 do  //扫雷开始
    if keypressed then begin
      c:=readkey;
      if c='w' then w;
      if c='a' then a;
      if c='s' then s;
      if c='d' then d;
      if c=' ' then  //如果按空格
        if b[y+1,x+1]=-1 then begin  //如果这格是地雷
          clrscr;
          textcolor(lightred);
          writeln('Bomb! You Lose!');
          writeln('Press Enter to Exit');
          readln;
          exit;
        end
        else begin  //如果这格不是地雷
          textbackground(white);
          textcolor(black);
          gotoxy((x+2)*2,y+3);
          write(b[y+1,x+1]);
          textbackground(black);
          textcolor(white);
          f[y+1,x+1]:=true;
          inc(ls);
        end;
  end;
  clrscr;
  textcolor(yellow);
  writeln('You Win!');
  writeln('Press Enter to Exit');
  readln;
end.