June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,28 @@
|
|||
countbulls(a, b) = sum([a[i] == b[i] for i in 1:length(a)])
|
||||
countcows(a, b) = sum([a[i] == b[j] for i in 1:length(a), j in 1:length(b) if i != j])
|
||||
validate(a, b) = typeof(a) == Int && typeof(b) == Int && a >= 0 && b >= 0 && a + b < 5
|
||||
|
||||
function doguess()
|
||||
poss = [a for a in collect(Base.product(1:9,1:9,1:9,1:9)) if length(unique(a))[1]==4]
|
||||
while(length(poss) > 0)
|
||||
ans = rand(poss, 1)[1]
|
||||
while true
|
||||
println("My guess: $(ans[1])$(ans[2])$(ans[3])$(ans[4]). How many bulls and cows?")
|
||||
regres = match(r"\D*(\d+)\D*(\d+)", readline())
|
||||
bul, cow = parse(Int,regres[1]), parse(Int,regres[2])
|
||||
if(validate(bul, cow))
|
||||
break
|
||||
else
|
||||
println("Please enter an integer each for bulls and cows.")
|
||||
end
|
||||
end
|
||||
if(bul == 4)
|
||||
return ans
|
||||
end
|
||||
filter!(i -> (countbulls(ans,i), countcows(ans, i)) == (bul, cow), poss)
|
||||
end
|
||||
Base.throw("ERROR: No solutions found. Inconsistent scoring by other player?")
|
||||
end
|
||||
|
||||
answ = doguess()
|
||||
println("The winning pick: $(answ[1])$(answ[2])$(answ[3])$(answ[4])")
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
My guess: 9517. How many bulls and cows?
|
||||
0 1
|
||||
My guess: 3285. How many bulls and cows?
|
||||
1 1
|
||||
My guess: 4298. How many bulls and cows?
|
||||
0 2
|
||||
My guess: 2465. How many bulls and cows?
|
||||
0 2
|
||||
My guess: 3926. How many bulls and cows?
|
||||
2 0
|
||||
My guess: 3724. How many bulls and cows?
|
||||
3 0
|
||||
My guess: 3124. How many bulls and cows?
|
||||
4 0
|
||||
The winning pick: 3124
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
# we use the [] reduction meta operator along with the Cartesian Product
|
||||
# operator X to create the Cartesian Product of four times [1..9] and then get
|
||||
# all the elements where the number of unique digits is four.
|
||||
my @candidates = ([X] [1..9] xx 4).tree.grep: *.uniq == 4;
|
||||
my @candidates = ([X] [1..9] xx 4).grep: *.unique == 4;
|
||||
|
||||
repeat {
|
||||
my $guess = @candidates.pick;
|
||||
|
|
|
|||
|
|
@ -1,60 +1,52 @@
|
|||
/*REXX program plays the Bulls & Cows game with CBLFs (Carbon Based Life Forms). */
|
||||
call gen@ /*generate all the possibilities. */
|
||||
call # /*get the first guess for the game. */
|
||||
parse arg ? .; if datatype(?,'W') then call random ,,? /*Random seed? Make repeatable*/
|
||||
L=1234; H=9876; call gen@ /*generate all possibilities. */
|
||||
do forever; g=random(L,H); if @.g\==. then leave /*obtain a random 1st guess. */
|
||||
end /*forever*/ /* [↑] obtain rand 1st guess.*/
|
||||
$$1= '───── How many bulls and cows were guessed with '; $$2=" ? [─── or QUIT]"
|
||||
do until #()<2 | bull==4; say; call ask /*examine @ list; get answer.*/
|
||||
do ?=L to H; if @.?==. then iterate /*is this already eliminated ?*/
|
||||
call bull# ?,g /*obtain bulls and cows count.*/
|
||||
if bull\==bulls | cow\==cows then @.?=. /*eliminate this possibility. */
|
||||
end /*?*/
|
||||
end /*until*/
|
||||
|
||||
do tries=1 until #()<2 | bull==4; say
|
||||
call prompter
|
||||
do ?=L to H /*traipse through the whole list. */
|
||||
if @.?==. then iterate /*was this choice already eliminated ? */
|
||||
call bull# ?,g /*obtain the bulls and cows count. */
|
||||
if bull\==bulls | cow\==cows then @.?=. /*eliminate choice. */
|
||||
end /*?*/
|
||||
call #
|
||||
end /*tries*/
|
||||
|
||||
if #==0 then do; call serr "At least one of your responses was invalid."; exit; end
|
||||
say; say " ╔═════════════════════════════════════════════════╗"
|
||||
say " ║ ║"
|
||||
say " ║ Your secret Bulls and Cows number is: " g " ║"
|
||||
say " ║ ║"
|
||||
say " ╚═════════════════════════════════════════════════╝"; say
|
||||
say tries 'tries.'
|
||||
if #==0 then do; call serr "At least one of your responses was invalid."; exit; end
|
||||
say; say " ╔═════════════════════════════════════════════════╗"
|
||||
say " ║ ║"
|
||||
say " ║ Your secret Bulls and Cows number is: " g " ║"
|
||||
say " ║ ║"
|
||||
say " ╚═════════════════════════════════════════════════╝"; say
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
#: #=0; do k=L to H; if @.k==. then iterate; #=#+1; g=k; end; return #
|
||||
gen@: @.=.; L=1234; H=9876; do j=L to H; if genOK() then @.j=j; end; return
|
||||
genOK: if pos(0, j)\==0 then return 0; return \rep()
|
||||
rep: do k=1 for 3; if pos(substr(j,k,1),j,k+1)\==0 then return 1; end; return 0
|
||||
serr: say; say pad '***error*** ' ! arg(1); return
|
||||
#: #=0; do k=L to H; if @.k==. then iterate; #=#+1; g=k; end; return #
|
||||
gen@: @.=.; do j=L to H; if \rep() & pos(0, j)==0 then @.j=j; end; return
|
||||
rep: do k=1 for 3; if pos(substr(j, k, 1), j, k+1)\==0 then return 1; end; return 0
|
||||
serr: say; say '───── ***error*** ' ! arg(1); return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
bull#: parse arg n,q; L=length(n); bulls=0; cows=0 /*initialize some vars.*/
|
||||
do j=1 for L; if substr(n,j,1) \== substr(q,j,1) then iterate
|
||||
bulls=bulls+1 /*bump the bull counter. */
|
||||
q=overlay(.,q,j) /*disallow this for a cow count.*/
|
||||
bull#: parse arg n,q; w=length(n); bulls=0 /*W: # digits in N; bull cntr=0 */
|
||||
do j=1 for w; if substr(n, j, 1) \== substr(q, j, 1) then iterate
|
||||
bulls=bulls+1; q=overlay(., q, j) /*bump counter; disallow for cow*/
|
||||
end /*j*/ /* [↑] bull count═══════════════*/
|
||||
|
||||
do k=1 for L; _=substr(n,k,1); if pos(_, q)==0 then iterate
|
||||
cows=cows + 1 /*bump the cow counter. */
|
||||
q=translate(q, , _) /*this allows for multiple digits*/
|
||||
cows=0 /*set the number of cows to zero.*/
|
||||
do k=1 for w; _=substr(n, k, 1); if pos(_, q)==0 then iterate
|
||||
cows=cows + 1; q=translate(q, , _) /*bump counter; allow multiple #*/
|
||||
end /*k*/ /* [↑] cow count═══════════════*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
prompter: pad= '─────' /*define PAD characters for msgs.*/
|
||||
do forever; say
|
||||
say pad "How many bulls and cows were guessed with " g '? [─── or QUIT]'
|
||||
pull x 1 bull cow _ . /*PULL capitalizes the arguments.*/
|
||||
if abbrev('QUIT', x, 1) then exit /*the user wants to quit playing.*/
|
||||
select
|
||||
when bull=='' then != "no numbers were entered."
|
||||
when cow =='' then != "not enough numbers were entered."
|
||||
when _\=='' then != "too many numbers entered: " x
|
||||
when \datatype(bull, 'W') then != "1st number (bulls) not an integer: " bull
|
||||
when \datatype(cow , 'W') then != "2nd number (cows) not an integer: " cow
|
||||
when bull <0 | bull >4 then != "1st number (bulls) not 0 ──► 4: " bull
|
||||
when cow <0 | cow >4 then != "2nd number (cows) not 0 ──► 4: " cow
|
||||
when bull + cow > 4 then != "sum of bulls and cows can't be > 4: " x
|
||||
otherwise !=
|
||||
end /*select*/
|
||||
if !\=='' then do; call serr; iterate; end /*prompt the user and try again. */
|
||||
bull=bull/1; cow=cow/1; return /*normalize bulls & cows numbers.*/
|
||||
end /*forever*/
|
||||
ask: do forever; say $$1 g $$2; pull x 1 bull cow . /*display prompt; obtain answer.*/
|
||||
select /* [↑] PULL capitalizes the args*/
|
||||
when abbrev('QUIT', x, 1) then exit /*the user wants to quit playing.*/
|
||||
when bull == '' then != "no numbers were entered."
|
||||
when cow == '' then != "not enough numbers were entered."
|
||||
when words(x) > 2 then != "too many numbers entered: " x
|
||||
when \datatype(bull, 'W') then != "1st number (bulls) not an integer: " bull
|
||||
when \datatype(cow , 'W') then != "2nd number (cows) not an integer: " cow
|
||||
when bull <0 | bull >4 then != "1st number (bulls) not 0 ──► 4: " bull
|
||||
when cow <0 | cow >4 then != "2nd number (cows) not 0 ──► 4: " cow
|
||||
when bull + cow > 4 then != "sum of bulls and cows can't be > 4: " x
|
||||
otherwise !=
|
||||
end /*select*/
|
||||
if !\=='' then do; call serr; iterate; end /*prompt the user and try again. */
|
||||
bull=bull/1; cow=cow/1; return /*normalize bulls & cows numbers.*/
|
||||
end /*forever*/
|
||||
|
|
|
|||
75
Task/Bulls-and-cows-Player/Ring/bulls-and-cows-player.ring
Normal file
75
Task/Bulls-and-cows-Player/Ring/bulls-and-cows-player.ring
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
# Project : Bulls and cows/Player
|
||||
# Date : 2017/11/21
|
||||
# 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 "secret = " + secret + nl
|
||||
|
||||
possible = ""
|
||||
for i = 1234 to 9876
|
||||
possible = possible + string(i)
|
||||
next
|
||||
|
||||
see "guess a four-digit number with no digit used twice." + nl
|
||||
guesses = 0
|
||||
while true
|
||||
bulls = 0
|
||||
cows = 0
|
||||
if len(possible) = 4
|
||||
guess = possible
|
||||
else
|
||||
guess = substr(possible, 4*random(len(possible) / 4) - 3, 4)
|
||||
ok
|
||||
see "computer guesses " + guess + nl
|
||||
guesses = guesses + 1
|
||||
if guess = secret
|
||||
see "correctly guessed after " + guesses + " guesses!" + nl
|
||||
exit
|
||||
ok
|
||||
if len(guess) = 4
|
||||
count(secret, guess, bulls, cows)
|
||||
ok
|
||||
i = 1
|
||||
testbulls = 0
|
||||
testcows = 0
|
||||
while i <= len(possible)
|
||||
temp = substr(possible, i, 4)
|
||||
if len(guess) = 4
|
||||
count(temp, guess, testbulls, testcows)
|
||||
ok
|
||||
if bulls=testbulls
|
||||
if cows=testcows
|
||||
i = i + 4
|
||||
ok
|
||||
else
|
||||
possible = left(possible, i-1) + substr(possible, i+4)
|
||||
ok
|
||||
end
|
||||
if substr(possible, secret) = 0
|
||||
exit
|
||||
ok
|
||||
end
|
||||
|
||||
func count(secret, guess, bulls, cows)
|
||||
bulls = 0
|
||||
cows = 0
|
||||
for nr = 1 to 4
|
||||
c = secret[nr]
|
||||
if guess != 0
|
||||
if guess[nr] = c
|
||||
bulls = bulls + 1
|
||||
if substr(guess, c) > 0
|
||||
cows = cows + 1
|
||||
ok
|
||||
ok
|
||||
ok
|
||||
next
|
||||
see "giving " + bulls + " bull(s) and " + cows + " cow(s)." + nl
|
||||
return [bulls, cows]
|
||||
105
Task/Bulls-and-cows-Player/VBA/bulls-and-cows-player.vba
Normal file
105
Task/Bulls-and-cows-Player/VBA/bulls-and-cows-player.vba
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
Option Explicit
|
||||
|
||||
Sub Main_Bulls_And_Cows_Player()
|
||||
Dim collSoluces As New Collection, Elem As Variant, Soluce As String
|
||||
Dim strNumber As String, cpt As Byte, p As Byte
|
||||
Dim i As Byte, Bulls() As Boolean, NbBulls As Byte, Cows As Byte, Poss As Long
|
||||
Const NUMBER_OF_DIGITS As Byte = 4
|
||||
|
||||
strNumber = CreateNb(NUMBER_OF_DIGITS)
|
||||
Debug.Print "TOSS : " & StrConv(strNumber, vbUnicode)
|
||||
Debug.Print "---------- START ------------"
|
||||
Set collSoluces = CollOfPossibleNumbers
|
||||
Poss = collSoluces.Count
|
||||
For Each Elem In collSoluces
|
||||
'Debug.Print "Number of possibilities : " & Poss
|
||||
Debug.Print "Attempt : " & StrConv(Elem, vbUnicode)
|
||||
NbBulls = 0: Soluce = Elem
|
||||
ReDim Bulls(NUMBER_OF_DIGITS - 1)
|
||||
For i = 1 To NUMBER_OF_DIGITS
|
||||
If IsBull(strNumber, Mid(Elem, i, 1), i) Then
|
||||
Bulls(i - 1) = True: NbBulls = NbBulls + 1
|
||||
RemoveIfNotBull collSoluces, Mid(Elem, i, 1), i
|
||||
End If
|
||||
Next i
|
||||
Cows = 0
|
||||
For i = 1 To NUMBER_OF_DIGITS
|
||||
If Not Bulls(i - 1) Then
|
||||
If IsCow(collSoluces, strNumber, Mid(Elem, i, 1), p) Then
|
||||
If Not Bulls(p - 1) Then Cows = Cows + 1
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
Poss = collSoluces.Count
|
||||
Debug.Print "Bulls : " & NbBulls & ", Cows : " & Cows
|
||||
If Poss = 1 Then Exit For
|
||||
Next
|
||||
Debug.Print "---------- THE END ------------"
|
||||
Debug.Print "TOSS WAS : " & StrConv(strNumber, vbUnicode) & " We found : " & StrConv(Soluce, vbUnicode)
|
||||
End Sub
|
||||
|
||||
Function CreateNb(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
|
||||
CreateNb = strTemp
|
||||
End Function
|
||||
|
||||
Function CollOfPossibleNumbers() As Collection
|
||||
Dim TempColl As New Collection
|
||||
Dim x As String
|
||||
Dim i As Long
|
||||
Dim Flag As Boolean
|
||||
Dim b As Byte
|
||||
|
||||
For i = 1234 To 9876
|
||||
Flag = False
|
||||
For b = 1 To 4
|
||||
x = CStr(i)
|
||||
If Len(Replace(x, Mid(x, b, 1), "")) < 3 Then
|
||||
Flag = True: Exit For
|
||||
End If
|
||||
Next
|
||||
If Not Flag Then TempColl.Add x, x
|
||||
Next i
|
||||
Set CollOfPossibleNumbers = TempColl
|
||||
End Function
|
||||
|
||||
Function IsBull(strgNb As String, Digit As String, place As Byte) As Boolean
|
||||
IsBull = (Mid(strgNb, place, 1) = Digit)
|
||||
End Function
|
||||
|
||||
Function IsCow(C As Collection, strgNb As String, Digit As String, place As Byte) As Boolean
|
||||
If (InStr(strgNb, Digit) > 0) Then
|
||||
IsCow = True: place = InStr(strgNb, Digit)
|
||||
RemoveIfNotCow C, Digit
|
||||
End If
|
||||
End Function
|
||||
|
||||
Sub RemoveIfNotBull(C As Collection, Digit As String, place As Byte)
|
||||
Dim E As Variant
|
||||
|
||||
For Each E In C
|
||||
If Mid(E, place, 1) <> Digit Then C.Remove E
|
||||
Next
|
||||
End Sub
|
||||
|
||||
Sub RemoveIfNotCow(C As Collection, Digit As String)
|
||||
Dim E As Variant
|
||||
|
||||
For Each E In C
|
||||
If (InStr(E, Digit) = 0) Then C.Remove E
|
||||
Next
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue