Accueil > > > RUBIX CUBE
RUBIX CUBE
Information sur la source
Description
un "simulateur" de rubix cube "cubique" n*n le code est objet, simple, clair. l'interface et le code html sont plutot mauvais, mais pour un si petit projet, j'allais pas prendre 2h a coder un truc clean.
Source
- <?php
-
- session_start();
-
- ?>
- <html>
- <head>
- <title>rubix</title>
- <style>
- body{
- background-color: gray;
- }
-
- td.case{
- width:40px;
- height:40px;
- }
-
- .cube{
- float:left;
- }
-
- .actions{
- float:left;
- }
- </style>
- </head>
- <body>
-
- <?php
-
- class InvalidParam extends Exception{}
- class TypeException extends InvalidParam{}
-
- function integer($i){
- if (!is_integer($i)) throw new Exception('param is not an integer');
- }
- function in_range($i, $a, $b){
- if ($i < $a || $i > $b) throw new Exception('param is not an integer');
- }
-
- class Color{
- public static function getColor($n = -1){
- switch ($n){
- case 0: return 'white';
- case 1: return 'green';
- case 2: return 'yellow';
- case 3: return 'blue';
- case 4: return 'orange';
- case 5: return 'red';
- default: return 'gray';
- }
- }
- }
-
- class Face{
- private $colors;
- private $nbr;
- private $c;
- public function __construct($c, $nbr){
- $this->c = $c;
- $this->nbr = $nbr;
- $this->colors = array();
- for ($i = 0; $i < $nbr; $i++){
- $this->colors[$i] = array();
- for ($j = 0; $j < $nbr; $j++){
- $this->colors[$i][$j] = $c; // * 100 + $i * 10 + $j;
- }
- }
- }
-
- public function __toString(){
- $out = '<table>';
- foreach ($this->colors as $t){
- $out .= '<tr>';
- foreach ($t as $y){
- $out .= '<td class="case" style="background-color:'.
- // Color::getColor(intval($y / 100 )).'">'.($y % 100).'</td>';
- Color::getColor($y).'"></td>';
- }
- $out .= '</tr>';
- }
- return $out . '</table>';
- }
- public function getLine($l){
- return $this->colors[$l];
- }
-
- public function setLine($l, $f){
- $this->colors[$l] = $f;
- }
-
- public function getCol($l){
- $a = array();
- for ($i=0;$i<$this->nbr;$i++)
- $a[$i]=$this->colors[$i][$l];
- return $a;
- }
-
- public function setCol($l, $f){
- for ($i=0;$i<$this->nbr;$i++)
- $this->colors[$i][$l] = $f[$i];
- }
-
- public function rotate($n = false){
- $c = array();
- for ($i = 0; $i < $this->nbr; $i++){
- $c[$i] = array();
- for ($j = 0; $j < $this->nbr; $j++){
- if ($n)
- $c[$i][$j] = $this->colors[$this->nbr - $j - 1][$i];
- else
- $c[$i][$j] = $this->colors[$j][$this->nbr - $i - 1];
- }
- }
- $this->colors = $c;
- }
- }
-
- class Cube{
- private $nbr, $faces;
- public function __construct($nbr){
- $this->nbr = $nbr;
- $this->faces = array();
- for ($i = 0; $i < 6; $i++){
- $this->faces[$i] = new Face($i, $nbr);
- }
- }
- private function getLine($f, $l){
- return $this->faces[$f]->getLine($l);
- }
- private function setLine($f, $l, $t){
- return $this->faces[$f]->setLine($l, $t);
- }
- private function getCol($f, $l){
- return $this->faces[$f]->getCol($l);
- }
- private function setCol($f, $l, $t){
- return $this->faces[$f]->setCol($l, $t);
- }
-
- private function getRLine($f, $l){
- return array_reverse($this->faces[$f]->getLine($l));
- }
- private function setRLine($f, $l, $t){
- return $this->faces[$f]->setLine($l, array_reverse($t));
- }
- private function getRCol($f, $l){
- return array_reverse($this->faces[$f]->getCol($l));
- }
- private function setRCol($f, $l, $t){
- return $this->faces[$f]->setCol($l, array_reverse($t));
- }
- public function rotateLine($f, $l){
- integer($f); integer($l);
- in_range($f, 0, 5); in_range($l, 0, $this->nbr);
-
- if ($f == 4 || $f == 5){ $f = 1; } // meme rotation
- else if ($f == 3){ $f = 1; $l = $this->nbr - 1 - $l; }
-
- if ($f == 2){
- $f = 0;
- }
-
- $cl = $this->nbr - $l - 1;
- if ($f == 1){
- $c0 = $this->getLine(1, $l);
- $this->setLine(1, $l, $this->getLine(4, $l));
- $this->setLine(4, $l, $this->getRLine(3, $cl));
- $this->setRLine(3, $cl, $this->getLine(5, $l));
- $this->setLine(5, $l, $c0);
-
- if ($l == 0){ $this->faces[0]->rotate(); }
- if ($l == $this->nbr - 1){ $this->faces[2]->rotate(true); }
- }else if ($f == 0){
- $c0 = $this->getRLine(0, $l);
- $this->setRLine(0, $l, $this->getRCol(5, $cl));
- $this->setRCol(5, $cl, $this->getLine(2, $cl));
- $this->setLine(2, $cl, $this->getCol(4, $l));
- $this->setCol(4, $l, $c0);
-
- if ($l == 0){ $this->faces[3]->rotate(true); }
- if ($l == $this->nbr - 1){ $this->faces[1]->rotate(); }
- }
- }
- public function rotateColone($f, $l){
- integer($f); integer($l);
- in_range($f, 0, 5); in_range($l, 0, $this->nbr);
-
- if ($f <= 3) $f = 1;
-
- if ($f == 4){ $f = 5; $l = $this->nbr - $l - 1; }
-
- $cl = $this->nbr - $l - 1;
- if ($f == 1){
-
- $c0 = $this->getCol(0, $l);
- $this->setCol(0, $l, $this->getCol(1, $l));
- $this->setCol(1, $l, $this->getCol(2, $l));
- $this->setCol(2, $l, $this->getCol(3, $l));
- $this->setCol(3, $l, $c0);
-
- if ($l == 0){ $this->faces[4]->rotate(); }
- if ($l == $this->nbr - 1){ $this->faces[5]->rotate(true); }
- }else{
- $this->rotateLine(0, $cl);
- }
-
- }
- public function getN(){ return $this->nbr; }
- public function __toString(){
- return '<table class="cube">
- <tr>
- <td></td>
- <td>'.$this->faces[0].'</td>
- <td></td>
- </tr>
- <tr>
- <td>'.$this->faces[4].'</td>
- <td>'.$this->faces[1].'</td>
- <td>'.$this->faces[5].'</td>
- </tr>
- <tr>
- <td></td>
- <td>'.$this->faces[2].'</td>
- <td></td>
- </tr>
- <tr>
- <td></td>
- <td>'.$this->faces[3].'</td>
- <td></td>
- </tr>
- </table>';
- }
- }
-
- if (isset($_GET['restart'])) unset($_SESSION['cube']);
-
- if (!isset($_SESSION['cube']))
- $_SESSION['cube'] = new Cube(3);
-
- $c = $_SESSION['cube'];
-
- if (isset($_GET['rotate'], $_GET['f']) && is_numeric($_GET['f'])){
- if (isset($_GET['l'])){
- $c->rotateLine((int)$_GET['f'], (int)$_GET['l']);
- }
- if (isset($_GET['c'])){
- $c->rotateColone((int)$_GET['f'], (int)$_GET['c']);
- }
- }
-
-
- echo $c;
-
- echo '<table class="actions">
- ';
- for ($i = 0; $i < 6; $i++){
- echo '<tr>
- <td class="case" style="background-color:'.Color::getColor($i).'"></td>';
- for ($j = 0; $j < $c->getN(); $j++){
- echo '<td><a href="rubix.php?rotate&f='.$i.'&l='.$j.'">
- rotate line '.($j+1).'</a></td>';
- echo '<td><a href="rubix.php?rotate&f='.$i.'&c='.$j.'">
- rotate col '.($j+1).'</a></td>';
- }
- echo '</tr>';
- }
- echo '</table>';
-
- ?>
- <div style="clear:both"></div>
- <hr />
- <a href="rubix.php?restart">restart</a>
- </body>
- </html>
<?php
session_start();
?>
<html>
<head>
<title>rubix</title>
<style>
body{
background-color: gray;
}
td.case{
width:40px;
height:40px;
}
.cube{
float:left;
}
.actions{
float:left;
}
</style>
</head>
<body>
<?php
class InvalidParam extends Exception{}
class TypeException extends InvalidParam{}
function integer($i){
if (!is_integer($i)) throw new Exception('param is not an integer');
}
function in_range($i, $a, $b){
if ($i < $a || $i > $b) throw new Exception('param is not an integer');
}
class Color{
public static function getColor($n = -1){
switch ($n){
case 0: return 'white';
case 1: return 'green';
case 2: return 'yellow';
case 3: return 'blue';
case 4: return 'orange';
case 5: return 'red';
default: return 'gray';
}
}
}
class Face{
private $colors;
private $nbr;
private $c;
public function __construct($c, $nbr){
$this->c = $c;
$this->nbr = $nbr;
$this->colors = array();
for ($i = 0; $i < $nbr; $i++){
$this->colors[$i] = array();
for ($j = 0; $j < $nbr; $j++){
$this->colors[$i][$j] = $c; // * 100 + $i * 10 + $j;
}
}
}
public function __toString(){
$out = '<table>';
foreach ($this->colors as $t){
$out .= '<tr>';
foreach ($t as $y){
$out .= '<td class="case" style="background-color:'.
// Color::getColor(intval($y / 100 )).'">'.($y % 100).'</td>';
Color::getColor($y).'"></td>';
}
$out .= '</tr>';
}
return $out . '</table>';
}
public function getLine($l){
return $this->colors[$l];
}
public function setLine($l, $f){
$this->colors[$l] = $f;
}
public function getCol($l){
$a = array();
for ($i=0;$i<$this->nbr;$i++)
$a[$i]=$this->colors[$i][$l];
return $a;
}
public function setCol($l, $f){
for ($i=0;$i<$this->nbr;$i++)
$this->colors[$i][$l] = $f[$i];
}
public function rotate($n = false){
$c = array();
for ($i = 0; $i < $this->nbr; $i++){
$c[$i] = array();
for ($j = 0; $j < $this->nbr; $j++){
if ($n)
$c[$i][$j] = $this->colors[$this->nbr - $j - 1][$i];
else
$c[$i][$j] = $this->colors[$j][$this->nbr - $i - 1];
}
}
$this->colors = $c;
}
}
class Cube{
private $nbr, $faces;
public function __construct($nbr){
$this->nbr = $nbr;
$this->faces = array();
for ($i = 0; $i < 6; $i++){
$this->faces[$i] = new Face($i, $nbr);
}
}
private function getLine($f, $l){
return $this->faces[$f]->getLine($l);
}
private function setLine($f, $l, $t){
return $this->faces[$f]->setLine($l, $t);
}
private function getCol($f, $l){
return $this->faces[$f]->getCol($l);
}
private function setCol($f, $l, $t){
return $this->faces[$f]->setCol($l, $t);
}
private function getRLine($f, $l){
return array_reverse($this->faces[$f]->getLine($l));
}
private function setRLine($f, $l, $t){
return $this->faces[$f]->setLine($l, array_reverse($t));
}
private function getRCol($f, $l){
return array_reverse($this->faces[$f]->getCol($l));
}
private function setRCol($f, $l, $t){
return $this->faces[$f]->setCol($l, array_reverse($t));
}
public function rotateLine($f, $l){
integer($f); integer($l);
in_range($f, 0, 5); in_range($l, 0, $this->nbr);
if ($f == 4 || $f == 5){ $f = 1; } // meme rotation
else if ($f == 3){ $f = 1; $l = $this->nbr - 1 - $l; }
if ($f == 2){
$f = 0;
}
$cl = $this->nbr - $l - 1;
if ($f == 1){
$c0 = $this->getLine(1, $l);
$this->setLine(1, $l, $this->getLine(4, $l));
$this->setLine(4, $l, $this->getRLine(3, $cl));
$this->setRLine(3, $cl, $this->getLine(5, $l));
$this->setLine(5, $l, $c0);
if ($l == 0){ $this->faces[0]->rotate(); }
if ($l == $this->nbr - 1){ $this->faces[2]->rotate(true); }
}else if ($f == 0){
$c0 = $this->getRLine(0, $l);
$this->setRLine(0, $l, $this->getRCol(5, $cl));
$this->setRCol(5, $cl, $this->getLine(2, $cl));
$this->setLine(2, $cl, $this->getCol(4, $l));
$this->setCol(4, $l, $c0);
if ($l == 0){ $this->faces[3]->rotate(true); }
if ($l == $this->nbr - 1){ $this->faces[1]->rotate(); }
}
}
public function rotateColone($f, $l){
integer($f); integer($l);
in_range($f, 0, 5); in_range($l, 0, $this->nbr);
if ($f <= 3) $f = 1;
if ($f == 4){ $f = 5; $l = $this->nbr - $l - 1; }
$cl = $this->nbr - $l - 1;
if ($f == 1){
$c0 = $this->getCol(0, $l);
$this->setCol(0, $l, $this->getCol(1, $l));
$this->setCol(1, $l, $this->getCol(2, $l));
$this->setCol(2, $l, $this->getCol(3, $l));
$this->setCol(3, $l, $c0);
if ($l == 0){ $this->faces[4]->rotate(); }
if ($l == $this->nbr - 1){ $this->faces[5]->rotate(true); }
}else{
$this->rotateLine(0, $cl);
}
}
public function getN(){ return $this->nbr; }
public function __toString(){
return '<table class="cube">
<tr>
<td></td>
<td>'.$this->faces[0].'</td>
<td></td>
</tr>
<tr>
<td>'.$this->faces[4].'</td>
<td>'.$this->faces[1].'</td>
<td>'.$this->faces[5].'</td>
</tr>
<tr>
<td></td>
<td>'.$this->faces[2].'</td>
<td></td>
</tr>
<tr>
<td></td>
<td>'.$this->faces[3].'</td>
<td></td>
</tr>
</table>';
}
}
if (isset($_GET['restart'])) unset($_SESSION['cube']);
if (!isset($_SESSION['cube']))
$_SESSION['cube'] = new Cube(3);
$c = $_SESSION['cube'];
if (isset($_GET['rotate'], $_GET['f']) && is_numeric($_GET['f'])){
if (isset($_GET['l'])){
$c->rotateLine((int)$_GET['f'], (int)$_GET['l']);
}
if (isset($_GET['c'])){
$c->rotateColone((int)$_GET['f'], (int)$_GET['c']);
}
}
echo $c;
echo '<table class="actions">
';
for ($i = 0; $i < 6; $i++){
echo '<tr>
<td class="case" style="background-color:'.Color::getColor($i).'"></td>';
for ($j = 0; $j < $c->getN(); $j++){
echo '<td><a href="rubix.php?rotate&f='.$i.'&l='.$j.'">
rotate line '.($j+1).'</a></td>';
echo '<td><a href="rubix.php?rotate&f='.$i.'&c='.$j.'">
rotate col '.($j+1).'</a></td>';
}
echo '</tr>';
}
echo '</table>';
?>
<div style="clear:both"></div>
<hr />
<a href="rubix.php?restart">restart</a>
</body>
</html>
Conclusion
l'interface n'est pas terrible, mais ca reste valable
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
class,PDF,XLS et boucles..... [ par booth ]
bonjour!j'ai un méga problème...je dois générer un PDF et une grillle excel toutes deux issues d'un moteur de recherche...pour le moteur de recherche
Retouches sur un scripts [ par apz ]
salut,ce script php, un guestbook, est le livre d'or que je cherchais.mais voila il se base sur les Class, que je ne connais pas desormais.alors je vo
fatal error sur chargement de class [ par fabrice_pi ]
salut à tous,j'utilise une classe PHP pour faire mes taleaux en html. depuis peu j'ai l'erreur suivante :Fatal error: Cannot instantiate non-existent
Class POO retourné le nom de l'objet [ par MeTh ]
Bonjour,Comment retourné le nom de l'objet déclaré?exemple :$monobjet = new GridR();comment recuperé $monobjet dans ma class?Merci
Include, class et array [ par Hades5k ]
Bonjour! J'ai un petit problème à utiliser un array dans un fichier que j'inclus... voici un peu le code : <?php $classNames = array(); include_onc
Problem d'affichage de resultat de requete sous forme de tableau [ par jbcaiz ]
explication : je fais un requete de recherche dans ma base, qui doit normalement me sortir plusieur résultat.je veux que ces résultat s'affiche dans u
Serveur SMTP [ par Marion0904 ]
Bonsoir, J'essai d'nvoyer des mails en utilisant la class phpmailer (disponible sur http://phpmailer.sourceforge.net/). J'incu la class php mail
templates avec poo [ par lesnes ]
bonjours je reprogramme totalement mon site en poo et je souhaiterai utiliser les templates mais l'on ne peut pas faire appel a une class exterieur a
Affichage page par page [ par Leneuf8000 ]
Bonjour, j'aimerai réaliser un affichage page par page. En fait je voudrai que tout soit gérer dans le même fichier. Ainsi pour l
Erreur de syntaxe [ par Leneuf8000 ]
Bonjour everybody, la, j'ai un problème de syntaxe, je ne comprends pas !!! Voici la requête qui pose problème : &n
|
Derniers Blogs
MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko [FRAMEWORK 4] LES TASKS ET LE THREAD UI[FRAMEWORK 4] LES TASKS ET LE THREAD UI par fathi
Je viens de passer quelques temps au TechDay's et j'ai pu voir pas mal de session intéressante. Par contre une chose m'a un peu étonné lors de certaines de ces sessions qui abordaient les améliorations du framework .NET (donc le 4.5) : en gros, bea...
Cliquez pour lire la suite de l'article par fathi WORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBEWORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBE par JeremyJeanson
Depuis déjà un an, je conseille vivement les utilisateurs de Workflow Foundation 3 à migrer vers la version 4. L'information qui va suivre ne devrait donc pas trop prendre au dépourvu les personnes qui m'ont suivi. Je profite de ce poste, pour faire le re...
Cliquez pour lire la suite de l'article par JeremyJeanson TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLETECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLE par ROMELARD Fabrice
Speakers: Julien Marechal, Gautier Confiant, Sébastien MEYER La session débute par le positionnement de la solution System Center par rapport aux concepts d'organisation ITIL. Le portail du catalogue de se...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|