C++ 为什么在我的connect 4游戏中,行中的其他符号会发生变化?
无论何时转到第三行及以后,从第二行到被占用列中最高行的符号将更改为与被占用列中最高行相同的符号。救命啊 我怀疑问题在于BasePoint类中的gravity()函数,但我不知道它为什么会这样工作。我真的需要任何人的帮助C++ 为什么在我的connect 4游戏中,行中的其他符号会发生变化?,c++,C++,无论何时转到第三行及以后,从第二行到被占用列中最高行的符号将更改为与被占用列中最高行相同的符号。救命啊 我怀疑问题在于BasePoint类中的gravity()函数,但我不知道它为什么会这样工作。我真的需要任何人的帮助 #include <iostream> #include <fstream> #include <array> using namespace std; class BasePoint { public: void setArray()
#include <iostream>
#include <fstream>
#include <array>
using namespace std;
class BasePoint
{
public:
void setArray()
{
for (int x=0;x<6;x++)
{
for (int y=0; y<7; y++)
{
arry[x][y]=0;
}
}
}
void displayBoard()
{
cout<< " 1 2 3 4 5 6 7 "<< endl;
for (int x=0; x<6; x++)
{
cout << "|";
for (int y=0; y<7; y++)
{
if (arry[x][y]==0)
{
cout<< " *";
}
else if (arry[x][y]==1)
{
cout<< " X";
}
else if (arry[x][y]==2)
{
cout<< " O";
}
}
cout << " |\n";
}
}
void prompt(string player)
{
cout<< player << "'s turn. Enter a column number: ";
cin>> dropChoice;
}
void gravity(int playr)
{
for (int x=0; x<6; x++)
{
if (arry[x][dropChoice-1]!=0)
{
if (playr==1)
{
arry[x-1][dropChoice-1]=1;
}
else if (playr==2)
{
arry[x-1][dropChoice-1]=2;
}
}
}
int count=0;
for (int x=0; x<6; x++)
{
if (arry[x][dropChoice-1]==0)
{
count+=1;
}
}
if (count==6)
{
if (playr==1)
{
arry[5][dropChoice-1]=1;
}
else if (playr==2)
{
arry[5][dropChoice-1]=2;
}
}
}
int checkWin(int plyr)
{
int result=0;
for (int x=5; x>=0; x--)
{
for (int y=6; y>=0; y--)
{
if (arry[x][y]==plyr && arry[x-1][y-1]==plyr && arry[x-2][y-2]==plyr && arry[x-3][y-3]==plyr)
{
result=1;
}
else if (arry[x][y]==plyr && arry[x][y-1]==plyr && arry[x][y-2]==plyr && arry[x][y-3]==plyr)
{
result=1;
}
else if (arry[x][y]==plyr && arry[x-1][y]==plyr && arry[x-2][y]==plyr && arry[x-3][y]==plyr)
{
result=1;
}
else if (arry[x][y]==plyr && arry[x][y+1]==plyr && arry[x][y+2]==plyr && arry[x][y+3]==plyr)
{
result=1;
}
}
}
return result;
}
private:
int dropChoice;
int arry[6][7];
};
class Point: public BasePoint
{
public:
void readFile()
{
fstream read;
read.open("account.txt");
for (int x=0; x<5; x++)
{
read >> name[x];
read >> num[x];
}
read.close();
}
string getName(int a)
{
string namez;
if (a==1)
{
namez=name[play1-1];
}
else if (a==2)
{
namez=name[play2-1];
}
return namez;
}
void accChoice()
{
cout<< "Accounts\tCredits" << endl;
for (int x=0; x<5; x++)
{
cout<< x+1 << ". " << name[x] << "\t" << num[x] << endl;
}
cout<< endl;
cout<< "Player 1 account (1-5): ";
cin>> play1;
int flag=0;
while (flag!=1)
{
cout<< "Player 2 account (1-5): ";
cin>> play2;
if (play2==play1)
{
cout<< "Sorry but this account has chosen. Choose again." << endl;
}
else
{
flag=1;
}
}
}
private:
string name[5];
int num[5];
int play1;
int play2;
};
int main()
{
Point thePoint;
int stopper=0;
thePoint.setArray();
thePoint.readFile();
thePoint.accChoice();
string playr1=thePoint.getName(1);
string playr2=thePoint.getName(2);
while(stopper!=1)
{
thePoint.displayBoard();
thePoint.prompt(playr1);
thePoint.gravity(1);
thePoint.displayBoard();
int final=thePoint.checkWin(1);
if (final==0)
{
thePoint.prompt(playr2);
thePoint.gravity(2);
final=thePoint.checkWin(2);
if (final==1)
{
stopper=1;
cout<< "Congratulations! " << playr2 << " has won the game!" << endl;
}
}
else if (final==1)
{
stopper=1;
cout<< "Congratulations! " << playr1 << " has won the game!" << endl;
}
}
}
#包括
#包括
#包括
使用名称空间std;
类基点
{
公众:
void setArray()
{
对于(int x=0;x)这里有几个地方可以让您访问超出边界的数组。例如:arry[x-1]
当x=0
@johnnymapp时,用户将输入1-7,这样它就不会越界,对于行,它将进行检查,直到被占用的行。然后,它将-1将其放入高于它的点。用户将输入1-7,这样它就不会越界-我们无法复制您的错误,因为它依赖于指定正在进行ic输入。for(int x=0;x)这里有几个地方可以让您访问超出边界的数组。例如:arry[x-1]
当x=0
@johnnymapp时,用户将输入1-7,这样它就不会越界,对于行,它将进行检查,直到被占用的行。然后,它将-1将其放入高于它的点。用户将输入1-7,这样它就不会越界-我们无法复制您的错误,因为它依赖于指定正在进行ic输入。for(int x=0;x