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

Published on 12 May 2024 09:40 PM

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

Source code in the transd programming language

#lang transd

MainModule: {

contains-duplicates: (λ s String() -> Bool()
    (with str String(s) (sort str) 
        (ret (neq (find-adjacent str) (end str))))
),

play: (λ locals:
    syms "0123456789" 
    len 4 
    thenum String() 
    guess String()

    (shuffle syms)
    (= thenum (substr syms 0 len))
    (textout "Your guess: ")
    (while (getline guess)
        (if (eq guess "q") break)
        (if (or (neq (size guess) len) 
                (neq (find-first-not-of guess syms) -1)
                (contains-duplicates guess))
            (lout guess " is not valid guess")
            (textout "Your guess: ")
            continue
        )
        (with bulls 0 cows 0 pl 0
            (for i in Range(len) do 
                (= pl (index-of thenum (subn guess i)))
                (if (eq pl i) (+= bulls 1)
                elsif (neq pl -1) (+= cows 1))
            )
            (lout "bulls: " bulls ", cows: " cows)
            (if (eq bulls len)
                (lout "Congratulations! You have found out the number!")
                (ret null)
            else (textout "Your guess: "))
        )
    )
    (lout "You quit the game.")
),

_start: (λ locals: s String()
    (lout "Welcome to \"Bulls and cows\"!") 
    (while true
        (while true
            (textout "Do you want to play? (yes|no) : ")
            (getline s)
            (if (not (size s)) 
                (lout "Didn't receive an answer. Exiting.") (exit)
            elsif (== (sub (tolower s) 0 1) "n") (lout "Bye!")(exit)
            elsif (== (sub (tolower s) 0 1) "y") break
            else (lout "(Hint: \"yes\" or \"no\".)"))
        )
        (play)
        (lout "Another game?")
        )
)
}


  

You may also check:How to resolve the algorithm SQL-based authentication step by step in the Julia programming language
You may also check:How to resolve the algorithm Permutations step by step in the zkl programming language
You may also check:How to resolve the algorithm Galton box animation step by step in the C++ programming language
You may also check:How to resolve the algorithm Bell numbers step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Create a file step by step in the Yabasic programming language