Bulls and cows: Difference between revisions

m (Moved Kotlin entry into correct alphabetical order)
Line 1,357:
 
=={{header|Elena}}==
ELENA 3.x1 :
<lang elena>#import system'routines.
#import system'routinesextensions.
#import extensions.
 
class GameMaster
Line 1,372 ⟶ 1,371:
var aRandomNumbers := (1,2,3,4,5,6,7,8,9) randomize:9.
theNumbers := aRandomNumbers Subarray &index:0 &length:4.
theAttempt := Integer new:1.
]
Line 1,378 ⟶ 1,377:
ask
[
var aRow := console writeLiteralprint:"Your Guess #":theAttempt:" ?"; readLine.
^ aRow toArray.
Line 1,389 ⟶ 1,388:
 
if (aGuess length != 4)
[ aBulls <<append int:(-1). ];
[
try(0 to:3 &doEach: (:i)
[
var ch := aGuess@i.
var aNumber := ch literal; toInt.
// check range
ifnot ((aNumber > 0) && (aNumber < 10))
[ InvalidArgumentException new; raise. ].
// check duplicates
var duplicate := aGuess seek &each: (:x )[ ^(x == ch)and:[ $(x equal &reference:ch; not ] )].
if (nil != duplicate)
[
InvalidArgumentException new; raise.
].
if (aNumber == (theNumbers@i))
[ aBulls +=append int:1. ];
[
(theNumbers ifExists:aNumber)
? [ aCows +=append int:1. ].
].
])
| if &Error: e{
[ generic : e
aBulls set:(-1).[
]. aBulls int := -1
]
}.
].
 
^ aBulls =>
-1 [ console writeLine:"Not a valid guess.". ^ true. ];
4 [ console writeLine:"Congratulations! You have won!". ^ false. ];
! [
theAttempt +=append int:1.
console writeLineprintLine:"Your Score is " : aBulls : " bulls and " : aCows : " cows".
^ true.
].
]
Line 1,437 ⟶ 1,438:
var aGameMaster := GameMaster new.
 
[ $(aGameMaster proceed:(aGameMaster ask) ]) doWhile.
console readChar.
Anonymous user