Advertisement
T3RRYT3RR0R

Batch 0-2 Player game connect 4

Oct 2nd, 2020 (edited)
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 6.72 KB | None | 0 0
  1. @Echo off & Rem /* Inspired by: https://www.dostips.com/forum/viewtopic.php?f=3&t=3259#p15721 */
  2. :Start rem /* game returns here after win loss or draw */
  3.  CHCP 65001 > nul & Rem Used for BG display
  4.  Pushd "%userprofile%" & (For /F "Delims=" %%C in ('dir BG*.exe /B /S') Do (%%C Cursor 0)) 2> Nul && POPD & Rem /* hides cursor if bg.exe is installed in userprofile */
  5.  CLS & Endlocal & Setlocal DISABLEdelayedexpansion & rem /* Ensure correct environment for macro definition */
  6. ::: Win position table. Hor Vert Diag :::\
  7.  Set "W[0]=[1;1][1;2][1;3][1;4]"&rem  :::H
  8.  Set "W[1]=[1;2][1;3][1;4][1;5]"&rem  :::H
  9.  Set "W[2]=[1;3][1;4][1;5][1;6]"&rem  :::H
  10.  Set "W[3]=[2;1][2;2][2;3][2;4]"&rem  :::H
  11.  Set "W[4]=[2;2][2;3][2;4][2;5]"&rem  :::H
  12.  Set "W[5]=[2;3][2;4][2;5][2;6]"&rem  :::H
  13.  Set "W[6]=[3;1][3;2][3;3][3;4]"&rem  :::H
  14.  Set "W[7]=[3;2][3;3][3;4][3;5]"&rem  :::H
  15.  Set "W[8]=[3;3][3;4][3;5][3;6]"&rem  :::H
  16.  Set "W[9]=[4;1][4;2][4;3][4;4]"&rem  :::H
  17.  Set "W[10]=[4;2][4;3][4;4][4;5]"&rem :::H
  18.  Set "W[11]=[4;3][4;4][4;5][4;6]"&rem :::H
  19.  Set "W[12]=[1;1][2;1][3;1][4;1]"&rem :::V
  20.  Set "W[13]=[1;2][2;2][3;2][4;2]"&rem :::V
  21.  Set "W[14]=[1;3][2;3][3;3][4;3]"&rem :::V
  22.  Set "W[15]=[1;4][2;4][3;4][4;4]"&rem :::V
  23.  Set "W[16]=[1;5][2;5][3;5][4;5]"&rem :::V
  24.  Set "W[17]=[1;6][2;6][3;6][4;6]"&rem :::V
  25.  Set "W[18]=[4;1][3;2][2;3][1;4]"&rem :::D
  26.  Set "W[19]=[4;2][3;3][2;4][1;5]"&rem :::D
  27.  Set "W[20]=[4;3][3;4][2;5][1;6]"&rem :::D
  28.  Set "W[21]=[4;6][3;5][2;4][1;3]"&rem :::D
  29.  Set "W[22]=[4;5][3;4][2;3][1;2]"&rem :::D
  30.  Set "W[23]=[1;1][2;2][3;3][4;4]"&rem :::D
  31. ::::::::::::::::::::::: Do not modify :::/
  32. (For /F %%a in ('echo prompt $E ^| cmd')do (Set "/E=%%a")
  33. %= Ascii Escape Character. Do Not Modify. =%)
  34.  Set "?Won=(For /F "Tokens=1,2 Delims==" %%G in  ('Set W[ 2^> Nul') Do (For /F "Tokens=1,2,3,4 Delims=[]" %%w in ("%%H")Do (If "!%%w!!%%x!!%%y!!%%z!"=="$$$$" (<Nul Set /P "=%/E%[5;1H%/E%[90m123456%/E%[0m%/E%[%%wH!$:7=4!%/E%[%%xH!$:7=4!%/E%[%%yH!$:7=4!%/E%[%%zH!$:7=4!"&Echo/%/E%!!%/E%[K!$! Won& (If Not "!Mode!"=="A" (Pause > Nul )Else ( Timeout 4 1> Nul ))&Endlocal&Goto :Start)))) & (IF "!AvMoves!"=="" (Echo/%/E%!!%/E%[K%/E%[90mDraw.%/E%[90m& Timeout 5 > Nul &Endlocal &Goto :Start))"
  35.  Set "Move=(Echo/%/E%[7;1H%/E%[K!$! Move&For /F "Delims=" %%G in ('Choice /N /C:123456EN')Do If "%%G" == "N" (Endlocal & Goto :Start)Else if /I "%%G" == "E" (Endlocal & Exit /B 0)Else Call :Test %%G & For %%e in (!Errorlevel!)Do If "%%e" == "0" (Goto :$Move)Else (<Nul Set /P "=%/E%[%%e;%%GH!$!"&Set "%%e;%%G=$"&Set AvMoves=!AvMoves:"%%e;%%G" =!))"
  36.  Set "AiMove=Call :Brains $" & Rem /* Substring modification is used to replace '$' with the active player */
  37.  Set "Menu=(<Nul Set /P"=%/E%[5;1H%/E%[90m123456%/E%[0m%/E%[1;20H%/E%[K%/E%[COLmSelect mode%/E%[0m:%/E%[2;20H%/E%[K[%/E%[COLm1%/E%[0m] Player%/E%[3;20H%/E%[K[%/E%[COLm2%/E%[0m] Player%/E%[4;20H%/E%[K[%/E%[COLmA%/E%[0m] AI battle%/E%[5;20H%/E%[K[%/E%[36mN%/E%[0m] New%/E%[6;20H%/E%[K[%/E%[36mE%/E%[0m] Exit%/E%[0m%/E%[7;20H%/E%[K[%/E%[COLmV%/E%[0m] View Source code%/E%[7;1H%/E%[36m?%/E%[0m")"
  38.  Set "X=%/E%[32;7mx%/E%[0m"&Set "O=%/E%[31;7mo%/E%[0m"&Set "iX=O"&Set "iO=X"
  39.  Set AvMoves="4;1" "4;2" "4;3" "4;4" "4;5" "4;6" "3;1" "3;2" "3;3" "3;4" "3;5" "3;6" "2;1" "2;2" "2;3" "2;4" "2;5" "2;6" "1;1" "1;2" "1;3" "1;4" "1;5" "1;6" &Rem Trailing space required
  40.  Set "ForPlayer=For /F "Tokens=1,2 Delims==" %%G in  ('Set W[ 2^> Nul') Do (For /F "Tokens=1,2,3,4 Delims=[]" %%w in ("%%H")Do (If "!%%w!!%%x!!%%y!!%%z!"=="@" ("
  41.  Set "ONfind=Set "AvMoves=!AvMoves:"%%#;%%d" =!" & Exit /B 0"
  42.  Set "?test=If "!%%$V!"=="" (For /F "Tokens=2 Delims=;" %%d in ("%%$V")Do For /L %%# in (4 -1 1)Do (For /F "Delims=" %%i in ('Set /A "i=%%#+1"')Do if not "%%~i"=="5" (If not "!%%i;%%d!"=="" If "!%%#;%%d!"=="" (<Nul Set /P "=%/E%[%%#;%%dH!e$!"&Set "%%!!%%d=!$!" & %OnFind%))Else (If "!%%#;%%d!"=="" (<Nul Set /P "=%/E%[%%#;%%dH!e$!"&Set "%%!!%%d=!$!" & %OnFind%))))"
  43.  Set "OnFind="
  44.  Setlocal EnableDelayedExpansion & rem /* Establish environment for macro expansion */
  45.  (For %%G in (!AvMoves!)Do <Nul Set /P"=%/E%[%%~GH%/E%[34mâ—˜%/E%[0m")
  46.  %Menu:COL=36% & rem /* display active game menu options */
  47.  Title Connect 4
  48.  (For /F "Delims=" %%G in ('Choice /T 10 /D A /N /C:NA21VE')Do If /I "%%G"=="N" (Endlocal & Goto :Start)Else If /I "%%G"=="E" (Endlocal & Exit /B 0)Else Set "Mode=%%G")
  49.  If "%Mode%"=="V" (Mode 1000 & TYPE "%~F0" & Timeout 10 /NoBreak > Nul & Endlocal & Echo/&Echo/Continue & Pause > Nul & Goto :Start)
  50.  %Menu:COL=90% & rem /* display which game menu options become inactive */
  51. :XMove rem /* [X][O]Move Labels used as turn loop and to control scriptflow for invalid moves. with the aid of substring modification */
  52.  (If "%Mode%" == "A" (TITLE AI Battle & <Nul Set /P "=%/E%[5;20H%/E%[K[%/E%[90mN%/E%[0m] New%/E%[5;1H%/E%[90m123456%/E%[0m%/E%[6;20H%/E%[K[%/E%[90mE%/E%[0m] Exit"& %AIMove:$=X%)Else (<Nul Set /P "=%/E%[5;1H%/E%[32m123456%/E%[0m" & %Move:$=X%)) & %?won:$=X%
  53. :OMove rem /* AI was dumb-ish. lookup table for 'smart' comparison of open moves vs player occupied coordinates has been implemented */
  54.  (If "%Mode%" == "2" (If Not "%Mode%" == "A" <Nul Set /P "=%/E%[5;1H%/E%[31m123456%/E%[0m" & %Move:$=O%) Else %AIMove:$=O%) & %?won:$=O%
  55. Goto :XMove
  56. :Test rem /* returns first avialable Y coordinate for the attempted X coordinate via Errorlevel or 0 for no valid coordinate */
  57.  For /L %%# in (4,-1,1)Do (Echo/"!AvMoves!"| %__AppDir__%findstr.exe /LIC:"%%#;%~1" > Nul 2> Nul && Exit /B %%#)
  58. Exit /B 0
  59. :Brains rem /* AI Calls for loop conditional test block to find an appropriate block / advance coordinate */
  60.  Set "$=%1"& Set "e$=!%1!"& Set "$o=!i%1!"
  61.  Call :ForeThought
  62.  If "%Errorlevel%"=="0" (Exit /B 0)
  63. :FindAIM rem /* test for and fill first valid Y position found at random x axis. repeats until position found */
  64.  (Echo/%/E%[7;1H%/E%[K!%1! Move& For /F "Delims=" %%G in ('Set /A "i=!Random! %%6 + 1"')Do (Call :Test %%G & For %%e in (!Errorlevel!)Do If "%%e" == "0" (Goto :FindAIM)Else (<Nul Set /P "=%/E%[%%e;%%GH!%1!"&Set "%%e;%%G=%1"&Set "AvMoves=!AvMoves:"%%e;%%G" =!"&Exit /B 0)))
  65.  Exit /B 0
  66. :ForeThought rem /* Iterate over W[ win pos array, test for a single empty position in a winning 4 point line and attempt to fill first one found - prioritising Ai's own winning sequence.
  67. rem - cont. - On finding valid 4 coordinate return to move loops. If none, test for 2 unoccupied positions in an AI occupied position set. to advance AI */
  68. %ForPlayer:@=!$!!$!!$!%
  69.    %?test:$V=w% Else %?test:$V=x% Else %?test:$V=y% Else %?test:$V=z%
  70. )))
  71. %ForPlayer:@=!$o!!$o!!$o!%
  72.    %?test:$V=w% Else %?test:$V=x% Else %?test:$V=y% Else %?test:$V=z%
  73. )))
  74. %ForPlayer:@=!$o!!$o!%
  75.    %?test:$V=w% Else %?test:$V=x% Else %?test:$V=y% Else %?test:$V=z%
  76. )))
  77. %ForPlayer:@=!$!!$!%
  78.    %?test:$V=w% Else %?test:$V=x% Else %?test:$V=y% Else %?test:$V=z%
  79. )))
  80. Exit /B 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement