Bonjour à tous,

Voici la feuille de mon classeur Excel :

Pièce jointe 303797

et voici maintenant le bout de code VBA (à ajouter au classeur), que je souhaite partager avec vous :

Code : 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
 
 
Option Explicit
'
Sub Fox_Trot()
  '
  Dim iNiT As Integer
  Dim i As Integer
  Dim j As Integer
  Dim k As Integer
  Dim x As Integer
  Dim y As Integer
  Dim z As Integer
  Dim x_iNiT As Integer
  Dim y_iNiT As Integer
  Dim a_iNiT As Integer
  Dim b_iNiT As Integer
  Dim ok As Integer
  Dim Fox(11, 11) As Integer
  '
  Randomize
  '
  iNiT = 4
  '
  For i = 1 To 10
    For j = 1 To 10
      Call Plot(i, j, "") ' cls...
    Next j
  Next i
  '
  For i = 0 To 11
    For j = 0 To 11
      Fox(i, j) = 0 ' raz...
    Next j
  Next i
  '
  For i = 1 To 4
    For j = 1 To (5 - iNiT)
      Do
        ok = 123
        x_iNiT = Int(Rnd * 10) + 1
        y_iNiT = Int(Rnd * 10) + 1
        '
        If Int(Rnd * 2) + 1 = 1 Then
          a_iNiT = iNiT
          b_iNiT = 1
        Else
          a_iNiT = 1
          b_iNiT = iNiT
        End If
        '
        For x = (x_iNiT - 1) To (x_iNiT + a_iNiT)
          For y = (y_iNiT - 1) To (y_iNiT + b_iNiT)
            If (x < 12) And (y < 12) Then
              If Fox(x, y) <> 0 Then ok = 0
            Else
              ok = 0
            End If
          Next y
        Next x
        '
      Loop Until ok = 123
       '
       For x = x_iNiT To (x_iNiT + a_iNiT - 1)
         For y = y_iNiT To (y_iNiT + b_iNiT - 1)
           Fox(x, y) = 1
           Call Plot(x, y, "O")
         Next y
       Next x
       '
    Next j
    '
    iNiT = iNiT - 1
    '
  Next i
  '
  MsgBox "iNiT ok..."
  '
  ' --------------------------------------------------------------------------------------------------------
  '
  Dim ordi As Integer
  Dim joueur As Integer
  '
 
  '
  ' Fox(?;?)=0 -> vide
  ' Fox(?;?)=1 -> boat
  ' Fox(?;?)=2 -> coché
  ' Fox(?;?)=3 -> touché
  '
  Dim Look As Integer
  Dim Look_Nb_Xy As Integer
  Dim Look_Xy(4, 2) As Integer
  Dim Look_Fox(4) As Integer
  Dim Look_Max As Integer
  '
  ordi = 20
  joueur = 20
  Look = 1
  '
  For i = 1 To 4
    Look_Fox(i) = 0
  Next i
  '
  Do
    '
    Select Case Look
    Case 1 ' ------------------------------------------------------------------
      Do
        ok = 123
        x = Int(Rnd * 10) + 1
        y = Int(Rnd * 10) + 1
        If Fox(x, y) > 1 Then ok = 0
        For i = (x - 1) To (x + 1)
          For j = (y - 1) To (y + 1)
            If Fox(i, j) = 3 Then ok = 0
          Next j
        Next i
      Loop Until ok = 123
      '
      If Fox(x, y) = 0 Then
        Fox(x, y) = 2
        Call Plot(x, y, "X")
      Else
        Fox(x, y) = 3
        Fox(x - 1, y - 1) = 2: Fox(x - 1, y + 1) = 2
        Fox(x + 1, y + 1) = 2: Fox(x + 1, y - 1) = 2
        '
        Call Plot(x, y, "#")
        '
        x_iNiT = x
        y_iNiT = y
        '
        ordi = ordi - 1
        '
        Look_Nb_Xy = 0
        '
        If x > 1 Then
          If Fox(x - 1, y) < 2 Then
            Look_Nb_Xy = 1
            Look_Xy(Look_Nb_Xy, 0) = 3
            Look_Xy(Look_Nb_Xy, 1) = x - 1
            Look_Xy(Look_Nb_Xy, 2) = y
          End If
        End If
        '
        If y > 1 Then
          If Fox(x, y - 1) < 2 Then
            Look_Nb_Xy = Look_Nb_Xy + 1
            Look_Xy(Look_Nb_Xy, 0) = 4
            Look_Xy(Look_Nb_Xy, 1) = x
            Look_Xy(Look_Nb_Xy, 2) = y - 1
          End If
        End If
        '
        If x < 10 Then
          If Fox(x + 1, y) < 2 Then
            Look_Nb_Xy = Look_Nb_Xy + 1
            Look_Xy(Look_Nb_Xy, 0) = 3
            Look_Xy(Look_Nb_Xy, 1) = x + 1
            Look_Xy(Look_Nb_Xy, 2) = y
          End If
        End If
        '
        If y < 10 Then
          If Fox(x, y + 1) < 2 Then
            Look_Nb_Xy = Look_Nb_Xy + 1
            Look_Xy(Look_Nb_Xy, 0) = 4
            Look_Xy(Look_Nb_Xy, 1) = x
            Look_Xy(Look_Nb_Xy, 2) = y + 1
          End If
        End If
        '
        If Look_Nb_Xy > 1 Then
          For z = 1 To Int(Rnd * 1234) + 123
            i = Int(Rnd * Look_Nb_Xy) + 1
            j = Int(Rnd * Look_Nb_Xy) + 1
            '
            k = Look_Xy(i, 0): Look_Xy(i, 0) = Look_Xy(j, 0): Look_Xy(j, 0) = k
            k = Look_Xy(i, 1): Look_Xy(i, 1) = Look_Xy(j, 1): Look_Xy(j, 1) = k
            k = Look_Xy(i, 2): Look_Xy(i, 2) = Look_Xy(j, 2): Look_Xy(j, 2) = k
          Next z
        End If
        '
        Look = 2
        '
        If (Look_Nb_Xy = 0) Or (Look_Fox(4) = 1 And Look_Fox(3) = 2 And Look_Fox(2) = 3) Then Look = 1
        '
      End If
      '
      MsgBox "je joue en : " + Chr(y + 64) + CStr(x)
      '
    Case 2 ' ------------------------------------------------------------------
      '
      x = Look_Xy(Look_Nb_Xy, 1)
      y = Look_Xy(Look_Nb_Xy, 2)
      '
      Look_Nb_Xy = Look_Nb_Xy - 1
      '
      If Look_Nb_Xy = 0 Then Look = 1
      '
      If Fox(x, y) = 0 Then
        Fox(x, y) = 2
        Call Plot(x, y, "X")
      Else
        Fox(x, y) = 3
        Fox(x - 1, y - 1) = 2: Fox(x - 1, y + 1) = 2
        Fox(x + 1, y + 1) = 2: Fox(x + 1, y - 1) = 2
        Call Plot(x, y, "#")
        ordi = ordi - 1
        Look_Max = 2
        '
        If Look_Fox(4) = 1 And Look_Fox(3) = 2 Then
          Look = 1
          Look_Fox(2) = Look_Fox(2) + 1
        Else
          Look = Look_Xy(Look_Nb_Xy + 1, 0)
        End If
      End If
      '
      MsgBox "je joue en : " + Chr(y + 64) + CStr(x)
      '
    Case 3 ' ------------------------------------------------------------------
      '
      x = x_iNiT
      y = y_iNiT
      '
      Look_Nb_Xy = 0
      '
      If x > 1 Then
        ok = 123
        For i = x To 1 Step -1
          If Fox(i, y) = 2 Then ok = 0
          If Fox(i, y) < 2 And ok = 123 Then
            ok = 0
            Look_Nb_Xy = 1
            Look_Xy(Look_Nb_Xy, 1) = i
            Look_Xy(Look_Nb_Xy, 2) = y
          End If
        Next i
      End If
      '
      If x < 10 Then
        ok = 123
        For i = x To 10
          If Fox(i, y) = 2 Then ok = 0
          If Fox(i, y) < 2 And ok = 123 Then
            ok = 0
            Look_Nb_Xy = Look_Nb_Xy + 1
            Look_Xy(Look_Nb_Xy, 1) = i
            Look_Xy(Look_Nb_Xy, 2) = y
          End If
        Next i
      End If
      '
      If Look_Nb_Xy = 0 Then
        Look = 1
        If Look_Max = 2 Then Look_Fox(2) = Look_Fox(2) + 1
        If Look_Max = 3 Then Look_Fox(3) = Look_Fox(3) + 1
        MsgBox "le boat est coulé..."
      Else
        Look_Nb_Xy = Int(Rnd * Look_Nb_Xy) + 1
        x = Look_Xy(Look_Nb_Xy, 1)
        y = Look_Xy(Look_Nb_Xy, 2)
        '
        If Fox(x, y) = 0 Then
          Fox(x, y) = 2
          Call Plot(x, y, "X")
          MsgBox "je joue en : " + Chr(y + 64) + CStr(x)
        Else
          Fox(x, y) = 3
          Fox(x - 1, y - 1) = 2: Fox(x - 1, y + 1) = 2
          Fox(x + 1, y + 1) = 2: Fox(x + 1, y - 1) = 2
          Call Plot(x, y, "#")
          ordi = ordi - 1
          Look_Max = Look_Max + 1
          '
          If Look_Max = 4 Then
            Look = 1
            Look_Fox(4) = 1
          End If
          '
          If Look_Max = 3 And Look_Fox(4) = 1 Then
            Look = 1
            Look_Fox(3) = Look_Fox(3) + 1
          End If
          '
          MsgBox "je joue en : " + Chr(y + 64) + CStr(x)
          '
        End If
      End If
      '
    Case 4 ' ------------------------------------------------------------------
      '
      x = x_iNiT
      y = y_iNiT
      '
      Look_Nb_Xy = 0
      '
      If y > 1 Then
        ok = 123
        For i = y To 1 Step -1
          If Fox(x, i) = 2 Then ok = 0
          If Fox(x, i) < 2 And ok = 123 Then
            ok = 0
            Look_Nb_Xy = 1
            Look_Xy(Look_Nb_Xy, 1) = x
            Look_Xy(Look_Nb_Xy, 2) = i
          End If
        Next i
      End If
      '
      If y < 10 Then
        ok = 123
        For i = y To 10
          If Fox(x, i) = 2 Then ok = 0
          If Fox(x, i) < 2 And ok = 123 Then
            ok = 0
            Look_Nb_Xy = Look_Nb_Xy + 1
            Look_Xy(Look_Nb_Xy, 1) = x
            Look_Xy(Look_Nb_Xy, 2) = i
          End If
        Next i
      End If
      '
      If Look_Nb_Xy = 0 Then
        Look = 1
        If Look_Max = 2 Then Look_Fox(2) = Look_Fox(2) + 1
        If Look_Max = 3 Then Look_Fox(3) = Look_Fox(3) + 1
        MsgBox "le boat est coulé..."
      Else
        Look_Nb_Xy = Int(Rnd * Look_Nb_Xy) + 1
        x = Look_Xy(Look_Nb_Xy, 1)
        y = Look_Xy(Look_Nb_Xy, 2)
        '
        If Fox(x, y) = 0 Then
          Fox(x, y) = 2
          Call Plot(x, y, "X")
          MsgBox "je joue en : " + Chr(y + 64) + CStr(x)
        Else
          Fox(x, y) = 3
          Fox(x - 1, y - 1) = 2: Fox(x - 1, y + 1) = 2
          Fox(x + 1, y + 1) = 2: Fox(x + 1, y - 1) = 2
          Call Plot(x, y, "#")
          ordi = ordi - 1
          Look_Max = Look_Max + 1
          '
          If Look_Max = 4 Then
            Look = 1
            Look_Fox(4) = 1
          End If
          '
          If Look_Max = 3 And Look_Fox(4) = 1 Then
            Look = 1
            Look_Fox(3) = Look_Fox(3) + 1
          End If
          '
          MsgBox "je joue en : " + Chr(y + 64) + CStr(x)
          '
        End If
      End If
      '
    End Select ' ------------------------------------------------------------------
    '
  Loop Until ordi = 0
  '
  MsgBox "c'est la fin..."
  '
  ' ---   Jean-Christophe Elies ---
  '
End Sub
 
Sub Plot(x As Integer, y As Integer, c As String)
  '
  Cells(y + 4, x + 2).Value = c
  '
End Sub
maintenant, il ne reste plus qu'à lancer la procédure "Fox Trot",

le bout de code va tenter de trouver et couler nos bateaux . . .

vous pouvez me donner vos remarques, et critiques . . .

merci . . .

Jean-Christophe . . .