我正在尝试为go游戏编写代码,只针对玩家的玩家。我创建了一个带有81个按钮的网格,当我点击一个按钮时,我有一个回调函数,它会在按钮中放置黑白图像。我正在使用CC ++进行编码,并使用FLTK librairy作为图形。
我的问题是,当我试图知道何时连接相同颜色的硬币时,即使条件不成立,也会执行if语句。
这是我的功能问题:
//populates groups into above arrays for the two colors
void populateCheckingVec()
{
groupsBlack.clear(); groupsWhite.clear();
vector<int> boxesChecked; // I create the diffrent vectors
vector<int> ToBeChecked;
vector <int> aroundInd;
int current=0;
int groupIndex=0;
for (int color = 2; (color == 1 || color == 2); color--) //ColorLoop
{
if (color==1)
{
cout<<endl<< "played colour is white" <<endl;
cout<<endl;
}
else if (color==2)
{
cout<<"played colour is black "<<endl;
cout<<endl;
}
boxesChecked.clear();
groupIndex=0;
vector<vector <int> > ¤tColorGroup = ((color==1)?groupsWhite:groupsBlack);
for (int i = 0; i < (sizeof(go)/sizeof(*go)); i++) // loop through the grid
{
if (go[i] == color && vectorContains(boxesChecked,i)==-1) //check only through the current color and not an already-checked box
// vectorContains gives me -1 if the value (here i) is not in the vector (here boxesChecked) else !=-1.
{
if (go[i]!=boxAdj(true, -1, i) && go[i]!=boxAdj(false, -1, i) ) // To get the index, boxAdj(true,-1,i) give me the color at the left of the i element,
//boxAdj(false, -1, i) gives me the color at the top of the i element
{
ToBeChecked.push_back(i);
cout<<"ToBeChecked's elements are :";
for(int l=0; l<ToBeChecked.size(); l++) cout<<ToBeChecked[l]<<", ";
cout<<endl;
cout<<"Neighboors'loop started"<<endl;
while (ToBeChecked.size()>0)
{
cout<< "Begin loop ToBeChecked.size() ="<<ToBeChecked.size()<<endl;
for (int j=0; j<ToBeChecked.size(); j++)
{
if (vectorContains(boxesChecked, ToBeChecked[j])==-1)
{
current=ToBeChecked[j];
cout<<"current is = "<<current<<endl;
break;
}
}
sameColorAround(aroundInd, current); //To get the same coulor around and put the values into the vector aroundInd.
cout<<"aroundInd's elements are :";
for (int m=0; m<aroundInd.size(); m++) cout<<aroundInd[m]<<", ";
cout<<endl;
for (int k=0; k<aroundInd.size(); k++)
{
cout<<"vectorContains(boxesChecked, aroundInd[k] ="<<vectorContains(boxesChecked, aroundInd[k])<<endl;
cout<<"boxesChecked's elements are :";
for (int w=0; w<boxesChecked.size(); w++) cout<<boxesChecked[w]<<", ";
cout<<endl;
if (vectorContains(boxesChecked, aroundInd[k]==-1))
{
ToBeChecked.push_back(aroundInd[k]);
cout<<"The neighboor is added"<<endl;
cout<<"ToBeChecked's elements are :";
for (int x=0; x<ToBeChecked.size(); x++) cout<<ToBeChecked[x]<<", ";
cout<<endl;
}
}
ToBeChecked.erase(std::remove(ToBeChecked.begin(), ToBeChecked.end(), current), ToBeChecked.end());
boxesChecked.push_back(current);
cout<< " ToBeChecked.size() fin ="<<ToBeChecked.size()<<endl;
cout<<"ToBeChecked's elements are :";
for (int z=0; z<ToBeChecked.size(); z++) cout<<ToBeChecked[z]<<", ";
cout<<endl;
}
groupIndex+=1;
currentColorGroup.resize(groupIndex);
for (int n=0; n<boxesChecked.size(); n++)
{
if (ifAlreadyGrouped(currentColorGroup, boxesChecked[n])==false)
{
cout<<"n= "<<n<<endl;
cout<<"groupIndex ="<<groupIndex<<endl;
cout<<"boxesChecked's elements are : "<<boxesChecked[n]<<endl;
(currentColorGroup[groupIndex-1]).push_back(boxesChecked[n]);
}
}
ToBeChecked.clear();
boxesChecked.clear();
}
}
}
}
}
我的问题是我有vectorContains(boxesChecked, aroundInd[k])==0
(见下文)并执行了我的if语句!所有这些cout
当然要检查发生了什么,抱歉,如果难以理解的话。
我点击按钮(i=11
(之前点击:i=10(black); i=16(white)
)后,我在控制台上发生了什么,因为我无法发布图片。
当我点击按钮(i=11)
并得到我的第一个黑色连接时发生了什么
player color is black
ToBeChecked's elements are : 10,
Neighboor's loop started
Begin loop ToBeChecked.size()=1
current is = 10
arounfInd's elements are : 11
vectorContains(boxesChecked, aroundInd[k] =-1
boxesChecked's elements are :
The neighboor is added
ToBeChecked's elements are : 10, 11
ToBeChecked.size() end =1
ToBeChecked's elements are : 11
// I have removed 10 from ToBeChecked and added it to boxesChecked
Begin loop ToBeChecked.size()=1
current is = 11
aroundInd's elements are : 10
vectorContains(boxesChecked, aroundInd[k] ==0)
// the if-statement is here
boxesChecked's elements are : 10
The neighboor is added
// The problem is here! Why the neighboor is added whereas I have ==0 and not -1 ??
ToBeChecked's elements are : 11, 10**
ToBeChecked.size() end=1
ToBeChecked's elements are : 10
(之后的while循环当然是无限的)
下面是我的vectorContains函数:
//returns the index for a value in vector
int vectorContains(vector <int> &vect, int val)
{
int r = -1;
for (int i = 0; i < vect.size(); i++)
{
if (vect[i] == val)
{
r=i;
break;
}
}
return r;
}
答案 0 :(得分:2)
答案非常简单:
cout<<"vectorContains(boxesChecked, aroundInd[k] ="<<vectorContains(boxesChecked, aroundInd[k])<<endl;
上面的显示vectorContains
的结果,而在此处:
if (vectorContains(boxesChecked, aroundInd[k]==-1))
您将aroundInd[k]==-1
作为参数传递给vectorContains
,并且在此if语句中没有与-1进行比较! :)
我想你想要:
if (vectorContains(boxesChecked, aroundInd[k])==-1)
顺便说一下,我建议你把这个大功能分成几个较小的功能。
答案 1 :(得分:0)
只需使用STL find
代替vectorContains
:
if ((pos_itr = find(vect.begin(), vect.end(), val)) != vect.end()) { // found...
// do found stuff with *pos_itr
} else { // not found...
// ...
}