我的连接代码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) {
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
}
}
答案 0 :(得分:0)
这是你的掉落功能:
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;
}
}
if语句“如果第5行被填充到下一行”,如果第5行被填满,则将光盘放在第4行,但实际上并不打算检查第4行是否为空。摆脱这个if语句,你只需要第一个if语句。 “进入下一行”已经由for循环处理。
首先使用它,然后你还应该返回一个bool:如果没有空间则返回false,如果放置了计数器则为true。然后,如果玩家选择一个完整的列,您可以要求玩家尝试不同的列。