Partage
  • Partager sur Facebook
  • Partager sur Twitter

jeu démineur php js

déchifrer un code, merci

    28 mai 2010 à 13:37:55

    Citation : kurtjulien

    Bonjour,

    Je souhaite réaliser un jeu démineur avec une sauvegarde des scores dans une BDD php.

    voici mon code php:

    <!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 Strict//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
    
    <head>
    <title>Démineur</title>
    <meta http-equiv="content-Style-Type" content="text/css" />
    <meta http-equiv="content-Style-Type" content="text/javascript" />
    <link rel="stylesheet" type="text/css" href="imgdem/demineurcss.css" />
    <script src="imgdem/demineur.js" type="text/javascript"></script>
    <meta content="MSHTML 6.00.2900.3492" name="GENERATOR" />
    </head>
    
    <body>
    
    <table class="table1">			<!-- ENTETE: Temps et nombre de mines -->
    
     <tr class="t1"> 
       <td class="t2">						TEMPS			</td>
       <td class="t3">
         <img id="digit2" src="imgdem/digit0.bmp" alt="" /> 
         <img id="digit1" src="imgdem/digit0.bmp" alt="" />
         <img id="digit0" src="imgdem/digit0.bmp" alt="" /></td>
     </tr>
    
     <tr class="t1"> 
       <td class="t2">						MINES RESTANTES		</td>
       <td class="t3">
         <img id="minedigit1" src="imgdem/digit0.bmp" alt="" /> 
         <img id="minedigit0" src="imgdem/digit0.bmp" alt="" /></td>
     </tr>
    
    </table>
    
    <div><br /><br /><div id="gamezone"></div><br /><br /></div>			<!-- GRILLE DU JEU -->
    
    <form name='nive'>
    
    <table class="table1">
    
     <tr class="t1"> 				<!-- BOUTON START -->
       <td bgcolor="#000000" colspan="2"><input onclick="newGame();" type="button" value="START" /></td>
     </tr>
    
     <tr class="t1"> 
       <td width="50%" class="t2">HORIZONTAL</td>
       <td class="t3"><input type="text" name="l" class="formulaire" style="text-align: center" id="gameWidth" size="2" maxlength="2" onchange="setWidth(this.value);" /></td>
     </tr>
    
     <tr class="t1"> 
       <td class="t2">VERTICAL</td>
       <td class="t3"><input type="text" name="h" class="formulaire" style="text-align: center" id="gameHeight" size="2" maxlength="2" onchange="setHeight(this.value);" /></td>
     </tr>
    
     <tr class="t1"> 
       <td class="t2">MINES</td>
       <td class="t3"><input type="text" name="m" class="formulaire" style="text-align: center" id="gameMines" size="2" maxlength="2" onchange="setMines(this.value);" /></td>
     </tr>
    
     <tr class="t1"> 
       <td class="t2">NIVEAU</td>
       <td class="t3">
    	<select id="niveau" onchange="alert('Vous avez choisis le niveau :\n\n' + this.options[this.selectedIndex].value); total(); newGame();">
    		<option selected="selected" value="niveau">Choisissez votre niveau</option>
    		<option value="Débutant">Débutant</option>
    		<option value="Intermédiaire">Intermédiaire</option>
    		<option value="Expert">Expert</option>
    		<option value="Personnalisé">Personnalisé</option>
    	</select>
       </td>
     </tr>
    
    </table>
    </form>
    
    <script type="text/javascript">
    function total() {
    if(document.nive.niveau.value=="Débutant") 
    	{document.getElementById('gameWidth').value = "9"; setWidth("9");
    	document.getElementById('gameHeight').value = "9"; setHeight("9");
    	document.getElementById('gameMines').value = "10"; nbMines = "10";}
    	else {
    		if(document.nive.niveau.value=="Intermédiaire") 
    		{document.getElementById('gameWidth').value = "16"; setWidth("16");
    		document.getElementById('gameHeight').value = "16"; setHeight("16");
    		document.getElementById('gameMines').value = "40"; setMines("40");} 
    		else {
    			if(document.nive.niveau.value=="Expert") 
    			{document.getElementById('gameWidth').value = "30"; setWidth("30");
    			document.getElementById('gameHeight').value = "16"; setHeight("16");
    			document.getElementById('gameMines').value = "90"; nbMines = "90";} 
    			else {
    				if(document.nive.niveau.value=="Personnalisé") 
    				{document.getElementById('gameWidth').value = ""; setWidth("");
    				document.getElementById('gameHeight').value = ""; setHeight("");
    				document.getElementById('gameMines').value = ""; nbMines = "";} 
    			}
    		}
    	}
    }
    </script>
    
    </body>
    </html>
    



    voir aussi le fichier javascript:

    var CASE_EMPTY = 0;			// 
    var CASE_FLAG = 1;			// 
    var CASE_CLICK = 2;			// 
    
    var TABLE_WIDTH = 9;		// Nombre de cases HORIZONTALE
    var TABLE_HEIGHT = 9;		// Nombre de cases VERTICALE
    
    var nbMines = 10;			// 
    var nbFlags = 0;			// 
    var nbrMaxClick = TABLE_WIDTH*TABLE_HEIGHT - nbMines;		// nombre de click maximum
    
    var alreadyClick = false;
    var elapsedTime = 0;
    var idto;					// nombre ?? temps ??
    
    var tab = new Array();			// 
    var tabFlag = new Array();		// 
    
    
    function initTabs()
    {
      alreadyClick = false;
      elapsedTime = 0;
      idto = null;
      nbFlags = 0;
      for(var i=0; i<TABLE_HEIGHT; i++)		// CREATION DES CASES VERTICALES
      {
        tab[i] = new Array();
        tabFlag[i] = new Array();
        for(var j=0; j<TABLE_WIDTH; j++)	// CREATION DES CASES HORIZONTALES (pourquoi le mettre dans le premier for? si je le met en dehors, est-ce que ca marche ?)
        {
          tab[i][j] = 0;
          tabFlag[i][j] = CASE_EMPTY;
        }
      }
    }
    
    
    function fillTab(NbMines, xPosInit, yPosInit)
    {
      var iNbMines = NbMines;
      var minValue = -2*NbMines;
      while(iNbMines > 0)
      {
        var xPos = Math.floor(Math.random()*TABLE_HEIGHT);
        var yPos = Math.floor(Math.random()*TABLE_WIDTH);
        if(tab[xPos][yPos] >= 0 && (xPosInit != xPos || yPosInit != yPos))
        {
          tab[xPos][yPos] = minValue;
          if(xPos > 0)
          {
            tab[xPos-1][yPos] ++;
            if(yPos > 0) tab[xPos-1][yPos-1] ++;
            if(yPos < TABLE_WIDTH - 1) tab[xPos-1][yPos+1] ++;
          }
          if(xPos < TABLE_HEIGHT - 1)
          {
            tab[xPos+1][yPos] ++;
            if(yPos > 0) tab[xPos+1][yPos-1] ++;
            if(yPos < TABLE_WIDTH - 1) tab[xPos+1][yPos+1] ++;
          }
          if(yPos > 0) tab[xPos][yPos-1] ++;
          if(yPos < TABLE_WIDTH - 1) tab[xPos][yPos+1] ++;
          iNbMines --;
        }
      }
      return true;
    }
    
    
    function writeTab(bFlag)
    {
      for(var xPos=0; xPos<tab.length; xPos++)
        for(var yPos=0; yPos<tab[xPos].length; yPos++)
          if(!tabFlag[xPos][yPos])
          {
            if(tab[xPos][yPos] < 0)
    	  document.getElementById('case'+(xPos*TABLE_WIDTH+yPos)).innerHTML = '<img src="imgdem/'+(bFlag?'flag':'mine')+'.bmp"/>'
            else
              document.getElementById('case'+(xPos*TABLE_WIDTH+yPos)).innerHTML = '<img src="imgdem/'+tab[xPos][yPos]+'.bmp"/>';
          }
          else
            if(tabFlag[xPos][yPos] == CASE_FLAG && tab[xPos][yPos] >= 0)
              document.getElementById('case'+(xPos*TABLE_WIDTH+yPos)).innerHTML = '<img src="imgdem/nomine.bmp"/>'
    }
    
    
    function caseClick(caseId)
    {
      var xPos = Math.floor(caseId/TABLE_WIDTH);
      var yPos = caseId - TABLE_WIDTH * xPos;
      if(!alreadyClick)
      {
        fillTab(nbMines, xPos, yPos);
        syncCompteur();
        alreadyClick=true;
      }
      if(tabFlag[xPos][yPos]) return; 
    
      if(checkEnd()) return;
    
      if(!tabFlag[xPos][yPos])
      {
        if(tab[xPos][yPos] < 0)				/* JEU PERDU */
        {
          window.clearTimeout(idto);
          idto = null;
          document.getElementById('case'+(xPos*TABLE_WIDTH+yPos)).innerHTML = '<img src="imgdem/mine.bmp"/>';
          tabFlag[xPos][yPos] = CASE_CLICK;
          writeTab();
    	alert("Désolé, vous avez perdu !");
          return;
        }
        else 
        {
          document.getElementById('case'+(xPos*TABLE_WIDTH+yPos)).innerHTML = '<img onmouseup="if(event.button == 2) balayage('+(xPos*TABLE_WIDTH+yPos)+');" src="imgdem/'+tab[xPos][yPos]+'.bmp"/>';
          nbrMaxClick --;
          checkEnd();
          tabFlag[xPos][yPos] = CASE_CLICK;
        }
    
        if(tab[xPos][yPos]==0)
        {
          if(xPos > 0)
          {
            caseClick(caseId - TABLE_WIDTH);
            if(yPos > 0) caseClick(caseId - TABLE_WIDTH - 1);
            if(yPos < TABLE_WIDTH - 1) caseClick(caseId - TABLE_WIDTH + 1);
          }
          if(xPos < TABLE_HEIGHT - 1)
          {
            caseClick(caseId + TABLE_WIDTH);
            if(yPos > 0) caseClick(caseId + TABLE_WIDTH - 1);
            if(yPos < TABLE_WIDTH - 1) caseClick(caseId + TABLE_WIDTH + 1);
          }
          if(yPos > 0) caseClick(caseId - 1);
          if(yPos < TABLE_WIDTH - 1) caseClick(caseId + 1);
        }
      }
    }
    
    
    function caseRightClick(caseId)					// CLIQUE DROIT -----------------------
    {
      if(!idto) return;
      if(!alreadyClick) return;
      var xPos = Math.floor(caseId/TABLE_WIDTH);
      var yPos = caseId - TABLE_WIDTH * xPos;
      if(!tabFlag[xPos][yPos] && nbFlags < nbMines)
      {
        document.getElementById('case'+(xPos*TABLE_WIDTH+yPos)).innerHTML = '<img src="imgdem/flag.bmp" onmouseup="if(event.button == 2) caseRightClick('+(xPos*TABLE_WIDTH+yPos)+'); else if(event.button == 1) caseClick('+(xPos*TABLE_WIDTH+yPos)+');"/>';
        tabFlag[xPos][yPos] = CASE_FLAG;
    		nbFlags ++;
    		mineCompteur();
      }
      else if(tabFlag[xPos][yPos] == CASE_FLAG || nbFlags == nbMines)
      {
    		if(tabFlag[xPos][yPos] == CASE_FLAG)
    		{
    		  nbFlags --;
    			mineCompteur();
    		}
        document.getElementById('case'+(xPos*TABLE_WIDTH+yPos)).innerHTML = '<img src="imgdem/vide.bmp" onmouseup="if(event.button == 2) caseRightClick('+(xPos*TABLE_WIDTH+yPos)+'); else caseClick('+(xPos*TABLE_WIDTH+yPos)+');">';
        tabFlag[xPos][yPos] = CASE_EMPTY;
      }
    	checkEnd();
    }
    
    
    function balayage(caseId)
    {
      if(!idto) return;
      if(!alreadyClick) return;
      var xPos = Math.floor(caseId/TABLE_WIDTH);
      var yPos = caseId - TABLE_WIDTH * xPos;
      document.getElementById('case'+(xPos*TABLE_WIDTH+yPos)).innerHTML = document.getElementById('case'+(xPos*TABLE_WIDTH+yPos)).innerHTML;
      var count = tab[xPos][yPos];
      if(xPos > 0)
      {
        if(tabFlag[xPos-1][yPos] == CASE_FLAG) count--;
        if(yPos > 0) if(tabFlag[xPos-1][yPos-1] == CASE_FLAG) count--;
        if(yPos < TABLE_WIDTH - 1) if(tabFlag[xPos-1][yPos+1] == CASE_FLAG) count--;
      }
      if(xPos < TABLE_HEIGHT - 1)
      {
        if(tabFlag[xPos+1][yPos] == CASE_FLAG) count--;
        if(yPos > 0) if(tabFlag[xPos+1][yPos-1] == CASE_FLAG) count--;
        if(yPos < TABLE_WIDTH - 1) if(tabFlag[xPos+1][yPos+1] == CASE_FLAG) count--;
      }
      if(yPos > 0) if(tabFlag[xPos][yPos-1] == CASE_FLAG) count--;
      if(yPos < TABLE_WIDTH - 1) if(tabFlag[xPos][yPos+1] == CASE_FLAG) count--;
    
      if(count) return;
    
      if(xPos > 0)
      {
        caseClick(caseId - TABLE_WIDTH);
        if(yPos > 0) caseClick(caseId - TABLE_WIDTH - 1);
        if(yPos < TABLE_WIDTH - 1) caseClick(caseId - TABLE_WIDTH + 1);
      }
      if(xPos < TABLE_HEIGHT - 1)
      {
        caseClick(caseId + TABLE_WIDTH);
        if(yPos > 0) caseClick(caseId + TABLE_WIDTH - 1);
        if(yPos < TABLE_WIDTH - 1) caseClick(caseId + TABLE_WIDTH + 1);
      }
      if(yPos > 0) caseClick(caseId - 1);
      if(yPos < TABLE_WIDTH - 1) caseClick(caseId + 1);
    }
    
    
    function checkEnd()					// JEU TERMINE -----------------------
    {
      if(nbrMaxClick) return false;
      window.clearTimeout(idto);
      idto = null;
      writeTab(true);
      alert('BRAVO ! VOUS AVEZ GAGNE');
      return true;
    }
    
    
    function syncCompteur()
    {
      var d2 = Math.floor(elapsedTime/100);
      var tmp = Math.floor(elapsedTime/10);
      var d1 = tmp - 10*d2;
      var d0 = elapsedTime - 10*tmp;  
      document.getElementById('digit0').src = 'imgdem/'+d0+'.bmp';
      document.getElementById('digit1').src = 'imgdem/'+d1+'.bmp';
      document.getElementById('digit2').src = 'imgdem/'+d2+'.bmp';
    	elapsedTime --;
    	compteur();
    }
    
    
    function compteur()					// COMPTEUR TEMPS -----------------------
    {
      idto = setTimeout('compteur()', 1000);
      elapsedTime ++;
      if(elapsedTime == 999) window.clearTimeout(idto);
      var d2 = Math.floor(elapsedTime/100);
      var tmp = Math.floor(elapsedTime/10);
      var d1 = tmp - 10*d2;
      var d0 = elapsedTime - 10*tmp;
      document.getElementById('digit0').src = 'imgdem/digit'+d0+'.bmp';
      if(d0) return;
      document.getElementById('digit1').src = 'imgdem/digit'+d1+'.bmp';
      if(d1) return;
      document.getElementById('digit2').src = 'imgdem/digit'+d2+'.bmp';
    }
    
    
    function newGame()					// NOUVEAU JEU -----------------------
    {
      nbrMaxClick = TABLE_WIDTH*TABLE_HEIGHT - nbMines;
      initTabs();
      mineCompteur();
    	
      var htmlCode = '';
      htmlCode += '<table cellspacing="0" cellpadding="0" summary="Zone de jeu">';
      for(var indexX = 0; indexX<TABLE_HEIGHT; indexX++)
      {
        htmlCode += '<TR>';
        for(var indexY = 0; indexY<TABLE_WIDTH; indexY++)
          htmlCode += '<TD><span style="width:25px; height:25px;" id="case'+(indexX*TABLE_WIDTH+indexY)+'"><img src=imgdem/vide.bmp onmouseup="if(event.button == 2) caseRightClick('+(indexX*TABLE_WIDTH+indexY)+'); else if(event.button == 1) caseClick('+(indexX*TABLE_WIDTH+indexY)+');"></span></TD>';
        htmlCode += '</TR>';
      }
      htmlCode += '</table>';
      document.getElementById('gamezone').innerHTML = htmlCode;
    }
    
    
    function setWidth(val)					// SELECTION WIDTH -----------------------
    {
      var tmpWidth = parseInt(val);
    	if(tmpWidth+'' == 'NaN' || tmpWidth <= 1) {document.getElementById('gameWidth').value = TABLE_WIDTH; return;}
    	TABLE_WIDTH = tmpWidth;
    	setMines(document.getElementById('gameMines').value);
    }
    
    
    function setHeight(val)					// SELECTION HEIGHT -----------------------
    {
      var tmpHeight = parseInt(val);
    	if(tmpHeight+'' == 'NaN' || tmpHeight <= 1) {document.getElementById('gameHeight').value = TABLE_HEIGHT; return;}
    	TABLE_HEIGHT = tmpHeight;
    	setMines(document.getElementById('gameMines').value);	
    }
    
    
    function setMines(val)					// SELECTION MINES -----------------------
    {
      var tmpMines = parseInt(val);
    	if(tmpMines+'' == 'NaN' || tmpMines < 1 || tmpMines >= TABLE_WIDTH*TABLE_HEIGHT)
    	{
    	document.getElementById('gameMines').value = ((TABLE_WIDTH*TABLE_HEIGHT>10)?10:TABLE_WIDTH*TABLE_HEIGHT-1);
    	alert('Vous avez choisi le niveau \n\nPersonnalisé');		/* Lors d'une sélection du nombre de mine, mettre comme niveau "personnalisé" */
    	document.getElementById('niveau').value = "Personnalisé"
    	return;
    	}
    	nbMines = tmpMines;
    }
    
    
    function mineCompteur()					// COMPTEUR MINE -----------------------
    {
      var val = nbMines - nbFlags;
    	var d1 = Math.floor(val/10);
      var d0 = val - 10*d1;
      document.getElementById('minedigit0').src = 'imgdem/digit'+d0+'.bmp';
      document.getElementById('minedigit1').src = 'imgdem/digit'+d1+'.bmp';
    }
    



    ca fonctionne, je l'ai récupéré sur un autre site, par contre, je ne le comprend pas... si quelqu'un pouvait m'aider en commentant comme j'ai commencé à le faire, ca m'aider beaucoup. merci

    je ne sais pas ou mettre l'envoie des socres et temps dans la BDD, c'est pour cela qu'elle n'appartait pas... dans le fichier php sachant que les variables sont dans le ficher js
    ou dans le fichier js? si n peut mettre du php dans du js

    merci de votre aide

    • Partager sur Facebook
    • Partager sur Twitter
      28 mai 2010 à 13:50:07

      Tu fais un formulaire qui pointe vers une page PHP qui enregistre dans la base de données =D
      • Partager sur Facebook
      • Partager sur Twitter

      jeu démineur php js

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown