[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 happy886rr 于 2017-7-6 22:57 编辑

回复 8# a2002
厉害,看得出来,你用for取代了很多if,优化了结构。
至于象棋引擎早就有了,它们叫UCCI,只有UCCI才能实现人机对战。单纯批处理根本无法具备象棋智能。
因为象棋还有开局棋谱,很多特殊走法,不是5000行能解决智能算法的。这里只能借助现成的UCCI象棋引擎库,用fen格式去传递当前棋局
  1. rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1
复制代码
有点类似前端的数据交互吧。

毕竟,你要发布的作品应该具备极强的智能,UCCI携带开局棋谱后几乎是必赢的,同样对于五子棋,也有很多引擎,在无禁手的情况下,先下者必赢。毕竟计算机可以准确无误的计算到很多步之后的权值,然后选择最佳的落子方案。

当然借助google的深度学习库tensorflow,可以通过训练,让机器具备人类棋手的经验。不过该库只能运行在64位python上。
还有阿里的Me2都是机器模拟人类思维的极佳方案,但已经涉及到量子计算、脑部扫描等前沿技术。

TOP

再琢磨一下看要不要用引擎,实现人机

如果能不用就不用

TOP

我缩减了代码,体积减少了1.1K,也许更便于看清楚和修改吧。更新至17.7.6.2

以下是代码:
  1. @echo off&setlocal enabledelayedexpansion&mode 90,33&title 批处理版象棋&color 76
  2. :Restart
  3. call :Module_StartWith
  4. :loop_1
  5. color 76
  6. title 批处理版象棋 - 请选棋
  7. call :Module_Echo
  8. set "msg_4=[信息]"
  9. call :Cmos loop_1 X Y
  10. call :Check_1
  11. :loop_2
  12. color 7e
  13. title 批处理版象棋 - 请走棋
  14. call :Cmos loop_2 New_X New_Y
  15. call :Check_2
  16. if "!P_ID_%New_X%_%New_Y%!" equ "c" msg * 红方(!E_j!)获胜,点击鼠标继续&color c0&cmos c f&goto :Restart
  17. if "!P_ID_%New_X%_%New_Y%!" equ "j" msg * 绿方(!E_c!)获胜,点击鼠标继续&color c0&cmos c f&goto :Restart
  18. set /a Undo_Num+=1
  19. set "Undo_!Undo_Num!=P_ID_!X!_!Y!=!P_ID_%X%_%Y%! P_ID_!New_X!_!New_Y!=!P_ID_%New_X%_%New_Y%!"
  20. set "P_ID_!New_X!_!New_Y!=!P_ID_%X%_%Y%!"&set "P_ID_!X!_!Y!=#"
  21. if !now! equ c (set now=j) else (set now=c)
  22. goto :loop_1
  23. :Module_StartWith
  24. set  "line_0=                           "
  25. set  "line_1= ┏━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┓ "
  26. set  "line_2= ┃  ┃  ┃  ┃╲ ┃ ╱┃  ┃  ┃  ┃ "
  27. set  "line_3= ┃  ┃  ┃  ┃ ╲┃╱ ┃  ┃  ┃  ┃ "
  28. set  "line_4= ┣━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━┫ "
  29. set  "line_5= ┃  ┃  ┃  ┃ ╱┃╲ ┃  ┃  ┃  ┃ "
  30. set  "line_6= ┃  ┃  ┃  ┃╱ ┃ ╲┃  ┃  ┃  ┃ "
  31. set  "line_7= ┣━━╬━━╋━━╋━━╋━━╋━━╋━━╬━━┫ "
  32. set  "line_8= ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃ "
  33. set  "line_9= ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃ "
  34. set "line_10= ╠━━╋━━╬━━╋━━╬━━╋━━╬━━╋━━╣ "
  35. set "line_11= ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃ "
  36. set "line_12= ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃ "
  37. set "line_13= ┣━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━┫ "
  38. set "line_14= ┃                       ┃ "
  39. set "line_15= ┃                       ┃ "
  40. set "line_16= ┣━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┫ "
  41. set "line_17= ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃ "
  42. set "line_18= ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃ "
  43. set "line_19= ╠━━╋━━╬━━╋━━╬━━╋━━╬━━╋━━╣ "
  44. set "line_20= ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃ "
  45. set "line_21= ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃ "
  46. set "line_22= ┣━━╬━━╋━━╋━━╋━━╋━━╋━━╬━━┫ "
  47. set "line_23= ┃  ┃  ┃  ┃╲ ┃ ╱┃  ┃  ┃  ┃ "
  48. set "line_24= ┃  ┃  ┃  ┃ ╲┃╱ ┃  ┃  ┃  ┃ "
  49. set "line_25= ┣━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━┫ "
  50. set "line_26= ┃  ┃  ┃  ┃ ╱┃╲ ┃  ┃  ┃  ┃ "
  51. set "line_27= ┃  ┃  ┃  ┃╱ ┃ ╲┃  ┃  ┃  ┃ "
  52. set "line_28= ┗━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━┛ "
  53. set "line_29=                           "
  54. for /l %%# in (1,1,10) do set "msg_%%#= "
  55. set now=j&set Undo_Num=0
  56. set "str=卒炮将士象马车兵炮帅仕相马车"&set "var=abcdefghijklmn"
  57. for /l %%# in (0,1,13) do set E_!var:~%%#,1!=!str:~%%#,1!
  58. for /l %%$ in (1,1,10) do for /l %%# in (1,1,9) do set P_ID_%%$_%%#=#
  59. for %%# in (1_1@g 1_2@f 1_3@e 1_4@d 1_5@c 1_6@d 1_7@e 1_8@f 1_9@g 10_1@n 10_2@m 10_3@l 10_4@k 10_5@j 10_6@k 10_7@l 10_8@m 10_9@n 3_2@b 3_8@b 8_2@i 8_8@i 4_1@a 4_3@a 4_5@a 4_7@a 4_9@a 7_1@h 7_3@h 7_5@h 7_7@h 7_9@h) do (set "t=%%#"&set "P_ID_!t:@==!")
  60. set "msg_1=批处理版象棋,由a2002编写完成,版本17.7.6.2,第三方cmos"
  61. set "msg_2=  ┏━┓┏━┓┏━┓      ┃悔┃┃重┃┃退┃      ┗━┛┗━┛┗━┛    "
  62. set "msg_4=[信息]"&set "c=h i j k l m n"&set "j=a b c d e f g"
  63. goto :eof
  64. :Module_Set
  65. for /l %%$ in (1,1,10) do for /l %%# in (1,1,9) do (
  66. if "!P_ID_%%$_%%#!" equ "#" (
  67. set /a t1=%%$*3-3,t2=t1+1,t3=t2+1,t4=%%#*3-3
  68. call :Command_1 "%%$" "%%#" "!t1!" "!t2!" "!t3!" "!t4!")
  69. for %%a in (a b c d e f g) do if "!P_ID_%%$_%%#!" equ "%%a" set "P_Top_%%$_%%#=┌─┐"&set "P_Between_%%$_%%#=│!E_%%a!│"&set "P_Bottom_%%$_%%#=└─┘"
  70. for %%a in (h i j k l m n) do if "!P_ID_%%$_%%#!" equ "%%a" set "P_Top_%%$_%%#=╔═╗"&set "P_Between_%%$_%%#=║!E_%%a!║"&set "P_Bottom_%%$_%%#=╚═╝")
  71. goto :eof
  72. :Module_Echo
  73. call :Module_Set
  74. set "msg_3=走棋方:!E_%now%!"
  75. cls
  76. echo ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
  77. for /l %%# in (1,1,10) do (
  78. echo ┃!P_Top_%%#_1!!P_Top_%%#_2!!P_Top_%%#_3!!P_Top_%%#_4!!P_Top_%%#_5!!P_Top_%%#_6!!P_Top_%%#_7!!P_Top_%%#_8!!P_Top_%%#_9!┃!msg_%%#:~0,15!
  79. echo ┃!P_Between_%%#_1!!P_Between_%%#_2!!P_Between_%%#_3!!P_Between_%%#_4!!P_Between_%%#_5!!P_Between_%%#_6!!P_Between_%%#_7!!P_Between_%%#_8!!P_Between_%%#_9!┃!msg_%%#:~15,15!
  80. echo ┃!P_Bottom_%%#_1!!P_Bottom_%%#_2!!P_Bottom_%%#_3!!P_Bottom_%%#_4!!P_Bottom_%%#_5!!P_Bottom_%%#_6!!P_Bottom_%%#_7!!P_Bottom_%%#_8!!P_Bottom_%%#_9!┃!msg_%%#:~30,15!)
  81. echo ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  82. goto :eof
  83. :Check_1
  84. if "!P_ID_%X%_%Y%!" equ "#" set "msg_4=棋不能为空!!!"&goto :loop_1
  85. for %%# in (!%now%!) do if "!P_ID_%X%_%Y%!" equ "%%#" set "msg_4=请拿自己的棋!!!"&goto :loop_1
  86. goto :eof
  87. :Check_2
  88. set t=0
  89. set F_c=!j!&set F_j=!c!
  90. for %%# in (!F_%now%!) do if "!P_ID_%New_X%_%New_Y%!" equ "%%#" set "msg_4=非法走棋!!!"&goto :loop_1
  91. call :CalcA
  92. if "!P_ID_%X%_%Y%!" equ "a" (if !X! leq 5 (if !Return! equ -1 goto :eof) else (for %%# in (-1 200 -200) do if !Return! equ %%# goto :eof))
  93. if "!P_ID_%X%_%Y%!" equ "h" (if !X! geq 6 (if !Return! equ 1 goto :eof) else (for %%# in (1 200 -200) do if !Return! equ %%# goto :eof))
  94. for %%# in (b i) do (if "!P_ID_%X%_%Y%!" equ "%%#" (if "!P_ID_%New_X%_%New_Y%!" equ "#" (
  95. if !Return! lss 200 if !Return! gtr 0 (call :CalcB 4&if !CalcB_Return! equ 1 goto :eof)
  96. if !Return! lss 0 if !Return! gtr -200 (call :CalcB 2&if !CalcB_Return! equ 1 goto :eof)
  97. if "!Return:~-2!" equ "00" if !Return! gtr 0 (call :CalcB 3&if !CalcB_Return! equ 1 goto :eof)
  98. if "!Return:~-2!" equ "00" if !Return! lss 0 (call :CalcB 1&if !CalcB_Return! equ 1 goto :eof)
  99. ) else (
  100. if !Return! lss 200 if !Return! gtr 0 (call :CalcB 4&if !CalcB_Return! equ 3 goto :eof)
  101. if !Return! lss 0 if !Return! gtr -200 (call :CalcB 2&if !CalcB_Return! equ 3 goto :eof)
  102. if "!Return:~-2!" equ "00" if !Return! gtr 0 (call :CalcB 3&if !CalcB_Return! equ 3 goto :eof)
  103. if "!Return:~-2!" equ "00" if !Return! lss 0 (call :CalcB 1&if !CalcB_Return! equ 3 goto :eof))))
  104. if "!P_ID_%X%_%Y%!" equ "c" (if !New_X! geq 1 if !New_X! leq 3 if !New_Y! geq 4 if !New_Y! leq 6 (for %%# in (1 -1 200 -200) do if !Return! equ %%# goto :eof)
  105. if "!P_ID_%New_X%_%New_Y%!" equ "j" (call :CalcB 2&if !CalcB_Return! equ 2 goto :eof))
  106. if "!P_ID_%X%_%Y%!" equ "j" (if !New_X! geq 8 if !New_X! leq 10 if !New_Y! geq 4 if !New_Y! leq 6 (for %%# in (1 -1 200 -200) do if !Return! equ %%# goto :eof)
  107. if "!P_ID_%New_X%_%New_Y%!" equ "c" (call :CalcB 4&if !CalcB_Return! equ 2 goto :eof))
  108. if "!P_ID_%X%_%Y%!" equ "d" (if !New_X! geq 1 if !New_X! leq 3 if !New_Y! geq 4 if !New_Y! leq 6 (
  109. for %%# in (201 199 -199 -201) do if !Return! equ %%# goto :eof))
  110. if "!P_ID_%X%_%Y%!" equ "k" (if !New_X! geq 8 if !New_X! leq 10 if !New_Y! geq 4 if !New_Y! leq 6 (
  111. for %%# in (201 199 -199 -201) do if !Return! equ %%# goto :eof))
  112. if "!P_ID_%X%_%Y%!" equ "e" (if !New_X! leq 5 (
  113. if !Return! equ 402 (set /a t1=X-1,t2=Y-1&call :Command_2 !t1! !t2!&if defined t goto :eof)
  114. if !Return! equ 398 (set /a t1=X+1,t2=Y-1&call :Command_2 !t1! !t2!&if defined t goto :eof)
  115. if !Return! equ -398 (set /a t1=X-1,t2=Y+1&call :Command_2 !t1! !t2!&if defined t goto :eof)
  116. if !Return! equ -402 (set /a t1=X+1,t2=Y+1&call :Command_2 !t1! !t2!&if defined t goto :eof)))
  117. if "!P_ID_%X%_%Y%!" equ "l" (if !New_X! geq 6 (
  118. if !Return! equ 402 (set /a t1=X-1,t2=Y-1&call :Command_2 !t1! !t2!&if defined t goto :eof)
  119. if !Return! equ 398 (set /a t1=X+1,t2=Y-1&call :Command_2 !t1! !t2!&if defined t goto :eof)
  120. if !Return! equ -398 (set /a t1=X-1,t2=Y+1&call :Command_2 !t1! !t2!&if defined t goto :eof)
  121. if !Return! equ -402 (set /a t1=X+1,t2=Y+1&call :Command_2 !t1! !t2!&if defined t goto :eof)))
  122. for %%# in (f m) do (if "!P_ID_%X%_%Y%!" equ "%%#" (
  123. for %%$ in (202 -198) do if !Return! equ %%$ (set /a t1=X-1,t2=Y&call :Command_2 !t1! !t2!&if defined t goto :eof)
  124. for %%$ in (401 399) do if !Return! equ %%$ (set /a t1=X,t2=Y-1&call :Command_2 !t1! !t2!&if defined t goto :eof)
  125. for %%$ in (-401 -399) do if !Return! equ %%$ (set /a t1=X,t2=Y+1&call :Command_2 !t1! !t2!&if defined t goto :eof)
  126. for %%$ in (198 -202) do if !Return! equ %%$ (set /a t1=X+1,t2=Y&call :Command_2 !t1! !t2!&if defined t goto :eof)))
  127. for %%# in (g n) do (if "!P_ID_%X%_%Y%!" equ "%%#" (if "!P_ID_%New_X%_%New_Y%!" equ "#" (
  128. if !Return! lss 200 if !Return! gtr 0 (call :CalcB 4&if !CalcB_Return! equ 1 goto :eof)
  129. if !Return! lss 0 if !Return! gtr -200 (call :CalcB 2&if !CalcB_Return! equ 1 goto :eof)
  130. if "!Return:~-2!" equ "00" if !Return! gtr 0 (call :CalcB 3&if !CalcB_Return! equ 1 goto :eof)
  131. if "!Return:~-2!" equ "00" if !Return! lss 0 (call :CalcB 1&if !CalcB_Return! equ 1 goto :eof)
  132. ) else (
  133. if !Return! lss 200 if !Return! gtr 0 (call :CalcB 4&if !CalcB_Return! equ 2 goto :eof)
  134. if !Return! lss 0 if !Return! gtr -200 (call :CalcB 2&if !CalcB_Return! equ 2 goto :eof)
  135. if "!Return:~-2!" equ "00" if !Return! gtr 0 (call :CalcB 3&if !CalcB_Return! equ 2 goto :eof)
  136. if "!Return:~-2!" equ "00" if !Return! lss 0 (call :CalcB 1&if !CalcB_Return! equ 2 goto :eof))))
  137. set "msg_4=非法走棋!!!"&goto :loop_1
  138. :CalcA
  139. set /a t1=X-New_X,t2=(Y-New_Y)*200
  140. set /a Return=t1+t2
  141. goto :eof
  142. :CalcB
  143. set CalcB_Return=0
  144. if "%~1" equ "1" for /l %%# in (!Y!,1,!New_Y!) do if "!P_ID_%X%_%%#!" neq "#" set /a CalcB_Return+=1
  145. if "%~1" equ "2" for /l %%# in (!X!,1,!New_X!) do if "!P_ID_%%#_%Y%!" neq "#" set /a CalcB_Return+=1
  146. if "%~1" equ "3" for /l %%# in (!New_Y!,1,!Y!) do if "!P_ID_%X%_%%#!" neq "#" set /a CalcB_Return+=1
  147. if "%~1" equ "4" for /l %%# in (!New_X!,1,!X!) do if "!P_ID_%%#_%Y%!" neq "#" set /a CalcB_Return+=1
  148. goto :eof
  149. :Cmos
  150. cmos c f
  151. set /a t1=!errorlevel!-1001
  152. if !errorlevel:~-3! equ 001 goto :%~1
  153. if !t1! leq 2000 goto :%~1
  154. set t2=!t1:~0,-3!
  155. set /a %~3=(t2-1)/6+1,%~2=(t1-t2*1000-1)/3+1
  156. if "!%~2!_!%~3!" equ "2_11" goto :Undo
  157. if "!%~2!_!%~3!" equ "2_12" goto :Restart
  158. if "!%~2!_!%~3!" equ "2_13" exit
  159. if !%~2! gtr 10 goto :%~1
  160. if !%~2! lss 1 goto :%~1
  161. if !%~3! gtr 9 goto :%~1
  162. if !%~3! lss 1 goto :%~1
  163. goto :eof
  164. :Undo
  165. if !Undo_Num! lss 2 set "msg_4=没有可悔的棋!!!"&goto :loop_1
  166. set /a t1=Undo_Num,t2=t1-1,Undo_Num-=2
  167. for /f "tokens=1,2 delims= " %%a in ("!Undo_%t1%!") do set "%%a"&set "%%b"
  168. for /f "tokens=1,2 delims= " %%a in ("!Undo_%t2%!") do set "%%a"&set "%%b"
  169. goto :loop_1
  170. :Command_1
  171. set "P_Top_%~1_%~2=!line_%~3:~%~6,3!"&set "P_Between_%~1_%~2=!line_%~4:~%~6,3!"&set "P_Bottom_%~1_%~2=!line_%~5:~%~6,3!"
  172. goto :eof
  173. :Command_2
  174. set t=&if "!P_ID_%~1_%~2!" equ "#" set t=1
  175. goto :eof
复制代码
1

评分人数

TOP

不过netbenton的我玩不了

TOP

本帖最后由 a2002 于 2017-7-6 18:48 编辑

回复 4# happy886rr

    我看了,感觉netbenton的比较精炼
    但是我的这个if有些重复、啰嗦,还有for中使用了call,降低了效率
    谢谢提醒,这点要改进

TOP

回复 3# a2002
我记得以前有人写过一个批处理人机对战象棋,你去批处理原创区找找。不过你的规则设计的更好,我试了马绊腿,你也能提示非法走步。这点值得赞赏。
1

评分人数

    • a2002: 谢谢提醒技术 + 1

TOP

对于那方形子,用圆角制表符在win7下就不能对齐了

我再改进一下吧

TOP

本帖最后由 happy886rr 于 2017-7-6 15:45 编辑

回复 1# a2002
象棋规则的实现非常赞,虽然只是人人象棋,不过稍微加几行修改,结合UCCI就能变人 机对战。
为何不用html写,批处理做这个体验都很差的。方形子感觉更像军棋,而不是象棋。

TOP

返回列表