我正在尝试制作一场BattleShip游戏,但我正在努力将这些游艇添加到游戏板上。
void Board::display() const{
//string filename;
int upC = 65, lowC = 97; //aqui temos o codigo ascii do a, primeira letra do alfabeto, 65 é o seu uppercase e 97 o seu lowercase
int icc = 0; //int char coluna
cout << " ";
while (icc < numLines){ //o int char coluna vai aumentando consoante as dimensoes do tabuleiro || e.g Se tivermos um tab 10x10
cout << (char)lowC << " "; //teremos um range de 65-75 [a-j] para as colunas e um range 97 -107 [A-J] para as linhas
icc++;
lowC++;
}
cout << endl;
icc = 0;
for (int y = 0; y< numLines; y++)
{
cout << (char)upC << " ";
for (int x = 0; x < numColumns; x++){
if(board[y][x] == -1)
cout << '.' << " ";
else
cout << ships[board[y][x]].getSymbol() << " ";
}
cout << endl;
upC++;
}
}
我初始化了一个满载-1的向量矢量,并在我的putShip函数上(见上文)我添加了船只。
int Board::putShip(const Ship &s){
int size = s.getSize();
char ori = s.getOri();
int line = s.getLin();
int col = s.getCol();
char symbol = s.getSymbol();
//vector< vector<char> > tab;
/* cout << size << endl;
cout << ori << endl;
cout << line << endl;
cout << col << endl;
cout << symbol << endl;*/
while (size > 0){
if((board[line][col] == -1) && (ori == 'H')){
board[line][col] = 0;
col++;
cout << "size " << size << endl;
return true;
}
else if ((board[line][col] == -1) && (ori == 'V')){
board[line][col] = symbol;
line++;
return true;
}
else
return false;
size--;
}
//cout << line << col << endl;
/*while (size > 0){
if(ori == 'H'){
board[line][col] = symbol;
col++;
}
else{
board[line][col] = symbol;
line++;
}
size--;
}*/
//return true;
}
但由于某种原因它只打印了第一个位置,即使它们的大小是4.我的while循环不起作用,我不知道为什么。
这是我的Board构造函数:
Board::Board(const string &filename){
string nome;
unsigned int size;
char simb;
unsigned int cor;
char ori;
PositionChar position;
string tmp;
ifstream config;
config.open(filename.c_str()); //abre o ficheiro config onde estao as informacoes do tabuleiro
if (config.is_open()) {
config >> tmp >> numLines >> tmp >> numColumns;
cout << numLines << endl;
cout << numColumns << endl;
board.resize(numColumns, vector<int> (numLines,-1));
while (!config.eof()) {
config >> simb >> tmp >> position.lin >> position.col >> tmp >> ori >> tmp >> size >> tmp >> cor;
if(!config.fail()){
ships.push_back(Ship(simb, position, ori, size, cor));
}
cout << "simbolo: " << simb << endl;
putShip(Ship(simb,position, ori, size, cor));
}
//display();
} else {
cout << "Ficheiro de config invalido" << endl;
exit(1);
}
config.close();
}
couts是调试调用,显然与问题无关。基本上,我得到的东西就像:
board: 10 x 10
P - Aa - H - 4 - 10
然后我得到符号(P),位置转换为整数(Aa - 位置(0,0),方向(水平),SIZE和颜色(不相关)。
然后我检查某个板位置x,y是否为-1,如果是,它对应于水,我用'填充'。如果不。如果它具有&gt; = 0,则它对应于每个船从txt读取的向量船的索引。
我的while循环应该填充4 P的线,因为方向是水平的但它没有这样做,我无法理解为什么。
答案 0 :(得分:2)
我想在您发布的大量代码中,您正在谈论此循环:
while (size > 0){
if((board[line][col] == -1) && (ori == 'H')){
board[line][col] = 0;
col++;
cout << "size " << size << endl;
return true;
}
else if ((board[line][col] == -1) && (ori == 'V')){
board[line][col] = symbol;
line++;
return true;
}
else
return false;
size--;
}
请注意,该条件的所有路径都有一个return语句。因此,while循环将执行一次或从不执行。