Check-in [43ad7c5a1d]

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

Overview
Comment:add move
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:43ad7c5a1d42a390fb181960e848ee62ec9a23ca
User & Date: tien 2016-11-29 00:04:17
Context
2016-11-29
00:10
add game Leaf check-in: a208ae1aed user: tien tags: trunk
00:04
add move check-in: 43ad7c5a1d user: tien tags: trunk
2016-11-28
23:45
changed databases for keeping track of buttons clicked check-in: ab64875b14 user: hhammadu tags: trunk
Changes

Added move.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
#=
move.jl <filename>
-update with a move
-move is made by whichever side currently has the move
-may resign
-never cheat
-[new] "deduct time from player"
=#
#possible AI: King in killing range
module move
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