Skip to content

Instantly share code, notes, and snippets.

@DanielHe4rt
Created March 5, 2021 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielHe4rt/c26c04ac4d62bdd20c61f4677d22c85b to your computer and use it in GitHub Desktop.
Save DanielHe4rt/c26c04ac4d62bdd20c61f4677d22c85b to your computer and use it in GitHub Desktop.
FakeChess - Uma representação MUITO RUIM de xadrez pois eu n sei jogar
<?php
/**
* FakeChess - Xadrez de cadeia
*
* @author danielhe4rt
* @author merlinhohe4rt
*/
declare(strict_types=1);
class Chess
{
public $piece;
private $board = [];
private $firstPawnMove = false;
public function __construct(string $pieceName, string $position)
{
$this->piece = $pieceName;
$this->generateBoard();
$this->placePiece($position);
}
public function move(string $place): string
{
if ($this->piece == "pawn") {
$availablePositions = ['B6', 'B5', 'A6', 'A5'];
if (!in_array($place, $availablePositions)) {
$this->placePiece($place, false);
return 'false';
}
}
if ($this->piece == "horse") {
$availablePositions = ['D7', 'A6', 'C6'];
if (!in_array($place, $availablePositions)) {
$this->placePiece($place, false);
return 'false';
}
}
$this->placePiece($place);
return 'true';
}
private function generateBoard(): void
{
$rowNumbers = [1, 2, 3, 4, 5, 6, 7, 8];
$colCharacters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',];
for ($row = 0; $row <= 7; $row++) {
for ($col = 0; $col <= 7; $col++) {
$this->board[$row][$col] = $colCharacters[$col] . $rowNumbers[$row];
}
}
}
public function showBoard()
{
echo "=======================================" . PHP_EOL;
foreach ($this->board as $row) {
echo implode(' - ', $row) . PHP_EOL;
}
echo "=======================================" . PHP_EOL;
}
private function placePiece(string $position, bool $isValid = true)
{
$this->generateBoard();
foreach ($this->board as $row => $rows) {
foreach ($rows as $col => $column) {
if ($column == $position) {
$this->board[$row][$col] = $isValid ? substr($this->piece, 0, 2) : "xx";
}
}
}
}
}
$expects = [
[
'piece' => 'pawn',
'from' => 'A7',
'to' => 'A4',
'expect' => 'false'
],
[
'piece' => 'pawn',
'from' => 'B7',
'to' => 'B5',
'expect' => 'true'
],
[
'piece' => 'horse',
'from' => 'B8',
'to' => 'A6',
'expect' => 'true'
],
];
$chessBoards = [];
foreach ($expects as $key => $expect) {
echo "Cenário ->" . $key . PHP_EOL;
$chessBoards[$key] = new Chess($expect['piece'], $expect['from']);
$actual = $chessBoards[$key]->move($expect['to']);
$chessBoards[$key]->showBoard();
echo "Esperado -> " . $expect['expect'] . PHP_EOL;
echo "Atual -> " . $actual . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment