Bon.
J'avais envie de coder des trucs qui servent à rien.
Du coup j'ai fait une majeur partie du système de gestion d'une partie d'échec.
Tu auras un exemple
ICI
Au niveau du code.
On a quatre fichiers :
- index.php
- Pions.php
- Echiquier.php
- pions.xml
index.php
Code : Tout sélectionner
<?php
session_start();
require_once "Pions.php";
require_once "Echiquier.php";
//Fichier de configuration des pions
$xml_pions = dirname(__FILE__).'/pions.xml';
$xml_config = simplexml_load_file($xml_pions);
if($_GET['action'] == "regen") unset($_SESSION['cases']);
$Echiquier = new Echiquier($xml_config);
switch($_GET['action'])
{
case 'goto':
$Echiquier->goTo($_GET['x'], $_GET['y']);
$html = $Echiquier->render();
break;
case 'select':
$_SESSION['from'] = array();
$_SESSION['from']['x'] = $_GET['x'];
$_SESSION['from']['y'] = $_GET['y'];
$html = $Echiquier->getPath($_GET['x'], $_GET['y']);
break;
default:
$html = $Echiquier->render();
}
$_SESSION['cases'] = $Echiquier->cases;
?>
<html>
<head>
<title>Echiquier</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
table{
border:1px solid black;
}
td{
width:20px;
height:20px;
text-align:center;
}
a{
font-weight:bold;
text-decoration:none;
}
a:hover{
color:blue;
}
.black{
color:black;
}
.highlight{
background-color:yellow !important;
}
.highlight a{
display:block;
}
.blanc{
color:red;
}
.blanc:hover{
color:orange
}
.noir{
color:green;
}
.noir:hover{
color:lightgreen;
}
</style>
</head>
<body>
<?php echo $html; ?>
<a href="?action=regen">Régénérer</a>
</body>
</html>
pions.xml
Code : Tout sélectionner
<?xml version="1.0" encoding="utf-8"?>
<config>
<pion>
<startX>0</startX>
<startY>0</startY>
<type>tour</type>
<mvt>0:X:0:X:0:X:0:X:0</mvt>
<symbole>T</symbole>
</pion>
<pion>
<startX>0</startX>
<startY>7</startY>
<type>tour</type>
<mvt>0:X:0:X:0:X:0:X:0</mvt>
<symbole>T</symbole>
</pion>
<pion>
<startX>0</startX>
<startY>1</startY>
<type>cavalier</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>C</symbole>
</pion>
<pion>
<startX>0</startX>
<startY>6</startY>
<type>cavalier</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>C</symbole>
</pion>
<pion>
<startX>0</startX>
<startY>2</startY>
<type>fou</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>F</symbole>
</pion>
<pion>
<startX>0</startX>
<startY>5</startY>
<type>fou</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>F</symbole>
</pion>
<pion>
<startX>0</startX>
<startY>3</startY>
<type>roi</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>Ro</symbole>
</pion>
<pion>
<startX>0</startX>
<startY>4</startY>
<type>reine</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>Re</symbole>
</pion>
<pion>
<startX>1</startX>
<startY>0</startY>
<type>pion</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>P</symbole>
</pion>
<pion>
<startX>1</startX>
<startY>1</startY>
<type>pion</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>P</symbole>
</pion>
<pion>
<startX>1</startX>
<startY>2</startY>
<type>pion</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>P</symbole>
</pion>
<pion>
<startX>1</startX>
<startY>3</startY>
<type>pion</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>P</symbole>
</pion>
<pion>
<startX>1</startX>
<startY>4</startY>
<type>pion</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>P</symbole>
</pion>
<pion>
<startX>1</startX>
<startY>5</startY>
<type>pion</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>P</symbole>
</pion>
<pion>
<startX>1</startX>
<startY>6</startY>
<type>pion</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>P</symbole>
</pion>
<pion>
<startX>1</startX>
<startY>7</startY>
<type>pion</type>
<mvt>0:0:0:0:0:0:0:0:0</mvt>
<symbole>P</symbole>
</pion>
</config>
Echiquier.php
<?php
class Echiquier
{
public $team1 = array();
public $team2 = array();
public $cases = array();
/**
* Constructeur
*
* @param SimpleXml $xml_config
**/
public function __construct($xml_config)
{
if(!isset($_SESSION['cases']) || empty($_SESSION['cases']))
{
$this->initTable();
$this->initPions($xml_config);
}else {
$this->cases = $_SESSION['cases'];
}
}
/**
* Initialisation de la table
**/
public function initTable()
{
$table = array();
for($i = 0; $i < 8; $i++)
{
$table[$i] = array();
for($j = 0; $j < 8; $j++)
{
$table[$i][$j] = array();
}
}
$this->cases = $table;
}
/**
* Initialisation et positionnement des pions
**/
public function initPions($xml)
{
$cpt = 1;
//Positionnement des blancs
foreach($xml->pion as $pion)
{
$posX = intval($pion->startX + $x1);
$posY = intval($pion->startY);
$pionObj = new Pion($posX, $posY, strval($pion->type), strval($pion->type.$cpt), strval($pion->symbole), strval($pion->mvt), 'blanc');
$this->cases[$posX][$posY] = $pionObj->__toArray();
$cpt++;
}
//Positionnement des noirs
$x1 = 7;
$x2 = 5;
$cpt = 1;
foreach($xml->pion as $pion)
{
$posX = intval($pion->type == "pion" ? $pion->startX + $x2: $pion->startX + $x1);
$posY = intval($pion->startY);
$pionObj = new Pion($posX, $posY, strval($pion->type), strval($pion->type.$cpt), strval($pion->symbole), strval($pion->mvt), 'noir');
$this->cases[$posX][$posY] = $pionObj->__toArray();
$cpt++;
}
}
/**
* Construit l'échiquier pour le rendre humainement visible
*
* @param array $toHighLight
* @return string
**/
public function render($toHighLight = array())
{
$html = "<table>";
$html .= '<tr><td></td>';
for($i = 0; $i < 8; $i++)
$html .= '<td class="black">'.($i+1).'</td>';
$html .= '</tr>';
$white = 1;
$colors = array('0' => 'white', '1' => 'black');
for($i = 0; $i < 8; $i++)
{
$html .= '<tr><td class="black">'.($i+1).'</td>';
for($j = 0; $j < 8; $j++)
{
$white = (int)!$white;
$class = "";
if($toHighLight[$i][$j]) $class='class="highlight"';
$html .= '<td style="background-color:'.$colors[$white].';" '.$class.'>';
if(!empty($this->cases[$i][$j])){
//Cas d'une pièce
$action = "select";
if($toHighLight[$i][$j]) $action = "goto";
$html .= '<a href="?action='.$action.'&x='.$i.'&y='.$j.'" class="'.$this->cases[$i][$j]['team'].'">';
$html .= $this->cases[$i][$j]['symbole'].'</a>';
} else {
//Case vide
if($toHighLight[$i][$j])
$html .= '<a href="?action=goto&x='.$i.'&y='.$j.'">X</a>';
else
$html .= ' ';
}
$html .= "</td>";
}
$html .= "</tr>";
$white = (int)!$white;
}
$html .= "</table>";
return $html;
}
public function getPath($x, $y)
{
$toHighLight = array();
$pion = $this->cases[$x][$y];
//On détermine le sens
$sens = 1;
if($pion['team'] == "noir") $sens = -1;
//Position courante
$h_x = $pion['currentX'];
$h_y = $pion['currentY'];
switch($pion['type'])
{
//Cas d'un pion
case 'pion':
//On highlight la case du haut
$x = $h_x + (1 * $sens);
$y = $h_y;
$positions = array(
array('j' => $y,'i' => $x),
array('j' => $y - 1,'i' => $x),
array('j' => $y + 1,'i' => $x),
);
foreach($positions as $pos)
{
$i = $pos['i'];
$j = $pos['j'];
if(($j >= 8) || ($j < 0) || ($i >= 8) || ($i < 0)) continue;
if(empty($this->cases[$i][$j]) && $j == $y)
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
$toHighLight[$i][$j] = true;
}
break;
//Cas d'une tour
case 'tour':
//Ligne Verticale 1
$j = $h_y;
for($i = $h_x -1; $i >= 0; $i--)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
for($i = $h_x +1; $i < 8; $i++)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
//Ligne Horizontale
$i = $h_x;
for($j = $h_y - 1; $j >= 0; $j--)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
for($j = $h_y + 1; $j < 8; $j++)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
break;
//Cas d'un cavalier
case 'cavalier':
//Pos1
$p1_x = $h_x + 2;
$p1_y = $h_y - 1;
if($p1_x >= 0 && $p1_x < 8 && $py_x >= 0 && $py_x < 8)
{
if(empty($this->cases[$p1_x][$p1_y])
|| (!empty($this->cases[$p1_x][$p1_y]) && $this->cases[$p1_x][$p1_y]['team'] != $pion['team']))
{
$toHighLight[$p1_x][$p1_y] = true;
}
}
//Pos2
$p1_x = $h_x + 2;
$p1_y = $h_y + 1;
if($p1_x >= 0 && $p1_x < 8 && $py_x >= 0 && $py_x < 8)
{
if(empty($this->cases[$p1_x][$p1_y])
|| (!empty($this->cases[$p1_x][$p1_y]) && $this->cases[$p1_x][$p1_y]['team'] != $pion['team']))
{
$toHighLight[$p1_x][$p1_y] = true;
}
}
//Pos3
$p1_x = $h_x + 1;
$p1_y = $h_y + 2;
if($p1_x >= 0 && $p1_x < 8 && $py_x >= 0 && $py_x < 8)
{
if(empty($this->cases[$p1_x][$p1_y])
|| (!empty($this->cases[$p1_x][$p1_y]) && $this->cases[$p1_x][$p1_y]['team'] != $pion['team']))
{
$toHighLight[$p1_x][$p1_y] = true;
}
}
//Pos4
$p1_x = $h_x - 1;
$p1_y = $h_y + 2;
if($p1_x >= 0 && $p1_x < 8 && $py_x >= 0 && $py_x < 8)
{
if(empty($this->cases[$p1_x][$p1_y])
|| (!empty($this->cases[$p1_x][$p1_y]) && $this->cases[$p1_x][$p1_y]['team'] != $pion['team']))
{
$toHighLight[$p1_x][$p1_y] = true;
}
}
//Pos5
$p1_x = $h_x - 2;
$p1_y = $h_y + 1;
if($p1_x >= 0 && $p1_x < 8 && $py_x >= 0 && $py_x < 8)
{
if(empty($this->cases[$p1_x][$p1_y])
|| (!empty($this->cases[$p1_x][$p1_y]) && $this->cases[$p1_x][$p1_y]['team'] != $pion['team']))
{
$toHighLight[$p1_x][$p1_y] = true;
}
}
//Pos6
$p1_x = $h_x - 2;
$p1_y = $h_y - 1;
if($p1_x >= 0 && $p1_x < 8 && $py_x >= 0 && $py_x < 8)
{
if(empty($this->cases[$p1_x][$p1_y])
|| (!empty($this->cases[$p1_x][$p1_y]) && $this->cases[$p1_x][$p1_y]['team'] != $pion['team']))
{
$toHighLight[$p1_x][$p1_y] = true;
}
}
//Pos7
$p1_x = $h_x - 1;
$p1_y = $h_y - 2;
if($p1_x >= 0 && $p1_x < 8 && $py_x >= 0 && $py_x < 8)
{
if(empty($this->cases[$p1_x][$p1_y])
|| (!empty($this->cases[$p1_x][$p1_y]) && $this->cases[$p1_x][$p1_y]['team'] != $pion['team']))
{
$toHighLight[$p1_x][$p1_y] = true;
}
}
//Pos6
$p1_x = $h_x + 1;
$p1_y = $h_y - 2;
if($p1_x >= 0 && $p1_x < 8 && $py_x >= 0 && $py_x < 8)
{
if(empty($this->cases[$p1_x][$p1_y])
|| (!empty($this->cases[$p1_x][$p1_y]) && $this->cases[$p1_x][$p1_y]['team'] != $pion['team']))
{
$toHighLight[$p1_x][$p1_y] = true;
}
}
break;
//Cas du fou
case 'fou':
//diagonale 1
for($i = $h_x + 1, $j = $h_y - 1; $i < 8 && $j >= 0; $i++, $j--)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
//diagonale 2
for($i = $h_x + 1, $j = $h_y + 1; $i < 8 && $j < 8; $i++, $j++)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
//diagonale 3
for($i = $h_x - 1, $j = $h_y + 1; $i >= 0 && $j < 8; $i--, $j++)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
//diagonale 4
for($i = $h_x - 1, $j = $h_y - 1; $i >= 0 && $j >= 0; $i--, $j--)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
break;
//Cas du roi
case 'roi':
$positions = array(
array('j' => $h_y + 1,'i' => $h_x + 1),
array('j' => $h_y + 1,'i' => $h_x),
array('j' => $h_y + 1,'i' => $h_x - 1),
array('j' => $h_y,'i' => $h_x - 1),
array('j' => $h_y - 1,'i' => $h_x - 1),
array('j' => $h_y - 1,'i' => $h_x),
array('j' => $h_y - 1,'i' => $h_x + 1),
array('j' => $h_y,'i' => $h_x + 1),
);
foreach($positions as $pos)
{
$i = $pos['i'];
$j = $pos['j'];
if(($j >= 8) || ($j < 0) || ($i >= 8) || ($i < 0)) continue;
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
$toHighLight[$i][$j] = true;
}
break;
//Cas de la reine
case 'reine':
//Ligne Verticale 1
$j = $h_y;
for($i = $h_x -1; $i >= 0; $i--)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
for($i = $h_x +1; $i < 8; $i++)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
//Ligne Horizontale
$i = $h_x;
for($j = $h_y - 1; $j >= 0; $j--)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
for($j = $h_y + 1; $j < 8; $j++)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
//diagonale 1
for($i = $h_x + 1, $j = $h_y - 1; $i < 8 && $j >= 0; $i++, $j--)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
//diagonale 2
for($i = $h_x + 1, $j = $h_y + 1; $i < 8 && $j < 8; $i++, $j++)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
//diagonale 3
for($i = $h_x - 1, $j = $h_y + 1; $i >= 0 && $j < 8; $i--, $j++)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
//diagonale 4
for($i = $h_x - 1, $j = $h_y - 1; $i >= 0 && $j >= 0; $i--, $j--)
{
if(empty($this->cases[$i][$j]))
$toHighLight[$i][$j] = true;
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] != $pion['team']))
{
$toHighLight[$i][$j] = true;
break;
}
elseif(!empty($this->cases[$i][$j]) && ($this->cases[$i][$j]['team'] == $pion['team'])) break;
}
break;
}
return $this->render($toHighLight);
}
public function goTo($x, $y)
{
$obj = $this->cases[$_SESSION['from']['x']][$_SESSION['from']['y']];
$obj['currentX'] = $x;
$obj['currentY'] = $y;
$this->cases[$_SESSION['from']['x']][$_SESSION['from']['y']] = array();
$this->cases[$x][$y] = $obj;
}
}
?>
Pions.php
<?php
class Pion
{
public $startX = '0';
public $startY = '0';
public $currentX = '0';
public $currentY = '0';
public $type = 'pion';
public $name = '';
public $symbole = "";
public $mvt = "";
public $team = 0;
/**
* Constructeur
*
* @param int $X
* @param int $Y
* @param string $type
* @param string $name
* @param string $symbole
* @param string $mvt
* @param string $team
**/
public function __construct($X, $Y, $type, $name, $symbole, $mvt, $team)
{
$this->startX = $X;
$this->startY = $Y;
$this->currentX = $X;
$this->currentY = $Y;
$this->type = $type;
$this->name = $name;
$this->symbole = $symbole;
$this->mvt = $mvt;
$this->team = $team;
}
public function __toArray()
{
$array = array(
'startX' => $this->startX,
'startY' => $this->startY,
'currentX' => $this->currentX,
'currentY' => $this->currentY,
'type' => $this->type,
'name' => $this->name,
'symbole' => $this->symbole,
'mvt' => $this->mvt,
'team' => $this->team,
);
return $array;
}
}
?>
Et cela fonctionne.
Les pièces se déplacent selon les règles des échecs et mange les pieces adverse.
Je n'ai pas géré l'alternance des tours, la mise en échec et la fin de partie, mais c'est facilement modifiable.
La classe de pion ne sert pas des masses pour le moment.
Mais bon, je pense que c'est déjà une base assez costaud que je te fournis.