-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSlide-Puzzle.bat
More file actions
342 lines (293 loc) · 8.57 KB
/
Slide-Puzzle.bat
File metadata and controls
342 lines (293 loc) · 8.57 KB
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
@goto main
rem Slide Puzzle
rem Updated on 2016-10-02
rem Made by wthe22 - http://winscr.blogspot.com/
:main
@echo off
prompt $s
setlocal EnableDelayedExpansion EnableExtensions
title Slide Puzzle by wthe22
chcp 437
:controlTypeIn
set "userInput=?"
cls
echo 1. Normal control
echo 2. Reverse control
echo=
echo 0. Exit
echo=
echo Choose your control:
set /p "controlType="
if "!controlType!" == "0" exit
if "!controlType!" == "1" goto gameModeIn
if "!controlType!" == "2" goto gameModeIn
goto controlTypeIn
:mainMenu
:gameModeIn
set "gameMode=?"
cls
echo Control type : !controlType!
echo=
echo 1. Normal slide puzzle
echo 2. Plan solving mode
echo=
echo 0. Back
echo=
echo Choose game mode:
set /p "gameMode="
if "!gameMode!" == "0" goto controlTypeIn
if "!gameMode!" == "1" goto puzzleSizeIn
if "!gameMode!" == "2" goto puzzleSizeIn
goto controlTypeIn
:puzzleSizeIn
cls
echo Control type : !controlType!
echo Game mode : !gameMode!
echo=
echo 0. Back
echo=
echo Input puzzle size: (Eg: 4x4)
set /p "userInput="
if "!userInput!" == "0" goto controlTypeIn
for /f "tokens=1-2 delims=Xx" %%a in ("!userInput: =!") do (
if %%a0 GEQ 10 if %%a0 LEQ 2000 (
if %%b0 GEQ 10 if %%b0 LEQ 2000 (
set "puzzleRow=%%a"
set "puzzleCol=%%b"
goto puzzleReset
)
)
)
goto puzzleSizeIn
:puzzleReset
cls
echo Control type : !controlType!
echo=
echo Puzzle size : !puzzleRow!x!puzzleCol!
echo=
echo Scrambling puzzle...
echo=
title Slide Puzzle !puzzleRow!x!puzzleCol!
set /a "puzzleArea=!puzzleRow! * !puzzleCol!"
rem Setup Control
rem ULDR
if "!controlType!" == "1" set "controlSet=SDWA"
if "!controlType!" == "2" set "controlSet=WASD"
rem Reset GUI
set "boardDisplay1="
set "boardDisplay2="
for /l %%n in (1,1,!puzzleCol!) do (
set "boardDisplay1=!boardDisplay1!ÍÍÍÍÍÍ"
set "boardDisplay2=!boardDisplay2!ÄÄÄÄÄÅ"
)
set "boardDisplay1=!boardDisplay1:~0,-1!"
set "boardDisplay2=!boardDisplay2:~0,-1!"
set "spacing="
for /l %%n in (1,1,5) do set "spacing=!spacing! "
for /l %%n in (1,1,5) do set "displayLine%%n="
if "!gameMode!" == "2" set "displayLine3=[Z] Undo"
set "displayLine4=[X] Quit"
if "!gameMode!" == "2" set "displayLine3=[C] Check"
rem Create Puzzle
set "numList="
set "emptyCell=?"
set /a "numMax=!puzzleArea!-1"
for /l %%n in (1,1,!numMax!) do (
title Slide Puzzle !puzzleRow!x!puzzleCol! - Preparing... [%%n/!numMax!]
set "tempVar1= %%n"
set "numList=!numList!!tempVar1:~-3,3!"
)
set "inversionSum=0"
for /l %%n in (!numMax!,-1,1) do (
title Slide Puzzle !puzzleRow!x!puzzleCol! - Generating... [%%n/!numMax!]
set /a "tempVar1=!random! %% %%n"
set /a "tempVar1*=3"
for %%c in (!tempVar1!) do (
set /a "puzzleCell%%n= !numList:~%%c,3!"
set "tempVar1=!numList:~%%c!"
set "numList=!numList:~0,%%c!!tempVar1:~3!"
)
for /l %%l in (%%n,1,!numMax!) do if !puzzleCell%%n! GTR !puzzleCell%%l! set /a "inversionSum+=1"
)
set "emptyCell=!puzzleArea!"
set "puzzleCell!emptyCell!= "
set /a "inversionSum=!inversionSum! %% 2"
if "!inversionSum!" == "1" (
set "tempVar1=!puzzleCell2!"
set "puzzleCell2=!puzzleCell1!"
set "puzzleCell1=!tempVar1!"
)
title Slide Puzzle !puzzleRow!x!puzzleCol!
set "startTime=!time!"
if "!gameMode!" == "1" goto puzzleSetup
if "!gameMode!" == "2" goto solveSetup
echo=
echo ERROR: Unknown gamemode
pause
exit
rem ============================================ Play Puzzle Mode ============================================
:puzzleSetup
set "moveCount=0"
:puzzlePlay
cls
call :puzzleBoard
choice /c X!controlSet! /n /m "> "
if "!errorlevel!" == "1" goto promptQuitPuzzle
if "!errorlevel!" == "2" call :move U
if "!errorlevel!" == "3" call :move L
if "!errorlevel!" == "4" call :move D
if "!errorlevel!" == "5" call :move R
call :checkSolved
if "!return!" == "true" goto puzzleSolved
goto puzzlePlay
:promptQuitPuzzle
echo=
choice /c YN /m "Quit the game?"
if "!errorlevel!" == "1" goto mainMenu
goto puzzlePlay
rem ============================================ Plan Solvings Mode ============================================
:solveSetup
set "moveCount=0"
set "moveSolvings="
:solvingIn
cls
call :puzzleBoard
echo=
echo Input solvings:
choice /c ZXC!controlSet! /n /m "!moveSolvings!"
set "userInput=!errorlevel!"
if "!userInput!" == "1" goto undoSolving
if "!userInput!" == "2" goto promptQuitSolving
if "!userInput!" == "3" goto promptSimulate
set /a "movesCount+=1"
if "!userInput!" == "4" set "moveSolvings=!moveSolvings!U"
if "!userInput!" == "5" set "moveSolvings=!moveSolvings!L"
if "!userInput!" == "6" set "moveSolvings=!moveSolvings!D"
if "!userInput!" == "7" set "moveSolvings=!moveSolvings!R"
goto solvingIn
:undoSolving
if "!movesCount!" == "0" goto solvingIn
set /a "movesCount-=1"
set "moveSolvings=!moveSolvings:~0,-1!"
goto solvingIn
:promptQuitSolving
echo=
choice /c YN /m "Quit now?"
if "!errorlevel!" == "1" goto mainMenu
goto solvingIn
:promptSimulate
echo=
choice /c YN /m "Check solvings now?"
if "!errorlevel!" == "2" goto solvingIn
set "moveCount=0"
cls
call :puzzleBoard
echo=
echo Moves :
echo=!moveSolvings!
pause > nul
:simulateSolving
call :checkSolved
if "!return!" == "true" goto puzzleSolved
if not defined moveSolvings goto puzzleUnsolved
set /a "moveCount+=1"
if "!moveSolvings:~0,1!" == "U" call :move U
if "!moveSolvings:~0,1!" == "D" call :move D
if "!moveSolvings:~0,1!" == "L" call :move L
if "!moveSolvings:~0,1!" == "R" call :move R
set "moveSolvings=!moveSolvings:~1!"
cls
call :puzzleBoard
echo=
echo Moves :
echo=!moveSolvings!
pause > nul
goto simulateSolving
rem ============================================ Common Menu ============================================
:puzzleSolved
call :difftime !time! !startTime!
call :ftime !return!
cls
call :puzzleBoard
echo=
echo Congratulations, you solved the puzzle in !return! with !moveCount! moves
pause > nul
goto mainMenu
:puzzleUnsolved
cls
call :puzzleBoard
echo=
echo You failed to solved the puzzle....
pause > nul
goto mainMenu
rem Functions
:checkSolved
set "return=false"
set /a "tempVar1=!puzzleArea!-1"
if not "!emptyCell!" == "!puzzleArea!" goto :EOF
for /l %%n in (!tempVar1!,-1,1) do if not "!puzzleCell%%n!" == "%%n" goto :EOF
set "return=true"
goto :EOF
:move
if "%1" == "U" set /a tempVar1=!emptyCell! - !puzzleCol!
if "%1" == "L" set /a tempVar1=!emptyCell! - 1
if "%1" == "D" set /a tempVar1=!emptyCell! + !puzzleCol!
if "%1" == "R" set /a tempVar1=!emptyCell! + 1
set /a tempVar2=!tempVar1! %% !puzzleCol!
if "%1" == "L" if "!tempVar2!" == "0" goto :EOF
if "%1" == "R" if "!tempVar2!" == "1" goto :EOF
if "%1" == "U" if !tempVar1! LSS 1 goto :EOF
if "%1" == "D" if !tempVar1! GTR !puzzleArea! goto :EOF
set "puzzleCell!emptyCell!=!puzzleCell%tempVar1%!"
set "puzzleCell!tempVar1!= "
set "emptyCell=!tempVar1!"
set /a moveCount+=1
goto :EOF
:puzzleBoard
echo É!boardDisplay1!»!spacing!Moves : !moveCount!
set "lineCount=1"
for /l %%a in (1,1,!puzzleArea!) do (
set "tempVar1= !puzzleCell%%a!"
if !puzzleCell%%a!0 LEQ 90 set "tempVar1=!tempVar1! "
set "display=!display! !tempVar1:~-3,3! ³"
set /a tempVar1= %%a %% !puzzleCol!
if "!tempVar1!" == "0" (
set /a "lineCount+=1"
for %%l in (!lineCount!) do echo º!display:~0,-1!º!spacing!!displayLine%%l!
if not "%%a" == "!puzzleArea!" (
set /a "lineCount+=1"
for %%l in (!lineCount!) do echo º!boardDisplay2!º!spacing!!displayLine%%l!
)
set "display="
)
)
echo È!boardDisplay1!¼
goto :EOF
rem Side displays
set /a tempVar2+=1
set "display1=!display1! Time:"
set "display2=!display2! !return!"
set "display4=!display4! Moves:"
set "display5=!display5! !movesCount!"
goto :EOF
:difftime [end_time] [start_time] [/n]
set "return=0"
for %%t in (%1:00:00:00:00 %2:00:00:00:00) do (
for /f "tokens=1-4 delims=:." %%a in ("%%t") do (
set /a "return+=24%%a %% 24 *360000+1%%b*6000+1%%c*100+1%%d-610100"
)
set /a "return*=-1"
)
if not "%3" == "/n" if !return! LSS 0 set /a "return+=8640000"
goto :EOF
:ftime [time_in_centisecond]
set /a tempVar1=%1 %% 8640000
set "return="
for %%n in (360000 6000 100 1) do (
set /a tempVar2=!tempVar1! / %%n
set /a tempVar1=!tempVar1! %% %%n
set "tempVar2=?0!tempVar2!"
set "return=!return!!tempVar2:~-2,2!:"
)
set "return=!return:~0,-4!.!return:~-3,2!"
goto :EOF