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

Published on 12 May 2024 09:40 PM

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

Source code in the action! programming language

DEFINE DIGNUM="4"

TYPE Score=[BYTE bulls,cows,err]

PROC Generate(CHAR ARRAY secret)
  DEFINE DIGCOUNT="9"
  CHAR ARRAY digits(DIGCOUNT)
  BYTE i,j,d,tmp,count

  FOR i=0 TO DIGCOUNT-1
  DO
    digits(i)=i+'1
  OD

  secret(0)=DIGNUM
  count=DIGCOUNT
  FOR i=1 TO DIGNUM
  DO
    d=Rand(count)
    secret(i)=digits(d)
    count==-1
    digits(d)=digits(count)
  OD
RETURN

PROC CheckScore(CHAR ARRAY code,guess Score POINTER res)
  BYTE i,j

  res.bulls=0
  res.cows=0
  IF guess(0)#DIGNUM THEN
    res.err=1
    RETURN
  FI
  res.err=0
  FOR i=1 TO DIGNUM
  DO
    IF guess(i)=code(i) THEN
      res.bulls==+1
    ELSE
      FOR j=1 TO DIGNUM
      DO
        IF j#i AND guess(j)=code(i) THEN
          res.cows==+1
          EXIT
        FI
      OD
    FI
  OD
RETURN

PROC Main()
  CHAR ARRAY code(DIGNUM+1),guess(255)
  Score res

  Generate(code)
  PrintE("Bull and cows game.") PutE()
  Print("I choose a 4-digit number from digits 1 to 9 without repetition. ")
  PrintE("Your goal is to guess it.") PutE()
  PrintE("Enter your guess:")
  DO
    InputS(guess)
    CheckScore(code,guess,res)
    Put(28) ;cursor up
    PrintF("%S -> ",guess)
    IF res.err THEN
      Print("Wrong input")
    ELSE
      PrintF("Bulls=%B Cows=%B",res.bulls,res.cows)
      IF res.bulls=DIGNUM THEN
        PutE() PutE()
        PrintE("You win!")
        EXIT
      FI
    FI
    PrintE(", try again:")
  OD
RETURN

  

You may also check:How to resolve the algorithm Forward difference step by step in the Python programming language
You may also check:How to resolve the algorithm UTF-8 encode and decode step by step in the Haskell programming language
You may also check:How to resolve the algorithm Validate International Securities Identification Number step by step in the Factor programming language
You may also check:How to resolve the algorithm Penney's game step by step in the Clojure programming language
You may also check:How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the Mathematica/Wolfram Language programming language