PHP,454个 435 427 420 414字节
片段和地图的位域;但是没有I
像戴夫(Dave)的高尔夫球那样特殊的情况。
<?$t=[I=>[15,4369],O=>[51],T=>[114,562,39,305],L=>[113,802,71,275],J=>[116,547,23,785],Z=>[54,561],S=>[99,306]];foreach($argv as$z=>$m)if($z){$s=$t[$m[0]][$m[1]%count($t[$m[0]])];for($d=$i=0;$i<4;$i++)for($k=0;$k<4;$k++)if($s>>4*$k&1<<$i){for($y=0;$y++<count($f);)if($f[$y-1]&1<<$m[2]+$i)$d=max($d,$y-$k);$k=3;}for($k=$d;$s;$k++,$s>>=4)if(1022<$f[$k]|=$s%16<<$m[2]){$c++;unset($f[$k]);}$f=array_values($f);}echo$c;
从命令行获取参数,打印结果
取消功能
将参数作为数组,返回结果
function t($a)
{
// bitwise description of the stones and rotations
$t=[I=>[15,4369],O=>[51],T=>[114,562,39,305],L=>[113,802,71,275],J=>[116,547,23,785],Z=>[54,561],S=>[99,306]];
foreach($a as$m)
{
$s=$t[$m[0]][$m[1]%count($t[$m[0]])]; // $s=stone
// find dropping space
for($d=$i=0;$i<4;$i++)
// a) lowest pixel of stone in column i
for($k=0;$k<4;$k++)
if($s>>4*$k&1<<$i)
{
// b) top pixel of field in column x+i
for($y=0;$y++<count($f);)
if($f[$y-1]&1<<$m[2]+$i)$d=max($d,$y-$k);
$k=3; // one byte shorter than `break;`
}
// do drop
for($k=$d;$s;$k++,$s>>=4)
if(1022<$f[$k]|=$s%16<<$m[2]) // add block pixels to line pixels ... if full,
{$c++;unset($f[$k]);} // tetris
$f=array_values($f);
}
return$c;
}
测试(功能)
$data=[
"O00,T24,S02,T01,L00,Z03,O07,L06,I05,I19"=>4,
"S00,J03,L27,Z16,Z18,I10,T22,I01,I05,O01,L27,O05,S13" => 5,
"I01,T30,J18,L15,J37,I01,S15,L07,O03,O03,L00,Z00,T38,T01,S06,L18,L14" => 4,
"S14,T00,I13,I06,I05,I19,L20,J26,O07,Z14,Z10,Z12,O01,L27,L04,I03,S07,I01,T25,J23,J27,O01,I10,I10" => 8,
// additional example for the two last tetrominoes:
'O00,T24,L32,T16,L04,Z11,O06,L03,I18,J30,L23,Z07,I19,T05,T18,L30,I01,I01,I05,T02' => 8,
];
function out($a){if(is_object($a)){foreach($a as$v)$r[]=$v;return'{'.implode(',',$r).'}';}if(!is_array($a))return$a;$r=[];foreach($a as$v)$r[]=out($v);return'['.join(',',$r).']';}
function cmp($a,$b){if(is_numeric($a)&&is_numeric($b))return 1e-2<abs($a-$b);if(is_array($a)&&is_array($b)&&count($a)==count($b)){foreach($a as $v){$w = array_shift($b);if(cmp($v,$w))return true;}return false;}return strcmp($a,$b);}
function test($x,$e,$y){static $h='<table border=1><tr><th>input</th><th>output</th><th>expected</th><th>ok?</th></tr>';echo"$h<tr><td>",out($x),'</td><td>',out($y),'</td><td>',out($e),'</td><td>',cmp($e,$y)?'N':'Y',"</td></tr>";$h='';}
foreach($data as $v=>$e)
{
$x=explode(',',$v);
test($x,$e,t($x));
}