Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我将如何用C++;?_C++_Bucket_Tetris - Fatal编程技术网

C++ 我将如何用C++;?

C++ 我将如何用C++;?,c++,bucket,tetris,C++,Bucket,Tetris,我已经是一名编程专业的学生大约一年了,我的任务是创建一个俄罗斯方块游戏。我试图显示用于游戏的桶,它必须是25 x 12。我试着思考和研究一种使用嵌套循环来实现这一点的方法,但没有效果。一切看起来都很好,只是我遇到了一个错误C2078:初始化者太多。有人能看一下代码,找到一些我没有看到的东西,或者找到一个更有效的方法来解决问题吗?任何帮助都将不胜感激,谢谢 #include "stdafx.h" #include <iostream> #include <Wind

我已经是一名编程专业的学生大约一年了,我的任务是创建一个俄罗斯方块游戏。我试图显示用于游戏的桶,它必须是25 x 12。我试着思考和研究一种使用嵌套循环来实现这一点的方法,但没有效果。一切看起来都很好,只是我遇到了一个错误C2078:初始化者太多。有人能看一下代码,找到一些我没有看到的东西,或者找到一个更有效的方法来解决问题吗?任何帮助都将不胜感激,谢谢

#include "stdafx.h"
    #include <iostream>
    #include <Windows.h>


    using namespace std;

    const int width = 12;
    const int height = 25;
    char bucket [width][height] ={"x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x","x","x","x","x","x","x","x","x","x","x","x",
    };
    void setCursorTo(int x, int y){
        HANDLE handle;
        COORD position;
        handle = GetStdHandle(STD_OUTPUT_HANDLE);
        position.X = x;
        position.Y = y;
        SetConsoleCursorPosition(handle, position);
    }


    int _tmain(int argc, _TCHAR* argv[])
    {

            setCursorTo(0,0);
            cout<<bucket<<endl;



        return 0;
    }enter code here
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
const int width=12;
const int height=25;
字符桶[宽度][高度]={“x”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”、、、、、、、、、、、、、、、、、、、、、、、、、”x”,
“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”,
};
void setCursorTo(整数x,整数y){
把手;
协调地位;
句柄=GetStdHandle(标准输出句柄);
位置X=X;
位置Y=Y;
设置控制台位置(手柄、位置);
}
int _tmain(int argc,_TCHAR*argv[]
{
setCursorTo(0,0);

cout初始化矩阵的正确方法如下:

char bucket[width][height] = { { 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
                               { 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
                               ...
                             };

“x”和
不是
char
类型。它们是字符串(类型
char[1]
), 因此,假设这些字符串中的前12个是您想要的 通过
bucket[11]
初始化
bucket[0]

如果使用
'x'
'
它们将是
char
类型

但也要检查数组的维度。
char bucket[12][25]
是一个由12行组成的数组,每行25个字符。 初始化列表中的前25个字符将变为
存储桶的第一行内容

声明
char bucket[height][width]
(注意高度和宽度的顺序) 因为我们通常认为数组的“高度”是行数 (你列出初始值设定人的方式表明
我想你可以试着用一个二进制数来表示任何一行。 然后你可以把它显示为一个数字数组,数组中的每个单元格都是直线。 short的数组可以,因为short有16位。(所有25X12只占用50字节。不像matrix那样占用12*25字节。) 如果它们的位置i是x,那么你把位i设为1。 我认为这是更优雅的解决方案

比如说

"x"," "," "," "," "," "," "," "," "," "," ","x"
那一定是

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'
在这种情况下,你有2位打开。 这行的二进制代码是

100000000001
它的值为

2049


我并不是说这是一个更好的解决方案,但这是另一种思考方式。

您需要在一个函数中使用for循环来显示您的存储桶。它看起来会像这样

    int main(){
        for (x = 0; x < height; x++){

        //loops for height

        for (y = 0; y < a; y++){
            //loops for width
            }
        }
    }

请参阅David K.comment了解这一点。

您的第二个示例需要另一个用于终止nul字符的插槽。第一个示例将1:1与容量匹配。@ThomasMatthews您是对的。需要为
null
char提供更多空间。感谢您指出这一点。
if ((number & 2^i) !=0){
   //The bit i is 1 so you draw x in column i.
}
    int main(){
        for (x = 0; x < height; x++){

        //loops for height

        for (y = 0; y < a; y++){
            //loops for width
            }
        }
    }
    char bucket[height][width]