我必须制作一副纸牌来制作扑克牌。我创建的卡片在屏幕上打印时看起来像这样:
----------
|K |
| |
| |
| |
| |
| |
| |
| |
| K|
----------
我将卡片输出定义为:
void deck::cardKing(){
cout << "----------" << endl;
cout << "|K " << setw(7) << "|" << endl;
cout << "|" << setw(9) << "|" << endl;
cout << "|" << setw(9) << "|" << endl;
cout << "|" << setw(9) << "|" << endl;
cout << "|" << setw(9) << "|" << endl;
cout << "|" << setw(9) << "|" << endl;
cout << "|" << setw(9) << "|" << endl;
cout << "|" << setw(9) << "|" << endl;
cout << "|" << setw(9) << "K|" << endl;
cout << "----------" << endl;
}
我的问题是,截至目前,当我打印出整副牌时,它们像这样(垂直)排成一行:
----------
|3 |
| |
| |
| |
| |
| |
| |
| |
| 3|
----------
----------
|10 |
| |
| |
| |
| |
| |
| |
| |
| 10|
----------
----------
|K |
| |
| |
| |
| |
| |
| |
| |
| K|
----------
当我需要像这样显示它们时:
--------- ---------
|K | |2 |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| K| | 2|
--------- ---------
让卡片水平显示而不是垂直显示的最佳方法是什么?我可以使用一张卡片的图片而不是将它们打印在屏幕上吗?
最佳答案
解决这个问题的一种方法是收集每一行的内容
enum { eEdge, eTop, eMid, eBot, eMax };
std::ostringstream cardline[eMax];
对于每张卡片,更新卡片行而不是写入屏幕
void Deck::CardKing()
{
cardline[eEdge] << "--------- ";
cardline[eTop] << "|K | ";
cardline[eMid] << "| | ";
cardline[eBot] << "| K| ";
}
然后当你需要打印的时候
std::cout << cardline[eEdge].str() << std::endl;
std::cout << cardline[eTop].str() << std::endl;
for (int ii = 2; ii < 9; ++ii)
std::cout << cardline[eMid].str() << std::endl;
std::cout << cardline[eBot].str() << std::endl;
std::cout << cardline[eEdge].str() << std::endl;
然后,当你想要一个新系列的卡片时
for (int ii = 0; ii < eMax; ++ii)
cardline[ii].str();
关于c++ - 并排显示卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46145058/