我正在为康威的生命游戏编写 .lif 105 的文件解析器。它工作正常,除了我想跳过 #N 上方的文本段,它标志着评论的结束。但这只是出于某种原因跳过了前三行。
这是一个例子:
#Life 1.05
#D Name: 1 beacon
#D Approximately the 32nd-most common oscillator.
#D www.conwaylife.com/wiki/index.php?title=1_beacon
#N
#P 10 10
..**
.*.*
*..*.**
**.*..*
.*.*
.*..*
..**
它将跳过行:
#Life 1.05
#D Name: 1 beacon
#D Approximately the 32nd-most common oscillator.
导致单元格段的起始位置比应有的低 3 条 y 线。
#include "fileparser.h"
#include <QCoreApplication>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
string filetype = "#Life 1.05";
void getGame(bool array[100][100], const string& fname){
// create some objects in memory
ifstream infile("C:/GameOfLife/ellisonp4hwemulator_105.lif");
string testType, line;
int xPos= 0, yPos= 0, temp;
// read objects
bool comments = true;
while (std::getline(infile, line) && comments)
{
if(line.find("#N") == std::string::npos)
comments = false;
}
std::getline(infile, line);
std::istringstream iss(line);
while(std::getline(infile, line)){
std::istringstream iss(line);
iss >> line;
temp = xPos;
for(char c : line){
if(c == '*')
array[temp][yPos] = true;
temp++;
}
yPos++;
}
infile.close(); // optional
}
对于那些愿意提供更多帮助的人来说,这是一个额外的问题!最初我希望#P 标记单元格的起始坐标。所以在这种情况下,它将从 X-10 Y-10 开始绘制。
但无法让它找到它。这是代码,如果你想帮助一些额外的:)
#include "fileparser.h"
#include <QCoreApplication>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
string filetype = "#Life 1.05";
void getGame(bool array[100][100], const string& fname){
// create some objects in memory
ifstream infile("C:/GameOfLife/ellisonp4hwemulator_105.lif");
string testType, line, emptyValue;
int xPos, yPos, temp;
// read objects
bool comments = true;
while (std::getline(infile, line) && comments)
{
if(line.find("#N") == std::string::npos)
comments = false;
}
std::getline(infile, line);
std::istringstream iss(line);
iss >> emptyValue >> xPos >> yPos;
while(std::getline(infile, line)){
std::istringstream iss(line);
iss >> line;
temp = xPos;
for(char c : line){
if(c == '*')
array[temp][yPos] = true;
temp++;
}
yPos++;
}
infile.close(); // optional
}
我想要 iss >> emptyValue >> xPos >> yPos;
捕捉值 emptyValue = #P, xPos = 10, yPos = 10
感谢您花时间阅读我冗长的问题:)