连接四个C 下降功能无法正常工作

Connect Four C++ drop function is not working correctly

本文关键字:常工作 功能 工作 四个 连接      更新时间:2023-10-16

我在连接4的代码方面遇到了麻烦。是我的下降功能是错误的还是我的主要功能?

#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;
    }
}

if语句"如果第5行填充到下一行",如果第5行填充了第4行,则将光盘放在第4行,但实际上并不费心检查第4行是否为空。摆脱此语句,您只需要第一个if语句即可。"去下一行"已经由for循环处理。

首先要工作,然后您还应该返回一个布尔:如果没有空间,则false,如果放置了柜台。然后,您可以要求玩家选择不同的列,如果他们选择了完整的列。