-
Notifications
You must be signed in to change notification settings - Fork 1
/
display_step.jl
254 lines (233 loc) · 6.45 KB
/
display_step.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
#=display.jl - will replay the game
from start to finish, displaying the current game state.
Accepts 1 command line argument,<filename> => database
=#
#include("square.jl")
include("dParse.jl")
include("board.jl")
using BM
#include("start.jl")
using SQLite
pathToDatabase = ARGS[1]
db = SQLite.DB(pathToDatabase) #Opens the database gamefile
#global board = ST.loadBoard()
#calculates all black piece positions
res = SQLite.query(db,"""SELECT "value" FROM meta WHERE "key" = 'type';""")
gameType = get(res[1,1])
#=----Gets last move number----=#
res = SQLite.query(db,"""SELECT MAX("move_number") FROM moves;""") #Finds the last played move (maximum move_number)
lastMoveIDNullable = res[1,1] #SQL query with max move_number (POSSIBLY "NULL" if no moves have been made)
if (!isnull(lastMoveIDNullable)) #Checks that lastMoveID was not NULL
lastMoveID = get(res[1,1]) #Parses the last move move_number as an Int
else #lastMoveID is NULL
lastMoveID = 0 #move_number "0" is unsused. This implies no moves have been made
end
currentMoveID=1
#=Helper functions=#
function displaymini()
board = BM.startGame(gameType)
global dboard = Array(Char,9,9)
for (x,y) in keys(board)
dboard[(x,y)]=board[(x,y)][1]
end
for x_index in (1:11)
for y_index in (1:11)
if y_index==1
if x_index==1
print("┌")
elseif x_index==11
print("└")
elseif rem(x_index,2)==0
print("|")
else
print("├")
end
elseif y_index==11
if x_index==1
print("┐")
elseif x_index==11
print("┘")
elseif rem(x_index,2)==0
print("|")
else
print("┤")
end
elseif rem(y_index,2)==1
if x_index==1
print("┬")
elseif x_index==11
print("┴")
elseif rem(x_index,2)==1
print("┼")
else
print("|")
end
else
if rem(x_index,2)==1
print("-")
else
if dboard[(div(x_index,2),(6-div(y_index,2)))]=='k' || dboard[(div(x_index,2),(6-div(y_index,2)))]=='K'
print_with_color(:yellow, "$(dboard[div(x_index,2),(6-div(y_index,2))])")
continue
end
print(dboard[(div(x_index,2),(6-div(y_index,2)))])
end
end
end
print("\n")
end
end
function displaystandard()
board = BM.startGame(gameType)
global dboard = Array(Char,9,9)
for (x,y) in keys(board)
dboard[(x,y)]=board[(x,y)][1]
end
for x_index in (1:19)
for y_index in (1:19)
if y_index==1
if x_index==1
print("┌")
elseif x_index==19
print("└")
elseif rem(x_index,2)==0
print("|")
else
print("├")
end
elseif y_index==19
if x_index==1
print("┐")
elseif x_index==19
print("┘")
elseif rem(x_index,2)==0
print("|")
else
print("┤")
end
elseif rem(y_index,2)==1
if x_index==1
print("┬")
elseif x_index==19
print("┴")
elseif rem(x_index,2)==1
print("┼")
else
print("|")
end
else
if rem(x_index,2)==1
print("-")
else
if dboard[(div(x_index,2),(10-div(y_index,2)))]=='k' || dboard[(div(x_index,2),(10-div(y_index,2)))]=='K'
print_with_color(:yellow, "$(dboard[(div(x_index,2),(10-div(y_index,2)))])")
continue
end
print(dboard[(div(x_index,2),(10-div(y_index,2)))])
end
end
end
print("\n")
end
function Setupboard()
res = SQLite.query(db,"""SELECT sourcex,sourcey,targetx,targety,move_type,option FROM moves WHERE "move_number" = $(currentMoveID);""")
sourcexNullable = res[1,1]
sourceyNullable = res[1,2]
targetxNullable = res[1,3]
targetyNullable = res[1,4]
targetx = get(targetxNullable)
targety = get(targetyNullable)
sourcex = get(sourcexNullable)
sourcey = get(sourceyNullable)
board[targetx,targety].piece = board[sourcex,sourcey].piece
board[targetx,targety].team = board[sourcex,sourcey].team
ST.clear!(board[sourcex,sourcey])
ST.saveBoard(board)
end
end#Func
#=----=#
#=----Replays the game until move_id = lastMoveID----=#
while currentMoveID<=lastMoveID
global board
res = SQLite.query(db,"""SELECT sourcex,sourcey,targetx,targety,move_type,option FROM moves WHERE "move_number" = $(currentMoveID);""")
sourcexNullable = res[1,1]
sourceyNullable = res[1,2]
targetxNullable = res[1,3]
targetyNullable = res[1,4]
move_type = get(res[1,5])
optionNullable = res[1,6]
if move_type == "move" #Regular Move
targetx = get(targetxNullable)
targety = get(targetyNullable)
sourcex = get(sourcexNullable)
sourcey = get(sourceyNullable)
if !(ST.isEmpty(board[targetx,targety]))# capture
#push(captures,board[targetx][targety])
end
board[targetx,targety].piece = board[sourcex,sourcey].piece
board[targetx,targety].team = board[sourcex,sourcey].team
ST.clear!(board[sourcex,sourcey])
elseif move_type == "drop"
option = get(optionNullable)
try
#deleteat!(captures,findfirst(x->x.piece==option))
end
targetx = get(targetxNullable)
targety = get(targetyNullable)
board[targetx,targety].piece = board[sourcex,sourcey].piece
board[targetx,targety].team = board[sourcex,sourcey].team
elseif move_type == "resign"
#Do nothing
end
ST.saveBoard(board)
if gameType == "standard"
displaystandard()
else
displaymini()
end
sleep(1) #Waits half a second before running displaying a new board
currentMoveID+=1
end
println("Enter b to go backward, or enter f to go forward\nTo exit viewing the display, enter quit")
instruction=readline(STDIN)
function backward()#function to go backward
currentMoveID-=1
if currentMoveID<1
println("This is the first move")
continue
end
Setupboard()
end
while instruction!="quit\n"
global board
if instruction=="b\n"
currentMoveID-=1
if currentMoveID<1
println("This is the first move")
continue
end
#Set up the team and piece of the board#
Setupboard()
if gameType == "standard"
displaystandard()
else
displaymini()
end
sleep(1)
elseif instruction=="f\n"
currentMoveID+=1
if currentMoveID>lastMoveID
println("Exceed the last move")
continue
end
Setupboard()
if gameType == "standard"
displaystandard()
else
displaymini()
end
sleep(1)
end
instruction=readline(STDIN)
end
export backward, Setupboard