I can't figure out how to keep it from running off the array and going crazy.
I tried a few functions, but they either get rid of my output or funk it up.

I want the knight to be a 3 the kill spots a zero and everything else a 1.

if you pick anything on the outside it usually errors out.

thanks, dcmiller
aka netjet

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/* Knight program - basic*/


# include <stdio.h>

int main(void)
{
    /* declare varibles */
int b[8][8],*ptr=&b[0][0],k,i=0,j=0,row,column;
double ROW,COLUMN;


/* set all spaces to 1 */
for(k=0;k<=63;k++)
    *(ptr+k)=1;

/* ask user for row and column */
printf(" enter value for row to place 'Knight' (0-7)  \n");
scanf("%i",&row);
printf(" enter a value for the column to place 'Knight' (0-7)  \n");
scanf("%i",&column);
ROW=row;
COLUMN=column;

/* make all spaces with hits 0's */



              b[column-2][row+1]=0;
              b[column-2][row-1]=0;
              b[column+2][row-1]=0;
              b[column+2][row+1]=0;

              b[column-1][row+2]=0;
              b[column-1][row-2]=0;
              b[column+1][row+2]=0;
              b[column+1][row-2]=0;





      for(i=0;i<=7;i++)
      {
          for(j=0;j<=7;j++)
          {





      /* make space where Knight is a 3 */
              b[row][column]=3;










      /* print values */
              printf(" %i",b[i][j]);
          } 
          printf(" \n");
      }

return 0;
}

I AM UNABLE TO UNDERSTAND WHAT UR PROBLEM IS BUT WHATEVER I COULD GATHER FROM THAT I COULD SAY THAT: U MUST CHECK BEFORE CALCULATING THE HITS THAT WHETHER BY SUBSTRACTING THE COLUMN BY 2 AND THE ROW BY 1 THE RESULTANT VALUE IS NOT BECOMING NEGATIVE OR EXCEEDING THE DIMENSIONS OF THE ARRAY.
FOR EXAMPLE,
IF THE USER INPUTS THE POSITION OF KNIGHT (1,1)
THEN; b[column-2][row-1]=0;
WILL GIVE COLUMN = -1, AND THEREFORE GIVING AN ERROR AS THE SUBSCRIPT OF AN ARRAY CAN`T BE NEGATIVE.

AGAIN IF THE ENTERED POSITION IS (7,6)
THEN; b[column+2][row+1]=0;
WILL GIVE COLUMN = 9, WHICH EXCEEDS THE LIMITS OF THE ARRAY.

THEREFORE WHILE ASSIGNING EACH AND EVERY HITS A VALUE OF ZERO DO THIS

if(column >=2 && row <=7)
b[column-2][row-1]=0;

this is only for the first hit. the rest is upon u.
and by the way if i`m not wrong in a 2d array rows are written before columns
i.e, b[row][column].

best of luck!

thanks for the tip, I did it with my other chess pieces but had a brain fart on the knights. I know it is basic programming, thank goodness I am an EE and not an CpE. LOL

netjet (<8

I have it down pretty well, but if the user inputs (0,1) or (1,0) or (0,0) it doesnt work, I am still working on it, below is what I have so far completed, tyvm for your help.

/* Knight program - basic*/



# include <stdio.h>


int main(void)
{
/* declare varibles */
int b[8][8],*ptr=&b[0][0],k,i=0,j=0,row,column;
double ROW,COLUMN;



/* set all spaces to 1 */
for(k=0;k<=63;k++)
*(ptr+k)=1;


/* ask user for row and column */
printf(" enter value for row to place 'Knight' (0-7)  \n");
scanf("%i",&row);
printf(" enter a value for the column to place 'Knight' (0-7)  \n");
scanf("%i",&column);
ROW=row;
COLUMN=column;


/* make all spaces with hits 0's */



if(column >=2 && row <=6)
b[column-2][row+1]=0;


if(column <=5 && row >=1)
b[column+2][row+1]=0;


if(column >=2 && row >=1)
b[column-2][row-1]=0;


if(column <=5 && row >=1)
b[column+2][row-1]=0;


if(column >=1 && row <=5)
b[column-1][row+2]=0;


if(column <=6 && row <=5)
b[column+1][row+2]=0;


if(column >=1 && row >=2)
b[column-1][row-2]=0;


if(column <=6 && row >=2)
b[column+1][row-2]=0;



for(i=0;i<=7;i++)
{


for(j=0;j<=7;j++)
{


/* make space where Knight is a 3 */
b[row][column]=3;



/* print values */
printf(" %i",b[j]);
}
printf(" \n");



}


return 0;
}

I found my boo-boo, I listed to place the 3 as [row][column]; when I should have had it as [column][row]--man was I ready to kick myself.

:rolleyes:

/* Knight program - basic*/



# include <stdio.h>


int main(void)
{
/* declare varibles */
int b[8][8],*ptr=&b[0][0],k,i=0,j=0,row,column;
double ROW,COLUMN;



/* set all spaces to 1 */
for(k=0;k<=63;k++)
*(ptr+k)=1;


/* ask user for row and column */
printf(" enter value for row to place 'Knight' (0-7)  \n");
scanf("%i",&row);
printf(" enter a value for the column to place 'Knight' (0-7)  \n");
scanf("%i",&column);
ROW=row;
COLUMN=column;


/* make space where Knight is a 3 */
b[column][row]=3; :eek:


/* make all spaces with hits 0's */


for(i=0;i<=7;i++)
{


for(j=0;j<=7;j++)
{


if(column <=1 && row <=1)
b[column+2][row+1]=0;
else


if(column >=2 && row <=6)
b[column-2][row+1]=0;


if(column <=5 && row >=1)
b[column+2][row+1]=0;


if(column >=2 && row >=1)
b[column-2][row-1]=0;


if(column <=5 && row >=1)
b[column+2][row-1]=0;


if(column >=1 && row <=5)
b[column-1][row+2]=0;


if(column <=6 && row <=5)
b[column+1][row+2]=0;


if(column >=1 && row >=2)
b[column-1][row-2]=0;


if(column <=6 && row >=2)
b[column+1][row-2]=0;


/* print values */
printf(" %i",b[j]);
}
printf(" \n");



}


return 0;
}

The Rows Should Be Written Before The Columns,
Therefore The Code Should Be
If(row<=6 && Column>=2)
B[row+1][column-2] = 0;
Check It And Say What Is The Outcome

I THINK IT SHOULD WORK FINE FOR(0,0),(1,0) OR (0,0) IF U JUST WRITE FOR EVERY HITS
B[ROW][COLUMN] = 0,
i.e,ROWS FIRST AND COLUMNS AFTER

All works just fine, thanks for the nudge in the right direction. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.