Level 1 : Project 4
The game Numbermind is a game for two, which is to guess a number.
The principle of the game is as follows:
-
The first player suggests a combination of 8 digits representing the number to guess.
-
The second player announces a proposition of 8 digits, if this proposition corresponds to the number to guess, this player is a winner otherwise we show him his proposal which we keep all the numbers well placed and the rest is replaced by dashes while mentioning the correct numbers but misplaced in the proposal.
-
Repeat the previous step until you find the number sought or reach a number of tries equal to 8.
Note:
This command clear the terminal to hide the suggested number.
print("\x1B[2J\x1B[0;0H");Example:
For the guess phone number 68456231, the program displays:
Propose a Phone Number
56426179
::::: --4-6--- :::::
5 at position 1 is correct but badly placed
6 at position 2 is correct but badly placed
2 at position 4 is correct but badly placed
1 at position 6 is correct but badly placed
You have 7 tries
Propose a Phone Number
63456331
::::: 6-456-31 :::::
You have 6 tries
Propose a Phone Number
68456231
::::: 68456231 :::::
Well done, you won
We propose to write a Dart program which:
-
Check if the proposed telephone number is composed only by 8 digits.
-
Check each number proposed by the second player, keeping the numbers well placed and replacing the rest with dashes while mentioning the correct ones but badly placed in the proposal and the number of tests remaining.
-
Stop the game once the second player gives a number identical to the number to guess or we reach a number of tries equal to 8.
-
Display the message "Well done, you won" if player 2 manages to guess the number and the message "Sorry, you lost" if he does not guess the number after 8 tries.
Solution:
import 'dart:io';
String suggestNumber() {
String number;
int testInt;
do {
print("Enter a suggested Phone Number");
number = stdin.readLineSync();
testInt = int.tryParse(number);
} while ((testInt == null) || (number.length != 8));
return number;
}
void play(String n) {
int tries = 8;
String propose = "";
do {
//printing the number of tries left
print("You have $tries tries");
int testInt;
//asking for a number
do {
print("Propose a Phone Number");
propose = stdin.readLineSync();
testInt = int.tryParse(propose);
} while ((testInt == null) || (propose.length != 8));
//Checking the number
var result = "", numbersLeft = [];
for (int i = 0; i < 8; i++) {
if (propose[i] == n[i])
result += n[i];
else {
result += "-";
numbersLeft.add(n[i]);
}
}
print("::::: $result :::::");
//lokking for the correct ones but badly placed;
for (int i = 0; i < 8; i++) {
if (propose[i] != n[i]) {
if (numbersLeft.contains(propose[i])) {
print("${propose[i]} at position ${i + 1} is correct but badly placed");
numbersLeft.remove(propose[i]);
}
}
}
//deincremennt the tries
tries--;
} while ((tries > 0) && (propose != n));
//printing the final result
if (propose == n)
print("Well done, you won");
else
print("Sorry, you lost");
}
void main() {
var number = suggestNumber();
//Clear the terminal to hide the suggested number.
print("\x1B[2J\x1B[0;0H");
play(number);
}
Propose a Phone Number
384
Propose a Phone Number
434932
Propose a Phone Number
ZADS8
Propose a Phone Number
ZA
Propose a Phone Number
Propose a Phone Number
12398876
::::: 123---7- :::::
8 at position 4 is correct but badly placed
6 at position 7 is correct but badly placed
You have 7 tries
Propose a Phone Number
87654321
::::: -------- :::::
8 at position 0 is correct but badly placed
7 at position 1 is correct but badly placed
6 at position 2 is correct but badly placed
5 at position 3 is correct but badly placed
4 at position 4 is correct but badly placed
3 at position 5 is correct but badly placed
2 at position 6 is correct but badly placed
1 at position 7 is correct but badly placed
You have 6 tries
Propose a Phone Number
87654321
::::: -------- :::::
8 at position 0 is correct but badly placed
7 at position 1 is correct but badly placed
6 at position 2 is correct but badly placed
5 at position 3 is correct but badly placed
4 at position 4 is correct but badly placed
3 at position 5 is correct but badly placed
2 at position 6 is correct but badly placed
1 at position 7 is correct but badly placed
You have 5 tries
Propose a Phone Number
76543123
::::: ---4---- :::::
7 at position 0 is correct but badly placed
6 at position 1 is correct but badly placed
5 at position 2 is correct but badly placed
You have 8 tries
Propose a Phone Number
21456378
::::: -------- :::::
2 at position 0 is correct but badly placed
1 at position 1 is correct but badly placed
5 at position 3 is correct but badly placed
6 at position 4 is correct but badly placed
3 at position 5 is correct but badly placed
8 at position 7 is correct but badly placed
You have 7 tries
Propose a Phone Number
83652109
::::: 8------- :::::
3 at position 1 is correct but badly placed
6 at position 2 is correct but badly placed
5 at position 3 is correct but badly placed
2 at position 4 is correct but badly placed
1 at position 5 is correct but badly placed
You have 6 tries
Propose a Phone Number
86352118
::::: 8------- :::::
6 at position 1 is correct but badly placed
3 at position 2 is correct but badly placed
5 at position 3 is correct but badly placed
2 at position 4 is correct but badly placed
1 at position 5 is correct but badly placed
8 at position 7 is correct but badly placed
You have 5 tries
Propose a Phone Number
8112536
Propose a Phone Number
81125368
::::: 8-125--- :::::
1 at position 1 is correct but badly placed
6 at position 6 is correct but badly placed
8 at position 7 is correct but badly placed
You have 4 tries
Propose a Phone Number
88125683
::::: 881256-- :::::
3 at position 7 is correct but badly placed
You have 3 tries
You have 8 tries
Propose a Phone Number
12345678
::::: -------- :::::
1 at position 1 is correct but badly placed
3 at position 3 is correct but badly placed
4 at position 4 is correct but badly placed
7 at position 7 is correct but badly placed
8 at position 8 is correct but badly placed
You have 7 tries
Propose a Phone Number
87431099
::::: ---3---- :::::
8 at position 1 is correct but badly placed
7 at position 2 is correct but badly placed
4 at position 3 is correct but badly placed
1 at position 5 is correct but badly placed
9 at position 7 is correct but badly placed
You have 6 tries
Propose a Phone Number
47833911
::::: ---3---1 :::::
4 at position 1 is correct but badly placed
7 at position 2 is correct but badly placed
8 at position 3 is correct but badly placed
9 at position 6 is correct but badly placed
You have 5 tries
Propose a Phone Number
98937441
::::: --93---1 :::::
8 at position 2 is correct but badly placed
7 at position 5 is correct but badly placed
4 at position 6 is correct but badly placed
4 at position 7 is correct but badly placed
You have 4 tries
Propose a Phone Number
847893471
Propose a Phone Number
74934481
::::: 74934-81 :::::
You have 3 tries
Propose a Phone Number
74934781
::::: 74934781 :::::
Well done, you won