Bulls and cows/Player: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 577: | Line 577: | ||
Score | 9 bull, 0 cow |
Score | 9 bull, 0 cow |
||
--------+--------------------</lang> |
--------+--------------------</lang> |
||
=={{header|C sharp|C#}}== |
|||
{{works with|C#|3.0}} |
|||
<lang csharp> |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
namespace BullsAndCows |
|||
{ |
|||
class Program |
|||
{ |
|||
const int ANSWER_SIZE = 4; |
|||
static IEnumerable<string> Permutations(int size) |
|||
{ |
|||
if (size > 0) |
|||
{ |
|||
foreach (string s in Permutations(size - 1)) |
|||
foreach (char n in "123456789") |
|||
if (!s.Contains(n)) |
|||
yield return s + n; |
|||
} |
|||
else |
|||
yield return ""; |
|||
} |
|||
static IEnumerable<T> Shuffle<T>(IEnumerable<T> source) |
|||
{ |
|||
Random random = new Random(); |
|||
List<T> list = source.ToList(); |
|||
while (list.Count > 0) |
|||
{ |
|||
int ix = random.Next(list.Count); |
|||
yield return list[ix]; |
|||
list.RemoveAt(ix); |
|||
} |
|||
} |
|||
static bool ReadBullsCows(out int bulls, out int cows) |
|||
{ |
|||
string[] input = Console.ReadLine().Split(',').ToArray(); |
|||
bulls = cows = 0; |
|||
if (input.Length < 2) |
|||
return false; |
|||
else |
|||
return int.TryParse(input[0], out bulls) |
|||
&& int.TryParse(input[1], out cows); |
|||
} |
|||
static void Main(string[] args) |
|||
{ |
|||
Console.WriteLine("Bulls and Cows"); |
|||
Console.WriteLine("=============="); |
|||
Console.WriteLine(); |
|||
List<string> answers = Shuffle(Permutations(ANSWER_SIZE)).ToList(); |
|||
while (answers.Count > 1) |
|||
{ |
|||
string guess = answers[0]; |
|||
Console.Write("My guess is {0}. How many bulls, cows? ", guess); |
|||
int bulls, cows; |
|||
if (!ReadBullsCows(out bulls, out cows)) |
|||
Console.WriteLine("Sorry, I didn't understand that. Please try again."); |
|||
else |
|||
for (int ans = answers.Count - 1; ans >= 0; ans--) |
|||
{ |
|||
int tb = 0, tc = 0; |
|||
for (int ix = 0; ix < ANSWER_SIZE; ix++) |
|||
if (answers[ans][ix] == guess[ix]) |
|||
tb++; |
|||
else if (answers[ans].Contains(guess[ix])) |
|||
tc++; |
|||
if ((tb != bulls) || (tc != cows)) |
|||
answers.RemoveAt(ans); |
|||
} |
|||
} |
|||
if (answers.Count == 1) |
|||
Console.WriteLine("Hooray! The answer is {0}!", answers[0]); |
|||
else |
|||
Console.WriteLine("No possible answer fits the scores you gave."); |
|||
} |
|||
} |
|||
} |
|||
</lang> |
|||
Example output:- |
|||
<pre> |
|||
Bulls and Cows |
|||
============== |
|||
My guess is 7854. How many bulls, cows? 0,1 |
|||
My guess is 1539. How many bulls, cows? 1,2 |
|||
My guess is 2935. How many bulls, cows? 2,1 |
|||
My guess is 9635. How many bulls, cows? 1,3 |
|||
Hooray! The answer is 5936! |
|||
</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 718: | Line 815: | ||
I found the secret number! |
I found the secret number! |
||
It is: 6281 |
It is: 6281 |
||
</pre> |
|||
=={{header|C sharp|C#}}== |
|||
{{works with|C#|3.0}} |
|||
<lang csharp> |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
namespace BullsAndCows |
|||
{ |
|||
class Program |
|||
{ |
|||
const int ANSWER_SIZE = 4; |
|||
static IEnumerable<string> Permutations(int size) |
|||
{ |
|||
if (size > 0) |
|||
{ |
|||
foreach (string s in Permutations(size - 1)) |
|||
foreach (char n in "123456789") |
|||
if (!s.Contains(n)) |
|||
yield return s + n; |
|||
} |
|||
else |
|||
yield return ""; |
|||
} |
|||
static IEnumerable<T> Shuffle<T>(IEnumerable<T> source) |
|||
{ |
|||
Random random = new Random(); |
|||
List<T> list = source.ToList(); |
|||
while (list.Count > 0) |
|||
{ |
|||
int ix = random.Next(list.Count); |
|||
yield return list[ix]; |
|||
list.RemoveAt(ix); |
|||
} |
|||
} |
|||
static bool ReadBullsCows(out int bulls, out int cows) |
|||
{ |
|||
string[] input = Console.ReadLine().Split(',').ToArray(); |
|||
bulls = cows = 0; |
|||
if (input.Length < 2) |
|||
return false; |
|||
else |
|||
return int.TryParse(input[0], out bulls) |
|||
&& int.TryParse(input[1], out cows); |
|||
} |
|||
static void Main(string[] args) |
|||
{ |
|||
Console.WriteLine("Bulls and Cows"); |
|||
Console.WriteLine("=============="); |
|||
Console.WriteLine(); |
|||
List<string> answers = Shuffle(Permutations(ANSWER_SIZE)).ToList(); |
|||
while (answers.Count > 1) |
|||
{ |
|||
string guess = answers[0]; |
|||
Console.Write("My guess is {0}. How many bulls, cows? ", guess); |
|||
int bulls, cows; |
|||
if (!ReadBullsCows(out bulls, out cows)) |
|||
Console.WriteLine("Sorry, I didn't understand that. Please try again."); |
|||
else |
|||
for (int ans = answers.Count - 1; ans >= 0; ans--) |
|||
{ |
|||
int tb = 0, tc = 0; |
|||
for (int ix = 0; ix < ANSWER_SIZE; ix++) |
|||
if (answers[ans][ix] == guess[ix]) |
|||
tb++; |
|||
else if (answers[ans].Contains(guess[ix])) |
|||
tc++; |
|||
if ((tb != bulls) || (tc != cows)) |
|||
answers.RemoveAt(ans); |
|||
} |
|||
} |
|||
if (answers.Count == 1) |
|||
Console.WriteLine("Hooray! The answer is {0}!", answers[0]); |
|||
else |
|||
Console.WriteLine("No possible answer fits the scores you gave."); |
|||
} |
|||
} |
|||
} |
|||
</lang> |
|||
Example output:- |
|||
<pre> |
|||
Bulls and Cows |
|||
============== |
|||
My guess is 7854. How many bulls, cows? 0,1 |
|||
My guess is 1539. How many bulls, cows? 1,2 |
|||
My guess is 2935. How many bulls, cows? 2,1 |
|||
My guess is 9635. How many bulls, cows? 1,3 |
|||
Hooray! The answer is 5936! |
|||
</pre> |
</pre> |
||
Line 2,000: | Line 2,000: | ||
end function |
end function |
||
</lang> |
</lang> |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
<lang Mathematica> |
<lang Mathematica> |
||
Line 2,209: | Line 2,209: | ||
Your secret number is 1357 |
Your secret number is 1357 |
||
msl@64Lucid:~/perl$</lang> |
msl@64Lucid:~/perl$</lang> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2018.12}} |
|||
{{trans|Perl}} |
|||
<lang perl6># 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).grep: *.unique == 4; |
|||
repeat { |
|||
my $guess = @candidates.pick; |
|||
my ($bulls, $cows) = read-score; |
|||
@candidates .= grep: &score-correct; |
|||
# note how we declare our two subroutines within the repeat block. This |
|||
# limits the scope in which the routines are known to the scope in which |
|||
# they are needed and saves us a lot of arguments to our two routines. |
|||
sub score-correct($a) { |
|||
my $exact = [+] $a >>==<< $guess; |
|||
# number of elements of $a that match any element of $b |
|||
my $loose = +$a.grep: any @$guess; |
|||
return $bulls == $exact && $cows == $loose - $exact; |
|||
} |
|||
sub read-score() { |
|||
loop { |
|||
my $score = prompt "My guess: {$guess.join}.\n"; |
|||
if $score ~~ m:s/^ $<bulls>=(\d) $<cows>=(\d) $/ |
|||
and $<bulls> + $<cows> <= 4 { |
|||
return +$<bulls>, +$<cows>; |
|||
} |
|||
say "Please specify the number of bulls and cows"; |
|||
} |
|||
} |
|||
} while @candidates > 1; |
|||
say @candidates |
|||
?? "Your secret number is {@candidates[0].join}!" |
|||
!! "I think you made a mistake with your scoring.";</lang> |
|||
{{out}} |
|||
<pre>My guess: 4235. |
|||
2 1 |
|||
My guess: 1435. |
|||
2 1 |
|||
My guess: 1245. |
|||
2 1 |
|||
Your secret number is 1234!</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,894: | Line 2,842: | ||
Bulls: 1, Cows: 0 |
Bulls: 1, Cows: 0 |
||
Bad scoring! nothing fits those scores you gave.</pre> |
Bad scoring! nothing fits those scores you gave.</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo|2018.12}} |
|||
{{trans|Perl}} |
|||
<lang perl6># 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).grep: *.unique == 4; |
|||
repeat { |
|||
my $guess = @candidates.pick; |
|||
my ($bulls, $cows) = read-score; |
|||
@candidates .= grep: &score-correct; |
|||
# note how we declare our two subroutines within the repeat block. This |
|||
# limits the scope in which the routines are known to the scope in which |
|||
# they are needed and saves us a lot of arguments to our two routines. |
|||
sub score-correct($a) { |
|||
my $exact = [+] $a >>==<< $guess; |
|||
# number of elements of $a that match any element of $b |
|||
my $loose = +$a.grep: any @$guess; |
|||
return $bulls == $exact && $cows == $loose - $exact; |
|||
} |
|||
sub read-score() { |
|||
loop { |
|||
my $score = prompt "My guess: {$guess.join}.\n"; |
|||
if $score ~~ m:s/^ $<bulls>=(\d) $<cows>=(\d) $/ |
|||
and $<bulls> + $<cows> <= 4 { |
|||
return +$<bulls>, +$<cows>; |
|||
} |
|||
say "Please specify the number of bulls and cows"; |
|||
} |
|||
} |
|||
} while @candidates > 1; |
|||
say @candidates |
|||
?? "Your secret number is {@candidates[0].join}!" |
|||
!! "I think you made a mistake with your scoring.";</lang> |
|||
{{out}} |
|||
<pre>My guess: 4235. |
|||
2 1 |
|||
My guess: 1435. |
|||
2 1 |
|||
My guess: 1245. |
|||
2 1 |
|||
Your secret number is 1234!</pre> |
|||
=={{header|Red}}== |
=={{header|Red}}== |
||
Line 2,955: | Line 2,956: | ||
(halted) |
(halted) |
||
</pre> |
</pre> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
About a third of the REXX program deals with presentation and/or validation of answers. |
About a third of the REXX program deals with presentation and/or validation of answers. |
||
Line 3,189: | Line 3,191: | ||
Ye-haw! |
Ye-haw! |
||
</pre> |
</pre> |
||
=={{header|SenseTalk}}== |
=={{header|SenseTalk}}== |