Check-in [599e56d2f2]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment: merged 2 files for random AI done by Tien
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:599e56d2f288e91fc53dfd70ae9c041e5406fb6e
User & Date: hhammadu 2016-12-02 03:33:34
Context
2016-12-02
09:39
setup created check-in: 2ff2c31880 user: hhammadu tags: trunk
03:33
merged 2 files for random AI done by Tien check-in: 599e56d2f2 user: hhammadu tags: trunk
2016-11-29
05:14
images of pieces check-in: 4d49120558 user: hhammadu tags: trunk
Changes

Added move_function.jl.







































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
module move_function
push!(LOAD_PATH,pwd())
export check_move
export each_dir_check_1
export each_dir_check_f
export loop_check_y
export loop_check_x
export loop_check_xy
export k_move
export q_move
export d_move
export h_move
export r_move
export b_move
export n_move
export x_move
export e_move
export t_move
export f_move
export g_move
export s_move
export c_move
export v_move
export m_move
export a_move
export l_move
export o_move
export p_move
export i_move
export chess_move
#-Moves
#coordinate right to left, top to bottom

#---------------Check Move Function---------------#
#check 8 directions for available move
#last 8 parameters are flags for each direction, 1 = free steps, 0 = 1 step
#return array of 8 direction array, each array contains[x,y,oppo_flag]
#oppo_flag = (x,y) belong to opponent
function check_move(x,y,board,f,b,r,l,lf,rf,lb,rb)
  piece_player = board[x,y].player
  re_array =  [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
  if f == 0 #check one step if empty or belong to opponent
    if piece_player == 1
      re_array[1] = each_dir_check_1(x,y-1,board,piece_player)
    else
      re_array[1] = each_dir_check_1(x,y+1,board,piece_player)
    end
  elseif f == 1
    re_array[1] = each_dir_check_f(x,y,board,1)
  elseif f == 2
    if piece_player == 1
      re_array[1] = each_dir_check_1(x,y-2,board,piece_player)
    else
      re_array[1] = each_dir_check_1(x,y+2,board,piece_player)
    end
  end
  if b == 0 #check one step if empty or belong to opponent
    if piece_player == 1
      re_array[2] = each_dir_check_1(x,y+1,board,piece_player)
    else
      re_array[2] = each_dir_check_1(x,y-1,board,piece_player)
    end
  elseif b == 1
    re_array[2] = each_dir_check_f(x,y,board,2)
  elseif b == 2
    if piece_player == 1
      re_array[2] = each_dir_check_1(x,y+2,board,piece_player)
    else
      re_array[2] = each_dir_check_1(x,y-2,board,piece_player)
    end
  end
  if r == 0 #check one step if empty or belong to opponent
    if piece_player == 1
      re_array[3] = each_dir_check_1(x-1,y,board,piece_player)
    else
      re_array[3] = each_dir_check_1(x+1,y,board,piece_player)
    end
  elseif r == 1
    re_array[3] = each_dir_check_f(x,y,board,3)
  elseif r == 2
    if piece_player == 1
      re_array[3] = each_dir_check_1(x-2,y,board,piece_player)
    else
      re_array[3] = each_dir_check_1(x+2,y,board,piece_player)
    end
  end
  if l == 0 #check one step if empty or belong to opponent
    if piece_player == 1
      re_array[4] = each_dir_check_1(x+1,y,board,piece_player)
    else
      re_array[4] = each_dir_check_1(x-1,y,board,piece_player)
    end
  elseif l == 1
    re_array[4] = each_dir_check_f(x,y,board,4)
  elseif l == 2
    if piece_player == 1
      re_array[4] = each_dir_check_1(x+2,y,board,piece_player)
    else
      re_array[4] = each_dir_check_1(x-2,y,board,piece_player)
    end
  end
  if lf == 0 #check one step if empty or belong to opponent
    if piece_player == 1
      re_array[5] = each_dir_check_1(x+1,y-1,board,piece_player)
    else
      re_array[5] = each_dir_check_1(x-1,y+1,board,piece_player)
    end
  elseif lf == 1
    re_array[5] = each_dir_check_f(x,y,board,5)
  elseif lf == 2
    if piece_player == 1
      re_array[5] = each_dir_check_1(x+2,y-2,board,piece_player)
    else
      re_array[5] = each_dir_check_1(x-2,y+2,board,piece_player)
    end
  end
  if rf == 0 #check one step if empty or belong to opponent
    if piece_player == 1
      re_array[6] = each_dir_check_1(x-1,y-1,board,piece_player)
    else
      re_array[6] = each_dir_check_1(x+1,y+1,board,piece_player)
    end
  elseif rf == 1
    re_array[6] = each_dir_check_f(x,y,board,6)
  elseif rf == 2
    if piece_player == 1
      re_array[6] = each_dir_check_1(x-2,y-2,board,piece_player)
    else
      re_array[6] = each_dir_check_1(x+2,y+2,board,piece_player)
    end
  end
  if lb == 0 #check one step if empty or belong to opponent
    if piece_player == 1
      re_array[7] = each_dir_check_1(x+1,y+1,board,piece_player)
    else
      re_array[7] = each_dir_check_1(x-1,y-1,board,piece_player)
    end
  elseif lb == 1
    re_array[7] = each_dir_check_f(x,y,board,7)
  elseif lb == 2
    if piece_player == 1
      re_array[7] = each_dir_check_1(x+2,y+2,board,piece_player)
    else
      re_array[7] = each_dir_check_1(x-2,y-2,board,piece_player)
    end
  end
  if rb == 0 #check one step if empty or belong to opponent
    if piece_player == 1
      re_array[8] = each_dir_check_1(x-1,y+1,board,piece_player)
    else
      re_array[8] = each_dir_check_1(x+1,y-1,board,piece_player)
    end
  elseif rb == 1 # rb == 1
    re_array[8] = each_dir_check_f(x,y,board,8)
  elseif rb == 2
    if piece_player == 1
      re_array[8] = each_dir_check_1(x-2,y+2,board,piece_player)
    else
      re_array[8] = each_dir_check_1(x+2,y-2,board,piece_player)
    end
  end
  return re_array
end #function check_move end
function each_dir_check_1(x,y,board,piece_player)
  gamesize = size(board,1)
  if x > gamesize || x < 1 || y > gamesize || y < 1
    return [0,0,0]
  end
  #return if piece is oppo's king
  if board[x,y].player != piece_player && lowercase(board[x,y].view) == 'k'
    return [-1,-1,-1]
  end
  if board[x,y].view == '.'
    return [x,y,0]
  elseif board[x,y].player != piece_player
    return [x,y,1]
  else
    return [0,0,0]
  end
end #function each_dir_check_1 end
function each_dir_check_f(x,y,board,dir)
  gamesize = size(board,1)
  piece_player = board[x,y].player
  if piece_player == 1
    if dir == 1 #front
      return loop_check_y(x,y-1,1,board,piece_player)
    elseif dir == 2 #back
      return loop_check_y(x,y+1,gamesize,board,piece_player)
    elseif dir == 3 #right
      return loop_check_x(y,x-1,1,board,piece_player)
    elseif dir == 4 #left
      return loop_check_x(y,x+1,gamesize,board,piece_player)
    end
  else #piece_player == 0
    if dir == 1 #front
      return loop_check_y(x,y+1,gamesize,board,piece_player)
    elseif dir == 2 #back
      return loop_check_y(x,y-1,1,board,piece_player)
    elseif dir == 3 #right
      return loop_check_x(y,x+1,gamesize,board,piece_player)
    elseif dir == 4 #left
      return loop_check_x(y,x-1,1,board,piece_player)
    end
  end
  if dir == 5 #left front
    return loop_check_xy(x,y,board,dir)
  elseif dir == 6 #right front
    return loop_check_xy(x,y,board,dir)
  elseif dir == 7 #left back
    return loop_check_xy(x,y,board,dir)
  elseif dir == 8 #right back
    return loop_check_xy(x,y,board,dir)
  end
end #function each_dir_check_f end
function loop_check_y(x,a,b,board,piece_player)
  gamesize = size(board,1)
  if a > gamesize || a < 1 || b > gamesize || b < 1
    return [0,0,0]
  end
  temp = [0,0,0]
  if a > b
    for i = a:-1:b
      #return if oppo's king in range
      if board[x,i].player != piece_player && lowercase(board[x,i].view) == 'k'
        return [-1,-1,-1]
      end
      if board[x,i].view == '.' #if space is empty move on
        temp = [x,i,0]
      elseif board[x,i].player != piece_player #if space is opponent's piece, return
        return [x,i,1]
      elseif board[x,i].player == piece_player #if space is own piece, return last empty space
        return temp
      else #if no available move
        return [0,0,0]
      end
    end
  else #a <=  b
    for i = a:b
      #return if oppo's king in range
      if board[x,i].player != piece_player && lowercase(board[x,i].view) == 'k'
        return [-1,-1,-1]
      end
      if board[x,i].view == '.' #if space is empty move on
        temp = [x,i,0]
      elseif board[x,i].player != piece_player #if space is opponent's piece, return
        return [x,i,1]
      elseif board[x,i].player == piece_player #if space is own piece, return last empty space
        return temp
      else #if no available move
        return [0,0,0]
      end
    end
  end
  return [0,0,0]
end #function loop_check_x end
function loop_check_x(y,a,b,board,piece_player)
  gamesize = size(board,1)
  if a > gamesize || a < 1 || b > gamesize || b < 1
    return [0,0,0]
  end
  temp = [0,0,0]
  if a > b
    for i = a:-1:b
      #return if oppo's king in range
      if board[i,y].player != piece_player && lowercase(board[i,y].view) == 'k'
        return [-1,-1,-1]
      end
      if board[i,y].view == '.' #if space is empty move on
        temp = [i,y,0]
      elseif board[i,y].player != piece_player #if space is opponent's piece, return
        return [i,y,1]
      elseif board[i,y].player == piece_player #if space is own piece, return last empty space
        return temp #if space is own piece, return last empty space
      else #if no available move
        return [0,0,0]
      end
    end
  else #a <= b
    for i = a:b
      #return if oppo's king in range
      if board[i,y].player != piece_player && lowercase(board[i,y].view) == 'k'
        return [-1,-1,-1]
      end
      if board[i,y].view == '.' #if space is empty move on
        temp = [i,y,0]
      elseif board[i,y].player != piece_player #if space is opponent's piece, return
        return [i,y,1]
      elseif board[i,y].player == piece_player #if space is own piece, return last empty space
        return temp #if space is own piece, return last empty space
      else #if no available move
        return [0,0,0]
      end
    end
  end
  return [0,0,0]
end #function loop_check_x end
function loop_check_xy(a,b,board,dir) #dir = [5,6,7,8] = [lf,rf,lb,rb]
  piece_player = board[a,b].player
  gamesize = size(board,1)
  if a > gamesize || a < 1 || b > gamesize || b < 1
    return [0,0,0]
  end
  if piece_player == 1
    if dir == 5
      x_inc = 1
      y_inc = -1
    elseif dir == 6
      x_inc = -1
      y_inc = -1
    elseif dir == 7
      x_inc = 1
      y_inc = 1
    elseif dir == 8
      x_inc = -1
      y_inc = 1
    end
  else
    if dir == 5
      x_inc = -1
      y_inc = 1
    elseif dir == 6
      x_inc = 1
      y_inc = 1
    elseif dir == 7
      x_inc = -1
      y_inc = -1
    elseif dir == 8
      x_inc = 1
      y_inc = -1
    end
  end
  a = a + x_inc
  b = b + y_inc
  temp = [0,0,0]
  while a <= gamesize && a > 0 && b <= gamesize && b > 0
    #return if oppo's king in range
    if board[a,b].player != piece_player && lowercase(board[a,b].view) == 'k'
      return [-1,-1,-1]
    end
    if board[a,b].view == '.' #if space is empty move on
      temp = [a,b,0]
    elseif board[a,b].player != piece_player #if space is opponent's piece, return
      return [a,b,1]
    elseif board[a,b].player == piece_player #if space is own piece, return last empty space
      return temp
    else #if no available move
      return [0,0,0]
    end
    a = a + x_inc
    b = b + y_inc
  end
  return [0,0,0]
end #function loop_check_xy end







#---------------Piece Move Function---------------#
#  -k: == t && p ///all directions 1 step (== p && t) No promotion
function k_move(x,y,board)
  return check_move(x,y,board,0,0,0,0,0,0,0,0)
end #function k_move end
#  -q: == r && b ///all directions free steps No promotiong
function q_move(x,y,board)
  return check_move(x,y,board,1,1,1,1,1,1,1,1)
end #function q_move end
#  -i: "A"/"B" sqares No promotion
function i_move(x,y,board)
 return [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
end
#  -d: == r && s  ///orthogonal free steps || diagonal 1 step
#  -D: all directions free steps except diagonal front with lion power
function d_move(x,y,board,promo)
  if promo == 1
    return check_move(x,y,board,1,1,1,1,-1,-1,1,1)
# !!!!!!!!!!!!!!!!!!!!!!!!!IMPLEMENTATION NEEDED!!!!!!!!!!!!!!!!!!!!!!!!!!!
  else #promo == 0
    return check_move(x,y,board,1,1,1,1,0,0,0,0)
  end #if promo end
end #function d_move end
#  -h: == b && g ///orthogonal 1 step || diagonal free steps
#  -H: all directions free steps except front with lion power
function h_move(x,y,board,promo)
  if promo == 1
    return check_move(x,y,board,-1,1,1,1,1,1,1,1)
# !!!!!!!!!!!!!!!!!!!!!!!!!IMPLEMENTATION NEEDED!!!!!!!!!!!!!!!!!!!!!!!!!!!
  else #promo == 0
    return check_move(x,y,board,0,0,0,0,1,1,1,1)
  end #if promo end
end #function h_move end
#  -r: orthogonal free steps
#  -R: == d
function r_move(x,y,board,promo)
  if promo == 1
    return d_move(x,y,board,0)
  else #promo == 0
    return check_move(x,y,board,1,1,1,1,-1,-1,-1,-1)
  end #if promo end
end #function r_move end
#  -b: diagonal free steps
#  -B: == h
function b_move(x,y,board,promo)
  if promo == 1
    return h_move(x,y,board,0)
  else #promo == 0
    return check_move(x,y,board,-1,-1,-1,-1,1,1,1,1)
  end #if promo end
end #function b_move end
#  -n: diagonal 1 step || orthogonal 2 steps
#  -N: == i
function n_move(x,y,board,promo)
  if promo == 1
    return i_move(x,y,board)
  else #promo == 0
    return check_move(x,y,board,2,2,2,2,0,0,0,0)
  end #if promo end
end #function n_move end
#  -x: orthogonal 1 step || diagonal 2 steps
#  -X: == q
function x_move(x,y,board,promo)
  if promo == 1
    return q_move(x,y,board)
  else #promo == 0
    return check_move(x,y,board,0,0,0,0,2,2,2,2)
  end #if promo end
end #function x_move end
#  -e: all directions 1 step except back
#  -E: == k
function e_move(x,y,board,promo)
  if promo == 1
    return k_move(x,y,board)
  else #promo == 0
    return check_move(x,y,board,0,-1,0,0,0,0,0,0)
  end #if promo end
end #function e_move end
#  -t: all directions 1 step except front
#  -T: == a && e ///all directions 1 step except front/back free steps
function t_move(x,y,board,promo)
  if promo == 1
    return check_move(x,y,board,1,1,0,0,0,0,0,0)
  else #promo == 0
    return check_move(x,y,board,-1,0,0,0,0,0,0,0)
  end #if promo end
end #function t_move end
#  -f: all directions 1 step except right/left
#  -F: == b
function f_move(x,y,board,promo)
  if promo == 1
    return b_move(x,y,board,0)
  else #promo == 0
    return check_move(x,y,board,0,0,-1,-1,0,0,0,0)
  end #if promo end
end #function t_move end
#  -g: all directions 1 step except diagonal back
#  -G: == r
function g_move(x,y,board,promo)
  if promo == 1
    return r_move(x,y,board,0)
  else #promo == 0
    return check_move(x,y,board,0,0,0,0,0,0,-1,-1)
  end #if promo end
end #function g_move end
#  -s: front 3 spaces || diagonal back
#  -S: == v
function s_move(x,y,board,promo)
  if promo == 1
    return v_move(x,y,board,0)
  else #promo == 0
    return check_move(x,y,board,0,-1,-1,-1,0,0,0,0)
  end #if promo end
end #function s_move end
#  -c: front 3 spaces || back
#  -C: == m
function c_move(x,y,board,promo)
  if promo == 1
    return m_move(x,y,board,0)
  else #promo == 0
    return check_move(x,y,board,0,0,-1,-1,0,0,-1,-1)
  end #if promo end
end #function c_move end
#  -v: left/right 1 step || front/back free steps
#  -V: == b && a ///all directions free except left/right
function v_move(x,y,board,promo)
  if promo == 1
    return check_move(x,y,board,1,1,-1,-1,1,1,1,1)
  else #promo == 0
    return check_move(x,y,board,1,1,0,0,-1,-1,-1,-1)
  end #if promo end
end #function v_move end
#  -m: front/back 1 step || left/right free steps
#  -M: all directions free except front/back
function m_move(x,y,board,promo)
  if promo == 1
    return check_move(x,y,board,-1,-1,1,1,1,1,1,1)
  else #promo == 0
    return check_move(x,y,board,0,0,1,1,-1,-1,-1,-1)
  end #if promo end
end #function m_move end
#  -a: front/back free steps
#  -A: front and back 3 spaces free steps
function a_move(x,y,board,promo)
  if promo == 1
    return check_move(x,y,board,1,1,-1,-1,-1,-1,1,1)
  else #promo == 0
    return check_move(x,y,board,1,1,-1,-1,-1,-1,-1,-1)
  end #if promo end
end #function a_move end
#  -l: front free steps
#  -L: back and front 3 spaces free steps
function l_move(x,y,board,promo)
  if promo == 1
    return check_move(x,y,board,1,1,-1,-1,1,1,-1,-1)
  else #promo == 0
    return check_move(x,y,board,1,-1,-1,-1,-1,-1,-1,-1)
  end #if promo end
end #function l_move end
#  -o: front/back 1 step
#  -O: == e
function o_move(x,y,board,promo)
  if promo == 1
    return e_move(x,y,board,0)
  else #promo == 0
    return check_move(x,y,board,0,0,-1,-1,-1,-1,-1,-1)
  end #if promo end
end #function o_move end
#  -p: front 1 step
#  -P: == g
function p_move(x,y,board,promo)
  if promo == 1
    return g_move(x,y,board,0)
  else #promo == 0
    return check_move(x,y,board,0,-1,-1,-1,-1,-1,-1,-1)
  end #if promo end
end #function p_move end

function chess_move(old_x,old_y,board)
  chess_type = lowercase(board[old_x,old_y].view)
  promo = board[old_x,old_y].promotion
  empty = 0
  if chess_type == 'k'
    m = k_move(old_x,old_y,board)
  elseif chess_type == 'q'
    m = q_move(old_x,old_y,board)
  elseif chess_type == 'd'
    m = d_move(old_x,old_y,board,promo)
  elseif chess_type == 'h'
    m = h_move(old_x,old_y,board,promo)
  elseif chess_type == 'r'
    m = r_move(old_x,old_y,board,promo)
  elseif chess_type == 'b'
    m = b_move(old_x,old_y,board,promo)
  elseif chess_type == 'n'
    m = n_move(old_x,old_y,board,promo)
  elseif chess_type == 'x'
    m = x_move(old_x,old_y,board,promo)
  elseif chess_type == 'e'
    m = e_move(old_x,old_y,board,promo)
  elseif chess_type == 't'
    m = t_move(old_x,old_y,board,promo)
  elseif chess_type == 'f'
    m = f_move(old_x,old_y,board,promo)
  elseif chess_type == 'g'
    m = g_move(old_x,old_y,board,promo)
  elseif chess_type == 's'
    m = s_move(old_x,old_y,board,promo)
  elseif chess_type == 'c'
    m = c_move(old_x,old_y,board,promo)
  elseif chess_type == 'v'
    m = v_move(old_x,old_y,board,promo)
  elseif chess_type == 'm'
    m = m_move(old_x,old_y,board,promo)
  elseif chess_type == 'a'
    m = a_move(old_x,old_y,board,promo)
  elseif chess_type == 'l'
    m = l_move(old_x,old_y,board,promo)
  elseif chess_type == 'o'
    m = o_move(old_x,old_y,board,promo)
  elseif chess_type == 'p'
    m = p_move(old_x,old_y,board,promo)
  elseif chess_type == 'i'
    m = i_move(old_x,old_y,board)
  else
    empty = 1
  end
  ava = [0,0]
  if empty == 0
    found = 0
    prior = [1,5,6,3,4,2,7,8]
    for i in prior
      if m[i][3] != 0 #opponent piece found, return
        return [m[i][1],m[i][2]]
      elseif m[i][1] != 0 && found == 0 #available empty space found, record
        ava = [m[i][1],m[i][2]]
        found = 1
      end
    end
  end
  if ava[1] == 0
    for i = 1:8
      if m[i][1] != 0
        ava = [m[i][1],m[i][2]]
      end
    end
  end
  return ava
end

end #module end

Changes to randomAI.jl.

1
2



















































3







4


























































































module randomAI
push!(LOAD_PATH,pwd())



















































println("random AI need to be done")







end




























































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
module randomAI
push!(LOAD_PATH,pwd())
using SQLite
using databases
using move_function
using board_function


#connect to database
db = SQLite.DB("database.db")

now_play = (Length_Of_Tables(db,"moves")+1)%2
#initialize the board
board = emptyBoard(db)
database_to_board(db,board)
gamesize = size(board,1)
s_x = 0
s_y = 0
t_x = 0
t_y = 0
if now_play == 1
  query = "SELECT * FROM Player WHERE player = 1 AND status = 1;"
else #now_play == 0
  query = "SELECT * FROM Player WHERE player = 0 AND status = 1;"
end
x=SQLite.query(db,query)

#first check if any piece can kill king
king_pos = find_king(board,now_play)
y_d = zeros(Int64,length(x[1]))
for i = 1:length(x[1])
  y_d[i] = i
end
luck = rand(1:length(y_d))
s_x = get(x[2][y_d[luck]])
s_y = get(x[3][y_d[luck]])
deleteat!(y_d,luck)
targetxy = chess_move(s_x,s_y,board)
while targetxy[1] != -1 && length(y_d) != 0
  luck = rand(1:length(y_d))
  s_x = get(x[2][y_d[luck]])
  s_y = get(x[3][y_d[luck]])
  deleteat!(y_d,luck)
  targetxy = chess_move(s_x,s_y,board)
end
if targetxy[1] == -1
  t_x = king_pos[1]
  t_y = king_pos[2]
  println("King targeted!!!")
end

#if king is not targeted random move
if t_x == 0
  #random AI
  luck = rand(1:2)
  #50% possibility of moving piece that is closest to king
  if luck == 1
    king_pos = find_king(board,now_play)
    y_d = zeros(Int64,length(x[1]))
    for i = 1:length(x[1])
      y_d[i] = abs(get(x[3][i])-king_pos[2]) #find pieces closest to king
    end
    close_king = findmin(y_d)
    s_x = get(x[2][close_king[2]])
    s_y = get(x[3][close_king[2]])
    deleteat!(y_d,close_king[2])
    targetxy = chess_move(s_x,s_y,board)
    while targetxy[1] == 0 && length(y_d) != 0#repeat if no available move
      close_king = findmin(y_d)
      s_x = get(x[2][close_king[2]])
      s_y = get(x[3][close_king[2]])
      deleteat!(y_d,close_king[2])
      targetxy = chess_move(s_x,s_y,board)
    end
  else #luck != 1
    y_d = zeros(Int64,length(x[1]))
    for i = 1:length(x[1])
      y_d[i] = i
    end
    luck = rand(1:length(y_d))
    s_x = get(x[2][y_d[luck]])
    s_y = get(x[3][y_d[luck]])
    deleteat!(y_d,luck)
    targetxy = chess_move(s_x,s_y,board)
    while targetxy[1] == 0 && length(y_d) != 0
      luck = rand(1:length(y_d))
      s_x = get(x[2][y_d[luck]])
      s_y = get(x[3][y_d[luck]])
      deleteat!(y_d,luck)
      targetxy = chess_move(s_x,s_y,board)
      println(length(y_d))
    end
  end
  if targetxy[1] != 0
    t_x = targetxy[1]
    t_y = targetxy[2]
  else
    println("No move available!")
  end
end

if t_x != 0
  println("\n Random move from ($s_x, $s_y) to ($t_x, $t_y)")
  #auto promote if land in promotion zone
  pro_poss_9 = ['s','n','l','b','r','p']
  pro_poss_12 = ['d','h','x','e','t','f','g','c','v','m','a','o','s','n','l','b','r','p']
  pro = 0
  if now_play == 0
    if gamesize == 9 && t_y > 6 && board[s_x,s_y].view in pro_poss_9
      pro = 1
    elseif gamesize == 5 && t_y == 5  && board[s_x,s_y].view in pro_poss_9
      pro = 1
    elseif gamesize == 12 && t_y > 8 && board[s_x,s_y].view in pro_poss_12
      pro = 1
    end
  else #now_play == 0
    if gamesize == 9 && t_y < 4 && board[s_x,s_y].view in pro_poss_9
      pro = 1
    elseif gamesize == 5 && t_y == 1  && board[s_x,s_y].view in pro_poss_9
      pro = 1
    elseif gamesize == 12 && t_y < 5 && board[s_x,s_y].view in pro_poss_12
      pro = 1
    end
  end

  #update player and moves table
  if pro == 1
    updateMoves(db,Length_Of_Tables(db,"moves")+1, "move" ,s_x, s_y, t_x, t_y, "!", 0, 0, 0)
    query = "UPDATE Player SET curr_x= $t_x ,curr_y= $t_y, promotion = $pro WHERE curr_x = $s_x AND curr_y = $s_y"
  else
    updateMoves(db,Length_Of_Tables(db,"moves")+1,"move",s_x,s_y,t_x,t_y,0,0,0,0)
    query = "UPDATE Player SET curr_x= $t_x ,curr_y= $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
  end
  SQLite.execute!(SQLite.Stmt(db,query))

  #if capture, update player
  if board[t_x,t_y].view != '.'
    if now_play == 1
      query = "UPDATE Player SET player = $now_play, status = 0, promotion = 0 WHERE curr_x = $t_x AND curr_y = $t_y AND player = 0"
    else
      query = "UPDATE Player SET player = $now_play, status = 0, promotion = 0 WHERE curr_x = $t_x AND curr_y = $t_y AND player = 1"
    end
    SQLite.execute!(SQLite.Stmt(db,query))
  end
end


#initialize and display the board
board = emptyBoard(db)
database_to_board(db,board)
displayCurr(board)
end