IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Déplacer un pion sur un damier avec math.random


Sujet :

JavaScript

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Webdesigner
    Inscrit en
    Novembre 2017
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Novembre 2017
    Messages : 4
    Points : 5
    Points
    5
    Par défaut Déplacer un pion sur un damier avec math.random
    Bonjour à tous et merci s'il y a des réponses

    EDIT : Le problème venait effectivement de mon switch (les if à la chaîne => c'est débile, tout comme celui qui les emploie => moi). Merci à tous

    D'ordinaire je n'ose pas demander de l'aide mais là, je suis assez pressé et je ne vois vraiment pas où est le problème
    Je cherche à faire se déplacer aléatoirement un pion sur un damier de 9 cases comme celui-ci :
    _______
    |1|2|3|
    |4|5|6|
    |7|8|9|

    avec les contraintes suivantes :
    - 1 appel de fonction = 1 tour.
    - 1 case maximum de déplacement par tour
    - uniquement verticalement ou horizontalement

    Par exemple si l'on était provisoirement sur la case 7, on ne pourra aller au tour suivant que sur la case 4 ou sur la 8

    Je me doute qu'il devait y avoir une façon élégante de l'écrire mais je voulais que ça marche rapidement (d'où le gros pâté)
    => échec
    Mon pion se déplace conformément à la règle la moitié du temps, sinon il se téléporte à l'autre bout du damier ou reste sur la même case plusieurs fois de suite (interdit aussi) Comprends pas.

    Voici mon code (en entier du coup et corrigé) :


    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
     
     
     
    <!DOCTYPE html>
    <html lang="fr" dir="ltr">
    <head>
    	<!-- cache-control avec max-age=60 pour le développement uniquement -->
      <meta http-equiv="cache-control" content="public, max-age=60">
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
      <meta name="author" content="Daniel Hagnoul">
    	<title>Test</title>
    <style>
            .checkerboard {z-index:80; float:right; position:fixed;  top:100px; right:70px; width:330px; height:400px; border:0px solid blue; background-color:white; text-align:left;}
            .damien {width:97%; border:0px solid red;}
            .case1 {width:25%; height:60px; border:1px solid black; margin:0px; text-align:left;}
            .cazix {width:100px; position:relative; height:100px; padding: 0px 4px; margin:0px; float:right; font-size:0.5em; text-align:left; vertical-align:top; }
            .central {width:100px; position:relative; height:100px; padding: 0px 4px; margin:0px; float:right; font-size:0.5em; text-align:left; vertical-align:top; }
            .playerpawn {z-index:300; float:right; position:fixed; top:285px; right:150px; width:18px; height:18px; background-color:red; border-radius: 10px; }
            .pnjpawn {z-index:300; float:right; position:fixed; top:285px; right:130px; width:18px; height:18px; background-color:lightgreen; border-radius: 10px; }
    </style>
     
     
    <div class="checkerboard">
    	<div class="blublu">
    		<table class="damien">
    			<tr>
    				<td class="case1">
    						<button name="cz1" id="cz1" class="cazix" value="1" onclick="movepawn('cz1','plyrpawn')"><span>A1<br><br><br><center></center></span></button>
    				<td>
    				<td class="case1">
    						<button name="cz2" id="cz2" class="cazix" value="2" onclick="movepawn('cz2','plyrpawn')"><span>A2<br><br><br><center></center></span></button>
    				<td>
    				<td class="case1">
    						<button name="cz3" id="cz3" class="cazix" value="3" onclick="movepawn('cz3','plyrpawn')"><span>A3<br><br><br><center></center></span></button>
    				<td>
    			</tr>
    			<tr>
    				<td class="case1">
    						<button name="cz4" id="cz4" class="cazix" value="4" onclick="movepawn('cz4','plyrpawn')"><span>B1<br><br><br><center></center></span></button>
    				<td>
    				<td class="case1">
    						<button name="cz5" id="cz5" class="central" value="5" onclick="movepawn('cz5','plyrpawn')"><span>B2<br><br><br><center></center></span></button>
    				<td>
    				<td class="case1">
    						<button name="cz6" id="cz6" class="cazix" value="6" onclick="movepawn('cz6','plyrpawn')"><span>B3<br><br><br><center></center></span></button>
    				<td>
    			</tr>
    			<tr>
    				<td class="case1">
    						<button name="cz7" id="cz7" class="cazix" value="7" onclick="movepawn('cz7','plyrpawn')"><span>C1<br><br><br><center></center></span></button>
    				<td>
    				<td class="case1">
    						<button name="cz8" id="cz8" class="cazix" value="8" onclick="movepawn('cz8','plyrpawn')"><span>C2<br><br><br><center></center></span></button>
    				<td>
    				<td class="case1">
    						<button name="cz9" id="cz9" class="cazix" value="9" onclick="movepawn('cz9','plyrpawn')"><span>C3<br><br><br><center></center></span></button>
    				<td>
    			</tr>
    		</table>
    	</div>
    </div>
     
     
     
     
     
    <div class="playerpawn" id="plyrpawn">&nbsp;</div>
    <div class="pnjpawn" id="pnjpawn">&nbsp;</div>
     
    </body>
     
     
    <script type="text/javascript">
     
    var pnjpoz = 6 ; 
    var plyrpoz = 5 ; 
     
    function movepnj() {
            divj = document.getElementById("pnjpawn");
     
            switch (pnjpoz) {
                    case 1:
                            min = Math.ceil(1); max = Math.floor(2);
                            var croco =  Math.floor(Math.random() * (max - min +1)) + min;
                                    if (croco=='1') { // va sur la case 2
                                            pnjpoz = 2 ;
                                            divj.style.top = "180px"; //
                                            divj.style.right = "230px"; //
                                    }
                                    if (croco=='2') { // va sur la case 4
                                            pnjpoz = 4 ;
                                            divj.style.top = "285px"; //
                                            divj.style.right = "340px"; //
                                    }
                    break;
                    case 2:
                            min = Math.ceil(1); max = Math.floor(3);
                            var croco =  Math.floor(Math.random() * (max - min +1)) + min;
                                    if (croco=='1') { // va sur la case 1
                                            pnjpoz = 1 ;
                                            divj.style.top = "180px"; //
                                            divj.style.right = "340px"; //
                                    }
                                    if (croco=='2') { // va sur la case 3
                                            pnjpoz = 3 ;
                                            divj.style.top = "180px"; //
                                            divj.style.right = "124px"; //
                                    }
                                    if (croco=='3') { // va sur la case 5
                                            pnjpoz = 5 ;
                                                    divj.style.top = "285px"; //
                                                    divj.style.right = "230px"; //
                                    }
                    break;
                    case 3: 
                            min = Math.ceil(1); max = Math.floor(2);
                            var croco =  Math.floor(Math.random() * (max - min +1)) + min;
                                    if (croco=='1') { // va sur la case 2
                                            pnjpoz = 2 ;
                                            divj.style.top = "180px"; //
                                            divj.style.right = "230px"; //
                                    }
                                    if (croco=='2') { // va sur la case 6
                                            pnjpoz = 6 ;
                                            divj.style.top = "285px"; //
                                            divj.style.right = "124px"; //
                                    }
                    break;
                    case 4: 
                            min = Math.ceil(1); max = Math.floor(3);
                            var croco =  Math.floor(Math.random() * (max - min +1)) + min;
                                    if (croco=='1') { // va sur la case 1
                                            pnjpoz = 1 ;
                                            divj.style.top = "180px"; //
                                            divj.style.right = "340px"; //
                                    }
                                    if (croco=='2') { // va sur la case 5
                                            pnjpoz = 5 ;
                                                    divj.style.top = "285px"; //
                                                    divj.style.right = "230px"; //
                                    }
                                    if (croco=='3') { // va sur la case 7
                                            pnjpoz = 7 ;
                                            divj.style.top = "390px"; //
                                            divj.style.right = "340px"; //
                                    }
                    break;
                    case 5: 
                            min = Math.ceil(1); max = Math.floor(4);
                            var croco =  Math.floor(Math.random() * (max - min +1)) + min;
                                    if (croco=='1') { // va sur la case 2
                                            pnjpoz = 2 ;
                                            divj.style.top = "180px"; //
                                            divj.style.right = "230px"; //
                                    }
                                    if (croco=='2') { // va sur la case 4
                                            pnjpoz = 4 ;
                                            divj.style.top = "285px"; //
                                            divj.style.right = "340px"; //
                                    }
                                    if (croco=='3') { // va sur la case 6
                                            pnjpoz = 6 ;
                                            divj.style.top = "285px"; //
                                            divj.style.right = "124px"; //
                                    }
                                    if (croco=='4') { // va sur la case 8
                                            pnjpoz = 8 ;
                                            divj.style.top = "390px"; //
                                            divj.style.right = "230px"; //
                                    }
                    break;
                    case 6: 
                            min = Math.ceil(1); max = Math.floor(3);
                            var croco =  Math.floor(Math.random() * (max - min +1)) + min;
                                    if (croco=='1') { // va sur la case 3
                                            pnjpoz = 3 ;
                                            divj.style.top = "180px"; //
                                            divj.style.right = "124px"; //
                                    }
                                    if (croco=='2') { // va sur la case 5
                                            pnjpoz = 5 ;
                                                    divj.style.top = "285px"; //
                                                    divj.style.right = "230px"; //
                                    }
                                    if (croco=='3') { // va sur la case 9
                                            pnjpoz = 9 ;
                                            divj.style.top = "390px"; //
                                            divj.style.right = "124px"; //
                                    }
                    break;
                    case 7: 
                            min = Math.ceil(1); max = Math.floor(2);
                            var croco =  Math.floor(Math.random() * (max - min +1)) + min;
                                    if (croco=='1') { // va sur la case 4
                                            pnjpoz = 4 ;
                                            divj.style.top = "285px"; //
                                            divj.style.right = "340px"; //
                                    }
                                    if (croco=='2') { // va sur la case 8
                                            pnjpoz = 8 ;
                                            divj.style.top = "390px"; //
                                            divj.style.right = "230px"; //
                                    }
                    break;
                    case 8: 
                            min = Math.ceil(1); max = Math.floor(3);
                            var croco =  Math.floor(Math.random() * (max - min +1)) + min;
                                    if (croco=='2') { // va sur la case 5
                                            pnjpoz = 5 ;
                                                    divj.style.top = "285px"; //
                                                    divj.style.right = "230px"; //
                                    }
                                    if (croco=='1') { // va sur la case 7
                                            pnjpoz = 7 ;
                                            divj.style.top = "390px"; //
                                            divj.style.right = "340px"; //
                                    }
                                    if (croco=='3') { // va sur la case 9
                                            pnjpoz = 9 ;
                                            divj.style.top = "390px"; //
                                            divj.style.right = "124px"; //
                                    }
                    break;
                    case 9: 
                            min = Math.ceil(1); max = Math.floor(2);
                            var croco =  Math.floor(Math.random() * (max - min +1)) + min;
                                    if (croco=='1') { // va sur la case 6
                                            pnjpoz = 6 ;
                                            divj.style.top = "285px"; //
                                            divj.style.right = "124px"; //
                                    }
                                    if (croco=='2') { // va sur la case 8
                                            pnjpoz = 8 ;
                                            divj.style.top = "390px"; //
                                            divj.style.right = "230px"; //
                                    }
                    break;
            }
            return pnjpoz;
    }       
     
     
    function movepawn(boutoncr, id) { // On déclare la fonction movepawn
      var div = document.getElementById(id); // On récupère le div ciblé grâce à l'id
      if(boutoncr=='cz1') { // si le bouton cliqué est le A1
        div.style.top = "180px"; //
        div.style.right = "370px"; //
                    plyrpoz = 1;
      }
      if(boutoncr=='cz2') { // si le bouton cliqué est le A2
        div.style.top = "180px"; //
        div.style.right = "260px"; //
                    plyrpoz = 2;
      }
      if(boutoncr=='cz3') { // si le bouton cliqué est le A3
        div.style.top = "180px"; //
        div.style.right = "154px"; //
                    plyrpoz = 3;
      }  
      if(boutoncr=='cz4') { // si le bouton cliqué est le B1
        div.style.top = "285px"; //
        div.style.right = "370px"; //
                    plyrpoz = 4;
      }
      if(boutoncr=='cz5') { // si le bouton cliqué est le B2
        div.style.top = "285px"; //
        div.style.right = "260px"; //
                    plyrpoz = 5;
     }
      if(boutoncr=='cz6') { // si le bouton cliqué est le B3
        div.style.top = "285px"; //
        div.style.right = "154px"; //
                    plyrpoz = 6;
      }
      if(boutoncr=='cz7') { // si le bouton cliqué est le C1
        div.style.top = "390px"; //
        div.style.right = "370px"; //
                    plyrpoz = 7;
      }
      if(boutoncr=='cz8') { // si le bouton cliqué est le C2
        div.style.top = "390px"; //
        div.style.right = "260px"; //
                    plyrpoz = 8;
      }
      if(boutoncr=='cz9') { // si le bouton cliqué est le C3
        div.style.top = "390px"; //
        div.style.right = "154px"; //
                    plyrpoz = 9;
      }
      movepnj();
    }
    </script>
    </html>

  2. #2
    Rédacteur

    Avatar de danielhagnoul
    Homme Profil pro
    Étudiant perpétuel
    Inscrit en
    Février 2009
    Messages
    6 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 73
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant perpétuel
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2009
    Messages : 6 389
    Points : 22 933
    Points
    22 933
    Billets dans le blog
    125
    Par défaut


    Si on ne peut bouger que d'une seule case horizontalement et verticalement les mouvements possibles sont faciles à déterminer :

    /*
    * Damier
    *
    * 1 2 3
    * 4 5 6
    * 7 8 9
    *
    *
    *
    * 1 : 2 ou 4
    * 4 : 1, 5 ou 7
    * 7 : 4 ou 8
    * 2 : 1, 3 ou 5
    * 5 : 2, 4, 6 ou 8
    * 8 : 5, 7 ou 9
    * 3 : 2 ou 6
    * 6 : 3, 5 ou 9
    * 9 : 6 ou 8
    */

    Impossible de tester votre code puisque vous ne donnez pas les codes CSS et HTML.

    Il me semble que vous avez bien démarré, mais je préfère un switch à une série de if. Exemple :

    Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    <!DOCTYPE html>
    <html lang="fr" dir="ltr">
    <head>
    	<!-- cache-control avec max-age=60 pour le développement uniquement -->
      <meta http-equiv="cache-control" content="public, max-age=60">
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
      <meta name="author" content="Daniel Hagnoul">
    	<title>Test</title>
      <style>
                    *,
                    *:after,
                    *:before {
                            box-sizing: border-box;
                    }
                    
                    /* CSS du test */
                    
                    th,
                    td {
                            padding: 0.3rem;
                            background-color: yellow;
                            width: 5rem;
                            height: 5rem;
                            text-align: center;
                            font-family: monospace;
                            font-size: 3rem;
                            font-weight: bold;
                    }
                    button {
                            margin-top: 1rem;
                            margin-left: 3rem;
                            font-size: 3rem;
                    }
                                    
                    /* Fin CSS du test */
     
      </style>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/locale/fr.js"></script>
      <script src="http://danielhagnoul.developpez.com/lib/dvjh/dvjhUtilities-1.4.0.js"></script>
    	<script>
        'use strict';
                    
                    document.addEventListener( "DOMContentLoaded", ev => {
                            // le DOM est construit, la page web n'est pas visible
                            moment.locale( "fr" );
                            klog( `DOM ready   : ${ new kDvjhDate() }` );
                            
          // code du test
     
     
                            // fin code du test
          
        }, false );
        
        window.addEventListener( "load", ev => { 
                            // le DOM est construit et la page web est visible
                            klog( `Window load : ${ new kDvjhDate() }` );
                            
          // code du test
                            
                            // colore la case du croco
                            function coloreLaCase( n = 9 ){
                                    
                                    if ( kGetType( n ) === "number" && n < 10 && n > 0 ){
                                            
                                            // jaune par défaut
                                            for ( const elem of k$all( "[id^='case']") ){
                                                    elem.style.backgroundColor = "yellow";
                                            }
                                            
                                            // rouge pour le croco
                                            k$( "#case" + n ).style.backgroundColor = "red";
                                            
                                    } else {
                                            kerror( `Le paramètre ${ n } doit être un nombre de 1 à 9` );
                                    }
                            }
                            
                            // clic sur le bouton jouer
                            k$( "#btnPlay" ).addEventListener( "click", ev => {
                                    
                                    /*
                                     * Damier
                                     * 
                                     * 1 2 3
                                     * 4 5 6
                                     * 7 8 9
                                     *
                                     * Si on ne peut bouger que d'une seule case horizontalement et verticalement :
                                     * 
                                     * 1 : 2 ou 4
                                     * 4 : 1, 5 ou 7
                                     * 7 : 4 ou 8
                                     * 2 : 1, 3 ou 5
                                     * 5 : 2, 4, 6 ou 8
                                     * 8 : 5, 7 ou 9
                                     * 3 : 2 ou 6
                                     * 6 : 3, 5 ou 9
                                     * 9 : 6 ou 8
                                     */
                                    function deplace( value = 9 ){
                                            let
                                                    origine = value,
                                                    n = value,
                                                    hasard = 0;
                                            
                                            while( n === origine ){
                                                    switch( n ){
                                                            case 1 :
                                                                    hasard =  Math.floor( Math.random() * 2 + 1 )
                                                                    switch( hasard ){
                                                                            case 1:
                                                                                    n = 2;
                                                                                    break;
                                                                            case 2 :
                                                                                    n = 4;
                                                                                    break;
                                                                    }
                                                                    break;
                                                            case 2 :
                                                                    hasard =  Math.floor( Math.random() * 3 + 1 )
                                                                    switch( hasard ){
                                                                            case 1:
                                                                                    n = 1;
                                                                                    break;
                                                                            case 2 :
                                                                                    n = 3;
                                                                                    break;
                                                                            case 3 :
                                                                                    n = 5;
                                                                                    break;
                                                                    }
                                                                    break;
                                                            case 3 :
                                                                    hasard =  Math.floor( Math.random() * 2 + 1 )
                                                                    switch( hasard ){
                                                                            case 1:
                                                                                    n = 2;
                                                                                    break;
                                                                            case 2 :
                                                                                    n = 6;
                                                                                    break;
                                                                    }
                                                                    break;
                                                            case 4 :
                                                                    hasard =  Math.floor( Math.random() * 3 + 1 )
                                                                    switch( hasard ){
                                                                            case 1:
                                                                                    n = 1;
                                                                                    break;
                                                                            case 2 :
                                                                                    n = 5;
                                                                                    break;
                                                                            case 3 :
                                                                                    n = 7;
                                                                                    break;
                                                                    }
                                                                    break;
                                                            case 5 :
                                                                    hasard =  Math.floor( Math.random() * 4 + 1 )
                                                                    switch( hasard ){
                                                                            case 1:
                                                                                    n = 2;
                                                                                    break;
                                                                            case 2 :
                                                                                    n = 4;
                                                                                    break;
                                                                            case 3 :
                                                                                    n = 6;
                                                                                    break;
                                                                            case 4 :
                                                                                    n = 8;
                                                                                    break;
                                                                    }
                                                                    break;
                                                            case 6 :
                                                                    hasard =  Math.floor( Math.random() * 3 + 1 )
                                                                    switch( hasard ){
                                                                            case 1:
                                                                                    n = 3;
                                                                                    break;
                                                                            case 2 :
                                                                                    n = 5;
                                                                                    break;
                                                                            case 3 :
                                                                                    n = 9;
                                                                                    break;
                                                                    }
                                                                    break;
                                                            case 7 :
                                                                    hasard =  Math.floor( Math.random() * 2 + 1 )
                                                                    switch( hasard ){
                                                                            case 1:
                                                                                    n = 4;
                                                                                    break;
                                                                            case 2 :
                                                                                    n = 8;
                                                                                    break;
                                                                    }
                                                                    break;
                                                            case 8 :
                                                                    hasard =  Math.floor( Math.random() * 3 + 1 )
                                                                    switch( hasard ){
                                                                            case 1:
                                                                                    n = 5;
                                                                                    break;
                                                                            case 2 :
                                                                                    n = 7;
                                                                                    break;
                                                                            case 3 :
                                                                                    n = 9;
                                                                                    break;
                                                                    }
                                                                    break;
                                                            case 9 :
                                                                    hasard =  Math.floor( Math.random() * 2 + 1 )
                                                                    switch( hasard ){
                                                                            case 1:
                                                                                    n = 6;
                                                                                    break;
                                                                            case 2 :
                                                                                    n = 8;
                                                                                    break;
                                                                    }
                                                                    break;
                                                    }
                                            }
                                            
                                            return n;
                                    }
                                    
                                    croco = deplace( croco );
                                    coloreLaCase( croco );
                            }, false );
                            
                            let croco = Math.floor( Math.random() * 9 + 1 );
                            
                            coloreLaCase( croco );
     
                            // fin code du test
                            
          kIDUnique();
        }, false );
      </script>
    </head>
    <body>
    	<main>
     
    		<table>
    			<tbody>
    				<tr>
    					<td id="case1">1</td>
    					<td id="case2">2</td>
    					<td id="case3">3</td>
    				</tr>
    				<tr>
    					<td id="case4">4</td>
    					<td id="case5">5</td>
    					<td id="case6">6</td>
    				</tr>
    				<tr>
    					<td id="case7">7</td>
    					<td id="case8">8</td>
    					<td id="case9">9</td>
    				</tr>
    			</tbody>
    		</table>
    		<button id="btnPlay">Jouer</button>
     
    	</main>
    </body>
    </html>

    Blog

    Sans l'analyse et la conception, la programmation est l'art d'ajouter des bogues à un fichier texte vide.
    (Louis Srygley : Without requirements or design, programming is the art of adding bugs to an empty text file.)

  3. #3
    Membre émérite
    Avatar de badaze
    Homme Profil pro
    Chef de projets info
    Inscrit en
    Septembre 2002
    Messages
    1 412
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Chef de projets info
    Secteur : Transports

    Informations forums :
    Inscription : Septembre 2002
    Messages : 1 412
    Points : 2 522
    Points
    2 522
    Par défaut
    Pour gérer un problème de ce genre mais sur une table de 10 x 10 pour un jeu de Dames. Je me suis borné à faire un tableau des positions possibles à partir d'une case donnée. Ensuite un simple contrôle d'existence de la case d'arrivée dans le tableau qui correspond à celle de départ suffit.
    Dans mon cas, j'ai un script php qui génère le tableau javascript mais dans le tien il ne doit pas y avoir beaucoup de combinaisons.

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
      var mapp = new Array();
                 mapp['C01'] = new Array('C01','C10','C12');
                 mapp['C03'] = new Array('C03','C12','C14');
                 mapp['C05'] = new Array('C05','C14','C16');
                 mapp['C07'] = new Array('C07','C16','C18');
                 mapp['C09'] = new Array('C09','C18');

    C01 => Ligne 0, colonne 1
    Cela ne sert à rien d'optimiser quelque chose qui ne fonctionne pas.

    Mon site : www.emmella.fr

    Je recherche le manuel de l'Olivetti Logos 80B.

  4. #4
    Futur Membre du Club
    Homme Profil pro
    Webdesigner
    Inscrit en
    Novembre 2017
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Novembre 2017
    Messages : 4
    Points : 5
    Points
    5
    Par défaut C'était précisément ça !
    @danielhagnoul : Vous aviez mis le doigt dessus (le switch). J'avais trouvé entre temps et corrigé. Forcément ça marche.
    Âne bâté que je suis. J'ai super honte !

    @badaze : Oui, c'est d'ailleurs précisément ce que j'aurais dû faire (et d'ailleurs je vais le faire !) mais voilà : Je code comme un cochon !

    Merci chaleureusement à tous deux. Bonne soirée.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Déplacement d'un pion sur un échiquier avec Qt 5
    Par Invité dans le forum Contribuez
    Réponses: 1
    Dernier message: 06/05/2013, 22h28
  2. image aléatoire avec math.random()
    Par aphroz dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 22/03/2010, 09h35
  3. Se déplacer sur une feuille avec "offset"
    Par Zak Blayde dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 23/11/2007, 10h41
  4. Réponses: 12
    Dernier message: 25/05/2007, 17h28
  5. vision d'un pion sur un damier.
    Par Med_Prog dans le forum Algorithmes et structures de données
    Réponses: 16
    Dernier message: 08/09/2005, 11h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo