How can I show my bucket for Tetris in C ++?

I have been involved in programming for about a year already and I was tasked with creating a Tetris game. I am trying to display the bucket that is being used for the game, it should be 25 x 12. I tried thinking and researching a way to do this with nested loops, to no avail. Everything looks great, but I get error C2078: Too many initializers. Can someone please take a look at the code and find something I just can't see or maybe find a better way to draw the bucket? Any help would be appreciated, thanks.

#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

      

+3


source to share


4 answers


"x"

and are " "

not of type char

. They are strings (of type char[1]

), so the first 12 of those lines are considered whatever you wanted to initialize bucket[0]

via bucket[11]

.

If you use 'x'

and ' '

, they will be of type char

.



But also check the dimensions of the array. char bucket [12][25]

is an array of 12 lines of 25 characters. The first 25 characters from your initialization list will become the content of the first line bucket

.

It might make sense to declare char bucket [height][width]

(note the order of height

and width

) since we usually think of the "height" of an array as the number of rows (and the way you specify your initializers assumes that's what you mean).

+1


source


I think you can try using a binary number to represent any string. and then you can show it as one array of numbers. each cell in the array will be a line. The short-circuit array will suffice since the short has 16 bits. (all 25X12 only accept 50 bytes, not as a matrix that takes 12 * 25 bytes.) if their x is in place i, you set bit i to 1. I think this is a more elegant solution.

eg

"x"," "," "," "," "," "," "," "," "," "," ","x"

      

that should be

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'

      

in this case you have included 2 bits. so the binary for this line is



100000000001

      

which matters

2049

      

...

if ((number & 2^i) !=0){
   //The bit i is 1 so you draw x in column i.
}

      

I'm not saying this is the best solution, but this is another way of thinking.

+1


source


The correct way to initialize your matrix is โ€‹โ€‹with something like:

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

      

or

char bucket[width + 1][height] = { "X          X",
                               "X          X",
                               ...
                             };

      

0


source


You will need a for loop in one of your functions to display your bucket. It will look something like this.

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

        //loops for height

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

      

This is just a structural example. And you have to initialize your bucket like this:

    char bucket[height][width]

      

Check out David K.'s comment for an understanding of this.

0


source







All Articles