Jump to content

Bulls and cows: Difference between revisions

m
(→‎{{header|REXX}}: point at version 2)
imported>Arakov
Line 2,351:
</pre>
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 2,357:
class GameMaster
{
field _numbers;
object theNumbers;
field _attempt;
object theAttempt;
 
constructor()
{
// generate secret number
var randomNumbers := new int[]{1,2,3,4,5,6,7,8,9}.randomize(9);
 
theNumbers_numbers := randomNumbers.Subarray(0, 4);
theAttempt_attempt := new Integer(1);
}
 
ask()
{
var row := console.print("Your Guess #",theAttempt_attempt," ?").readLine();
^ row.toArray()
}
 
proceed(guess)
{
int cows := 0;
int bulls := 0;
 
if (guess.Length != 4)
{
bulls := -1
}
else
{
try
{
for (int i := 0, i < 4, i+=1) {
var ch := guess[i];
var number := ch.toString().toInt();
// check range
ifnot (number > 0 && number < 10)
{ InvalidArgumentException.raise() };
// check duplicates
var duplicate := guess.seekEach:(x => (x == ch)&&(x.equalReference(ch).Inverted));
if (nil != duplicate)
{
InvalidArgumentException.raise()
};
if (number == theNumbers_numbers[i])
{
bulls += 1
}
else
{
if (theNumbers_numbers.ifExists(number))
{ cows += 1 }
}
}
}
catch(Exception e)}
catch(Exception {e)
bulls := -1{
}bulls := -1
};
};
 
bulls =>
-1 { console.printLine:"Not a valid guess."; ^ true }
4 { console.printLine:"Congratulations! You have won!"; ^ false }
: {
theAttempt_attempt.append(1);
console.printLine("Your Score is ",bulls," bulls and ",cows," cows");
^ true
}
}
}
 
public program()
{
var gameMaster := new GameMaster();
 
var (process := $lazy: gameMaster.proceed(gameMaster.ask())).doWhile();
 
process.doWhile();
console.readChar()
}</syntaxhighlight>
{{out}}
Line 2,457 ⟶ 2,459:
Congratulations! You have won!
</pre>
 
=={{header|Elixir}}==
{{works with|Elixir|1.2}}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.