我正在编码一个挂手游戏。我有一个由10种不同的悬挂式形状组成的字符串阵列。假设我的字符串阵列称为myhangmanpics:
std::string myHangmanPics[9];
这是一个索引中包含的内容的示例:
我存储在myhangmanpics [0]
的文本形状(手枪)的示例阵列的每个索引(尺寸10)包含一个不同的悬挂形状,例如上面的形状。我已经使用一个随机数生成器来随机选择一个hang子形状阵列的索引(从0-9),从而随机hang子形状。
好吧,现在问题。
我想从所选的每个错误的用户猜测中从所选索引中连续打印出一行(连续)。第一个猜测将打印第一行,仅如上图所示。第二个错误的猜测将打印第二行,依此类推,直到打印整个形状为止。这是我的意思的示例:
1-5个错误猜测后所需的输出。
我已经成功地弄清楚了如何注册错误的猜测,但是我很难一次打印一行。形状确实在形状的每一行末尾都具有newline字符' n',如您在第一张图片中所见。
鉴于此信息,实施此信息的最简单方法是什么?高度赞赏。
Merge
功能在这里将两个字符串与2D图像相结合,将空间视为透明。两个字符串中的第一个字符将在结果中处于左上角。main
中的示例演示了如何使用lineFeed和太空填充将字符串定位在合并结果中。
编辑为不使用自动
的同一代码的版本#include <iostream>
#include <string>
// NextLine returns the substring in s starting at pos and ending
// in a newline character (not included).
// pos is set to the beginning of the next line, or the
// length of the string if the end is reached.
std::string NextLine(const std::string &s, std::size_t &pos) {
if(pos >= s.length()) { return {}; }
const auto start = pos;
const auto nlpos = s.find('n', start);
if(nlpos == std::string::npos) {
pos = s.length();
return s.substr(start);
} else {
pos = nlpos + 1;
return s.substr(start, nlpos - start);
}
}
// Merge creates a compisition of two strings
// The space character is treated as transparent
// If two non-transparent characters occupy the same
// position, the character from string b is used.
std::string Merge(std::string a, std::string b) {
std::size_t pos_a=0, pos_b=0;
std::string result;
while(pos_a < a.length() || pos_b < b.length()) {
if(!result.empty()) { result += 'n'; }
auto line_a = NextLine(a, pos_a);
auto line_b = NextLine(b, pos_b);
auto len = line_a.length();
if(len > line_b.length()) {
line_b += std::string(len - line_b.length(), ' ');
}
else if(len < line_b.length()) {
len = line_b.length();
line_a += std::string(len - line_a.length(), ' ');
}
for(std::size_t i=0; i<len; ++i) {
result += line_b[i] == ' ' ? line_a[i] : line_b[i];
}
}
return result;
}
int main() {
std::string parts[] = {
" O ",
"n | ",
"\ /",
"nn/ \"
};
std::string man;
bool first = true;
for(auto p : parts) {
if(first) { first = false; }
else { std::cout << "---n"; }
man = Merge(man, p);
std::cout << man << 'n';
}
}
输出
O
---
O
|
---
O/
|
---
O/
|
/
这是一个不使用auto
的版本:
#include <iostream>
#include <string>
std::string NextLine(const std::string &s, std::size_t &pos) {
if(pos >= s.length()) { return {}; }
const std::size_t start = pos;
const std::size_t nlpos = s.find('n', start);
if(nlpos == std::string::npos) {
pos = s.length();
return s.substr(start);
} else {
pos = nlpos + 1;
return s.substr(start, nlpos - start);
} }
std::string Merge(std::string a, std::string b) {
std::size_t pos_a=0, pos_b=0;
std::string result;
while(pos_a < a.length() || pos_b < b.length()) {
if(!result.empty()) { result += 'n'; }
std::string line_a = NextLine(a, pos_a);
std::string line_b = NextLine(b, pos_b);
std::size_t len = line_a.length();
if(len > line_b.length()) {
line_b += std::string(len - line_b.length(), ' ');
} else if(len < line_b.length()) {
len = line_b.length();
line_a += std::string(len - line_a.length(), ' ');
}
for(std::size_t i=0; i<len; ++i) {
result += line_b[i] == ' ' ? line_a[i] : line_b[i];
} }
return result;
}
int main() {
std::string parts[] = {" O ", "n | ", "\ /", "nn/ \" };
std::string man;
for(std::size_t i=0; i<sizeof(parts)/sizeof(parts[0]); ++i) {
if(i) { std::cout << "---n"; }
man = Merge(man, parts[i]);
std::cout << man << 'n';
} }