Bulls and cows: Difference between revisions

m (→‎version 1: added whitespace to the output.)
Line 1,357:
 
=={{header|Elena}}==
ELENA 3.12 :
<lang elena>import system'routines.
import extensions.
Line 1,370:
// generate secret number
var aRandomNumbers := (1,2,3,4,5,6,7,8,9) randomize:9.
 
theNumbers := aRandomNumbers Subarray:4 indexat:0 length:4.
theAttempt := Integer new:1.
]
Line 1,377:
ask
[
var aRow := console print:("Your Guess #":,theAttempt:," ?"); readLine.
^ aRow toArray.
Line 1,390:
[ aBulls append int:(-1). ];
[
try(0 to:3 doEach: do(:i)
[
var ch := aGuess@[i].
var aNumber := ch literal; toInt.
Line 1,400:
// check duplicates
var duplicate := aGuess seek each: seekEach(:x)[ ^((x == ch)and:$(x equal reference:ch; not)]).
if ($nil != duplicate)
[
InvalidArgumentException new; raise.
].
if (aNumber == theNumbers@[i])
[ aBulls append int:1 ];
[
Line 1,414:
])
{
generic :on(Exception e)
[
aBulls int := -1
Line 1,421:
].
 
^ aBulls =>
-1 [ console writeLineprintLine:"Not a valid guess.". ^ true ];
4 [ console writeLineprintLine:"Congratulations! You have won!". ^ false ];
! [
theAttempt append int:1.
console printLine:("Your Score is " : ,aBulls : ," bulls and " : ,aCows : ," cows").
^ true
Anonymous user