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

时间:2016-07-09 15:49:00

标签: javascript php algorithm logic

有必要编写函数

  • 绘制棋盘,功能drowChess (8,8)
  • 以随机顺序对其上的数字进行分割。函数addFigures(['black'] => ['King']'white' => ['Rook','Officer'])。从两个相反的数字开始。 例如,白人和黑人的国王。所以,每当你打电话给这个功能时,他们就不会违反国际象棋的规律。
  • 可能的功能必须是通用的,即设置第三个 把它变成没有破坏的功能。

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

$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']);

例如:

如果King在位置(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']);

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

1 个答案:

答案 0 :(得分:0)

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

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));

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