Hi,
have iniatlised all elements in a array to zero except for [1][1],where its value is 1.I then try to move it up,down left and right.It seems to work for down,left and right but when you press up it goes to position[0][0] instead of [0][1]
Code:
#include<iostream>
#include<string>
#include<ctime>
#include <stdlib.h>
#include"test.h"
using namespace std;
void printArray( int theGrid[][3])
{
cout<<theGrid[0][0]<<" "<<theGrid[0][1]<<" "<<theGrid[0][2]<<endl;
cout<<theGrid[1][0]<<" "<<theGrid[1][1]<<" "<<theGrid[1][2]<<endl;
cout<<theGrid[2][0]<<" "<<theGrid[2][1]<<" "<<theGrid[2][2]<<endl;
}
int main(int argc,char* argv[])
{
int theGrid[3][3]={ {0,0,0},
{0,1,0},
{0,0,0}};
printArray(theGrid);
char playerInput;
cin>>playerInput;
switch(playerInput){
case 'w':
for(int row=0;row<3;row++){
for(int col=0;col<3;col++){
if(theGrid[row][col]==1){
cout<<"test-where 1 is at first- [1][1] "<<row<<" "<<col<<endl;
theGrid[row][col]=0;//changes where 1 was to 0
row-=1;
cout<<"test-where 1 is at after being moved-[0][1] "<<row<<" "<<col<<endl;
theGrid[row][col]=1;//changes new position from 0 to 1
}}}
case 'a':
for(int row=0;row<3;row++){
for(int col=0;col<3;col++){
if(theGrid[row][col]==1){
theGrid[row][col]=0;
col-=1;
theGrid[row][col]=1;
}}}
break;
case 'd':
for(int row=0;row<3;row++){
for(int col=0;col<3;col++){
if(theGrid[row][col]==1){
theGrid[row][col]=0;
col+=1;
theGrid[row][col]=1;
}}}
break;
case 's':
for (int row=0;row<3;row++){
for(int col=0;col<3;col++){
if(theGrid[row][col]==1){
theGrid[row][col]=0;
row+=1;
theGrid[row][col]=1;
}}}
break;
default:
break;
}
printArray(theGrid);
return 0;
}
the case for down('s')works.Its the same code as for up but 1 is added to row..but if i put col+=1 in to the case for up it puts the value 1 in right place....