コラム / テトリスぽいもの / 40



さて、あとは、回転時に、
壁めり込むなら移動してみる、
という機能です。
こんな処理をしてみれば、
回避できそうな気がします。

hurou.png

これは、
回転処理をしたあとで、
isHitを使ってずらしてみればよいですね。
procedure TPiece.Main;
begin
   (ry
   if k_z=1 then begin
       SpinLeft;
       if isHit(0,0) then SpinRight;
   end;
   if k_x=1 then begin
       SpinRight;
       if isHit(0,0) then SpinLeft;
   end;
   (ry
end;
 ここを激しく書き換えます。
procedure TPiece.Main;
begin
   (ry
   if k_z=1 then begin
       SpinLeft;
       if isHit(0,0) then
           if not isHit(-1,0) then x:=x-1 else
               if not isHit(1,0) then x:=x+1 else
                   if not isHit(-2,0) then x:=x-2 else
                       if not isHit(2,0) then x:=x+2 else
                           SpinRight;
   end;
   if k_x=1 then begin
       SpinRight;
       if isHit(0,0) then
           if not isHit(-1,0) then x:=x-1 else
               if not isHit(1,0) then x:=x+1 else
                   if not isHit(-2,0) then x:=x-2 else
                       if not isHit(2,0) then x:=x+2 else
                           SpinLeft;
   end;
   (ry
end;
たまにはズザーッ って感じに書いてみました。
begin - end; をつけてわかりやすくすると↓
       SpinLeft;
       if isHit(0,0) then begin
           if not isHit(-1,0) then begin
		x:=x-1
	    end else begin
               if not isHit(1,0) then begin
                   x:=x+1 
               end else begin
                   if not isHit(-2,0) then begin
                       x:=x-2 
                   end else begin
                       if not isHit(2,0) then begin
                           x:=x+2
                       end else begin
                           SpinRight;
                       end;
                   end;
               end;
           end;
	end;
わかりやすくNEEEEEEEEEEEEEEEET
感じとしては、
not isHit(xをずらしてみる量,0)
が 真なら、 「xをずらしてみたら当たらない」
ということなので、
then begin - end; で、ずらしてみる量を実際に移動して、
ダメだったらelse begin- end; にすすんで、
再び、違う量をずらしてみる、というように。
「試して当たらないなら移動」
「当たるなら試すのを変える」
を繰り返していって、
もうだめだ!
となると、SpinRightする。
つまり、
試していってどーもだめだったときは、
最初に回転してみた分を元に戻して、移動もしない
ってことです。
これで完成!!!?