连接四C++下拉函数不能正常工作
我的connect 4代码有问题。它一直工作到最后的第二行,而不是第三行,它只是替换第二行。是我的drop函数出错了还是我的主函数出错了连接四C++下拉函数不能正常工作,c++,C++,我的connect 4代码有问题。它一直工作到最后的第二行,而不是第三行,它只是替换第二行。是我的drop函数出错了还是我的主函数出错了 #include <iostream> using namespace std; void drop(int b[][7], int column, int disc)//drops the game pieces { for (int i = 5; i >= 0; i--)//starts at 5(the bottom) {
#include <iostream>
using namespace std;
void drop(int b[][7], int column, int disc)//drops the game pieces {
for (int i = 5; i >= 0; i--)//starts at 5(the bottom) {
if (b[i][column - 1] == 0)//if row 5 is not filled it will be 1
{
b[i][column - 1] = disc;
break;
}
if (b[i][column - 1] != 0)//if row 5 is filled go to next row
{
b[i - 1][column - 1] = disc;
break;
}
}
}
void display(int c[][7])//displays the gameboard
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
cout << c[i][j];
}
cout << endl;
}
cout << endl;
}
void initial(int arr[][7])//sets the initial gameboard with all 0's
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
arr[i][j] = 0;
}
}
}
void main()
{
int a[6][7];//declare the 2d array
int column;//declares the variable for column
initial(a);
display(a);//prints the initial
cout << "Enter 0 if you win or want to quit"<<endl;//user exit
for (;;)//infinite for loop so the game will continue until user exits
{
cout << "Enter column 1-7 ";
cin >> column;//reading in what the user has inputted
if (column == 0)//if it is 0 the program will end
break;
drop(a, column, 1);//player 1
display(a);//displays board after player 1 put piece in
cout << "Enter column 1-7 ";
cin >> column;
if (column == 0)
break;
drop(a, column, 2);//player 2
display(a);//displays board after player 2 put piece in
}
}
这是您的下降功能:
void drop(int b[][7], int column, int disc)//drops the game pieces
{
for (int i = 5; i >= 0; i--)//starts at 5(the bottom) {
if (b[i][column - 1] == 0)//if row 5 is not filled it will be 1
{
b[i][column - 1] = disc;
break;
}
if (b[i][column - 1] != 0)//if row 5 is filled go to next row
{
b[i - 1][column - 1] = disc;
break;
}
}
如果第5行已填充,则if语句转到下一行。如果第5行已填充,则将光盘放在第4行,但实际上它并不费心检查第4行是否为空。去掉这个if语句,您只需要第一个if语句。转到下一行已经由for循环处理
首先让它工作,然后还应该返回bool:如果没有空间,则返回false;如果放置了计数器,则返回true。然后,如果玩家选择一个完整的C++,可以让玩家尝试不同的列。真正地另外,为什么不使用std::array或std::vectorWelcome来实现堆栈溢出。请花点时间阅读并参考您可以在此处询问的内容和方式。解决此类问题的正确工具是调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,你应该[编辑]你的问题,包括一个重现你的问题的例子,以及你在调试器中的观察结果。你的drop函数中不应该有两个if语句,这就是为什么你已经有了一个循环,简化这一点。