How to resolve the algorithm Bulls and cows step by step in the Forth programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Bulls and cows step by step in the Forth programming language
Table of Contents
Problem Statement
Bulls and Cows is an old game played with pencil and paper that was later implemented using computers.
Create a four digit random number from the digits 1 to 9, without duplication. The program should:
The score is computed as:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bulls and cows step by step in the Forth programming language
Source code in the forth programming language
include random.fs
create hidden 4 allot
: ok? ( str -- ? )
dup 4 <> if 2drop false exit then
1 9 lshift 1- -rot
bounds do
i c@ '1 -
dup 0 9 within 0= if 2drop false leave then
1 swap lshift over and
dup 0= if nip leave then
xor
loop 0<> ;
: init
begin
hidden 4 bounds do 9 random '1 + i c! loop
hidden 4 ok?
until ;
: check? ( addr -- solved? )
0
4 0 do
over i + c@
4 0 do
dup hidden i + c@ = if swap
i j = if 8 else 1 then + swap
then
loop drop
loop nip
8 /mod tuck . ." bulls, " . ." cows"
4 = ;
: guess: ( "1234" -- )
bl parse 2dup ok? 0= if 2drop ." Bad guess! (4 unique digits, 1-9)" exit then
drop check? if cr ." You guessed it!" then ;
You may also check:How to resolve the algorithm Named parameters step by step in the PHP programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Red programming language
You may also check:How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the 11l programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Kaprekar numbers step by step in the Python programming language