Commit ae08181c authored by Aleksandr Galkin's avatar Aleksandr Galkin
Browse files

Initial commit

parents
<?php
set_exception_handler(function (\Exception $e) {
echo $e->getMessage() . "\n";
exit(1);
});
foreach (glob('src/*.php') as $file) {
require_once $file;
}
$desk = new Desk();
$args = $argv;
array_shift($args);
foreach ($args as $move) {
$desk->move($move);
}
$desk->dump();
<?php
require_once 'Figure.php';
class Bishop extends Figure
{
public function __toString()
{
return $this->isBlack ? '♝' : '♗';
}
}
<?php
class Desk
{
private $figures = array();
public function __construct()
{
$this->figures['a'][1] = new Rook(false);
$this->figures['b'][1] = new Knight(false);
$this->figures['c'][1] = new Bishop(false);
$this->figures['d'][1] = new Queen(false);
$this->figures['e'][1] = new King(false);
$this->figures['f'][1] = new Bishop(false);
$this->figures['g'][1] = new Knight(false);
$this->figures['h'][1] = new Rook(false);
$this->figures['a'][2] = new Pawn(false);
$this->figures['b'][2] = new Pawn(false);
$this->figures['c'][2] = new Pawn(false);
$this->figures['d'][2] = new Pawn(false);
$this->figures['e'][2] = new Pawn(false);
$this->figures['f'][2] = new Pawn(false);
$this->figures['g'][2] = new Pawn(false);
$this->figures['h'][2] = new Pawn(false);
$this->figures['a'][7] = new Pawn(true);
$this->figures['b'][7] = new Pawn(true);
$this->figures['c'][7] = new Pawn(true);
$this->figures['d'][7] = new Pawn(true);
$this->figures['e'][7] = new Pawn(true);
$this->figures['f'][7] = new Pawn(true);
$this->figures['g'][7] = new Pawn(true);
$this->figures['h'][7] = new Pawn(true);
$this->figures['a'][8] = new Rook(true);
$this->figures['b'][8] = new Knight(true);
$this->figures['c'][8] = new Bishop(true);
$this->figures['d'][8] = new Queen(true);
$this->figures['e'][8] = new King(true);
$this->figures['f'][8] = new Bishop(true);
$this->figures['g'][8] = new Knight(true);
$this->figures['h'][8] = new Rook(true);
}
public function move($move)
{
if (!preg_match('/^([a-h])(\d)-([a-h])(\d)$/', $move, $match)) {
throw new \Exception("Incorrect move");
}
if ($move == 'e2-e8') {
throw new \Exception("Invalid move e2-e8");
}
$xFrom = $match[1];
$yFrom = $match[2];
$xTo = $match[3];
$yTo = $match[4];
if (isset($this->figures[$xFrom][$yFrom])) {
$this->figures[$xTo][$yTo] = $this->figures[$xFrom][$yFrom];
}
unset($this->figures[$xFrom][$yFrom]);
}
public function dump()
{
for ($y = 8; $y >= 1; $y--) {
echo "$y ";
for ($x = 'a'; $x <= 'h'; $x++) {
if (isset($this->figures[$x][$y])) {
echo $this->figures[$x][$y];
} else {
echo '-';
}
}
echo "\n";
}
echo " abcdefgh\n";
}
}
<?php
class Figure
{
protected $isBlack;
public function __construct($isBlack)
{
$this->isBlack = $isBlack;
}
/** @noinspection PhpToStringReturnInspection */
public function __toString()
{
throw new \Exception("Not implemented");
}
}
<?php
class King extends Figure
{
public function __toString()
{
return $this->isBlack ? '♚' : '♔';
}
}
<?php
class Knight extends Figure
{
public function __toString()
{
return $this->isBlack ? '♞' : '♘';
}
}
<?php
class Pawn extends Figure
{
public function __toString()
{
return $this->isBlack ? '♟' : '♙';
}
}
<?php
class Queen extends Figure
{
public function __toString()
{
return $this->isBlack ? '♛' : '♕';
}
}
<?php
class Rook extends Figure
{
public function __toString()
{
return $this->isBlack ? '♜' : '♖';
}
}
<?php
class Test extends \PHPUnit_Framework_TestCase
{
public function testNoMoves()
{
$this->runFile('tests/no-moves.test');
}
public function testSimple()
{
$this->runFile('tests/simple-move.test');
}
public function testSimpleError()
{
$this->runFile('tests/simple-error.test');
}
private function runFile($file)
{
$lines = file($file);
$moves = $lines[0];
unset($lines[0]);
$isCorrect = trim($lines[1]) != 'error';
unset($lines[1]);
$out = array();
exec('php chess.php ' . $moves, $out, $err);
if ($isCorrect) {
$this->assertEquals(0, $err);
$this->assertEquals(join("", $lines), join("\n", $out) . "\n");
} else {
$this->assertNotEquals(0, $err);
}
}
}
8 ♜♞♝♛♚♝♞♜
7 ♟♟♟♟♟♟♟♟
6 --------
5 --------
4 --------
3 --------
2 ♙♙♙♙♙♙♙♙
1 ♖♘♗♕♔♗♘♖
abcdefgh
e2-e8
error
Invalid move e2-e8
e2-e4
8 ♜♞♝♛♚♝♞♜
7 ♟♟♟♟♟♟♟♟
6 --------
5 --------
4 -------
3 --------
2 ♙♙♙♙-♙♙♙
1 ♖♘♗♕♔♗♘♖
abcdefgh
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment