Terminal output in much more user-friendly format

This commit is contained in:
Matúš Púll 2024-11-02 17:54:58 +01:00
parent e403157b65
commit 79c02ef701
2 changed files with 24 additions and 6 deletions

View file

@ -37,11 +37,21 @@ int main(void) {
if(gen == RANDOM)
sequence = generate(N, M);
else if(gen == HUMAN) {
cout << "Enter " << N << " space-separated colors from 0 to " << M-1 << std::endl;
cout << "Enter " << N << " space-separated numbers from 0 to " << M-1 << std::endl;
for(int n = 0; n < N; n++)
cin >> sequence[n];
}
// Terminal clearing
cout << std::endl;
// Human info
if(player == HUMAN) {
cout << "Guesses are " << N << " space-separated numbers from 0 to " << M-1 << std::endl
<< "Responses are in format [correct out of place] / [correct in place]" << std::endl;
cout << std::endl;
}
// Init solver
Solver bot(N, M);
@ -62,7 +72,6 @@ int main(void) {
// Human playing
else if(player == HUMAN) {
cout << "Guess " << N << " space-separated colors from 0 to " << M-1 << std::endl;
for(int n = 0; n < N; n++)
cin >> guess[n];
@ -74,8 +83,11 @@ int main(void) {
if(response[1] == N) break;
}
// Terminal clearing
cout << std::endl;
// Print game statistics
cout << "How well did the game go\n";
cout << "How did the game go\n";
for(auto guess : history)
cout << format_guess_history(sequence, guess) << std::endl;

View file

@ -6,6 +6,11 @@ using std::vector;
using std::string;
using std::to_string;
#define YELLOW "\033[33m"
#define GREEN "\033[32m"
#define BACK "\033[0m"
vector<int> validate(vector<int> sequence, vector<int> guess) {
int S = sequence.size();
@ -40,7 +45,8 @@ vector<int> validate(vector<int> sequence, vector<int> guess) {
}
string format_response(vector<int> response) {
return to_string(response[0]) + " somewhere / " + to_string(response[1]) + " on the right spot\n";
return YELLOW + to_string(response[0]) + BACK + " / " +
GREEN + to_string(response[1]) + BACK + "\n";
}
string format_guess(vector<int> guess) {
string r = "";
@ -56,7 +62,7 @@ string format_guess_history(vector<int> sequence, vector<int> guess) {
// Find correct values
for(int i = 0; i < N; i++) {
if(sequence[i] == guess[i]) {
r[i] = "\033[32m" + to_string(guess[i]) + "\033[0m";
r[i] = GREEN + to_string(guess[i]) + BACK;
guess[i] = -1;
sequence[i] = -1;
}
@ -67,7 +73,7 @@ string format_guess_history(vector<int> sequence, vector<int> guess) {
if(guess[i] == -1) continue;
for(int j = 0; j < N; j++) {
if(sequence[j] == guess[i]) {
r[i] = "\033[33m" + to_string(guess[i]) + "\033[0m";
r[i] = YELLOW + to_string(guess[i]) + BACK;
guess[i] = -1;
sequence[i] = -1;
break;