How to resolve the algorithm Bulls and cows step by step in the Oforth programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bulls and cows step by step in the Oforth 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 Oforth programming language

Source code in the oforth programming language

: bullsAndCows
| numbers guess digits bulls cows |

   ListBuffer new ->numbers
   while(numbers size 4 <>) [ 9 rand dup numbers include ifFalse: [ numbers add ] else: [ drop ] ]

   while(true) [
      "Enter a number of 4 different digits between 1 and 9 : " print
      System.Console askln ->digits
      digits asInteger isNull digits size 4 <> or ifTrue: [ "Number of four digits needed" println continue ]
      digits map(#asDigit) ->guess

      guess numbers zipWith(#==) occurrences(true) ->bulls
      bulls 4 == ifTrue: [ "You won !" println return ]

      guess filter(#[numbers include]) size bulls - ->cows
      System.Out "Bulls = " << bulls << ", cows = " << cows << cr
      ] ;

  

You may also check:How to resolve the algorithm Matrix transposition step by step in the D programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Maple programming language
You may also check:How to resolve the algorithm Even or odd step by step in the XLISP programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the Logo programming language
You may also check:How to resolve the algorithm Perlin noise step by step in the Racket programming language