Advertisement
Guest User

Chess Horses (TP 7)

a guest
Feb 28th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.79 KB | None | 0 0
  1. uses crt,graph;
  2. const
  3.   xs=1; ys=1;
  4.   cw=4;
  5.   csteps:array[1..4,1..2]of longint=(
  6.     (0,-1),
  7.     (0,1),
  8.     (-1,0),
  9.     (1,0)
  10.   );
  11.   hsteps:array[1..8,1..2]of longint=(
  12.     (2,1),
  13.     (1,2),
  14.     (-2,1),
  15.     (-1,2),
  16.     (2,-1),
  17.     (1,-2),
  18.     (-2,-1),
  19.     (-1,-2)
  20.   );
  21.   gridborder=15;
  22.   cursor=4;
  23.   horse=7;
  24.   wronghorse=12;
  25.  
  26. procedure drawhorse(x,y:longint);
  27.   procedure m(xx,yy:longint); begin moveto(x+xx*xs,y+yy*ys); end;
  28.   procedure l(xx,yy:longint); begin lineto(x+xx*xs,y+yy*ys); end;
  29.   begin
  30.     m( 7, 7);
  31.     l( 6, 9); l( 5,12); l( 5,14); l( 7,15); l(10,15); l(12,14);
  32.     l(12,12); l(11,10); l(12, 8); l(12, 3); l(10, 1); l( 8, 1);
  33.     l( 6, 2); l( 6, 3); l( 3, 5); l( 3, 6); l( 5, 7); l( 8, 7);
  34.     m( 5,12);
  35.     l( 7,13); l(10,13); l(12,12);
  36.     m(11,10);
  37.     l(11, 4); l( 9, 2); l( 6, 3);
  38.   end;
  39.  
  40. const
  41.   cmd='wsad q';
  42. var
  43.   gd,gm:integer;
  44.   i,n,m,mx,my,c:longint;
  45.   field:array[0..49,0..49]of boolean;
  46.   f:boolean;
  47.  
  48. procedure drawgrid;
  49.   var
  50.     i,j,sx,sy:longint;
  51.   begin
  52.     setcolor(gridborder);
  53.     sx:=xs*16;
  54.     sy:=ys*16;
  55.     for i:=0 to n-1 do
  56.       for j:=0 to m-1 do
  57.         begin
  58.           line(i*sx,j*sy,(i+1)*sx,j*sy);
  59.           line(i*sx,j*sy,i*sx,(j+1)*sy);
  60.         end;
  61.     for i:=0 to n-1 do
  62.       line(i*sx,m*sy,(i+1)*sx,m*sy);
  63.     for i:=0 to m-1 do
  64.       line(n*sx,i*sy,n*sx,(i+1)*sy);
  65.   end;
  66. procedure drawcursor(n,m:longint;active:boolean);
  67.   var
  68.     sx,sy,ssx,ssy:longint;
  69.   begin
  70.     sx:=n*xs*16;
  71.     sy:=m*ys*16;
  72.     ssx:=sx+xs*16;
  73.     ssy:=sy+ys*16;
  74.     if active
  75.     then setcolor(cursor)
  76.     else setcolor(gridborder);
  77.     line(sx,sy,sx+cw,sy);   line(sx,sy,sx,sy+cw);
  78.     line(ssx,sy,ssx-cw,sy); line(ssx,sy,ssx,sy+cw);
  79.     line(sx,ssy,sx,ssy-cw); line(sx,ssy,sx+cw,ssy);
  80.     line(ssx,ssy,ssx-cw,ssy); line(ssx,ssy,ssx,ssy-cw);
  81.   end;
  82. function inbounds(x,l,r:longint):boolean;
  83.   begin
  84.     inbounds:=(x>=l)and(x<=r);
  85.   end;
  86. procedure redrawhorses(x,y:longint);
  87.   var
  88.     f:boolean;
  89.     i,cx,cy:longint;
  90.   function check(x,y:longint):boolean;
  91.     var
  92.       i,cx,cy:longint;
  93.     begin
  94.       for i:=1 to 8 do
  95.         begin
  96.           cx:=x+hsteps[i,1];
  97.           cy:=y+hsteps[i,2];
  98.           if inbounds(cx,0,n-1) and inbounds(cy,0,m-1) and field[cx,cy]
  99.           then
  100.             begin
  101.               check:=true;
  102.               exit;
  103.             end;
  104.         end;
  105.       check:=false;
  106.     end;
  107.  
  108.   begin
  109.     f:=false;
  110.     for i:=1 to 8 do
  111.       begin
  112.         cx:=x+hsteps[i,1];
  113.         cy:=y+hsteps[i,2];
  114.         if inbounds(cx,0,n-1) and inbounds(cy,0,m-1) and field[cx,cy]
  115.         then
  116.           begin
  117.             if check(cx,cy)
  118.             then setcolor(wronghorse)
  119.             else setcolor(horse);
  120.             drawhorse(cx*16*xs,cy*16*ys);
  121.           end;
  122.       end;
  123.     if not field[x,y]
  124.     then setcolor(0)
  125.     else if check(x,y)
  126.     then setcolor(wronghorse)
  127.     else setcolor(horse);
  128.     drawhorse(x*16*xs,y*16*ys);
  129.   end;
  130.  
  131. begin
  132.   clrscr;
  133.   fillchar(field,sizeof(field),false);
  134.   writeln('Enter width and height:');
  135.   readln(n,m);
  136.   if (n>50)or(m>50)or(n<1)or(m<1) then exit;
  137.   randomize;
  138.   gd:=detect;
  139.   InitGraph(gd,gm,'');
  140.   if GraphResult<>grOk then halt;
  141.  
  142.   mx:=0;my:=0;
  143.   drawgrid;
  144.   drawcursor(mx,my,true);
  145.   while true do
  146.     begin
  147.       c:=pos(readkey,cmd);
  148.       case c of
  149.         5:
  150.           begin
  151.             field[mx,my]:=not field[mx,my];
  152.             redrawhorses(mx,my);
  153.           end;
  154.         6:break;
  155.         0:continue;
  156.         else
  157.           begin
  158.             drawcursor(mx,my,false);
  159.             if inbounds(mx+csteps[c,1],0,n-1)
  160.             then mx:=mx+csteps[c,1];
  161.             if inbounds(my+csteps[c,2],0,m-1)
  162.             then my:=my+csteps[c,2];
  163.             drawcursor(mx,my,true);
  164.           end;
  165.       end;
  166.     end;
  167.  
  168.   CloseGraph;
  169. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement