c++ - 并排显示卡片

标签 c++ formatting

我必须制作一副纸牌来制作扑克牌。我创建的卡片在屏幕上打印时看起来像这样:

----------
|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/

相关文章:

c++ - #define 数组索引不起作用

c++ - 标准库实现可以专门化标准类型吗?

c++ - 我将如何实现 SJF 和 Round Robin 调度模拟器?

windows - 使用 PowerShell 在表中格式化三个数据列表

c++ - 正确格式化有错误的数字 (C++)

c++ - 快速计算 n 次 10 的负 m 次方

c++ - std::unordered_map operator[] 是否对不存在的键进行零初始化?

Java DecimalFormat 将不正确的十进制数解析为整数

python - 格式化可选文本字符串的更有效方法

database - 出生日期如 "YYYY-00-00"/"00/00/YYYY"