Check-in [914c78945f]

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

Overview
Comment:infrastructure of game
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:914c78945f09042cfe277976f2bcf025ed4a6036
User & Date: hhammadu 2016-11-29 04:19:49
Context
2016-11-29
05:12
half files still need to be implemented, please try to go through the code play.jl so that you may know where to merege your files check-in: 61e704ab66 user: hhammadu tags: trunk
04:19
infrastructure of game check-in: 914c78945f user: hhammadu tags: trunk
01:29
game.jl check-in: f1abf65d52 user: hhammadu tags: trunk
Changes

Added HardAI.jl.









>
>
>
>
1
2
3
4
module HardAI
push!(LOAD_PATH,pwd())
println("hard AI need to be done")
end

Added NormalAI.jl.









>
>
>
>
1
2
3
4
module NormalAI
push!(LOAD_PATH,pwd())
println("Normal AI need to be done")
end

Added board_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
module board_function
push!(LOAD_PATH,pwd())
using SQLite
using databases
using GUI
export synBoard
export initializeBoard
export displayCurr
export chess
export emptyBoard
export database_to_board
export check_win
export find_king

type chess
  x_axis::Int8
  y_axis::Int8
  status::Bool # status 1 for live / 0 for captured
  promotion::Bool # promotion = promotion status; 1 for active promotion
  view::Char #view = chess' view by char
  player::Int8 # 1 for player 1 / 0 for player 2
end

function find_king(board,now_play)
  for i = 1:size(board,1)
    for j = 1:size(board,1)
      if now_play == 1
        if board[i,j].view == 'k' && board[i,j].player == 1
          return [i,j]
        end
      else #player 2
        if board[i,j].view == 'k' && board[i,j].player == 0
          return [i,j]
        end
      end
    end
  end
  return [0,0]
end

function synBoard(board,piece) # After every move, drop, and setup, please call this function for chess/board synchronizing
  board[piece.x_axis,piece.y_axis] = piece
end

function initializeBoard(gamesize) #initialize the board
  board = Matrix{chess}(gamesize,gamesize)
  for i in 1:gamesize
    for j in 1:gamesize
      board[i,j] = chess(i,j,0,0,'.',-1) # Setup a null chess for every cell on the board
    end
  end
  if gamesize == 5 # Setup all chesses to its initial postion
    synBoard(board, chess(5,1,1,0,'r',0))
    synBoard(board, chess(1,5,1,0,'r',1))
    synBoard(board, chess(4,1,1,0,'b',0))
    synBoard(board, chess(2,5,1,0,'b',1))
    synBoard(board, chess(3,1,1,0,'s',0))
    synBoard(board, chess(3,5,1,0,'s',1))
    synBoard(board, chess(2,1,1,0,'g',0))
    synBoard(board, chess(4,5,1,0,'g',1))
    synBoard(board, chess(1,1,1,0,'k',0))
    synBoard(board, chess(5,5,1,0,'k',1))
    synBoard(board, chess(1,2,1,0,'p',0))
    synBoard(board, chess(5,4,1,0,'p',1))
  elseif gamesize == 9
    synBoard(board, chess(1,1,1,0,'l',0))
    synBoard(board, chess(9,1,1,0,'l',0))
    synBoard(board, chess(1,9,1,0,'l',1))
    synBoard(board, chess(9,9,1,0,'l',1))
    synBoard(board, chess(2,1,1,0,'n',0))
    synBoard(board, chess(8,1,1,0,'n',0))
    synBoard(board, chess(2,9,1,0,'n',1))
    synBoard(board, chess(8,9,1,0,'n',1))
    synBoard(board, chess(3,1,1,0,'s',0))
    synBoard(board, chess(7,1,1,0,'s',0))
    synBoard(board, chess(3,9,1,0,'s',1))
    synBoard(board, chess(7,9,1,0,'s',1))
    synBoard(board, chess(4,1,1,0,'g',0))
    synBoard(board, chess(6,1,1,0,'g',0))
    synBoard(board, chess(4,9,1,0,'g',1))
    synBoard(board, chess(6,9,1,0,'g',1))
    synBoard(board, chess(5,1,1,0,'k',0))
    synBoard(board, chess(5,9,1,0,'k',1))
    synBoard(board, chess(8,2,1,0,'r',0))
    synBoard(board, chess(2,8,1,0,'r',1))
    synBoard(board, chess(2,2,1,0,'b',0))
    synBoard(board, chess(8,8,1,0,'b',1))
    for i=1:gamesize
      synBoard(board, chess(i,3,1,0,'p',0))
      synBoard(board, chess(i,7,1,0,'p',1))
    end
  else #gamesize == 12
    synBoard(board, chess(1,1,1,0,'l',0))
    synBoard(board, chess(12,1,1,0,'l',0))
    synBoard(board, chess(1,12,1,0,'l',1))
    synBoard(board, chess(12,12,1,0,'l',1))
    synBoard(board, chess(2,1,1,0,'f',0))
    synBoard(board, chess(11,1,1,0,'f',0))
    synBoard(board, chess(2,12,1,0,'f',1))
    synBoard(board, chess(11,12,1,0,'f',1))
    synBoard(board, chess(3,1,1,0,'c',0))
    synBoard(board, chess(10,1,1,0,'c',0))
    synBoard(board, chess(3,12,1,0,'c',1))
    synBoard(board, chess(10,12,1,0,'c',1))
    synBoard(board, chess(4,1,1,0,'s',0))
    synBoard(board, chess(9,1,1,0,'s',0))
    synBoard(board, chess(4,12,1,0,'s',1))
    synBoard(board, chess(9,12,1,0,'s',1))
    synBoard(board, chess(5,1,1,0,'g',0))
    synBoard(board, chess(8,1,1,0,'g',0))
    synBoard(board, chess(5,12,1,0,'g',1))
    synBoard(board, chess(8,12,1,0,'g',1))
    synBoard(board, chess(6,1,1,0,'k',0))
    synBoard(board, chess(7,12,1,0,'k',1))
    synBoard(board, chess(7,1,1,0,'e',0))
    synBoard(board, chess(6,12,1,0,'e',1))
    synBoard(board, chess(1,2,1,0,'a',0))
    synBoard(board, chess(12,2,1,0,'a',0))
    synBoard(board, chess(1,11,1,0,'a',1))
    synBoard(board, chess(12,11,1,0,'a',1))
    synBoard(board, chess(3,2,1,0,'b',0))
    synBoard(board, chess(10,2,1,0,'b',0))
    synBoard(board, chess(3,11,1,0,'b',1))
    synBoard(board, chess(10,11,1,0,'b',1))
    synBoard(board, chess(5,2,1,0,'t',0))
    synBoard(board, chess(8,2,1,0,'t',0))
    synBoard(board, chess(5,11,1,0,'t',1))
    synBoard(board, chess(8,11,1,0,'t',1))
    synBoard(board, chess(1,3,1,0,'m',0))
    synBoard(board, chess(12,3,1,0,'m',0))
    synBoard(board, chess(1,10,1,0,'m',1))
    synBoard(board, chess(12,10,1,0,'m',1))
    synBoard(board, chess(7,2,1,0,'x',0))
    synBoard(board, chess(6,11,1,0,'x',1))
    synBoard(board, chess(6,2,1,0,'n',0))
    synBoard(board, chess(7,11,1,0,'n',1))
    synBoard(board, chess(2,3,1,0,'v',0))
    synBoard(board, chess(11,3,1,0,'v',0))
    synBoard(board, chess(2,10,1,0,'v',1))
    synBoard(board, chess(11,10,1,0,'v',1))
    synBoard(board, chess(3,3,1,0,'r',0))
    synBoard(board, chess(10,3,1,0,'r',0))
    synBoard(board, chess(3,10,1,0,'r',1))
    synBoard(board, chess(10,10,1,0,'r',1))
    synBoard(board, chess(4,3,1,0,'h',0))
    synBoard(board, chess(9,3,1,0,'h',0))
    synBoard(board, chess(4,10,1,0,'h',1))
    synBoard(board, chess(9,10,1,0,'h',1))
    synBoard(board, chess(5,3,1,0,'d',0))
    synBoard(board, chess(8,3,1,0,'d',0))
    synBoard(board, chess(5,10,1,0,'d',1))
    synBoard(board, chess(8,10,1,0,'d',1))
    synBoard(board, chess(6,3,1,0,'i',0))
    synBoard(board, chess(7,10,1,0,'i',1))
    synBoard(board, chess(7,3,1,0,'q',0))
    synBoard(board, chess(6,10,1,0,'q',1))
    synBoard(board, chess(4,5,1,0,'o',0))
    synBoard(board, chess(9,5,1,0,'o',0))
    synBoard(board, chess(4,8,1,0,'o',1))
    synBoard(board, chess(9,8,1,0,'o',1))
    for i=1:gamesize
      synBoard(board, chess(i,4,1,0,'p',0))
      synBoard(board, chess(i,9,1,0,'p',1))
    end
  end
  return board
end

function displayCurr1(board)
  gamesize = size(board,1)
  c = Set_board(gamesize)
  for j = 1:gamesize
	for i = 1: gamesize
		put_piece(gamesize,board[j,i],c)
	end
  end
end
function displayCurr(board)
  gamesize = size(board,1)
  print("\n")
  print(repeat("-",gamesize*2+1))
  print("\n")
  for j = 1:gamesize
    print("|")
    for i = gamesize:-1:1
      if board[i,j].view == 'k'
        print_with_color(:yellow,string(board[i,j].view))
      elseif board[i,j].player == 0 && board[i,j].view !='.'
        print_with_color(:blue,string(board[i,j].view))
      elseif board[i,j].player == 1
        print_with_color(:red,string(board[i,j].view))
      else
        print_with_color(:bold,string(board[i,j].view))
      end
      print("|")
    end
    print(" $j\n")
  end
  print(repeat("-",gamesize*2+1))
  print("\n")
  if gamesize == 5 || gamesize == 9
    print(" ")
  end
  for i = gamesize:-1:1
    print(i)
    print(" ")
  end
  print("\n")
end



function database_to_board(db,board)
  query = "SELECT * FROM Player WHERE player = 0;"
  table=SQLite.query(db,query)
  for i = 1:size(table,1)
    player = get(table[1][i])
    x = get(table[2][i])
    y = get(table[3][i])
    status = get(table[4][i])
    promo = get(table[5][i])
    piece = get(table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),0)
      else
        board[x,y] = chess(x,y,status,promo,piece,0)
      end
    end
  end
  query = "SELECT * FROM Player WHERE player = 1;"
  table=SQLite.query(db,query)
  for i = 1:size(table,1)
    player = get(table[1][i])
    x = get(table[2][i])
    y = get(table[3][i])
    status = get(table[4][i])
    promo = get(table[5][i])
    piece = get(table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),1)
      else
        board[x,y] = chess(x,y,status,promo,piece,1)
      end
    end
  end
end

function emptyBoard(db)
  meta_row = Access_By_key(db,"type")
  if meta_row[2] == "standard"
    gamesize = 9
  elseif meta_row[2] == "minishoghi"
    gamesize = 5
  else
    gamesize = 12
  end

  board = Matrix{chess}(gamesize,gamesize)
  for i in 1:gamesize
    for j in 1:gamesize
      board[i,j] = chess(i,j,0,0,'.',-1) # Setup a null chess for every cell on the board
    end
  end
  return board
end

function check_win(db)
	result = 0
	query1 = "SELECT * FROM Player WHERE player = 1 AND piece = 'k';"
	k1_exists=SQLite.query(db,query1)
	l1=size(k1_exists)[1]
	if l1 == 0
	    result = 1
		updateScore(db,0,1)
	end

	query2 = "SELECT * FROM Player WHERE player = 0 AND piece = 'k';"
	k2_exists=SQLite.query(db,query2)
	l2=size(k2_exists)[1]
	if l2 == 0
		result = 1
		updateScore(db,1,0)
	end
	if timed(db) == 1
		gote_t = get_time(db,"gote_time")
		sente_t = get_time(db,"sente_time")
		if gote_t <= 0
			updateScore(db,0,1)
			result = 1
		elseif sente_t <=0 
			updateScore(db,1,0)
			result = 1
		end
	end

	return result
end

end #module function

Changes to game.jl.

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
...
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
...
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
...
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
				g[1,16] = Turn
				idt = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module start")
					destroy(win)
				end
				
				setproperty!(g, :row_spacing, 15)  # introduce a 15-pixel gap between columns
				setproperty!(g, :column_homogeneous, true) # setproperty!(g,:homogeoneous,true) for gtk2
				setproperty!(g, :column_spacing, 15)  # introduce a 15-pixel gap between columns
				push!(win, g)
................................................................................
				id22 = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module start")
					destroy(win)
				end
				showall(win)
			end

			idJ = signal_connect(Join, "clicked") do widget
				update_gameval(db,"join")
................................................................................
				id22 = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module start")
					destroy(win)
				end
				showall(win)
				
			end
		
			idha = signal_connect(Host_AI, "clicked") do widget
................................................................................
				g[1,16] = Turn
				idt = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module start")
					destroy(win)
				end
				
				setproperty!(g, :row_spacing, 15)  # introduce a 15-pixel gap between columns
				setproperty!(g, :column_homogeneous, true) # setproperty!(g,:homogeoneous,true) for gtk2
				setproperty!(g, :column_spacing, 15)  # introduce a 15-pixel gap between columns
				push!(win, g)
................................................................................
				id22 = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module start")
					destroy(win)
				end
				showall(win)
			end

			id6 = signal_connect(Host_Email, "clicked") do widget
				update_gameval(db,"host_email")
................................................................................
				id22 = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module start")
					destroy(win)
				end
				showall(win)
			end
		end
		








|







 







|







 







|







 







|







 







|







 







|







146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
...
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
...
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
...
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
				g[1,16] = Turn
				idt = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module play")
					destroy(win)
				end
				
				setproperty!(g, :row_spacing, 15)  # introduce a 15-pixel gap between columns
				setproperty!(g, :column_homogeneous, true) # setproperty!(g,:homogeoneous,true) for gtk2
				setproperty!(g, :column_spacing, 15)  # introduce a 15-pixel gap between columns
				push!(win, g)
................................................................................
				id22 = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module play")
					destroy(win)
				end
				showall(win)
			end

			idJ = signal_connect(Join, "clicked") do widget
				update_gameval(db,"join")
................................................................................
				id22 = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module play")
					destroy(win)
				end
				showall(win)
				
			end
		
			idha = signal_connect(Host_AI, "clicked") do widget
................................................................................
				g[1,16] = Turn
				idt = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module play")
					destroy(win)
				end
				
				setproperty!(g, :row_spacing, 15)  # introduce a 15-pixel gap between columns
				setproperty!(g, :column_homogeneous, true) # setproperty!(g,:homogeoneous,true) for gtk2
				setproperty!(g, :column_spacing, 15)  # introduce a 15-pixel gap between columns
				push!(win, g)
................................................................................
				id22 = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module play")
					destroy(win)
				end
				showall(win)
			end

			id6 = signal_connect(Host_Email, "clicked") do widget
				update_gameval(db,"host_email")
................................................................................
				id22 = signal_connect(Turn, "toggled") do widget
					update_gameval(db,"firstturn")
				end
				
				StartGamebutton = @Button("Start Game Now")
				g[1:5,17] = StartGamebutton
				id71 = signal_connect(StartGamebutton, "clicked") do widget
					println("call the module play")
					destroy(win)
				end
				showall(win)
			end
		end
		

Added play.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
module play
push!(LOAD_PATH,pwd())
using SQLite
using databases
db = SQLite.DB("database.db")

x = get_optval(db,"start")
if x == 1
	Create_Table(db,"Score")
	x1 = get_optval(db,"AI")
	if x1 == 1
		include("startAI.jl")
    end
	x2 = get_optval(db,"human")
	if x2 == 1
		include("starthuman.jl")
	end
	x3 = get_optval(db,"join")
	if  x3 == 1
		include("startjoin.jl")
	end
	x4 = get_optval(db,"host_AI")
	if x4 == 1
		include("starthost_AI.jl")
	end
	x5 = get_optval(db,"host_human")
	if x5 == 1
		include("starthost_human.jl")
	end
	x6 = get_optval(db,"host_email")
	if x6 == 1
		include("starthost_email.jl")
	end
end

end

Added protecteddeathAI.jl.









>
>
>
>
1
2
3
4
module protecteddeathAI
push!(LOAD_PATH,pwd())
println("protecteddeath AI need to be done")
end

Added randomAI.jl.









>
>
>
>
1
2
3
4
module randomAI
push!(LOAD_PATH,pwd())
println("random AI need to be done")
end

Added startAI.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
module startAI
push!(LOAD_PATH,pwd())
using SQLite
using databases
using board_function
gamesize = 0
######################################################################################################################################################################################
db = SQLite.DB("database.db")
function Decide_Type()
  minishoghi = get_optval(db,"minishoghi")
  stshoghi = get_optval(db,"stshoghi")
  chshoghi = get_optval(db,"chshoghi")
  if minishoghi == 1
	A2 = "M"
	global gamesize = 5
  end
  if stshoghi == 1
	A2 = "S"
	global gamesize = 9
  end
  if chshoghi == 1
	A2 = "C"
	global gamesize = 12
  end
  return A2
end

function Decide_Cheat()
  cheat = get_optval(db, "cheat")
  if cheat == 1
	A3 = "T"
  else
	A3 = "N"
  end
  return A3
end

function Timed()
	timed = get_optval(db, "timed")
	if timed == 1
		A4 = "timed"
	else
		A4 = "n"
	end
end

function Game_Setup()
  
  A2 = Decide_Type()
  A3 = Decide_Cheat()
  A4 = Timed()
  Result = [A2,A3,A4]
  return Result
end

Result = Game_Setup()
A2 = Result[1]
A3 = Result[2]
A4 = Result[3]



Create_Table(db,"meta")
Create_Table(db,"moves")
Create_Table(db,"Player")
updateMeta(db,A2,A3,A4,9999999999,9999999999)
####################################################################################################################################################################################


  #initialize the board
  board = initializeBoard(gamesize)

  #set up Player table
 if gamesize == 5
                  
	updatePlayer(db,1,1,5,1,0,"r")
    updatePlayer(db,0,5,1,1,0,"r")
    updatePlayer(db,0,4,1,1,0,"b") 
    updatePlayer(db,1,2,5,1,0,"b")
    updatePlayer(db,0,3,1,1,0,"s")
    updatePlayer(db,1,3,5,1,0,"s")
    updatePlayer(db,0,2,1,1,0,"g")
    updatePlayer(db,1,4,5,1,0,"g")
    updatePlayer(db,0,1,1,1,0,"k")
    updatePlayer(db,1,5,5,1,0,"k")
    updatePlayer(db,0,1,2,1,0,"p")
    updatePlayer(db,1,5,4,1,0,"p")
	                                  
  
  elseif gamesize == 9
    updatePlayer(db,0,1,1,1,0,"l")
	updatePlayer(db,0,9,1,1,0,"l")
	updatePlayer(db,1,1,9,1,0,"l")
	updatePlayer(db,1,9,9,1,0,"l")
	updatePlayer(db,0,2,1,1,0,"n")
	updatePlayer(db,0,8,1,1,0,"n")
	updatePlayer(db,1,2,9,1,0,"n")
	updatePlayer(db,1,8,9,1,0,"n")
	updatePlayer(db,0,3,1,1,0,"s")
	updatePlayer(db,0,7,1,1,0,"s")
	updatePlayer(db,1,3,9,1,0,"s")
	updatePlayer(db,1,7,9,1,0,"s")
	updatePlayer(db,0,4,1,1,0,"g")
	updatePlayer(db,0,6,1,1,0,"g")
	updatePlayer(db,1,4,9,1,0,"g")
	updatePlayer(db,1,6,9,1,0,"g")
	updatePlayer(db,0,5,1,1,0,"k")
	updatePlayer(db,1,5,9,1,0,"k")
	updatePlayer(db,0,8,2,1,0,"r")
	updatePlayer(db,1,2,8,1,0,'r')
	updatePlayer(db,0,2,2,1,0,"b")
	updatePlayer(db,1,8,8,1,0,"b")
	for i=1:9
		updatePlayer(db,0,i,3,1,0,"p")
		updatePlayer(db,1,i,7,1,0,"p")
	end
    else 
		updatePlayer(db,0,1,1,1,0,"l")
		updatePlayer(db,0,12,1,1,0,"l")
		updatePlayer(db,1,1,12,1,0,"l")
		updatePlayer(db,1,12,12,1,0,"l")
		updatePlayer(db,0,2,1,1,0,"f")
		updatePlayer(db,0,11,1,1,0,"f")
		updatePlayer(db,1,2,12,1,0,"f")
		updatePlayer(db,1,11,12,1,0,"f")
		updatePlayer(db,0,3,1,1,0,"c")
		updatePlayer(db,0,10,1,1,0,"c")
		updatePlayer(db,1,3,12,1,0,"c")
		updatePlayer(db,1,10,12,1,0,"c")
		updatePlayer(db,0,4,1,1,0,"s")
		updatePlayer(db,0,9,1,1,0,"s")
		updatePlayer(db,1,4,12,1,0,"s")
		updatePlayer(db,1,9,12,1,0,"s")
		updatePlayer(db,0,5,1,1,0,"g")
		updatePlayer(db,0,8,1,1,0,"g")
		updatePlayer(db,1,5,12,1,0,"g")
		updatePlayer(db,1,8,12,1,0,"g")
		updatePlayer(db,0,6,1,1,0,"k")
		updatePlayer(db,1,7,12,1,0,"k")
		updatePlayer(db,0,7,1,1,0,"e")
		updatePlayer(db,1,6,12,1,0,"e")
		updatePlayer(db,0,1,2,1,0,"a")
		updatePlayer(db,0,12,2,1,0,"a")
		updatePlayer(db,1,1,11,1,0,"a")
		updatePlayer(db,1,12,11,1,0,"a")
		updatePlayer(db,0,3,2,1,0,"b")
		updatePlayer(db,0,10,2,1,0,"b")
		updatePlayer(db,1,3,11,1,0,"b")
		updatePlayer(db,1,10,11,1,0,"b")
		updatePlayer(db,0,5,2,1,0,"t")
		updatePlayer(db,0,8,2,1,0,"t")
		updatePlayer(db,1,5,11,1,0,"t")
		updatePlayer(db,1,8,11,1,0,"t")
		updatePlayer(db,0,7,2,1,0,"x")
		updatePlayer(db,1,6,11,1,0,"x")
		updatePlayer(db,0,6,2,1,0,"n")
		updatePlayer(db,1,7,11,1,0,"n")
		updatePlayer(db,0,1,3,1,0,"m")
		updatePlayer(db,0,12,3,1,0,"m")
		updatePlayer(db,1,1,10,1,0,"m")
		updatePlayer(db,1,12,10,1,0,"m")
		updatePlayer(db,0,2,3,1,0,"v")
		updatePlayer(db,0,11,3,1,0,"v")
		updatePlayer(db,1,2,10,1,0,"v")
		updatePlayer(db,1,11,10,1,0,"v")
		updatePlayer(db,0,3,3,1,0,"r")
		updatePlayer(db,0,10,3,1,0,"r")
		updatePlayer(db,1,3,10,1,0,"r")
		updatePlayer(db,1,10,10,1,0,"r")
		updatePlayer(db,0,4,3,1,0,"h")
		updatePlayer(db,0,9,3,1,0,"h")
		updatePlayer(db,1,4,10,1,0,"h")
		updatePlayer(db,1,9,10,1,0,"h")
		updatePlayer(db,0,5,3,1,0,"d")
		updatePlayer(db,0,8,3,1,0,"d")
		updatePlayer(db,1,5,10,1,0,"d")
		updatePlayer(db,1,8,10,1,0,"d")
		updatePlayer(db,0,6,3,1,0,"i")
		updatePlayer(db,1,7,10,1,0,"i")
		updatePlayer(db,0,7,3,1,0,"q")
		updatePlayer(db,1,6,10,1,0,"q")
		updatePlayer(db,0,4,5,1,0,"o")
		updatePlayer(db,0,9,5,1,0,"o")
		updatePlayer(db,1,4,8,1,0,"o")
		updatePlayer(db,1,9,8,1,0,"o")
		for i=1:12
			updatePlayer(db,0,i,4,1,0,"p")
			updatePlayer(db,1,i,9,1,0,"p")
		end
		
end

displayCurr(board)
	
normal = get_optval(db, "normal")
hard = get_optval(db,"hard")
suicidal = get_optval(db,"suicidal")
protecteddeath = get_optval(db, "protecteddeath")
randomAI = get_optval(db, "randomAI")

if normal == 1
	include("user_move_NAI.jl")
elseif hard == 1
	include("user_move_HAI.jl")
elseif suicidal == 1
	include("user_move_SAI.jl")
elseif protecteddeath == 1
	include("user_move_PAI.jl")
else
	include("user_move_RAI.jl")
end



end

Added suicidalAI.jl.









>
>
>
>
1
2
3
4
module suicidalAI
push!(LOAD_PATH,pwd())
println("suicidal AI need to be done")
end

Added user_move_HAI.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
#=
move_user_move.jl <filename> <xsource> <ysource> <xtarget> <ytarget> <promote>
-moves the piece at the source into the target area
-if promote is ”T”, promote the piece after completed the move
-do not attempt to validate the move
=#
module user_move_HAI
push!(LOAD_PATH,pwd())
using SQLite
using board_function

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

#######################################################################################################################################################################################
#sets the board according to the given database
function database_to_board(board)
  query1 = "SELECT * FROM Player WHERE player = 1;"
  p1_table=SQLite.query(db,query1)
  l1=size(p1_table)[1]
  query2 = "SELECT * FROM Player WHERE player = 0;"
  p2_table=SQLite.query(db,query2)
  l2=size(p2_table)[1]
  for i = 1:l1
    x = get(p1_table[2][i])
    y = get(p1_table[3][i])
    status = get(p1_table[4][i])
    promo = get(p1_table[5][i])
    piece = get(p1_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),1)
      else
        board[x,y] = chess(x,y,status,promo,piece,1)
      end
	end
  end

  for i = 1:l2
    x = get(p2_table[2][i])
    y = get(p2_table[3][i])
    status = get(p2_table[4][i])
    promo = get(p2_table[5][i])
    piece = get(p2_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),0)
      else
        board[x,y] = chess(x,y,status,promo,piece,0)
      end
    end
 end
end

#######################################################################################################################################################################################




########################################################################################################################################################################################

#check if piece exist at source x y and set up board

function Take_inputs(db,gamesize,now_play)
  t=time()
  if timed(db) == 1
	  update_time(db,t,"time_add")
	  if now_play == 1
		update_time(db,t,"gote_time")
		println("Time left for player 1:")
		println(get_time(db,"gote_time"))
	  else
		update_time(db,t,"sente_time")
		println("Time left for player 2:")
		println(get_time(db,"sente_time"))
	  end
	  print("\n")
  end
  results = check_win(db)
  if results == 1 
	println("\nGame Ended!\n")
	include("game.jl")
  end
  print("Please enter source x: ")
  s_x = readline(STDIN)
  s_x=parse(s_x)
  print("Please enter source y: ")
  s_y = readline(STDIN)
  s_y=parse(s_y)
  print("Please enter target x: ")
  t_x = readline(STDIN)
  t_x=parse(t_x)
  print("Please enter target y: ")
  t_y = readline(STDIN)
  t_y=parse(t_y)

  print("Please enter promote or not after move, 'T' if yes: ")
  p = readline(STDIN)
  if p[end]=='\n'
   p = p[1:end-1]
  end
  if p == "T"
    p = 1
  else
    p = 0
  end

  if board[s_x,s_y].view == '.'
    println("Error: There is no chess piece on the source position!")
	println("Enter valid input")
	quit()
  end

  if board[s_x,s_y].view != '.' && board[s_x,s_y].player != now_play
    println("Error: You are moving another player's piece")
	println("Enter valid input")
	quit()
  end

  if board[t_x,t_y].player == now_play
	println(board[t_x,t_y].view)
	println("Error: You are killing your own player")
	println("Enter valid input")
	println(now_play)
	quit()
  end
  t_x2=0
  t_y2=0

  if gamesize == 12
	print("Please enter target x 2:")
	t_x2 = readline(STDIN)
	t_x2 = parse(t_x2)
	if typeof(t_x2) == Void
		t_x2 = 0
	end
	print("Please enter target y 2:")
	t_y2 = readline(STDIN)
	t_y2 = parse(t_y2)
	if typeof(t_y2) == Void
		t_y2 = 0
	end
	if t_x2 != 0 && t_y2!=0
		if board[t_x2,t_y2].player == now_play
			println("Error: You are killing your own player")
			println("Enter valid input")
			println(now_play)
			quit()
		end
	end
  end
  return [s_x,s_y,t_x,t_y,t_x2,t_y2, p]
end

################################################################################################################################################################################

now_play = (Length_Of_Tables(db,"moves")+1)%2

board = emptyBoard(db)
gamesize = size(board,1)

database_to_board(board)

x_in = Take_inputs(db,gamesize,now_play)
s_x=x_in[1]
s_y=x_in[2]
t_x=x_in[3]
t_y=x_in[4]
t_x2=x_in[5]
t_y2=x_in[6]
pro=x_in[7]

println("now playing $now_play")

if gamesize == 5
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
elseif gamesize == 9
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
else
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
	if t_x2 !=0 && t_y2!=0
		if board[t_x2,t_y2].view != '.' && board[t_x2,t_y2].player !=now_play
			query="DELETE FROM Player WHERE curr_x= $t_x2 AND curr_y = $t_y2;"
			stmt=SQLite.Stmt(db,query)
			SQLite.execute!(stmt)
			na = board[t_x2,t_y2].view
			updatePlayer(db,now_play,0, 0, 0, 0, na)
		end

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

board_new = Matrix{chess}(gamesize,gamesize)
for i in 1:gamesize
  for j in 1:gamesize
    board_new[i,j] = chess(i,j,0,0,'.',-1) # Setup a null chess for every cell on the board
  end
end

database_to_board(board_new)
displayCurr(board_new)
x = 1
while true
	if x % 2 == 1
		include("HardAI.jl")
		x =x+1
	else
		include("user_move_HAI.jl")
		x = x+1
	end
end
	

end #module end

Added user_move_NAI.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
#=
move_user_move.jl <filename> <xsource> <ysource> <xtarget> <ytarget> <promote>
-moves the piece at the source into the target area
-if promote is ”T”, promote the piece after completed the move
-do not attempt to validate the move
=#
module user_move_NAI
push!(LOAD_PATH,pwd())
using SQLite
using board_function

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

#######################################################################################################################################################################################
#sets the board according to the given database
function database_to_board(board)
  query1 = "SELECT * FROM Player WHERE player = 1;"
  p1_table=SQLite.query(db,query1)
  l1=size(p1_table)[1]
  query2 = "SELECT * FROM Player WHERE player = 0;"
  p2_table=SQLite.query(db,query2)
  l2=size(p2_table)[1]
  for i = 1:l1
    x = get(p1_table[2][i])
    y = get(p1_table[3][i])
    status = get(p1_table[4][i])
    promo = get(p1_table[5][i])
    piece = get(p1_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),1)
      else
        board[x,y] = chess(x,y,status,promo,piece,1)
      end
	end
  end

  for i = 1:l2
    x = get(p2_table[2][i])
    y = get(p2_table[3][i])
    status = get(p2_table[4][i])
    promo = get(p2_table[5][i])
    piece = get(p2_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),0)
      else
        board[x,y] = chess(x,y,status,promo,piece,0)
      end
    end
 end
end

#######################################################################################################################################################################################




########################################################################################################################################################################################

#check if piece exist at source x y and set up board

function Take_inputs(db,gamesize,now_play)
  t=time()
  if timed(db) == 1
	  update_time(db,t,"time_add")
	  if now_play == 1
		update_time(db,t,"gote_time")
		println("Time left for player 1:")
		println(get_time(db,"gote_time"))
	  else
		update_time(db,t,"sente_time")
		println("Time left for player 2:")
		println(get_time(db,"sente_time"))
	  end
	  print("\n")
  end
  results = check_win(db)
  if results == 1 
	println("\nGame Ended!\n")
	include("game.jl")
  end
  print("Please enter source x: ")
  s_x = readline(STDIN)
  s_x=parse(s_x)
  print("Please enter source y: ")
  s_y = readline(STDIN)
  s_y=parse(s_y)
  print("Please enter target x: ")
  t_x = readline(STDIN)
  t_x=parse(t_x)
  print("Please enter target y: ")
  t_y = readline(STDIN)
  t_y=parse(t_y)

  print("Please enter promote or not after move, 'T' if yes: ")
  p = readline(STDIN)
  if p[end]=='\n'
   p = p[1:end-1]
  end
  if p == "T"
    p = 1
  else
    p = 0
  end

  if board[s_x,s_y].view == '.'
    println("Error: There is no chess piece on the source position!")
	println("Enter valid input")
	quit()
  end

  if board[s_x,s_y].view != '.' && board[s_x,s_y].player != now_play
    println("Error: You are moving another player's piece")
	println("Enter valid input")
	quit()
  end

  if board[t_x,t_y].player == now_play
	println(board[t_x,t_y].view)
	println("Error: You are killing your own player")
	println("Enter valid input")
	println(now_play)
	quit()
  end
  t_x2=0
  t_y2=0

  if gamesize == 12
	print("Please enter target x 2:")
	t_x2 = readline(STDIN)
	t_x2 = parse(t_x2)
	if typeof(t_x2) == Void
		t_x2 = 0
	end
	print("Please enter target y 2:")
	t_y2 = readline(STDIN)
	t_y2 = parse(t_y2)
	if typeof(t_y2) == Void
		t_y2 = 0
	end
	if t_x2 != 0 && t_y2!=0
		if board[t_x2,t_y2].player == now_play
			println("Error: You are killing your own player")
			println("Enter valid input")
			println(now_play)
			quit()
		end
	end
  end
  return [s_x,s_y,t_x,t_y,t_x2,t_y2, p]
end

################################################################################################################################################################################

now_play = (Length_Of_Tables(db,"moves")+1)%2

board = emptyBoard(db)
gamesize = size(board,1)

database_to_board(board)

x_in = Take_inputs(db,gamesize,now_play)
s_x=x_in[1]
s_y=x_in[2]
t_x=x_in[3]
t_y=x_in[4]
t_x2=x_in[5]
t_y2=x_in[6]
pro=x_in[7]

println("now playing $now_play")

if gamesize == 5
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
elseif gamesize == 9
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
else
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
	if t_x2 !=0 && t_y2!=0
		if board[t_x2,t_y2].view != '.' && board[t_x2,t_y2].player !=now_play
			query="DELETE FROM Player WHERE curr_x= $t_x2 AND curr_y = $t_y2;"
			stmt=SQLite.Stmt(db,query)
			SQLite.execute!(stmt)
			na = board[t_x2,t_y2].view
			updatePlayer(db,now_play,0, 0, 0, 0, na)
		end

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

board_new = Matrix{chess}(gamesize,gamesize)
for i in 1:gamesize
  for j in 1:gamesize
    board_new[i,j] = chess(i,j,0,0,'.',-1) # Setup a null chess for every cell on the board
  end
end

database_to_board(board_new)
displayCurr(board_new)

x = 1
while true
	if x % 2 == 1
		include("NormalAI.jl")
		x =x+1
	else
		include("user_move_NAI.jl")
		x = x+1
	end
end
	

end #module end

Added user_move_PAI.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
#=
move_user_move.jl <filename> <xsource> <ysource> <xtarget> <ytarget> <promote>
-moves the piece at the source into the target area
-if promote is ”T”, promote the piece after completed the move
-do not attempt to validate the move
=#
module user_move_PAI
push!(LOAD_PATH,pwd())
using SQLite
using board_function

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

#######################################################################################################################################################################################
#sets the board according to the given database
function database_to_board(board)
  query1 = "SELECT * FROM Player WHERE player = 1;"
  p1_table=SQLite.query(db,query1)
  l1=size(p1_table)[1]
  query2 = "SELECT * FROM Player WHERE player = 0;"
  p2_table=SQLite.query(db,query2)
  l2=size(p2_table)[1]
  for i = 1:l1
    x = get(p1_table[2][i])
    y = get(p1_table[3][i])
    status = get(p1_table[4][i])
    promo = get(p1_table[5][i])
    piece = get(p1_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),1)
      else
        board[x,y] = chess(x,y,status,promo,piece,1)
      end
	end
  end

  for i = 1:l2
    x = get(p2_table[2][i])
    y = get(p2_table[3][i])
    status = get(p2_table[4][i])
    promo = get(p2_table[5][i])
    piece = get(p2_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),0)
      else
        board[x,y] = chess(x,y,status,promo,piece,0)
      end
    end
 end
end

#######################################################################################################################################################################################




########################################################################################################################################################################################

#check if piece exist at source x y and set up board

function Take_inputs(db,gamesize,now_play)
  t=time()
  if timed(db) == 1
	  update_time(db,t,"time_add")
	  if now_play == 1
		update_time(db,t,"gote_time")
		println("Time left for player 1:")
		println(get_time(db,"gote_time"))
	  else
		update_time(db,t,"sente_time")
		println("Time left for player 2:")
		println(get_time(db,"sente_time"))
	  end
	  print("\n")
  end
  results = check_win(db)
  if results == 1 
	println("\nGame Ended!\n")
	include("game.jl")
  end
  print("Please enter source x: ")
  s_x = readline(STDIN)
  s_x=parse(s_x)
  print("Please enter source y: ")
  s_y = readline(STDIN)
  s_y=parse(s_y)
  print("Please enter target x: ")
  t_x = readline(STDIN)
  t_x=parse(t_x)
  print("Please enter target y: ")
  t_y = readline(STDIN)
  t_y=parse(t_y)

  print("Please enter promote or not after move, 'T' if yes: ")
  p = readline(STDIN)
  if p[end]=='\n'
   p = p[1:end-1]
  end
  if p == "T"
    p = 1
  else
    p = 0
  end

  if board[s_x,s_y].view == '.'
    println("Error: There is no chess piece on the source position!")
	println("Enter valid input")
	quit()
  end

  if board[s_x,s_y].view != '.' && board[s_x,s_y].player != now_play
    println("Error: You are moving another player's piece")
	println("Enter valid input")
	quit()
  end

  if board[t_x,t_y].player == now_play
	println(board[t_x,t_y].view)
	println("Error: You are killing your own player")
	println("Enter valid input")
	println(now_play)
	quit()
  end
  t_x2=0
  t_y2=0

  if gamesize == 12
	print("Please enter target x 2:")
	t_x2 = readline(STDIN)
	t_x2 = parse(t_x2)
	if typeof(t_x2) == Void
		t_x2 = 0
	end
	print("Please enter target y 2:")
	t_y2 = readline(STDIN)
	t_y2 = parse(t_y2)
	if typeof(t_y2) == Void
		t_y2 = 0
	end
	if t_x2 != 0 && t_y2!=0
		if board[t_x2,t_y2].player == now_play
			println("Error: You are killing your own player")
			println("Enter valid input")
			println(now_play)
			quit()
		end
	end
  end
  return [s_x,s_y,t_x,t_y,t_x2,t_y2, p]
end

################################################################################################################################################################################

now_play = (Length_Of_Tables(db,"moves")+1)%2

board = emptyBoard(db)
gamesize = size(board,1)

database_to_board(board)

x_in = Take_inputs(db,gamesize,now_play)
s_x=x_in[1]
s_y=x_in[2]
t_x=x_in[3]
t_y=x_in[4]
t_x2=x_in[5]
t_y2=x_in[6]
pro=x_in[7]

println("now playing $now_play")

if gamesize == 5
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
elseif gamesize == 9
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
else
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
	if t_x2 !=0 && t_y2!=0
		if board[t_x2,t_y2].view != '.' && board[t_x2,t_y2].player !=now_play
			query="DELETE FROM Player WHERE curr_x= $t_x2 AND curr_y = $t_y2;"
			stmt=SQLite.Stmt(db,query)
			SQLite.execute!(stmt)
			na = board[t_x2,t_y2].view
			updatePlayer(db,now_play,0, 0, 0, 0, na)
		end

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

board_new = Matrix{chess}(gamesize,gamesize)
for i in 1:gamesize
  for j in 1:gamesize
    board_new[i,j] = chess(i,j,0,0,'.',-1) # Setup a null chess for every cell on the board
  end
end

database_to_board(board_new)
displayCurr(board_new)
x = 1
while true
	if x % 2 == 1
		include("protecteddeathAI.jl")
		x =x+1
	else
		include("user_move_PAI.jl")
		x = x+1
	end
end
	

end #module end

Added user_move_RAI.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
#=
move_user_move.jl <filename> <xsource> <ysource> <xtarget> <ytarget> <promote>
-moves the piece at the source into the target area
-if promote is ”T”, promote the piece after completed the move
-do not attempt to validate the move
=#
module user_move_RAI
push!(LOAD_PATH,pwd())
using SQLite
using board_function

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

#######################################################################################################################################################################################
#sets the board according to the given database
function database_to_board(board)
  query1 = "SELECT * FROM Player WHERE player = 1;"
  p1_table=SQLite.query(db,query1)
  l1=size(p1_table)[1]
  query2 = "SELECT * FROM Player WHERE player = 0;"
  p2_table=SQLite.query(db,query2)
  l2=size(p2_table)[1]
  for i = 1:l1
    x = get(p1_table[2][i])
    y = get(p1_table[3][i])
    status = get(p1_table[4][i])
    promo = get(p1_table[5][i])
    piece = get(p1_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),1)
      else
        board[x,y] = chess(x,y,status,promo,piece,1)
      end
	end
  end

  for i = 1:l2
    x = get(p2_table[2][i])
    y = get(p2_table[3][i])
    status = get(p2_table[4][i])
    promo = get(p2_table[5][i])
    piece = get(p2_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),0)
      else
        board[x,y] = chess(x,y,status,promo,piece,0)
      end
    end
 end
end

#######################################################################################################################################################################################




########################################################################################################################################################################################

#check if piece exist at source x y and set up board

function Take_inputs(db,gamesize,now_play)
  t=time()
  if timed(db) == 1
	  update_time(db,t,"time_add")
	  if now_play == 1
		update_time(db,t,"gote_time")
		println("Time left for player 1:")
		println(get_time(db,"gote_time"))
	  else
		update_time(db,t,"sente_time")
		println("Time left for player 2:")
		println(get_time(db,"sente_time"))
	  end
	  print("\n")
  end
  results = check_win(db)
  if results == 1 
	println("\nGame Ended!\n")
	include("game.jl")
  end
  print("Please enter source x: ")
  s_x = readline(STDIN)
  s_x=parse(s_x)
  print("Please enter source y: ")
  s_y = readline(STDIN)
  s_y=parse(s_y)
  print("Please enter target x: ")
  t_x = readline(STDIN)
  t_x=parse(t_x)
  print("Please enter target y: ")
  t_y = readline(STDIN)
  t_y=parse(t_y)

  print("Please enter promote or not after move, 'T' if yes: ")
  p = readline(STDIN)
  if p[end]=='\n'
   p = p[1:end-1]
  end
  if p == "T"
    p = 1
  else
    p = 0
  end

  if board[s_x,s_y].view == '.'
    println("Error: There is no chess piece on the source position!")
	println("Enter valid input")
	quit()
  end

  if board[s_x,s_y].view != '.' && board[s_x,s_y].player != now_play
    println("Error: You are moving another player's piece")
	println("Enter valid input")
	quit()
  end

  if board[t_x,t_y].player == now_play
	println(board[t_x,t_y].view)
	println("Error: You are killing your own player")
	println("Enter valid input")
	println(now_play)
	quit()
  end
  t_x2=0
  t_y2=0

  if gamesize == 12
	print("Please enter target x 2:")
	t_x2 = readline(STDIN)
	t_x2 = parse(t_x2)
	if typeof(t_x2) == Void
		t_x2 = 0
	end
	print("Please enter target y 2:")
	t_y2 = readline(STDIN)
	t_y2 = parse(t_y2)
	if typeof(t_y2) == Void
		t_y2 = 0
	end
	if t_x2 != 0 && t_y2!=0
		if board[t_x2,t_y2].player == now_play
			println("Error: You are killing your own player")
			println("Enter valid input")
			println(now_play)
			quit()
		end
	end
  end
  return [s_x,s_y,t_x,t_y,t_x2,t_y2, p]
end

################################################################################################################################################################################

now_play = (Length_Of_Tables(db,"moves")+1)%2

board = emptyBoard(db)
gamesize = size(board,1)

database_to_board(board)

x_in = Take_inputs(db,gamesize,now_play)
s_x=x_in[1]
s_y=x_in[2]
t_x=x_in[3]
t_y=x_in[4]
t_x2=x_in[5]
t_y2=x_in[6]
pro=x_in[7]

println("now playing $now_play")

if gamesize == 5
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
elseif gamesize == 9
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
else
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
	if t_x2 !=0 && t_y2!=0
		if board[t_x2,t_y2].view != '.' && board[t_x2,t_y2].player !=now_play
			query="DELETE FROM Player WHERE curr_x= $t_x2 AND curr_y = $t_y2;"
			stmt=SQLite.Stmt(db,query)
			SQLite.execute!(stmt)
			na = board[t_x2,t_y2].view
			updatePlayer(db,now_play,0, 0, 0, 0, na)
		end

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

board_new = Matrix{chess}(gamesize,gamesize)
for i in 1:gamesize
  for j in 1:gamesize
    board_new[i,j] = chess(i,j,0,0,'.',-1) # Setup a null chess for every cell on the board
  end
end

database_to_board(board_new)
displayCurr(board_new)
x = 1
while true
	if x % 2 == 1
		include("randomAI.jl")
		x =x+1
	else
		include("user_move_RAI.jl")
		x = x+1
	end
end
	

end #module end

Added user_move_SAI.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
#=
move_user_move.jl <filename> <xsource> <ysource> <xtarget> <ytarget> <promote>
-moves the piece at the source into the target area
-if promote is ”T”, promote the piece after completed the move
-do not attempt to validate the move
=#
module user_move_SAI
push!(LOAD_PATH,pwd())
using SQLite
using board_function

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

#######################################################################################################################################################################################
#sets the board according to the given database
function database_to_board(board)
  query1 = "SELECT * FROM Player WHERE player = 1;"
  p1_table=SQLite.query(db,query1)
  l1=size(p1_table)[1]
  query2 = "SELECT * FROM Player WHERE player = 0;"
  p2_table=SQLite.query(db,query2)
  l2=size(p2_table)[1]
  for i = 1:l1
    x = get(p1_table[2][i])
    y = get(p1_table[3][i])
    status = get(p1_table[4][i])
    promo = get(p1_table[5][i])
    piece = get(p1_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),1)
      else
        board[x,y] = chess(x,y,status,promo,piece,1)
      end
	end
  end

  for i = 1:l2
    x = get(p2_table[2][i])
    y = get(p2_table[3][i])
    status = get(p2_table[4][i])
    promo = get(p2_table[5][i])
    piece = get(p2_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),0)
      else
        board[x,y] = chess(x,y,status,promo,piece,0)
      end
    end
 end
end

#######################################################################################################################################################################################




########################################################################################################################################################################################

#check if piece exist at source x y and set up board

function Take_inputs(db,gamesize,now_play)
  t=time()
  if timed(db) == 1
	  update_time(db,t,"time_add")
	  if now_play == 1
		update_time(db,t,"gote_time")
		println("Time left for player 1:")
		println(get_time(db,"gote_time"))
	  else
		update_time(db,t,"sente_time")
		println("Time left for player 2:")
		println(get_time(db,"sente_time"))
	  end
	  print("\n")
  end
  results = check_win(db)
  if results == 1 
	println("\nGame Ended!\n")
	include("game.jl")
  end
  print("Please enter source x: ")
  s_x = readline(STDIN)
  s_x=parse(s_x)
  print("Please enter source y: ")
  s_y = readline(STDIN)
  s_y=parse(s_y)
  print("Please enter target x: ")
  t_x = readline(STDIN)
  t_x=parse(t_x)
  print("Please enter target y: ")
  t_y = readline(STDIN)
  t_y=parse(t_y)

  print("Please enter promote or not after move, 'T' if yes: ")
  p = readline(STDIN)
  if p[end]=='\n'
   p = p[1:end-1]
  end
  if p == "T"
    p = 1
  else
    p = 0
  end

  if board[s_x,s_y].view == '.'
    println("Error: There is no chess piece on the source position!")
	println("Enter valid input")
	quit()
  end

  if board[s_x,s_y].view != '.' && board[s_x,s_y].player != now_play
    println("Error: You are moving another player's piece")
	println("Enter valid input")
	quit()
  end

  if board[t_x,t_y].player == now_play
	println(board[t_x,t_y].view)
	println("Error: You are killing your own player")
	println("Enter valid input")
	println(now_play)
	quit()
  end
  t_x2=0
  t_y2=0

  if gamesize == 12
	print("Please enter target x 2:")
	t_x2 = readline(STDIN)
	t_x2 = parse(t_x2)
	if typeof(t_x2) == Void
		t_x2 = 0
	end
	print("Please enter target y 2:")
	t_y2 = readline(STDIN)
	t_y2 = parse(t_y2)
	if typeof(t_y2) == Void
		t_y2 = 0
	end
	if t_x2 != 0 && t_y2!=0
		if board[t_x2,t_y2].player == now_play
			println("Error: You are killing your own player")
			println("Enter valid input")
			println(now_play)
			quit()
		end
	end
  end
  return [s_x,s_y,t_x,t_y,t_x2,t_y2, p]
end

################################################################################################################################################################################

now_play = (Length_Of_Tables(db,"moves")+1)%2

board = emptyBoard(db)
gamesize = size(board,1)

database_to_board(board)

x_in = Take_inputs(db,gamesize,now_play)
s_x=x_in[1]
s_y=x_in[2]
t_x=x_in[3]
t_y=x_in[4]
t_x2=x_in[5]
t_y2=x_in[6]
pro=x_in[7]

println("now playing $now_play")

if gamesize == 5
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
elseif gamesize == 9
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
else
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
	if t_x2 !=0 && t_y2!=0
		if board[t_x2,t_y2].view != '.' && board[t_x2,t_y2].player !=now_play
			query="DELETE FROM Player WHERE curr_x= $t_x2 AND curr_y = $t_y2;"
			stmt=SQLite.Stmt(db,query)
			SQLite.execute!(stmt)
			na = board[t_x2,t_y2].view
			updatePlayer(db,now_play,0, 0, 0, 0, na)
		end

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

board_new = Matrix{chess}(gamesize,gamesize)
for i in 1:gamesize
  for j in 1:gamesize
    board_new[i,j] = chess(i,j,0,0,'.',-1) # Setup a null chess for every cell on the board
  end
end

database_to_board(board_new)
displayCurr(board_new)
x = 1
while true
	if x % 2 == 1
		include("suicidalAI.jl")
		x =x+1
	else
		include("user_move_SAI.jl")
		x = x+1
	end
end
	

end #module end