Reputation: 31
The title is a mess, it's probably a lot faster to just see the code
p1_dado = rand() % 6 + 1;
switch (p1_dado) {
case 1:
cout << ".-----." << endl;
cout << "| |" << endl;
cout << "| o |" << endl;
cout << "| |" << endl;
cout << "._____." << endl << endl;
p1_somma = p1_somma + p1_dado;
break;
case 2:
cout << ".-----." << endl;
cout << "| o |" << endl;
cout << "| |" << endl;
cout << "| o |" << endl;
cout << "._____." << endl << endl;
p1_ndadi--;
break;
case 3:
cout << ".-----." << endl;
cout << "|o |" << endl;
cout << "| o |" << endl;
cout << "| o|" << endl;
cout << "._____." << endl << endl;
p1_somma = p1_somma + p1_dado;
break;
case 4:
cout << ".-----." << endl;
cout << "| o o |" << endl;
cout << "| |" << endl;
cout << "| o o |" << endl;
cout << "._____." << endl << endl;
p1_somma = p1_somma + p1_dado;
break;
case 5:
cout << ".-----." << endl;
cout << "| o o |" << endl;
cout << "| o |" << endl;
cout << "| o o |" << endl;
cout << "._____." << endl << endl;
p1_ndadi--;
break;
case 6:
cout << ".-----." << endl;
cout << "| o o |" << endl;
cout << "| o o |" << endl;
cout << "| o o |" << endl;
cout << "._____." << endl << endl;
p1_somma = p1_somma + p1_dado;
break;
}
I should also mention that this piece of code is in a for loop so the switch case will be ran for 4-5 times. the way this outputs is all the dices on top of each other, is there a way to show all the dices in 1 "row" ? Like:
1 2 3 4 5
instead of:
1
2
3
4
5
Thanks for the help :)
Upvotes: 3
Views: 2383
Reputation: 3319
Code snippet:
char* hline = ".-----.";
char* dice[6][3] = { {"| |", "| o |", "| |" }, { ... }}; // 3 middle lines for each dice
for (int l=0; l < 5; ++l) {
for(int i=0; i < 6; ++i) // or any random dice sequence with index = dice value - 1
cout << (l == 0 || l == 4) ? hline : dice[i][l-1] << " ";
cout << "\n";
}
Upvotes: 0
Reputation: 153840
Assuming you install something intercepting and rearranging the output the actual output can stay mostly unchanged. Since I'd be inclined to key sending compiled lines off a flush for the stream, the only change really necessary is replacing the excessive use of std::endl
by uses of '\n'
. That's something I strongly recommend anyway (for an explanation have a look at the link).
So, assuming there is a something called dicebuf
defined, the main()
function could look like this (the interesting part is the first definition inside the function):
int main()
{
dicebuf buf(std::cout);
for (int i = 0; i != 5; ++i)
{
int p1_dado = rand() % 6 + 1;
switch (p1_dado) {
case 1:
std::cout << ".-----." << '\n';
std::cout << "| |" << '\n';
std::cout << "| o |" << '\n';
std::cout << "| |" << '\n';
std::cout << "._____." << '\n' << '\n';
break;
case 2:
std::cout << ".-----." << '\n';
std::cout << "| o |" << '\n';
std::cout << "| |" << '\n';
std::cout << "| o |" << '\n';
std::cout << "._____." << '\n' << '\n';
break;
case 3:
std::cout << ".-----." << '\n';
std::cout << "|o |" << '\n';
std::cout << "| o |" << '\n';
std::cout << "| o|" << '\n';
std::cout << "._____." << '\n' << '\n';
break;
case 4:
std::cout << ".-----." << '\n';
std::cout << "| o o |" << '\n';
std::cout << "| |" << '\n';
std::cout << "| o o |" << '\n';
std::cout << "._____." << '\n' << '\n';
break;
case 5:
std::cout << ".-----." << '\n';
std::cout << "| o o |" << '\n';
std::cout << "| o |" << '\n';
std::cout << "| o o |" << '\n';
std::cout << "._____." << '\n' << '\n';
break;
case 6:
std::cout << ".-----." << '\n';
std::cout << "| o o |" << '\n';
std::cout << "| o o |" << '\n';
std::cout << "| o o |" << '\n';
std::cout << "._____." << '\n' << '\n';
break;
}
}
}
The idea here is that the dicebuf
constructor injects a custom stream buffer which aggregates lines. It simply doesn't output things but just adds characters to a current row. If a newline ('\n'
) is encountered, it goes to the next row. If a two newlines in a row are encountered, it assumes it time to go back to the top and carry on adding to the current line.
Creating such a utility intercepting the output is done by implementing a suitable stream buffer, i.e., a class derived from std::streambuf
. Here is an example implementation:
#include <utility>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
#include <vector>
struct dicebuf
: std::streambuf
{
std::ostream& out;
std::streambuf* sbuf;
std::vector<std::string> rows;
std::vector<std::string>::size_type row;
public:
dicebuf(std::ostream& out)
: out(out)
, sbuf(out.rdbuf())
, rows(1)
, row(0) {
out.rdbuf(this);
}
~dicebuf() {
this->sync();
this->out.rdbuf(this->sbuf);
}
int overflow(int c) {
if (c != std::char_traits<char>::eof()) {
if (c == '\n') {
if (this->rows[this->row].empty()) {
this->row = 0;
}
else {
++this->row;
if (this->rows.size() == this->row) {
this->rows.push_back(std::string());
}
}
}
else {
this->rows[this->row].push_back(c);
}
}
return std::char_traits<char>::not_eof(c);
}
int sync() {
std::ostream out(this->sbuf);
std::copy(this->rows.begin(), this->rows.end(),
std::ostream_iterator<std::string>(out, "\n"));
this->sbuf->pubsync();
this->row = 0;
this->rows.resize(0);
this->rows.push_back(std::string());
return 0;
}
};
Together with the main()
above it should produce the output as desired. If you want multiple lines with results, you'll just inject a flush, e.g., using
std::flush;
at strategic points.
Upvotes: 2