First of this is a homework assignment, so I'm not looking for any solutions just for some assistance to point me in the right direction. I've look at a couple post on the forums but haven't really found anything to help me with my issue, so any assistance is welcome.
I'm trying to work out an issue with my collision function but after going over the code for a few hours I'm not really getting anywhere. Currently, The shapes are properly displaying on the board and my movement function is working as it should (minus the collision detection), but I can't seem to figure out why my collision function isn't working. So my issue is the shape will continue to drop through the bottom on the board or you can move the shape through the sides of the board by moving it, so I'm fairly certain my issue lies in my collision function.
I have two 2D char arrays:
char matrix[25][12]; // the board
char shape[4][4];
Within my movement function, I'm checking that the collision function returns false for the next space before moving the shape to that space. However, it's not detecting if the next space is filled or not and lets me move the shape through the border of the board and off of the board altogether.
1 2 3 4 5 6 7
|
// Drop the shape
if (!collision(localShape, x, y + 1)){
clear(localShape, x, y); // clear old shape location
++y; // increment shape vertically
updateMatrix(localShape, x, y); // Display shape at new location
Sleep(900); // Add delay for shape movement
}
|
Here is what my collision function looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
bool collision(Shapes localShape, int x, int y){
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 4; ++j){
// shape array not blank
if (localShape.shape[i][j] != ' '){
// matrix not empty
if (matrix[i + y][j + x] != ' '){
// collision detected
return true;
}
// matrix empty
else {
return false;
}
}
// shape element blank
else {
return false;
}
}
}
}
|
It should be checking that the shape array has a value and that the board has a value and if both those conditions are true, return true. So my guess is there is something wrong in my logic here.
Also, I'm working on this in Visual Studio 2013 on Windows 7.