Anonymous user
Bulls and cows: Difference between revisions
→{{header|Elena}}
m (Added Sidef) |
|||
Line 884:
=={{header|Elena}}==
<lang elena>#define
#define
#define
#method get▼
[▼
^ self numeric - 30h.▼
]▼
#class GameMaster
Line 905 ⟶ 892:
#field theNumbers.
#
[
// generate secret number
#var aRandomNumbers :=
▲ ]
▲ [
#var aRow := consoleEx write:"Your Guess ?" readLine.
^ stringControl toArray:aRow.
▲ theNumbers := aRandomNumbers~earrayop read &from:0 &to:3 &:esubarray.
]
#method proceed : aGuess
[
#
'program'output << "Not a valid guess.%n".▼
^ $self.▼
].▼
!
[
// check
! [ #throw InvalidArgumentException new. ].
[▼
// check duplicates
#var duplicate := arrayControl seek:aGuess &for: &&:x [ (x == ch)and:[ x equal &type'object:ch not ] ].
nil != duplicate ?
▲ [
#throw InvalidArgumentException new.
▲ ].
? [ aBulls += 1. ]
! [▼
arrayControl ifExist:aNumber &in:theNumbers
? [ aCows += 1. ].
].
▲ ]
| ifFailed:
].
▲ #if Control if:(aNumber == theNumbers@aNumber index)
▲ [
4 ? [
].
▲ ].
▲ #if (aBulls == 4)?
▲ $self fail.
▲ 'program'output << "%nYour Score is " << aBulls << " bulls and " << aCows << " cows%n".
]
}
#symbol
[
#var aGameMaster := GameMaster new.
].</lang>
|