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
UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
Comparez les prix

HTC Magic
Entre 429€ et 429€
|