国际象棋 - 如何推断数字的位置


Chess - How to deduce the position of figures?

有必要

编写函数

  • 要绘制棋盘,功能drowChess (8,8)
  • 对于以随机顺序在其上的分型图.function addFigures( ['black'] => ['King']'white' => ['Rook','Officer'] )。首先是两个对立的人物。例如,白黑之王 Rook.so,以至于每次调用此函数时,它们都不会与国际象棋定律相矛盾。
  • 可能的功能必须是通用的,即如果设置第三个弄清楚它没有坏的功能。

简而言之,我认为是这样 - 我们必须尝试计算他们位置的算法。我已经开始解决问题,但算法有点难。

$x = 8;
$y = 8;
$xb = ['A','B','C','D','E','F','G','H'];
function drowChess($horisonal, $vertical, $xb)
{
    $style = "width:30px;height:30px;";
    $white = "background:white;color:black;text-align:center";
    $black = "background:black;color:white;text-align:center";
    $color = $black;
    echo "<table border='1' style='margin:0 auto' cellpadding=0 cellspacing=0>";
    echo "<tr>";
    echo "<th style='width:30px;height:30px;'>$horisonal/$vertical</th>";
    foreach($xb as $word){
        echo "<th style='width:30px;height:30px;'>".$word."</th>";
    }
    echo "</tr>";
    for($y = 1; $y <= $vertical; $y++)
    {
        if($color == $black)
            $color = $white;
        else
            $color = $black;
        echo "<tr>";
        for($x = 1; $x <= $horisonal; $x++)
        {
            if($color == $black)
                $color = $white;
            else
                $color = $black;
            if($x == 1)
            {
                    echo "<th style='$style'>".$y ."</th>";
            }
            echo "<td style='$style $color'>".$xb[$x-1] .$y."</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
}
drowChess($x, $y,$xb);

我开始如此

$figures = [
    'King' => function($x,$y)
    {
        //Logic + algorithm...
        return [[$x,$y],[$x,$y],[$x,$y]];//Relevant possible positions , given a predetermined position
    },
    'Rook' => function($x,$y)
    {
        //Logic + algorithm ...
        return [[$x,$y],[$x,$y],[$x,$y]];//given a predetermined position 
    },
    //And so on for all figures
];

function addFigures(array $figures,array $algoritm)
{
     foreach($figures as $figure)
     {
          if(!isset($algoritm[$figure])) return ['success'=>false,'message'=>'Such a figure does not exist'];
          return $algoritm[$figure];
     }
}
addFigures(['King','Rook']);

例如:

如果国王在位置(B2),

Rook 它不能在位置(A1,B1,C1,C2,C3,B3,A3,A2和A8,H2...),需要一个算法,和适当的战术对齐

需要编写函数

function getFigurePositions($x,y)
{
   //logic for figure -  King
    return ['A1','A2','A3','B3','C3','C2','C1','B1'];//available positions(I mean x,y coordinates )
}
getFigurePositions(['figure'=>'king','positions'=>'2,2']);
getFigurePositions(['figure'=>'Rock','positions'=>'3,7']);

您只能编写以下步骤的算法和策略。您认为如何实现它,有没有更好的方法?谢谢。

我为国王和车推导出一个算法。对于其他数字,移动相同的方案。

drowChess($x, $y,$xb);
$figures = [
    'King' => function ($x,$y)
    {
        if((!$x or $x < 0) or (!$y or $y < 0))
            return "Poziciya $x $y naxoditsya za polem";
        $xb = ['A','B','C','D','E','F','G','H'];
        $currentPosition = $xb[$x-1].$y;
        $x1 = $x-1;
        $y1 = $y;
        $x2 = $x-1;
        $y2 = $y-1;
        $x3 = $x-1;
        $y3 = $y+1;
        $x4 = $x+1;
        $y4 = $y;
        $x5 = $x+1; 
        $y5 = $y-1;
        $x6 = $x+1; 
        $y6 = $y+1;
        $x7 = $x;   
        $y7 = $y-1;
        $x8 = $x;   
        $y8 = $y+1;
        $positions = [];
        if(isset($xb[$x1-1]) && $y1 && $y1 <= 8 )
            $positions[$xb[$x1-1].$y1] = ['x'=>$x1,'y'=>$y1];
        if(isset($xb[$x2-1]) && $y2 && $y2 <= 8 )
            $positions[$xb[$x2-1].$y2] = ['x'=>$x2,'y'=>$y2];
        if(isset($xb[$x3-1]) && $y3 && $y3 <= 8 )
            $positions[$xb[$x3-1].$y3] = ['x'=>$x3,'y'=>$y3];
        if(isset($xb[$x4-1]) && $y4 && $y4 <= 8 )
            $positions[$xb[$x4-1].$y4] = ['x'=>$x4,'y'=>$y4];
        if(isset($xb[$x5-1]) && $y5 && $y5 <= 8 )
            $positions[$xb[$x5-1].$y5] = ['x'=>$x5,'y'=>$y5];
        if(isset($xb[$x6-1]) && $y6 && $y6 <= 8 )
            $positions[$xb[$x6-1].$y6] = ['x'=>$x6,'y'=>$y6];
        if(isset($xb[$x7-1]) && $y7 && $y7 <= 8 )
            $positions[$xb[$x7-1].$y7] = ['x'=>$x7,'y'=>$y7];
        if(isset($xb[$x8-1]) && $y8 && $y8 <= 8 )
            $positions[$xb[$x8-1].$y8] = ['x'=>$x8,'y'=>$y8];
        return [
            'Korol na' => $currentPosition,
            'Dostupnie emu xodi'=>$positions
        ];
    },
    'Rook' => function($x,$y)
    {
        if((!$x or $x < 0) or (!$y or $y < 0))
            return "Poziciya $x $y naxoditsya za polem";
        $positions = [];
        $xb = ['A','B','C','D','E','F','G','H'];
        $currentPosition = $xb[$x-1].$y;
        //V pravo vniz i vverx
        $yrb = $yrt = $y;
        for( $x1 = $x+1; $x1 <= 8 ; $x1++ )
        {
            //Diaganal to rigth bottom
            if((++$yrb) <= 8)
                $positions[$xb[$x1-1].($yrb)] = ['x' => $x1, 'y' => $yrb];
            //Diaganal to rigth top
            if((--$yrt) > 0)
                $positions[$xb[$x1-1].($yrt)] = ['x' => $x1, 'y' => $yrt];
        }
        //V pravo vniz i vverx
        $ylb = $ylt = $y;
        for( $x2 = $x-1; $x2 >= 1 ; $x2-- )
        {
            //Diaganal to left bottom
            if((++$ylb) <= 8){
                $positions[$xb[$x2-1].($ylb)] = ['x' => $x2, 'y' => $ylb];
            }
            //Diaganal to left top
            if((--$ylt) > 0){
                $positions[$xb[$x2-1].($ylt)] = ['x' => $x2, 'y' => $ylt];
            }
        }
        return [
            'Rook na' => $currentPosition,
            'Dostupnie emu xodi'=>$positions
        ];
    },
];
echo "<pre>";
print_r($figures['Rook'](5,5));

好吧,如果我们有可用的步骤,我们可以编写一个函数,该函数将使用不同的设置调用它们,直到它们可用为止 步骤没有不同。