Wednesday, May 2, 2012

C++ Moving a char in a 2d array

I have to make a game for a university project. In this game i have to have a character that can be moved in the 2d grid by use of keyboard input. I can only get the character to move one block to the left, right, up,down and that all. it does not allow me for example to move two positions to the left from the original.



Here is my code:



void Opstical(char**array,int row,int col,int direction) {
int a(0);
int b(1);
//here is my problem where i cant get it to move
//more than one block away from the original spot.
//char 2 i want to move

array[a][b] = 2;
if(direction == 4) {
array[a][b]=array[a][b-1]=2;
}
if(direction == 6) {
array[a][b]=array[a][b+1]=2;
}
if(direction == 8) {
array[a][b]=array[a-1][b]=2;
}
if(direction == 2) {
array[a][b]=array[a+1][b]=2;
}

for(int i = 0;i < row ; i++) {
for(int c = 0; c < col; c++) {
cout << array[i][c]<<" ";
}
cout <<endl<<endl;
}
}


Thanks.





No comments:

Post a Comment