Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Echo off & Rem /* Inspired by: https://www.dostips.com/forum/viewtopic.php?f=3&t=3259#p15721 */
- :Start rem /* game returns here after win loss or draw */
- CHCP 65001 > nul & Rem Used for BG display
- 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 */
- CLS & Endlocal & Setlocal DISABLEdelayedexpansion & rem /* Ensure correct environment for macro definition */
- ::: Win position table. Hor Vert Diag :::\
- Set "W[0]=[1;1][1;2][1;3][1;4]"&rem :::H
- Set "W[1]=[1;2][1;3][1;4][1;5]"&rem :::H
- Set "W[2]=[1;3][1;4][1;5][1;6]"&rem :::H
- Set "W[3]=[2;1][2;2][2;3][2;4]"&rem :::H
- Set "W[4]=[2;2][2;3][2;4][2;5]"&rem :::H
- Set "W[5]=[2;3][2;4][2;5][2;6]"&rem :::H
- Set "W[6]=[3;1][3;2][3;3][3;4]"&rem :::H
- Set "W[7]=[3;2][3;3][3;4][3;5]"&rem :::H
- Set "W[8]=[3;3][3;4][3;5][3;6]"&rem :::H
- Set "W[9]=[4;1][4;2][4;3][4;4]"&rem :::H
- Set "W[10]=[4;2][4;3][4;4][4;5]"&rem :::H
- Set "W[11]=[4;3][4;4][4;5][4;6]"&rem :::H
- Set "W[12]=[1;1][2;1][3;1][4;1]"&rem :::V
- Set "W[13]=[1;2][2;2][3;2][4;2]"&rem :::V
- Set "W[14]=[1;3][2;3][3;3][4;3]"&rem :::V
- Set "W[15]=[1;4][2;4][3;4][4;4]"&rem :::V
- Set "W[16]=[1;5][2;5][3;5][4;5]"&rem :::V
- Set "W[17]=[1;6][2;6][3;6][4;6]"&rem :::V
- Set "W[18]=[4;1][3;2][2;3][1;4]"&rem :::D
- Set "W[19]=[4;2][3;3][2;4][1;5]"&rem :::D
- Set "W[20]=[4;3][3;4][2;5][1;6]"&rem :::D
- Set "W[21]=[4;6][3;5][2;4][1;3]"&rem :::D
- Set "W[22]=[4;5][3;4][2;3][1;2]"&rem :::D
- Set "W[23]=[1;1][2;2][3;3][4;4]"&rem :::D
- ::::::::::::::::::::::: Do not modify :::/
- (For /F %%a in ('echo prompt $E ^| cmd')do (Set "/E=%%a")
- %= Ascii Escape Character. Do Not Modify. =%)
- 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))"
- 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" =!))"
- Set "AiMove=Call :Brains $" & Rem /* Substring modification is used to replace '$' with the active player */
- 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")"
- Set "X=%/E%[32;7mx%/E%[0m"&Set "O=%/E%[31;7mo%/E%[0m"&Set "iX=O"&Set "iO=X"
- 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
- 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!"=="@" ("
- Set "ONfind=Set "AvMoves=!AvMoves:"%%#;%%d" =!" & Exit /B 0"
- 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%))))"
- Set "OnFind="
- Setlocal EnableDelayedExpansion & rem /* Establish environment for macro expansion */
- (For %%G in (!AvMoves!)Do <Nul Set /P"=%/E%[%%~GH%/E%[34mâ—˜%/E%[0m")
- %Menu:COL=36% & rem /* display active game menu options */
- Title Connect 4
- (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")
- If "%Mode%"=="V" (Mode 1000 & TYPE "%~F0" & Timeout 10 /NoBreak > Nul & Endlocal & Echo/&Echo/Continue & Pause > Nul & Goto :Start)
- %Menu:COL=90% & rem /* display which game menu options become inactive */
- :XMove rem /* [X][O]Move Labels used as turn loop and to control scriptflow for invalid moves. with the aid of substring modification */
- (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%
- :OMove rem /* AI was dumb-ish. lookup table for 'smart' comparison of open moves vs player occupied coordinates has been implemented */
- (If "%Mode%" == "2" (If Not "%Mode%" == "A" <Nul Set /P "=%/E%[5;1H%/E%[31m123456%/E%[0m" & %Move:$=O%) Else %AIMove:$=O%) & %?won:$=O%
- Goto :XMove
- :Test rem /* returns first avialable Y coordinate for the attempted X coordinate via Errorlevel or 0 for no valid coordinate */
- For /L %%# in (4,-1,1)Do (Echo/"!AvMoves!"| %__AppDir__%findstr.exe /LIC:"%%#;%~1" > Nul 2> Nul && Exit /B %%#)
- Exit /B 0
- :Brains rem /* AI Calls for loop conditional test block to find an appropriate block / advance coordinate */
- Set "$=%1"& Set "e$=!%1!"& Set "$o=!i%1!"
- Call :ForeThought
- If "%Errorlevel%"=="0" (Exit /B 0)
- :FindAIM rem /* test for and fill first valid Y position found at random x axis. repeats until position found */
- (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)))
- Exit /B 0
- :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.
- 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 */
- %ForPlayer:@=!$!!$!!$!%
- %?test:$V=w% Else %?test:$V=x% Else %?test:$V=y% Else %?test:$V=z%
- )))
- %ForPlayer:@=!$o!!$o!!$o!%
- %?test:$V=w% Else %?test:$V=x% Else %?test:$V=y% Else %?test:$V=z%
- )))
- %ForPlayer:@=!$o!!$o!%
- %?test:$V=w% Else %?test:$V=x% Else %?test:$V=y% Else %?test:$V=z%
- )))
- %ForPlayer:@=!$!!$!%
- %?test:$V=w% Else %?test:$V=x% Else %?test:$V=y% Else %?test:$V=z%
- )))
- Exit /B 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement