This function will draw a card in the console
vector<string> draw_card(const Carta& carta){
//The class Carta has two members, carta.value and carta.seme
//both are integers representing the card value or suit
//the card is represented on 9 lines where the second will be the
//card name and suite, while the middle will show a pattern
/*
|-----------|
|Asso picche|
| |
| * |
| * * |
| ***** |
| * * |
| * * |
| |
*/
//card opening and closing line
string card_delimiter = "|-----------|";
// vector representing the card
vector<string> v;
v.push_back(card_delimiter);
// construct the second line, so that it will fit different cards names
// and suites
string nome_seme = create_semi(carta.seme);
string nome_valore = create_nome_valore(carta.valore);
string init_string = "| |";
//characters in the init_string will be subsittuted according to the string
//nome_seme and then after a space nome_valore
uint valore_offset = 1;
uint seme_offset = nome_valore.size() + valore_offset + 1;
for(uint i = 0; i < init_string.size(); ++i){
if(i >= valore_offset && i < nome_valore.size() + valore_offset){
init_string[i] = nome_valore[i - valore_offset];
}
if(i >= seme_offset && i <= nome_seme.size() + seme_offset - 1){
init_string[i] = nome_seme[i - seme_offset];
}
}
// update the main vector holding the string
v.push_back(init_string);
// initialize the pattern for the center drawing
vector<uint16_t> middle_matrix;
middle_matrix.push_back(0x0000); //0 b0000000000000000
middle_matrix.push_back(0x0400); //1024 b0000010000000000
middle_matrix.push_back(0x0A00); //2560 b0000101000000000
middle_matrix.push_back(0x1F00); //7936 b0001111100000000
middle_matrix.push_back(0x2080); //8320 b0010000010000000
middle_matrix.push_back(0x4040); //16448 b0100000001000000
middle_matrix.push_back(0x0000); //0 b0000000000000000
// scroll through the vector
for(uint i = 0; i < middle_matrix.size(); ++i){
string line = "|";
// bitshift the mask to find if the character will be a space or a *
for(uint16_t mask = 0x8000; mask != 0x0010; mask >>= 1){
if(middle_matrix[i] & mask){ line += "*"; }
else{ line += " "; }
}
line += "|";
v.push_back(line);
}
// close the card
v.push_back(card_delimiter);
//print the card
for(unsigned int i = 0; i < v.size(); ++i){
cout << v[i] << endl;
}
return v;
}
I'm posting this for a couple of questions, first of all I'm pretty new to C++ and I would like to know if you would write the whole procedure differently, maybe using replace functions or functions to map the bit pattern.
Second this version will print only one pattern, what would you suggest to do for the other 13? What I did now was to take a text file, draw the pattern, realize it could be expressed in binary and now I'm here with this integers.
How would you go to maintain clarity? Shall I read in the pattern from a file? Declare a static array that contains the numbered patterns? Where shall I place the function that reads the values? or maybe create a class that reads from a "pattern" file and stores them in some member variable, and call it afterwards?
#include
lines, and amain()
that shows how to call your function. As it is, we can't even compile the posted code, because you haven't provided the definition of theCarta
type! \$\endgroup\$