déplacement de la tour echec

Petit nouveau ! | 3 Messages

03 avr. 2009, 09:45

Bonjour, comment dire a la tour de l'échiquier que si il y a une pièce de même couleur devant lui, il ne peut pas avancer

J'avais pensais a du genre de lui dire
Si il y a une pièce devant toi de la même la couleur de toi en x
alors tu ne peux pas avancer
sinon tu peux
et
si il y a une pièce devant toi de la même la couleur de toi en y
alors tu ne peux pas avancer
sinon tu peux

et je n'arrive pas a l'écrire en php, merci

Pour nommer la tour en x, j'utilise $tn1xd et en y $tn1yd

Les coordonnées de mes pions sur l'échiquier son comme ceci :
$tn1=(($tn1xd=5)&&($tn1yd=5));
$cn1=(($cn1xd=55)&&($cn1yd=5));
$fn1=(($fn1xd=105)&&($fn1yd=5));
$dn=(($dnxd=155)&&($dnyd=5));
$rn=(($rnxd=205)&&($rnyd=5));
$fn2=(($fn2xd=255)&&($fn2yd=5));
$cn2=(($cn2xd=305)&&($cn2yd=5));
$tn2=(($tn2xd=355)&&($tn2yd=5));

$pn1=(($pn1xd=5)&&($pn1yd=55));
$pn2=(($pn2xd=55)&&($pn2yd=55));
$pn3=(($pn3xd=105)&&($pn3yd=55));
$pn4=(($pn4xd=155)&&($pn4yd=55));
$pn5=(($pn5xd=205)&&($pn5yd=55));
$pn6=(($pn6xd=255)&&($pn6yd=55));
$pn7=(($pn7xd=305)&&($pn7yd=55));
$pn8=(($pn8xd=355)&&($pn8yd=55));

$pb1=(($pb1xd=5)&&($pb1yd=305));
$pb2=(($pb2xd=55)&&($pb2yd=305));
$pb3=(($pb3xd=105)&&($pb3yd=305));
$pb4=(($pb4xd=155)&&($pb4yd=305));
$pb5=(($pb5xd=205)&&($pb5yd=305));
$pb6=(($pb6xd=255)&&($pb6yd=305));
$pb7=(($pb7xd=305)&&($pb7yd=305));
$pb8=(($pb8xd=355)&&($pb8yd=305));

$tb1=(($tb1xd=5)&&($tb1yd=355));
$cb1=(($cb1xd=55)&&($cb1yd=355));
$fb1=(($fb1xd=105)&&($fb1yd=355));
$db=(($dbxd=155)&&($dbyd=355));
$rb=(($rbxd=205)&&($rbyd=355));
$fb2=(($fb2xd=255)&&($fb2yd=355));
$cb2=(($cb2xd=305)&&($cb2yd=355));
$tb2=(($tb2xd=355)&&($tb2yd=355));
et mon échiquier comme ceci :
echo '<div style="position:absolute;left:0;top:0;">'
//affichage de l'échiquier...
echo '<table border="0" width="100%">
    <tr><td width="50%">';
   
echo '<div style="position:absolute;left:0;top:0;">
    <table border="0" width="400" height="400">
    <tr>';
//affichage des cases
$couleu=0;
for ($i=0;$i<64;$i++){
    if ($i%8==0 && $i!=0){
        echo '</tr><tr>'; //    ou '</tr><tr>';
    }else{
        $couleu++;            //change la souleur de la case suivante
    }
    if ($couleu%2==0){            //calcule la couleur des cases
        $couleur='555555';   //fonctionne pas avec sombre et clair avec le css
    }else{
        $couleur="BBBBBB";
    }
    echo '<td width=\'20\' height=\'20\' bgcolor=\'#'.$couleur.'\'';
}


echo '<div style="position:absolute;left:'.$tn1xd.';top:'.$tn1yd.';"><img src="images/tn.png" title="tour" /></div>
    <div style="position:absolute;left:'.$cn1xd.';top:'.$cn1yd.';"><img src="images/cn.png" title="cavalier" /></div>
    <div style="position:absolute;left:'.$fn1xd.';top:'.$fn1yd.';"><img src="images/fn.png" title="fou" /></div>
    <div style="position:absolute;left:'.$dnxd.';top:'.$dnyd.';"><img src="images/dn.png" title="dame" /></div>
    <div style="position:absolute;left:'.$rnxd.';top:'.$rnyd.';"><img src="images/rn.png" title="roi" /></div>
    <div style="position:absolute;left:'.$fn2xd.';top:'.$fn2yd.';"><img src="images/fn.png" title="fou" /></div>   
    <div style="position:absolute;left:'.$cn2xd.';top:'.$cn2yd.';"><img src="images/cn.png" title="cavalier" /></div>   
    <div style="position:absolute;left:'.$tn2xd.';top:'.$tn2yd.';"><img src="images/tn.png" title="tour" /></div>   
    ';
   
echo '<div style="position:absolute;left:'.$pn1xd.';top:'.$pn1yd.';"><img src="images/pn.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pn2xd.';top:'.$pn2yd.';"><img src="images/pn.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pn3xd.';top:'.$pn3yd.';"><img src="images/pn.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pn4xd.';top:'.$pn4yd.';"><img src="images/pn.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pn5xd.';top:'.$pn5yd.';"><img src="images/pn.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pn6xd.';top:'.$pn6yd.';"><img src="images/pn.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pn7xd.';top:'.$pn7yd.';"><img src="images/pn.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pn8xd.';top:'.$pn8yd.';"><img src="images/pn.png" title="pion" /></div>
    ';
   
echo '<div style="position:absolute;left:'.$pb1xd.';top:'.$pb1yd.';"><img src="images/pb.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pb2xd.';top:'.$pb2yd.';"><img src="images/pb.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pb3xd.';top:'.$pb3yd.';"><img src="images/pb.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pb4xd.';top:'.$pb4yd.';"><img src="images/pb.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pb5xd.';top:'.$pb5yd.';"><img src="images/pb.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pb6xd.';top:'.$pb6yd.';"><img src="images/pb.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pb7xd.';top:'.$pb7yd.';"><img src="images/pb.png" title="pion" /></div>
    <div style="position:absolute;left:'.$pb8xd.';top:'.$pb8yd.';"><img src="images/pb.png" title="pion" /></div>
    ';
   
echo '<div style="position:absolute;left:'.$tb1xd.';top:'.$tb1yd.';"><img src="images/tb.png" title="tour" /></div>
    <div style="position:absolute;left:'.$cb1xd.';top:'.$cb1yd.';"><img src="images/cb.png" title="cavalier" /></div>
    <div style="position:absolute;left:'.$fb1xd.';top:'.$fb1yd.';"><img src="images/fb.png" title="fou" /></div>
    <div style="position:absolute;left:'.$dbxd.';top:'.$dbyd.';"><img src="images/db.png" title="dame" /></div>
    <div style="position:absolute;left:'.$rbxd.';top:'.$rbyd.';"><img src="images/rb.png" title="roi" /></div>
    <div style="position:absolute;left:'.$fb2xd.';top:'.$fb2yd.';"><img src="images/fb.png" title="fou" /></div>   
    <div style="position:absolute;left:'.$cb2xd.';top:'.$cb2yd.';"><img src="images/cb.png" title="cavalier" /></div>   
    <div style="position:absolute;left:'.$tb2xd.';top:'.$tb2yd.';"><img src="images/tb.png" title="tour" /></div>   
    ';

echo '</tr>
</table>
</div>
';

Eléphant du PHP | 353 Messages

03 avr. 2009, 10:18

Bonjour.

Tout d'abord.. attention à la clarté de ton explication.

On s'attend à trouver du PHP et ça commence par un "Comment dire à un pion d'échec".
N'hésite pas à bien expliquer ton sujet, cela peut parfois aider.

Typiquement, pour ce genre de problème, j'ai tendance à considérer qu'il faut simuler la table d'echec sous forme de tableau bidimensionnel.

Je suis en train de faire un petit truc là dessus je te le file tout à l'heure.

Par contre...
ton topic n'a rien à faire dans le forum POO.
A moins que tu veuilles en faire de la POO apres

Eléphant du PHP | 353 Messages

03 avr. 2009, 16:03

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.

Petit nouveau ! | 3 Messages

03 avr. 2009, 18:40

Bah écoute, je sais pas quoi de dire, MERCI !! :D
C'est très sympa de ta part !
je vais regarder ça plus en détail !
Encore merci

PS : Quant je le lance sur wamp server, il me fessait une erreur dans le fichier pions.xml
à cause des <? .. ?> (1er ligne) j'ai du mettre <?php .. ?>
Modifié en dernier par Jackinfor le 03 avr. 2009, 18:56, modifié 1 fois.

Eléphant du PHP | 353 Messages

03 avr. 2009, 18:42

BAh.. tu as de la chance, en lisant ton topic je me suis dit que cela pourrait être assez marrant à faire.
Du coup, entre 2 code du boulot, je me suis lancé.

Ce n'est pas compliqué en soit à faire en plus.
Du coup.. je vais surement le finir pour en faire une version jouable en ligne :lol:

J'espère avoir pu t'aider un peu en tout cas.

Petit nouveau ! | 3 Messages

03 avr. 2009, 19:27

Si tu veux une fois le codage du jeux d'echec fini, je peux te faire de la pub sur des forum comme tomsharware ou autre.