A few years back, I'd written a Mastermind clone in javascript:
<
https://busybox.hypermart.net/mastermind.html>
And was interested to see if there were any adaptations of the
game written in batch & I dug one up here:
<
https://www.rosettacode.org/wiki/Bulls_and_cows#Batch_File>
Thought perhaps I could write a smaller version, (but couldn't
despite my best efforts...) At any rate, here's my take on the
game. If you spot any flaky behavior, let me know if so compelled.
@echo off & setlocal enabledelayedexpansion
:: Bulls & Cows Code Breaking Game: Michael Sanders
::
https://busybox.hypermart.net/bulls-and-cows.html
::
:: An old code breaking game thought to be over
:: 100 years old (yup grandpa was a steampunk!)
title Bulls ^& Cows
set x=0987654321
set y=0
set z=9
:scramble
set /a r=%random%%%(11-(!y!+1))
set p=!x:~%r%,1!
set n[!y!]=!p!
set x=!x:%p%=!
set /a y+=1
if !y! lss 4 goto :scramble
:blurb
cls & echo.
echo Bulls ^& Cows: Code Breaking Game
echo.
echo Objective:
echo.
echo A unique 4 digit code has been randomly generated ^& you'll need
echo to determine its sequence in no more than 9 attempts to win...
echo.
echo After every failed attempt, the following clues are revealed:
echo.
echo . 1 bull for each digit in the CORRECT position
echo.
echo . 1 cow for each correct digit in the WRONG position
echo.
echo Press q at any time to quit.
:play
call :feedback 0 t
echo. & set /p s=!t! guess:
if !s! equ q goto :quit
if !s! equ Q goto :quit
set v=!s:~3!& if not defined v call :feedback 1 & goto :play
set v=!s:~4!& if defined v call :feedback 1 & goto :play
set v=& for /f "delims=1234567890" %%x in ("!s!") do set v=%%x
if defined v call :feedback 1 & goto :play
for /l %%q in (0,1,3) do set g[%%q]=!s:~%%q,1!
for /l %%j in (0,1,3) do (
for /l %%k in (0,1,3) do (
if %%j neq %%k if !g[%%j]! equ !g[%%k]! (
call :feedback 2 & goto :play
)
)
)
:clues
set b=0
set c=0
for /l %%q in (0,1,3) do if !g[%%q]! equ !n[%%q]! set /a b+=1
if !b! equ 4 (goto :win)
for /l %%j in (0,1,3) do (
for /l %%k in (0,1,3) do (
if %%j neq %%k if !g[%%j]! equ !n[%%k]! set /a c+=1
)
)
set /a z-=1
if !z! equ 0 (goto :lose) else (call :feedback 3 & goto :play)
:win
set e=1
set /a z=10-!z!
cls
call :feedback 4
goto :gameover
:lose
set e=1
cls
call :feedback 5
goto :gameover
:quit
echo. & set /p q=quit? [y]/n:||set q=y
if !q! equ y goto :gameover
if !q! equ Y goto :gameover
if !q! equ n goto :play
if !q! equ N goto :play
goto :quit
:feedback
if %~1 equ 0 (
if !z! equ 9 set %~2=1st
if !z! equ 8 set %~2=2nd
if !z! equ 7 set %~2=3rd
if !z! leq 6 (set /a q=10-!z! & set %~2=!q!th)
goto :eof
)
if %~1 equ 1 set f=invalid input...
if %~1 equ 2 set f=dupliate digits forbidden...
if %~1 equ 3 set f=bulls: !b! cows: !c!
if %~1 equ 4 set f=you won: secret code [!s!] cracked in !z! attempts
if %~1 equ 5 set f=game over: you failed to crack the code homeboy
echo.&echo.!f!
:gameover
if !e! equ 1 if %0 equ "%~0" echo. & pause>nul|set /p =any key to exit...
endlocal & exit /b 0
:eof
--
later on,
Mike
https://busybox.hypermart.net