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>