Reputation: 73
I have been a programming student for about a year now and I have been tasked with creating a Tetris game. I am trying to display the bucket that is used for the game, it must be 25 x 12. I tried thinking and researching a way to do this using nested loops but to no avail. Everything looks fine except I get an error C2078: too many initializers. Could someone please take a look at the code and find something that I am just not seeing or maybe find a more efficient way to draw the bucket? Any help would be appreciated, thank you.
#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
Upvotes: 3
Views: 386
Reputation: 1
You will want 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 should initialize your bucket like so:
char bucket[height][width]
Refer to David K. comment for understanding of this.
Upvotes: 0
Reputation: 2239
The right way to initialize your matrix is something like:
char bucket[width][height] = { { 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
{ 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
...
};
Or
char bucket[width + 1][height] = { "X X",
"X X",
...
};
Upvotes: 0
Reputation: 2929
I think that you may try using a binary number to represent any line. and then you can show this as a one array of numbers. each cell in the array will be line. Array of short will be ok cause short has 16 bit. (all the 25X12 take only 50 bytes. not like matrix that takes 12*25 bytes.) if their is x in place i, you put the bit i to be 1. I think this is more elegant solution.
for example
"x"," "," "," "," "," "," "," "," "," "," ","x"
that have to be
'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'
in this case you have 2 bits turned on . so the binary for this line is
100000000001
that has a value of
2049
.
if ((number & 2^i) !=0){
//The bit i is 1 so you draw x in column i.
}
I dont say that this is better solution but this is another way of thinking.
Upvotes: 1
Reputation: 3132
"x"
and " "
are not of type char
. They are strings (of type char[1]
),
so the first 12 of those strings are assumed to be what you wanted to
initialize bucket[0]
through bucket[11]
.
If you use 'x'
and ' '
they will be of type char
.
But also check your array dimensions.
char bucket [12][25]
is an array of 12 rows of 25 characters each.
The first 25 characters from your initialization list will become
the contents of the first row of bucket
.
It might make more 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 listed your initializers suggests that that's
what you had in mind).
Upvotes: 1