Bulls and cows: Difference between revisions

(Add SETL)
 
(10 intermediate revisions by 7 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 lang="text">
dig[] = [ 1 2 3 4 5 6 7 8 9 ]
for i = 1 to 4
h = i - 1 + randomrandint (10 - i)
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
.
.
.
. until ok = 1
.
until ok = 1
print g[]
.
attempts += 1
print g[]
attempts +bulls = 10
bulls cows = 0
cows for i = 01 to 4
for i = 1 toif 4g[i] = dig[i]
if g[i] bulls += dig[i]1
bulls += 1else
for j = 1 to 4
else
for j = 1 to 4 if dig[j] = g[i]
if dig[j] cows += g[i]1
cows += 1.
.
.
.
print "bulls:" & bulls & " cows:" & cows
.
print "bulls:" &until bulls & " cows:" &= cows4
until bulls = 4
.
print "Well done! " & attempts & " attempts needed."</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 2,389 ⟶ 2,474:
try
{
for (int i := 0,; i < 4,; i+=1) {
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:("Not a valid guess."); ^ true }
4 { console.printLine:("Congratulations! You have won!"); ^ false }
:! {
_attempt.append(1);
Line 4,091 ⟶ 4,176:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scalakotlin">// version 1.1.2
 
import java.util.Random
 
const val MAX_GUESSES = 20 // say
 
fun main(args: Array<String>) {
val num = ('1'..'9').shuffled().take(4).joinToString("")
val r = Random()
var num: String
// generate a 4 digit random number from 1234 to 9876 with no zeros or repeated digits
do {
num = (1234 + r.nextInt(8643)).toString()
} while ('0' in num || num.toSet().size < 4)
 
println("All guesses should have exactly 4 distinct digits excluding zero.")
Line 4,110 ⟶ 4,188:
while (true) {
print("Enter your guess : ")
val guess = readLinereadln().trim()!!
if (guess == num) {
println("You've won with ${++guesses} valid guesses!")
returnbreak
}
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.
Line 6,024 ⟶ 6,103:
Loop
</syntaxhighlight>
 
=={{header|Quackery}}==
 
<code>transpose</code> is defined at [[Matrix transposition#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ size 4 = dup not if
[ say "Must be four digits." cr ] ] is 4chars ( $ --> b )
 
[ true swap witheach
[ char 1 char 9 1+ within not if
[ say "Must be 1-9 only." cr
not conclude ] ] ] is 1-9 ( $ --> b )
 
[ 0 9 of
swap witheach
[ 1 unrot char 1 - poke ]
0 swap witheach +
4 = dup not if
[ say "Must all be different." cr ] ] is all-diff ( $ --> b )
 
 
[ $ "Guess four digits, 1-9, no duplicates: "
input
dup 4chars not iff drop again
dup 1-9 not iff drop again
dup all-diff not iff drop again ] is guess ( $ --> $ )
 
[ $ "123456789" shuffle 4 split drop ] is rand$ ( --> $ )
 
[ 2 pack transpose
[] swap witheach
[ dup unpack != iff
[ nested join ]
else drop ]
dup [] != if
[ transpose unpack ]
4 over size - ] is -bulls ( $ $ --> $ $ n )
 
[ join sort
0 swap
behead swap witheach
[ tuck = if [ dip 1+ ] ]
drop ] is cows ( $ $ --> n )
 
[ say "Guess the four numbers." cr cr
say "They are all different and"
say " between 1 and 9 inclusive." cr cr
randomise rand$
[ guess
over -bulls
dup 4 = iff say "Correct." done
dup echo 1 = iff
[ say " bull." cr ]
else
[ say " bulls." cr ]
cows
dup echo 1 = iff
[ say " cow." cr ]
else
[ say " cows." cr ]
again ]
cr drop 2drop ] is play ( --> )</syntaxhighlight>
 
{{out}}
 
<pre>Guess the four numbers.
 
They are all different and between 1 and 9 inclusive.
 
Guess four digits, 1-9, no duplicates: 1234
0 bulls.
0 cows.
Guess four digits, 1-9, no duplicates: 5678
1 bull.
3 cows.
Guess four digits, 1-9, no duplicates: 5786
1 bull.
3 cows.
Guess four digits, 1-9, no duplicates: 5867
0 bulls.
4 cows.
Guess four digits, 1-9, no duplicates: 7658
2 bulls.
2 cows.
Guess four digits, 1-9, no duplicates: 7685
0 bulls.
4 cows.
Guess four digits, 1-9, no duplicates: 6758
Correct.
</pre>
 
=={{header|R}}==
{{works with|R|2.8.1}}
Line 6,363 ⟶ 6,533:
end
</syntaxhighlight>
=={{header|RPL}}==
{{works with|HP|49}}
« CLEAR 0 ""
1 4 '''START'''
'''WHILE''' RAND 9 * CEIL R→I →STR DUP2 POS '''REPEAT''' '''DROP''' END +
'''NEXT'''
→ count solution
« '''DO''' 1 CF
'''DO''' "Guess? [CONT]" PROMPT →STR
'''CASE'''
DUP SIZE 4 ≠ '''THEN''' DROP "Not 4 characters" 1 DISP 0.5 WAIT '''END'''
{ 9 } 0 CON
1 4 '''FOR''' j
OVER j DUP SUB STR→
'''IFERR''' 1 PUT '''THEN''' 3 DROPN "Invalid character" 1 DISP 0.5 WAIT '''END'''
'''NEXT'''
DUP 1 CON DOT 4 ≠ '''THEN''' DROP "Repeated digits" 1 DISP 0.5 WAIT '''END'''
1 SF
'''END'''
'''UNTIL''' 1 FS? '''END'''
" → " + 0
1 4 '''FOR''' j
solution PICK3 j DUP SUB POS
'''IF''' DUP '''THEN''' IF j == '''THEN''' 1 '''ELSE''' .1 '''END END''' +
'''NEXT'''
SWAP OVER + 'count' INCR DROP
'''UNTIL''' SWAP 4 == '''END'''
count "guess" →TAG
» » '<span style="color:blue">BU&CO</span>' ST0
 
=={{header|Ruby}}==
Inspired by Tcl
Line 6,445 ⟶ 6,645:
puts "Bulls: #{bulls}; Cows: #{cows}"
end</syntaxhighlight>
 
=={{header|Rust}}==
{{libheader|rand}}
Line 8,295 ⟶ 8,496:
{{libheader|Wren-set}}
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./set" for Set
import "./ioutil" for Input
44

edits