How to resolve the algorithm Bulls and cows step by step in the Lasso programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Bulls and cows step by step in the Lasso 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 Lasso programming language
Source code in the lasso programming language
[
define randomizer() => {
local(n = string)
while(#n->size < 4) => {
local(r = integer_random(1,9)->asString)
#n !>> #r ? #n->append(#r)
}
return #n
}
define cowbullchecker(n::string,a::string) => {
integer(#n) == integer(#a) ? return (:true,map('cows'=0,'bulls'=4,'choice'=#a))
local(cowbull = map('cows'=0,'bulls'=0,'choice'=#a),'checked' = array)
loop(4) => {
if(#checked !>> integer(#a->values->get(loop_count))) => {
#checked->insert(integer(#a->values->get(loop_count)))
if(integer(#n->values->get(loop_count)) == integer(#a->values->get(loop_count))) => {
#cowbull->find('bulls') += 1
else(#n->values >> #a->values->get(loop_count))
#cowbull->find('cows') += 1
}
}
}
#cowbull->find('bulls') == 4 ? return (:true,map('cows'=0,'bulls'=4,'choice'=#a))
return (:true,#cowbull)
}
session_start('user')
session_addvar('user', 'num')
session_addvar('user', 'historic_choices')
// set up rand
var(num)->isNotA(::string) ? var(num = randomizer)
var(historic_choices)->isNotA(::array) ? var(historic_choices = array)
local(success = false)
// check answer
if(web_request->param('a')->asString->size) => {
local(success,result) = cowbullchecker($num,web_request->param('a')->asString)
$historic_choices->insert(#result)
}
if(web_request->params->asStaticArray >> 'restart') => {
$num = randomizer
$historic_choices = array
}
]
Bulls and Cows
Guess the 4-digit number...
Your win if the guess is the same as the randomly chosen number.
- A score of one bull is accumulated for each digit in your guess that equals the corresponding digit in the randomly chosen initial number.
- A score of one cow is accumulated for each digit in your guess that also appears in the randomly chosen number, but in the wrong position.
[
local(win = false)
if($historic_choices->size) => {
with c in $historic_choices do => {^
''+#c->find('choice')+': Bulls: '+#c->find('bulls')+', Cows: '+#c->find('cows')
if(#c->find('bulls') == 4) => {^
' - YOU WIN!'
#win = true
^}
''
^}
}
if(not #win) => {^
]
Restart
[else
'Restart'
^}]
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the МК-61/52 programming language
You may also check:How to resolve the algorithm Multiplicative order step by step in the C# programming language
You may also check:How to resolve the algorithm Empty program step by step in the Global Script programming language
You may also check:How to resolve the algorithm Table creation/Postal addresses step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Window creation/X11 step by step in the Icon and Unicon programming language