How to resolve the algorithm Bulls and cows step by step in the SenseTalk programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Bulls and cows step by step in the SenseTalk 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 SenseTalk programming language
Source code in the sensetalk programming language
repeat forever
repeat forever
put random(1111,9999) into num
if character 1 of num is not equal to character 2 of num
if character 1 of num is not equal to character 3 of num
if character 1 of num is not equal to character 4 of num
if character 2 of num is not equal to character 3 of num
if character 2 of num is not equal to character 4 of num
if character 3 of num is not equal to character 4 of num
if num does not contain 0
exit repeat
end if
end if
end if
end if
end if
end if
end if
end repeat
set description to "Guess the 4 digit number" & newline & "- zero's excluded" & newline & "- each digit is unique" & newline & newline & "Receive 1 Bull for each digit that equals the corresponding digit in the random number." & newline & newline & "Receive 1 Cow for each digit that appears in the wrong position." & newline
repeat forever
repeat forever
Ask "Guess the number" title "Bulls & Cows" message description
put it into guess
if number of characters in guess is equal to 4
exit repeat
else if guess is ""
Answer "" with "Play" or "Quit" title "Quit Bulls & Cows?"
put it into myAnswer
if myAnswer is "Quit"
exit all
end if
end if
end repeat
set score to {
bulls: {
qty: 0,
values: []
},
cows: {
qty: 0,
values: []
}
}
repeat the number of characters in num times
if character the counter of guess is equal to character the counter of num
add 1 to score.bulls.qty
insert character the counter of guess into score.bulls.values
else
if num contains character the counter of guess
if character the counter of guess is not equal to character the counter of num
if score.bulls.values does not contain character the counter of guess and score.cows.values does not contain character the counter of guess
add 1 to score.cows.qty
insert character the counter of guess into score.cows.values
end if
end if
end if
end if
end repeat
set showScores to "Your score is:" & newline & newline & "Bulls:" && score.bulls.qty & newline & newline & "Cows:" && score.cows.qty
if guess is not equal to num
Answer showScores with "Guess Again" or "Quit" title "Score"
put it into myAnswer
if myAnswer is "Quit"
exit all
end if
else
set winShowScores to showScores & newline & newline & "Your Guess:" && guess & newline & "Random Number:" && num & newline
Answer winShowScores with "Play Again" or "Quit" title "You Win!"
put it into myAnswer
if myAnswer is "Quit"
exit all
end if
exit repeat
end if
end repeat
end repeat
You may also check:How to resolve the algorithm QR decomposition step by step in the F# programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the RPL programming language
You may also check:How to resolve the algorithm Test a function step by step in the Prolog programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Rosetta Code/Find unimplemented tasks step by step in the Run BASIC programming language