Bulls and cows: Difference between revisions
→{{header|Kotlin}}
(Added Quackery.) |
|||
(7 intermediate revisions by 4 users not shown) | |||
Line 237:
bufdef: db 4,0 ; User input buffer
buf: ds 4</syntaxhighlight>
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN random.digit:
RETURN choice "123456789"
HOW TO MAKE SECRET secret:
PUT "" IN secret
FOR i IN {1..4}:
PUT random.digit IN digit
WHILE SOME j IN {1..i-1} HAS secret item j = digit:
PUT random.digit IN digit
PUT secret^digit IN secret
HOW TO RETURN guess count.bulls secret:
PUT 0 IN bulls
FOR i IN {1..4}:
IF secret item i = guess item i: PUT bulls+1 IN bulls
RETURN bulls
HOW TO RETURN guess count.cows secret:
PUT -(guess count.bulls secret) IN cows
FOR c IN guess:
IF c in secret: PUT cows+1 IN cows
RETURN cows
HOW TO REPORT has.duplicates guess:
FOR i IN {1..3}:
FOR j IN {i+1..4}:
IF guess item i = guess item j: SUCCEED
FAIL
HOW TO REPORT is.valid guess:
IF SOME digit IN guess HAS digit not.in "123456789":
WRITE "Invalid digit: ", digit/
FAIL
IF #guess <> 4:
WRITE "Guess must contain 4 digits."/
FAIL
IF has.duplicates guess:
WRITE "No duplicates allowed"/
FAIL
SUCCEED
HOW TO READ GUESS guess:
WHILE 1=1:
WRITE "Guess? "
READ guess RAW
IF is.valid guess: QUIT
HOW TO PLAY BULLS AND COWS:
PUT 0, 0, 0 IN tries, bulls, cows
MAKE SECRET secret
WRITE "Bulls and cows"/
WRITE "--------------"/
WRITE /
WHILE bulls<>4:
READ GUESS guess
PUT guess count.bulls secret IN bulls
PUT guess count.cows secret IN cows
WRITE "Bulls:",bulls,"- Cows:",cows/
PUT tries+1 IN tries
WRITE "You win! Tries:", tries
PLAY BULLS AND COWS</syntaxhighlight>
{{out}}
<pre>Bulls and cows
--------------
Guess? 1234
Bulls: 0 - Cows: 1
Guess? 5678
Bulls: 0 - Cows: 2
Guess? 1679
Bulls: 0 - Cows: 2
Guess? 1689
Bulls: 0 - Cows: 2
Guess? 1659
Bulls: 1 - Cows: 2
Guess? 2659
Bulls: 1 - Cows: 2
Guess? 3659
Bulls: 1 - Cows: 3
Guess? 9356
Bulls: 4 - Cows: 0
You win! Tries: 8</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE DIGNUM="4"
Line 334 ⟶ 419:
You win!
</pre>
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
Line 2,164 ⟶ 2,250:
=={{header|EasyLang}}==
<syntaxhighlight
dig[] = [ 1 2 3 4 5 6 7 8 9 ]
for i = 1 to 4
h = i - 1 +
swap dig[i] dig[h]
.
# print dig[]
Line 2,174 ⟶ 2,260:
attempts = 0
repeat
repeat
ok = 0
s$[] = strchars input
if len s$[] = 4
ok = 1
for i = 1 to 4
g[i] = number s$[i]
if g[i] = 0
ok = 0
.
.
.
.
print g[]
attempts += 1
for j = 1 to 4
.
.
print "bulls:" & bulls & " cows:" & cows
.
print "Well done! " & attempts & " attempts needed."</syntaxhighlight>
=={{header|Eiffel}}==
Line 2,389 ⟶ 2,474:
try
{
for (int i := 0
var ch := guess[i];
var number := ch.toString().toInt();
Line 2,398 ⟶ 2,483:
// check duplicates
var duplicate := guess.seekEach::(x => (x == ch)&&(x.equalReference(ch).Inverted));
if (nil != duplicate)
{
Line 2,422 ⟶ 2,507:
bulls =>
-1 { console.printLine
4 { console.printLine
_attempt.append(1);
Line 4,091 ⟶ 4,176:
=={{header|Kotlin}}==
<syntaxhighlight lang="
const val MAX_GUESSES = 20 // say
fun main(
val num = ('1'..'9').shuffled().take(4).joinToString("")
println("All guesses should have exactly 4 distinct digits excluding zero.")
Line 4,110 ⟶ 4,188:
while (true) {
print("Enter your guess : ")
val guess =
if (guess == num) {
println("You've won with ${++guesses} valid guesses!")
}
val n = guess.toIntOrNull()
Line 4,162 ⟶ 4,240:
You've won with 9 valid guesses!
</pre>
=={{header|Lasso}}==
This game uses an HTML form to submit the answer. The random number and history are stored in a session using Lasso's built in session management.
|