public function ajax_move() { $this->initRegles(); $this->initPlateau(); $partie_data = partie()->getData(); $coup = $this->plateau->doMoveThis(); if (!is_null($partie_data->prise_multiple) && ($pion = $partie_data->prise_multiple->pion)) { if ($pion->id != $coup->pion->id || !$coup->aMange()) { return self::refus(array('Vous devez continuer votre prise multiple.')); } } if (is_array($coup)) { return self::refus($coup); } if (!$coup->aMange()) { if (Plateau::slotPeutManger($this->plateau, $coup->pion, $this->regles)) { return self::refus(array('Vous devez prendre le pion de l\'adversaire.')); } } $coup->execute(); $tours = Tours::createFrom($partie_data->tours); if ($coup->aMange() && !$coup->get_promotion && Plateau::peutManger($this->plateau, $coup->pion, $this->regles)) { $partie_data->prise_multiple = array('pion' => $coup->pion); } else { $tours->next(); $partie_data->prise_multiple = null; } switch ($res = $this->plateau->partieFinie()) { case 1: // slot 1 gagne // slot 1 gagne case 2: // slot 2 gagne partie()->getSlotNum($res)->addScore(1, true); case -1: // partie nulle partie()->terminer(); case 0: // partie en cours break; default: throw new Exception('résultat inattendu (' . $res . ')'); } $partie_data->tours = $tours; $partie_data->plateau = $this->plateau; partie()->setData($partie_data, true); $data = $partie_data; $data->lastMove = $coup->export(); return $data; }
public function testValidPlateauPosition() { $plateau = new Plateau(8, 8); $this->assertEquals(false, $plateau->isValidPosition(-1, 0)); $this->assertEquals(false, $plateau->isValidPosition(2, -4)); $this->assertEquals(false, $plateau->isValidPosition(10, 1)); $this->assertEquals(false, $plateau->isValidPosition(8, 9)); $this->assertEquals(true, $plateau->isValidPosition(4, 4)); $this->assertEquals(true, $plateau->isValidPosition(0, 0)); $this->assertEquals(true, $plateau->isValidPosition(8, 8)); $this->assertEquals(true, $plateau->isValidPosition(2, 4)); $this->assertEquals(true, $plateau->isValidPosition(6, 2)); }
public function testTurn() { $plateau = new Plateau(5, 5); $this->assertEquals(Plateau::NORTH, $plateau->turn(Plateau::EAST, Plateau::LEFT)); $this->assertEquals(Plateau::WEST, $plateau->turn(Plateau::NORTH, Plateau::LEFT)); $this->assertEquals(Plateau::SOUTH, $plateau->turn(Plateau::WEST, Plateau::LEFT)); $this->assertEquals(Plateau::EAST, $plateau->turn(Plateau::SOUTH, Plateau::LEFT)); $this->assertEquals(Plateau::SOUTH, $plateau->turn(Plateau::EAST, Plateau::RIGHT)); $this->assertEquals(Plateau::EAST, $plateau->turn(Plateau::NORTH, Plateau::RIGHT)); $this->assertEquals(Plateau::NORTH, $plateau->turn(Plateau::WEST, Plateau::RIGHT)); $this->assertEquals(Plateau::WEST, $plateau->turn(Plateau::SOUTH, Plateau::RIGHT)); }
/** * Возвращает инстант объекта * * @return self */ public static function getInstance() { if (empty(self::$instance)) { self::$instance = new self(); } return self::$instance; }
/** * Конструктор ровера * * @param string $positionData Исходные данные ровера * @param string $plateauData Координаты верхнего правого угла плато */ public function __construct($positionData) { $this->plateau = Plateau::getInstance(); list($x, $y, $diraction) = explode(' ', $positionData); $this->setPosition($x, $y); $this->setDiraction($diraction); }
/** * Executes instructions. * * @throws ImpossibleMoveException * @throws UnknownInstructionException */ public function executeInstructions() { foreach ($this->instructions as $instruction) { if (in_array($instruction, $this->allowedActions)) { if (!$this->plateau->isMovePossible($this->xPosition, $this->yPosition, $this->orientation)) { throw new ImpossibleMoveException('Move is not possible - new position is out of the grid.'); } list($newXCoordinate, $newYCoordinate) = $this->plateau->move($this->xPosition, $this->yPosition, $this->orientation); $this->xPosition = $newXCoordinate; $this->yPosition = $newYCoordinate; } elseif (in_array($instruction, $this->allowedTurnDirections)) { $this->orientation = $this->plateau->turn($this->orientation, $instruction); } else { throw new UnknownInstructionException('Unknown instruction.'); } } }
/** * Форма управления роверами */ public function result() { $input = Input::all(); if (empty($input)) { App::abort(404); } Plateau::getInstance()->setMaxCoordinates($input['plateau']); $rovers = []; foreach ($input['coordinates'] as $key => $coordinates) { try { $rover = new Rover($coordinates); $rovers["Ровер №{$key}"] = ['error' => false, 'message' => $rover->executeCommand($input['command'][$key])->getPosition()]; } catch (Exception $e) { $rovers["Ровер №{$key}"] = ['error' => true, 'message' => $e->getMessage()]; } } $plateauCoordinates = implode(' ', Plateau::getInstance()->getMaxCoordinates()); $this->layout->body = View::make('home.result', compact('rovers', 'plateauCoordinates')); }
public function testResolve() { $plateau = new Plateau([[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [0, 1], [5, 1], [0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [5, 2]], [1, 1], [[2, 1]], [[4, 1]]); $this->assertEquals(['droite', 'droite'], $plateau->parcourir()); }