Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bulls & Cows Code Breaking Game (long post)

30 views
Skip to first unread message

Mike Sanders

unread,
May 16, 2017, 10:48:39 AM5/16/17
to
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

Batchman

unread,
May 17, 2017, 2:20:44 AM5/17/17
to
Mike Sanders wrote:

> 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>

I wrote one years ago which uses Ansi commands for coloUrs and cursor
positioning. It is a batch file designed to run under Command.com but I then
was able to arrange for it to work under Cmd.

It generates a random code, compares your guess, displays appropriate hints
and allows 10 (I think) guesses.

I'll dig it out and send you a copy, here.


Mike Sanders

unread,
May 17, 2017, 9:25:26 AM5/17/17
to
Batchman <batc...@dosville.foo> wrote:

> I'll dig it out and send you a copy, here.

Sounds good, eager to study what you came up with.

Ammammata

unread,
May 22, 2017, 4:39:53 AM5/22/17
to
Il giorno Tue 16 May 2017 04:45:17p, *Mike Sanders* ha inviato su
alt.msdos.batch.nt il messaggio news:off39t$glk$1...@dont-email.me. Vediamo
cosa ha scritto:

>:: Bulls & Cows Code Breaking Game: Michael Sanders
>


you won: secret code [5160] cracked in 7 attempts


the last three tries were 5168 5169 5160

--
/-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
-=- -=- -=- -=- -=- -=- -=- -=- - -=-
>>>>> http://www.bb2002.it :) <<<<<
........... [ al lavoro ] ...........

Mike Sanders

unread,
May 22, 2017, 7:02:05 AM5/22/17
to
Ammammata <amma...@tiscalinet.it> wrote:

> you won: secret code [5160] cracked in 7 attempts

Cool =)

Mike Sanders

unread,
May 22, 2017, 12:09:32 PM5/22/17
to
Batchman <batc...@dosville.foo> wrote:

>
> So here you go and you get a bonus BATCH game!
>
> Both games were created by me back in the Dos, Fidonet, Echo mail era.
>
> I was a regular in the International Batch Echo message area and when others
> began creating batch games, came up with these two.
>
> Noughts & Crosses (Tic Tac Toe to you) was my first creation with MicroCode
> (Master Mind) being the next.
>
> Both have been tested under Win Xp using the Ansi.Com included in the
> archive.
>
> Here's hoping you will feel inspired!
>
> [-- application/zip, encoding base64, 253 lines, name: BATGAME2.ZIP --]

Many thanks Batchman. Downloaded & extracted okay, will rename *._
as per your included instructions. Appreciate you taking the time
to hunt these down & post them. Will study...
0 new messages