How to resolve the algorithm Bulls and cows step by step in the Mathematica/Wolfram Language programming language
How to resolve the algorithm Bulls and cows step by step in the Mathematica/Wolfram Language programming language
Table of Contents
Problem Statement
Bulls and Cows is an old game played with pencil and paper that was later implemented using computers.
Create a four digit random number from the digits 1 to 9, without duplication. The program should:
The score is computed as:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bulls and cows step by step in the Mathematica/Wolfram Language programming language
Explanation:
The code is a game of Bulls and Cows, where the user has to guess a four-digit code generated by the computer. The game provides hints to the user in the form of "bulls" and "cows":
- Bulls: The number of digits that are correct and in the correct position.
- Cows: The number of digits that are correct but in the wrong position.
Detailed Breakdown:
-
digits: The code generates a random four-digit code using
FixedPointList
. This ensures that the generated code has distinct digits. -
codes: The character codes of the digits in the generated code are stored in
codes
as a list. -
While loop: The code enters a
While
loop that continues until the user breaks out by entering$Canceled
. -
User input: Inside the loop, the user is prompted to input a guess. If the user cancels the input by pressing Ctrl+C or Ctrl+Z, the loop breaks out.
-
Guess evaluation: The character codes of the user's guess are stored in
userCodes
. The code then checks ifuserCodes
matchescodes
. If they match, the game ends with a message indicating that the user guessed correctly. -
Bulls and cows calculation: If
userCodes
does not matchcodes
, the code checks if the lengths of both lists are equal. If they are not, the user is prompted to enter a four-digit guess. -
Bulls and cows printing: If the lengths of
userCodes
andcodes
are equal, the code calculates the number of bulls and cows. It does this by subtractinguserCodes
fromcodes
and counting the number of occurrences of 0. This gives the number of correct digits in the correct position (bulls). The number of cows is calculated as the intersection ofcodes
anduserCodes
minus the number of bulls. -
Game continues: Finally, the code prints the user's guess followed by the number of bulls and cows. The loop continues until the user breaks out by guessing correctly or canceling the input.
Source code in the wolfram programming language
digits=Last@FixedPointList[If[Length@Union@#==4,#,Table[Random[Integer,{1,9}],{4}]]&,{}]
codes=ToCharacterCode[StringJoin[ToString/@digits]];
Module[{r,bulls,cows},
While[True,
r=InputString[];
If[r===$Canceled,Break[],
With[{userCodes=ToCharacterCode@r},
If[userCodes===codes,Print[r<>": You got it!"];Break[],
If[Length@userCodes==Length@codes,
bulls=Count[userCodes-codes,0];cows=Length@Intersection[codes,userCodes]-bulls;
Print[r<>": "<>ToString[bulls]<>"bull(s), "<>ToString@cows<>"cow(s)."],
Print["Guess four digits."]]]]]]]
You may also check:How to resolve the algorithm Vogel's approximation method step by step in the Perl programming language
You may also check:How to resolve the algorithm World Cup group stage step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Pascal matrix generation step by step in the Ruby programming language
You may also check:How to resolve the algorithm Introspection step by step in the Racket programming language
You may also check:How to resolve the algorithm Pointers and references step by step in the Kotlin programming language