【发布时间】:2021-11-07 12:13:07
【问题描述】:
#include <iostream>
#include <vector>
using namespace std;
enum type{
noboard,
peg,
empty
};
class board{
public:
//create getter
vector<vector<int>>getVector() const;
//create setter
bool control_vect();
board(vector<vector<int>> pvect);
//display() function that prints elements of the vect data member
void display()
{
for(const vector<int> &elem: vect)
{
for(int intElem: elem)
{
if(intElem==1){
cout<<'P';
}
else if(intElem==0){
cout<<' ';
}
else if(intElem==2){
cout<<'.';
}
}
cout<<endl;
}
}
private:
vector<vector<int>> vect;
};
vector<vector<int>> board :: getVector() const
{
return vect;
}
board :: board(vector<vector<int>> pvect)
{
vect = pvect;
}
bool board :: control_vect(){
long unsigned int i,j;
bool controller=false;
cout<<vect[0][0];
for (i = 0; i < vect.size(); i++){
for ( j = 0; j < vect[i].size()-2; j++){
if(vect[i][j]==1 && vect[i][j+1]==1 && vect[i][j+2]==0){
controller=true;
break;
}
}
}
return controller;
}
int main()
{
//create a board instance
// board myBoard;
vector<vector<int>> pvect{
{peg,peg,peg,peg,peg},
{noboard,peg,peg,empty,peg},
{peg,peg,peg,peg,peg},
};
//cout<<pvect[0][5];
//use the setter to set the vect data member
board a(pvect);
a.display();
if(a.control_vect()==true){
cout<<"a";
}
else{
cout<<"b";
}
//lets print out the elements of the data member vect for the object myBoard using the display() member function
// myBoard.display();
return 0;
}
我编写上行代码并运行。然后我在 control_vect 函数中尝试更复杂的条件,该函数不给出返回值。但上行最不复杂的 control_funcion 返回一个布尔值并打印 vect[0][0] 元素.但缺点不打印 bool 或 vect[0][0]。为什么 underside 函数不返回 bool 值?虽然类似。另外我正在尝试循环并打印所有向量二的运行。示例输出:İf 条件提供打印一个,不要通过程序提供 print b。
bool board :: control_vect(){
long unsigned int i,j;
for (i = 0; i < vect.size(); i++){
for ( j = 0; j < vect[i].size(); j++){ //Look for legal moves on the board.If there are legal moves program will continue.
if(vect[i][j]==1 && vect[i][j+1]==1 && vect[i][j+2]==2){
controller=true;
break;
}
else if(vect[i][j]==2 && vect[i][j+1]==1 && vect[i][j+2]==1){
controller=true;
break;
}
else if(vect[i][j] == 1 && vect[i+1][j] == 1 && vect[i+2][j] == 2){
controller=true;
break;
}
else if (vect[i][j] == 2 && vect[i+1][j] == 1 && vect[i+2][j] == 1){
controller=true;
break;
}
}
}
return controller;
}
【问题讨论】:
-
请格式化您的代码,创建一个minimal reproducible example,清楚地显示预期输出和实际输出。您是否启用了编译器警告?您是否尝试使用调试器单步执行代码?底部函数不包含
controller定义,所以它根本不应该编译。 -
您能写出上述代码的预期输出吗?它与您现在得到的实际输出有何不同?另外,请澄清您在上述代码中遇到的问题。