c++ - 为什么我的 connect 4 游戏中行中的其他符号会发生变化?
问题描述
每当它进入第三行及以后,从第二行到被占用列中最高行的符号将变为与被占用列中最高行相同的符号。帮助!
我怀疑问题是 BasePoint 类中的重力()函数,但我不知道它为什么会这样工作。我真的需要任何人的帮助!
#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;
}
}
}
解决方案
推荐阅读
- go - Go-micro rabbit mq 插件 - 优先发布消息
- php - 错误:您的要求无法解析为一组可安装的软件包。拉拉维尔
- powershell - 用于勾选代理设置的 PowerShell 脚本:“使用自动配置脚本”和“自动检测设置”
- javascript - 调试 Angular 路由模块源图
- javascript - 动态导入库 next.js
- windows - 适用于 Windows 的 Docker 桌面
- ios - 如何更改 NohanaImagePicker 中的完成按钮标题?
- esp8266 - PushBullet Nodemcu
- android - 在 Viewmodel 中调用 Mainactivity(更新)
- oracle11g - Oracle 11g 访问绑定变量值