标题很乱,看到代码
可能要快得多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;
}
我还应该提一下,这段代码在for循环中,因此switch case将运行4-5次。 这个输出的方式是彼此重叠的所有骰子,有没有办法在1&#34;行&#34;中显示所有骰子。 ?像:
1 2 3 4 5
而不是:
1
2
3
4
5
感谢您的帮助:)
答案 0 :(得分:2)
假设您安装了拦截和重新排列输出的东西,实际输出可以保持不变。由于我倾向于将流编译行的密钥发送到流的刷新,因此唯一真正需要的更改是使用'\n'
替换excessive use of std::endl
。这是我强烈推荐的东西(有关解释,请查看link)。
因此,假设有一个名为dicebuf
的东西被定义,main()
函数可能看起来像这样(有趣的部分是函数内的第一个定义):
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;
}
}
}
这里的想法是dicebuf
构造函数注入一个聚合行的自定义流缓冲区。它根本不输出东西,只是将字符添加到当前行。如果遇到换行符('\n'
),则会转到下一行。如果遇到连续的两个换行符,则会假定有时间返回到顶部并继续添加到当前行。
通过实现合适的流缓冲区(即,从std::streambuf
派生的类)来创建拦截输出的这种实用程序。以下是一个示例实现:
#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;
}
};
与上面的main()
一起,它应该根据需要产生输出。如果您想要多行显示结果,您只需注入一个刷新,例如,使用
std::flush;
在战略要点。
答案 1 :(得分:0)
代码段:
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";
}