June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,70 +1,70 @@
import ceylon.random {
DefaultRandom
DefaultRandom
}
shared void run() {
value random = DefaultRandom();
function generateDigits() =>
random.elements(1..9).distinct.take(4).sequence();
function validate(String guess) {
variable value ok = true;
if(!guess.every((Character element) => element.digit)) {
print("numbers only, please");
ok = false;
}
if('0' in guess) {
print("only 1 to 9, please");
ok = false;
}
if(guess.distinct.shorterThan(guess.size)) {
print("no duplicates, please");
ok = false;
}
if(guess.size != 4) {
print("4 digits please");
ok = false;
}
return ok;
}
function score({Integer*} target, {Integer*} guess) {
variable value bulls = 0;
variable value cows = 0;
for([a, b] in zipPairs(target, guess)) {
if(a == b) {
bulls++;
} else if(target.contains(b)) {
cows++;
}
}
return [bulls, cows];
}
while(true) {
value digits = generateDigits();
print("I have chosen my four digits, please guess what they are.
Use only the digits 1 to 9 with no duplicates and enter them with no spaces. eg 1234
Enter q or Q to quit.");
while(true) {
if(exists line = process.readLine()) {
if(line.uppercased == "Q") {
return;
}
if(validate(line)) {
value guessDigits = line.map((Character element) => parseInteger(element.string)).coalesced;
value [bulls, cows] = score(digits, guessDigits);
if(bulls == 4) {
print("You win!");
break;
} else {
print("Bulls: ``bulls``, Cows: ``cows``");
}
}
}
}
}
value random = DefaultRandom();
function generateDigits() =>
random.elements(1..9).distinct.take(4).sequence();
function validate(String guess) {
variable value ok = true;
if (!guess.every((Character element) => element.digit)) {
print("numbers only, please");
ok = false;
}
if ('0' in guess) {
print("only 1 to 9, please");
ok = false;
}
if (guess.distinct.shorterThan(guess.size)) {
print("no duplicates, please");
ok = false;
}
if (guess.size != 4) {
print("4 digits please");
ok = false;
}
return ok;
}
function score({Integer*} target, {Integer*} guess) {
variable value bulls = 0;
variable value cows = 0;
for ([a, b] in zipPairs(target, guess)) {
if (a == b) {
bulls++;
} else if (target.contains(b)) {
cows++;
}
}
return [bulls, cows];
}
while (true) {
value digits = generateDigits();
print("I have chosen my four digits, please guess what they are.
Use only the digits 1 to 9 with no duplicates and enter them with no spaces. eg 1234
Enter q or Q to quit.");
while (true) {
if (exists line = process.readLine()) {
if (line.uppercased == "Q") {
return;
}
if (validate(line)) {
value guessDigits = line.map((Character element) => Integer.parse(element.string)).narrow<Integer>();
value [bulls, cows] = score(digits, guessDigits);
if (bulls == 4) {
print("You win!");
break;
}
else {
print("Bulls: ``bulls``, Cows: ``cows``");
}
}
}
}
}
}

View file

@ -28,7 +28,7 @@ class GameMaster
var aBulls := Integer new:0.
if (aGuess length != 4)
[ aBulls append int:(-1). ];
[ aBulls append(-1). ];
[
try(0 to:3 do(:i)
[
@ -40,17 +40,17 @@ class GameMaster
[ InvalidArgumentException new; raise. ].
// check duplicates
var duplicate := aGuess seekEach(:x)((x == ch)and:$(x equal reference:ch; not)).
if ($nil != duplicate)
var duplicate := aGuess seekEach(:x)((x == ch)&&(x equalReference:ch; inverted)).
if (nil != duplicate)
[
InvalidArgumentException new; raise.
].
if (aNumber == theNumbers[i])
[ aBulls append int:1 ];
[ aBulls append(1) ];
[
(theNumbers ifExists:aNumber)
? [ aCows append int:1 ].
? [ aCows append(1) ].
].
])
{
@ -65,7 +65,7 @@ class GameMaster
-1 [ console printLine:"Not a valid guess.". ^ true ];
4 [ console printLine:"Congratulations! You have won!". ^ false ];
! [
theAttempt append int:1.
theAttempt append(1).
console printLine("Your Score is ",aBulls," bulls and ",aCows," cows").
@ -74,7 +74,7 @@ class GameMaster
]
}
program =
public program =
[
var aGameMaster := GameMaster new.

View file

@ -1,24 +1,23 @@
/*REXX program scores the Bulls & Cows game with CBLFs (Carbon Based Life Forms). */
?=; do until length(?)==4 /*generate a unique four-digit number. */
r=random(1, 9) /*change 1──► 0 to allow a zero digit*/
/*REXX program scores the Bulls & Cows game with CBLFs (Carbon Based Life Forms). */
?=; do until length(?)==4; r=random(1, 9) /*generate a unique four-digit number. */
if pos(r, ?)\==0 then iterate /*don't allow a repeated digit/numeral.*/
?=? || r /*add random digit by concatenation. */
end /*until length ···*/ /* [↑] builds a unique four-digit num.*/
id= ' [Bulls & Cows] '
$= ' [Bulls & Cows] '
do until bulls==4; say /*play until guessed or enters "QUIT".*/
say id 'Please enter a 4-digit guess (with no zeroes) [or Quit]:'
pull n; n=space(n,0); if abbrev('QUIT',n,1) then exit /*wants to quit ? */
q=?; L=length(n); bulls=0; cows=0 /*initialize some REXX variables. */
say $ 'Please enter a 4-digit guess (with no zeroes) [or Quit]:'
pull n; n=space(n, 0); if abbrev('QUIT', n, 1) then exit /*wants to quit ?*/
q=?; L=length(n); bulls=0; cows=0 /*initialize some REXX variables. */
do j=1 for L; if substr(n,j,1)\==substr(q, j, 1) then iterate /*bull? */
bulls=bulls+1; q=overlay(.,q,j) /*bump bull count; disallow for cow*/
end /*j*/ /* [↑] bull count─────────────────*/
/*cow ? */
do k=1 for L; _=substr(n,k,1); if pos(_,q)==0 then iterate
cows=cows+1 ; q=translate(q, , _) /*bump cow count; allow mult digits*/
end /*k*/ /* [↑] cow count─────────────────*/
say; @='You got' bulls
if L\==0 & bulls\==4 then say id @ 'bull's(bulls) "and" cows 'cow's(cows).
do j=1 for L; if substr(n, j, 1)\==substr(q, j, 1) then iterate /*bull?*/
bulls=bulls +1; q=overlay(., q, j) /*bump bull count; disallow for cow.*/
end /*j*/ /* [↑] bull count────────~─────────*/
/*cow ?*/
do k=1 for L; _=substr(n, k, 1); if pos(_, q)==0 then iterate
cows=cows + 1; q=translate(q, , _) /*bump cow count; allow mult digits.*/
end /*k*/ /* [↑] cow count─────────~────────*/
say; @= 'You got' bulls
if L\==0 & bulls\==4 then say $ @ 'bull's(bulls) "and" cows 'cow's(cows).
end /*until bulls==4*/
say
say " ┌─────────────────────────────────────────┐"
@ -28,4 +27,4 @@ say
say " └─────────────────────────────────────────┘"; say
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return ''; return 's' /*this function handles pluralization. */
s: if arg(1)==1 then return ''; return "s" /*this function handles pluralization. */

View file

@ -0,0 +1,42 @@
# Project : Bulls and cows
# Date : 2017/11/19
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
secret = ""
while len(secret) != 4
c = char(48 + random(9))
if substr(secret, c) = 0
secret = secret + c
ok
end
see "guess a four-digit number with no digit used twice."
guesses = 0
guess = ""
while true
guess = ""
while len(guess) != 4
see "enter your guess: "
give guess
if len(guess) != 4
see "must be a four-digit number" + nl
ok
end
guesses = guesses + 1
if guess = secret
see "you won after " + guesses + " guesses!"
exit
ok
bulls = 0
cows = 0
for i = 1 to 4
c = secret[i]
if guess[i] = c
bulls = bulls + 1
but substr(guess, c) > 0
cows = cows + 1
ok
next
see "you got " + bulls + " bull(s) and " + cows + " cow(s)." + nl
end

View file

@ -0,0 +1,73 @@
Option Explicit
Sub Main_Bulls_and_cows()
Dim strNumber As String, strInput As String, strMsg As String, strTemp As String
Dim boolEnd As Boolean
Dim lngCpt As Long
Dim i As Byte, bytCow As Byte, bytBull As Byte
Const NUMBER_OF_DIGITS As Byte = 4
Const MAX_LOOPS As Byte = 25 'the max of lines supported by MsgBox
strNumber = Create_Number(NUMBER_OF_DIGITS)
Do
bytBull = 0: bytCow = 0: lngCpt = lngCpt + 1
If lngCpt > MAX_LOOPS Then strMsg = "Max of loops... Sorry you loose!": Exit Do
strInput = AskToUser(NUMBER_OF_DIGITS)
If strInput = "Exit Game" Then strMsg = "User abort": Exit Do
For i = 1 To Len(strNumber)
If Mid(strNumber, i, 1) = Mid(strInput, i, 1) Then
bytBull = bytBull + 1
ElseIf InStr(strNumber, Mid(strInput, i, 1)) > 0 Then
bytCow = bytCow + 1
End If
Next i
If bytBull = Len(strNumber) Then
boolEnd = True: strMsg = "You win in " & lngCpt & " loops!"
Else
strTemp = strTemp & vbCrLf & "With : " & strInput & " ,you have : " & bytBull & " bulls," & bytCow & " cows."
MsgBox strTemp
End If
Loop While Not boolEnd
MsgBox strMsg
End Sub
Function Create_Number(NbDigits As Byte) As String
Dim myColl As New Collection
Dim strTemp As String
Dim bytAlea As Byte
Randomize
Do
bytAlea = Int((Rnd * 9) + 1)
On Error Resume Next
myColl.Add CStr(bytAlea), CStr(bytAlea)
If Err <> 0 Then
On Error GoTo 0
Else
strTemp = strTemp & CStr(bytAlea)
End If
Loop While Len(strTemp) < NbDigits
Create_Number = strTemp
End Function
Function AskToUser(NbDigits As Byte) As String
Dim boolGood As Boolean, strIn As String, i As Byte, NbDiff As Byte
Do While Not boolGood
strIn = InputBox("Enter your number (" & NbDigits & " digits)", "Number")
If StrPtr(strIn) = 0 Then strIn = "Exit Game": Exit Do
If strIn <> "" Then
If Len(strIn) = NbDigits Then
NbDiff = 0
For i = 1 To Len(strIn)
If Len(Replace(strIn, Mid(strIn, i, 1), "")) < NbDigits - 1 Then
NbDiff = 1
Exit For
End If
Next i
If NbDiff = 0 Then boolGood = True
End If
End If
Loop
AskToUser = strIn
End Function