Bulls and cows: Difference between revisions

Content added Content deleted
m (→‎{{header|Sidef}}: minor code correction)
Line 952: Line 952:
{
{
#field theNumbers.
#field theNumbers.
#field theAttempt.


#constructor new
#constructor new
[
[
// generate secret number
#var aRandomNumbers := randomControl randomize:9 &array:(1,2,3,4,5,6,7,8,9).
#var aRandomNumbers := randomControl randomize:9 &array:(1,2,3,4,5,6,7,8,9).
theNumbers := arrayControl Subarray:aRandomNumbers &from:0 &length:4.
theNumbers := arrayControl Subarray:aRandomNumbers &from:0 &length:4.
theAttempt := Integer new:1.
]
]


#method ask
#method ask
[
[
#var aRow := consoleEx write:"Your Guess ?" readLine.
#var aRow := consoleEx write:"Your Guess #":theAttempt:" ?" readLine.
^ literalControl toArray:aRow.
^ literalControl toArray:aRow.
Line 976: Line 977:
? [ aBulls << -1. ]
? [ aBulls << -1. ]
! [
! [
control from:0 &to:3 &do: i
control forrange &int:0 &int:3 &do: (&int:i)
[
[
#var ch := aGuess@i.
#var ch := aGuess@i.
Line 1,009: Line 1,010:
4 ? [ consoleEx writeLine:"Congratulations! You have won!". ^ false. ]
4 ? [ consoleEx writeLine:"Congratulations! You have won!". ^ false. ]
! [
! [
theAttempt += 1.
consoleEx writeLine:"Your Score is " : aBulls : " bulls and " : aCows : " cows".
consoleEx writeLine:"Your Score is " : aBulls : " bulls and " : aCows : " cows".