How to resolve the algorithm Bulls and cows step by step in the Ring programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Bulls and cows step by step in the Ring 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 Ring programming language
Source code in the ring programming language
# Project : Bulls and cows
secret = ""
while len(secret) != 4
c = char(48 + random(9))
if substr(secret, c) = 0
secret = secret + c
ok
end
see "guess a four-digit number with no digit used twice."
guesses = 0
guess = ""
while true
guess = ""
while len(guess) != 4
see "enter your guess: "
give guess
if len(guess) != 4
see "must be a four-digit number" + nl
ok
end
guesses = guesses + 1
if guess = secret
see "you won after " + guesses + " guesses!"
exit
ok
bulls = 0
cows = 0
for i = 1 to 4
c = secret[i]
if guess[i] = c
bulls = bulls + 1
but substr(guess, c) > 0
cows = cows + 1
ok
next
see "you got " + bulls + " bull(s) and " + cows + " cow(s)." + nl
end
You may also check:How to resolve the algorithm Tarjan step by step in the 11l programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Vedit macro language programming language
You may also check:How to resolve the algorithm Vampire number step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Fairshare between two and more step by step in the Phix programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the Joy programming language