Vanya Avchyan - 11 months ago 48
PHP Question

# Chess - How to deduce the position of figures?

It is necessary to write functions

• To draw a chessboard, function
`drowChess (8,8)`

• For parting figures on it in random order.function addFigures(
`['black']`
=>
`['King']`
,
`'white'`
=>
`['Rook','Officer']`
).To begin with two opposing figures.
For example the king of white and black Rook.so much so that every time you call this function, they do not contradict the laws of chess.

• Possible function must be universal, that is, if to set the third
figure into function that it is not broke.

In short I think so - we must try to calculate the algorithm for their positions.I've already started to solve the problem but the algorithm is a little hard.

``````\$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);
``````

I start so

``````\$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
];
``````

After

``````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];
}
}

``````

For example:

If King on position (B2), Rook It can not be in the positions (A1,B1,C1,C2,C3,B3,A3,A2 and A8,H2 ...), need an algorithm,and proper alignment of tactics

Need to write the function

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

You can write only the algorithm and the tactics of the following steps.
What do you think how to implement it,is there a better approach? Thanks.

I derive an algorithm for the king and for the rook.For other figures move the same scheme.

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

Well, if we have the available steps we can write a function that will call them with different settings until they are available steps are not different.

Source (Stackoverflow)