连接四个C 下降功能无法正常工作
Connect Four C++ drop function is not working correctly
我在连接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,如果放置了柜台。然后,您可以要求玩家选择不同的列,如果他们选择了完整的列。
相关文章:
- OpenSSL 解密功能无法正常工作
- 链表中的显示功能无法正常工作
- 打印功能无法正常工作 - 链表
- 排序功能在C++中未按预期工作
- 如何组织 git 工作流以修复错误并同时引入新功能
- 为什么必须动态分配扩展数组才能使此功能正常工作C++
- Qt C++括号匹配和突出显示当前线路功能无法协同工作
- 下面的代码是如何工作的?它输出分解的数字并且功能齐全,我只是不明白它是如何做到的
- C 功能如何内部工作
- 大写功能无法正常工作
- 为什么这个base64解码/编码功能不能正常工作?
- 如何使功能"run"工作。程序在调用运行时退出
- C 11螺纹 - 非静态成员功能的使用无效 - 工作GCC 5.1.0损坏的GCC 7.3.1
- Lower_bound功能无法正常工作
- 仅一个功能的链接错误.在同一类和CPP文件中具有相似签名的其他函数工作正常
- 功能测试宏在 Visual C++ 中无法正常工作
- 将Linux打开,读,写,关闭功能以在Windows上工作
- 如何使用字符串更改此插入功能以在链接列表中工作
- 澄清功能如何工作并与类相关
- C //尝试重写一个工作代码以包括我的功能