Search code examples
cchess

Programming moves of the Knight in Chess with C


Hi assignment is basicly putting K to the entered value of the Knight then putting X to the rest of the board. Then for each move that Knight can do we put number then we increase the number for example like this:

X X X X X X X X
X X X X X X X X
X X X 1 X 2 X X
X X 4 X X X 3 X
X X X X K X X X
X X 5 X X X 6 X
X X X 7 X 8 X X
X X X X X X X X

My Code is down below but when i run it i get the output as this: 42108284210828421082842108284210828421082842108284210828 42108284210828421082842108284210828421082842108284210828 42108284210828421082814210828242108284210828 42108284210828342108284210828421082844210828 42108284210828421082842108284210825421082842108284210828 42108284210828542108284210828421082864210828 42108284210828421082874210828842108284210828 42108284210828421082842108284210828421082842108284210828

So can you help me fix my code. Since i am a newbie my if else conditions are pretty lame so i could really apperiacte it if you could also help me putting simplier conditions inside the brickets.

   #include <stdio.h>
#include <stdlib.h>

void boardDefine();
void boardDraw();

int main()

{
    char board[8][8];
    int i,j,row,column;
    int nextMove;

    printf("Please enter the position of the Knight on the board\n");
    scanf("%d%d",&row,&column);
    if(row<1||row>9||column<1||column>9)
    {
        printf("You must enter a value greater than zero");
    }

    boardDefine(board[8][8],i,j,row,column,nextMove);
    boardDraw(board[8][8],i,j);


    return 0;
}

void boardDefine(char board[8][8],int i, int j,int row,int column,int nextMove)
{
    nextMove=1;
    for( j=1;j<=8;j++)
    {
             for(i=1;i<=8;i++)
        {

            if(i==row&&j==column)
            {
                board[i][j]="K ";//Places the Knight to the position that entered by user
            }
            /*From here we are basicly showing where the Knight can move from its current position
            for this we first check that if both row and column values are inside the board or not
            after the L move if not then we put the nextMove value at that adress of the array
            */
            else if(row-1<=8&&row-1>=0&&column+2<=8&&column+2>=0&&i==row-1&&j==column+2)
            {
                board[i][j]='0'+ nextMove;
                nextMove++;
            }
            else if(row-1<=8&&row-1>=0&&column-2<=8&&column-2>=0&&i==row-1&&j==column-2)
            {
                board[i][j]='0'+ nextMove;
                nextMove++;
            }
            else if(row+1<=8&&row+1>=0&&column+2<=8&&column+2>=0&&i==row+1&&j==column+2)
            {
                board[i][j]='0'+ nextMove;
                nextMove++;
            }
            else if(row+1<=8&&row+1>=0&&column-2<=8&&column-2>=0&&i==row+1&&j==column-2)
            {
                board[i][j]='0'+ nextMove;
                nextMove++;
            }
            else if(row-2<=8&&row-2>=0&&column+1<=8&&column+1>=0&&i==row-2&&j==column+1)
            {
                board[i][j]='0'+ nextMove;
                nextMove++;
            }
            else if(row-2<=8&&row-2>=0&&column-1<=8&&column-1>=0&&i==row-2&&j==column-1)
            {
                board[i][j]='0'+ nextMove;
                nextMove++;
            }
            else if(row+2<=8&&row+2>=0&&column-1<=8&&column-1>=0&&i==row+2&&j==column-1)
            {
                board[i][j]='0'+ nextMove;
                nextMove++;
            }
            else if(row+2<=8&&row+2>=0&&column+1<=8&&column+1>=0&&i==row+2&&j==column+1)
            {
                board[i][j]='0'+ nextMove;
                nextMove++;
            }
            else
            {
                board[i][j]="X ";//Places X to the places where Knight cant move.
            }
        }
        printf("\n");
    }

}

//Then we use this function to print
void boardDraw(char board[8][8],int i, int j)
{
    for( j=1;j<=8;j++)
    {
             for(i=1;i<=8;i++)
             {
                 printf("%c",board[i][j]);
             }
             printf("\n");
    }

}

Solution

  • So there are several issues with your code, I will try to address as many as I can that should get your code running -

    First of all, since you want to store a single printable character, you should change your type of the array to char instead of int. You will then assign single characters to the array position. So when assigning K it will be board[i][j] = 'k'; and x will be board[i][j]='x';

    Since you have now changed your type to char you will have to change the printf call to

    printf("%c ", board[i][j]);
    

    Finally for assigning the single digit numbers you cant simply assign nextMove. You will have to assign '0' + nextMove. This way you will get the ASCII equivalent of the number in the array.

    Another mistake you have made is you have passed board[8][8] to the function. board[8][8] would just mean the single element in the array. If you want to pass the entire array you have to pass just board.

    Finally you ran your loops from 1 to 8. when the array is declared as board[8][8]. Any array declared as arr[x] goes from 0 to (x-1) and not 1 to x. So your loops should be changed to

    for (i = 0; i < 8; i++)
    

    and similarly for j. Rest all can be optimized but should work fine for now.