Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello frnds! I am having a trouble in dealing with 2 dimensional arrays in C. Following is the design & code I tried for....
1 4 15 7
8 10 2 11
14 3 6 13
12 9 5 _
The last element is space whr the cursor should be placed & whenever the arrow keys are moved the value must be swapped with the space. Like if now left arrow key is pressed, 5 will move to the right & space will replace 5. Likewise for the remaining arrow keys. The getkey() method below gives the scan code for each arrow key.
// PUZZLE
#include<stdio.h>
#include<conio.h>
#include<dos.h>
int main()
{
	int a[4][4]={{1,4,15,7},{8,10,2,11},{14,3,6,13},{12,9,5,}};
	int i,j,cnt=0,key,ukey,dkey,lkey,rkey,x,y;
	int pos,val;
	 int temp;
	clrscr();
	printf("\nFollowing is the puzzle:\n");
	printf("\nUse the arrow keys for solving the puzzle\n---------------------------------------");
	for(i=0;i<4;i++)
	{
	   printf("\n");
	   for(j=0;j<4;j++)
	   {
	      if(i==3 && j==3)
		{
		     a[3][3]=' ';
		     printf("%c",a[3][3]);
		     break;
		}
		printf("%d\t",a[i][j]);
	   }
	}
	//pos=a[4][4];
	if(cnt==0)
	{
	   key=getkey();
	   if(key==77 || key==80) //check for right & down key and do nothing
	   {   }
	   else
	   {
	       i=3;j=3;
	       if(key==72)        //up arrow
	       {
		   // printf("\nYou pressed the %d arrow key",key);
		   temp=a[i][j];
		   a[i][j]=a[i-1][j];
		   a[i-1][j]=temp;		 
		   for(i=0,j=0;i<4,j<4;i++,j++)
		       printf("%d",a[i][j]);
	       }
	       else if(key==75)        //left arrow
	       {
		   //pos=a[i][j];
		   temp=a[i][j];
		   a[i][j]=a[i][j-1];
		   a[i][j-1]=temp;
	       }
	   }//else
	   cnt++;
	}//outer if
	getch();
	return 0;
}
int getkey()
{
	union REGS i,o;
	while(!kbhit())
		;
	i.h.ah=0;
	int86(22,&i,&o);
	return(o.h.ah);
}

Any suggestion will be appreciated. Thanks.
Posted
v2

1 solution

Firstly, use more sensible names: if you call your variables by names that reflect what they do, it is a lot easier to understand what you are doing. For example, have two variables called userRow and userCol to tell you where the user actually is.
Secondly, use routines to break up your code and make it easier to read:
#include <stdio.h>
#define ROWS_COUNT 4
#define COLS_COUNT 4
#define TRUE (1==1)
int playSurface[ROWS_COUNT][COLS_COUNT]={{1,4,15,7},{8,10,2,11},{14,3,6,13},{12,9,5,-1}};
int userRow = ROWS_COUNT - 1;
int userCol = COLS_COUNT - 1;
int main()
   {
   int key;
   do
      {
      ShowPlaySurface();
      key = GetKey();
      if (key == 'Q' || key == 'q')
         {
         break;
         }
      DoMove(key);
      } while (TRUE);
   exit(0);
   }
int ShowPlaySurface()
   {
   int i, j;
   for (i = 0; i < ROWS_COUNT; i++)
      {
      for (j = 0; j < COLS_COUNT; j++)
         {
         if (i == userRow && j == userCol)
            {
            printf ("\t");
            }
         else
            {
            printf ("%d\t", playSurface[i][j]);
            }
         }
      printf("\n");
      }
   }
   
int GetKey()
   {
   return (int) getch();
   }
int DoMove(int key)
   {
   }
Now, all you have to do is concentrate on the Do Move function - which is just a case of checking keys, and moving in the appropriate direction, if you can. Easy!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900