On a sheet of paper, the players each write a 4-digit secret number.
The digits must be all different. Then, in turn, the players try to guess their opponent's number who
gives the number of matches. If the matching digits are in their right positions, they are "bulls",
if in different positions, they are "cows".
Example: Secret number: 4271 Opponent's try: 1234
Answer: 1 bull and 2 cows. (The bull is "2", the cows are "4" and "1".)
What steps you will take to make a mobile app for this game?
Write pseudo code for executing game logic?
- Create a new Flutter app.
- Add a function to calculate bulls and cows. Here we do 3 things ok 4.
- Send a user input as an argument.
- Have a random number generator to generate secret number.
- And then make checks on the user input number against the secret number.
- then return the secret number, cows and bulls. (We can either create a model to do this.. or behave badly and return it as a Map.)
- Add text Field for user input.
- Call the above function every time the user calls onSubmitted method.
- Show calculated cows and bulls along with secret numbers in a cool way.
- Finally, make sure the value changes every time the user inputs a new number!! (use a value listener builder or some other state management system.)
Map getBullsOrCows(String input) {
final random = Random();
String secret = '';
for (var i = 0; i < 4; i++) {
secret = secret + random.nextInt(9).toString();
}
final a = x.split('');
final b = secret.split('');
int bull = 0;
int cow = 0;
for (int i = 0; i < a.length; i++) {
if (b.contains(a[i])) {
if (a[i] == b[i]) {
cow++;
} else {
bull++;
}
}
}
return {'secret': secret, 'bull': '$bull', 'cow': '$cow'};
}