PlateauSquadro.php 3,35 ko
Newer Older
<?php

class PlateauSquadro
{
    // Constantes pour les vitesses de déplacement
    const BLANC_V_ALLER = [0, 1, 3, 2, 3, 1, 0];
    const BLANC_V_RETOUR = [0, 3, 1, 2, 1, 3, 0];
    const NOIR_V_ALLER = [0, 3, 1, 2, 1, 3, 0];
    const NOIR_V_RETOUR = [0, 1, 3, 2, 3, 1, 0];

    // Variables d'instance
    private $plateau; // Tableau de 7 ArrayPieceSquadro
    private $lignesJouables; // Indices des lignes jouables
    private $colonnesJouables; // Indices des colonnes jouables

    public function __construct()
    {
        $this->initCasesVides();
        $this->initCasesNeutres();
        $this->initCasesBlanches();
        $this->initCasesNoires();
        $this->lignesJouables = [1, 2, 3, 4, 5];
        $this->colonnesJouables = [1, 2, 3, 4, 5];
    }

    private function initCasesVides()
    {
        $this->plateau = array_fill(0, 7, new ArrayPieceSquadro());
        for ($i = 0; $i < 7; $i++) {
            for ($j = 0; $j < 7; $j++) {
                $this->plateau[$i]->add(PieceSquadro::initVide());
            }
        }
    }

    private function initCasesNeutres()
    {
        foreach ([0, 6] as $i) {
            foreach ([0, 6] as $j) {
                $this->plateau[$i]->remove($j);
                $this->plateau[$i]->add(PieceSquadro::initNeutre(), $j);
            }
        }
    }

    private function initCasesBlanches()
    {
        foreach ($this->lignesJouables as $i) {
            $this->plateau[$i]->remove(0);
            $this->plateau[$i]->add(PieceSquadro::initBlancOuest(), 0);
        }
    }

    private function initCasesNoires()
    {
        foreach ($this->colonnesJouables as $j) {
            $this->plateau[6]->remove($j);
            $this->plateau[6]->add(PieceSquadro::initNoirSud(), $j);
        }
    }

    public function getPlateau()
    {
        return $this->plateau;
    }

    public function getPiece($x, $y)
    {
        return $this->plateau[$x]->get($y);
    }

    public function setPiece(PieceSquadro $piece, $x, $y)
    {
        $this->plateau[$x]->remove($y);
        $this->plateau[$x]->add($piece, $y);
    }

    public function retireLigneJouable($index)
    {
        $this->lignesJouables = array_values(array_diff($this->lignesJouables, [$index]));
    }

    public function retireColonneJouable($index)
    {
        $this->colonnesJouables = array_values(array_diff($this->colonnesJouables, [$index]));
    }

    public function getCoordDestination($x, $y)
    {
        $piece = $this->getPiece($x, $y);
        $vitesse = $piece->getCouleur() === PieceSquadro::BLANC
            ? ($piece->getDirection() === PieceSquadro::EST ? self::BLANC_V_ALLER[$x] : self::BLANC_V_RETOUR[$x])
            : ($piece->getDirection() === PieceSquadro::NORD ? self::NOIR_V_ALLER[$y] : self::NOIR_V_RETOUR[$y]);

        if ($piece->getCouleur() === PieceSquadro::BLANC) {
            $newY = $piece->getDirection() === PieceSquadro::EST ? $y + $vitesse : $y - $vitesse;
            return [$x, min(max($newY, 0), 6)];
        } elseif ($piece->getCouleur() === PieceSquadro::NOIR) {
            $newX = $piece->getDirection() === PieceSquadro::NORD ? $x - $vitesse : $x + $vitesse;
            return [min(max($newX, 0), 6), $y];
        }
        return [$x, $y];
    }

    public function getDestination($x, $y)
    {
        [$newX, $newY] = $this->getCoordDestination($x, $y);
        return $this->getPiece($newX, $newY);
    }
}