#include "head.h"


char match_turn(char currFigure,char whosTurn){
        if((currFigure>='a' && currFigure<='z' && whosTurn-1)
                // a-z white && turn !white 
            ||(currFigure>='A' && currFigure<='Z' && whosTurn)){
            // A-Z black && turn white
        printf("Err, turn missmatch");
        return 1;
    }
    return 0;
}

char match_move(char board[8][8], __TURN* turn, char currFigure)
{
    int rowD = turn->nextRow - turn->currRow;
    int colD = turn->nextCol - turn->currCol;

    if (turn->nextRow < 0 || turn->nextRow > 7 ||
        turn->nextCol < 0 || turn->nextCol > 7)
    {
        printf("Err, move out of board\n");
        return 1;
    }

    switch (currFigure)
    {
    case 'P':
    case 'p':
    {
        int dir = (currFigure == 'P') ? -1 : 1;  

        if (colD == 0)
        {
            if (board[turn->nextRow][turn->nextCol] != ' ')
                return 1;

            if (rowD == dir)
                return 0;

            if ((currFigure == 'P' && turn->currRow == 6 && rowD == 2*dir &&
                 board[turn->currRow + dir][turn->currCol] == ' ') ||
                (currFigure == 'p' && turn->currRow == 1 && rowD == 2*dir &&
                 board[turn->currRow + dir][turn->currCol] == ' '))
                return 0;
        }

        if (abs(colD) == 1 && rowD == dir)
        {
            if (board[turn->nextRow][turn->nextCol] != ' ')
                return 0;
        }

        printf("Err, pawn move mismatch\n");
        return 1;
    }

    case 'R':
    case 'r':
    {
        if (rowD != 0 && colD != 0)
        {
            printf("Err, rook move mismatch\n");
            return 1;
        }

        if (rowD == 0)  
        {
            int step = (colD > 0) ? 1 : -1;
            for (int c = turn->currCol + step;
                 c != turn->nextCol;
                 c += step)
            {
                if (board[turn->currRow][c] != ' ')
                {
                    printf("Err, rook blocked\n");
                    return 1;
                }
            }
        }
        else  
        {
            int step = (rowD > 0) ? 1 : -1;
            for (int r = turn->currRow + step;
                 r != turn->nextRow;
                 r += step)
            {
                if (board[r][turn->currCol] != ' ')
                {
                    printf("Err, rook blocked\n");
                    return 1;
                }
            }
        }
        return 0;
    }

    case 'N':
    case 'n':
    {
        if ((abs(rowD) == 2 && abs(colD) == 1) ||
            (abs(rowD) == 1 && abs(colD) == 2))
            return 0;
        printf("Err, knight move mismatch\n");
        return 1;
    }

    case 'B':
    case 'b':
    {
        if (abs(rowD) != abs(colD))
        {
            printf("Err, bishop move mismatch\n");
            return 1;
        }

        int stepR = (rowD > 0) ? 1 : -1;
        int stepC = (colD > 0) ? 1 : -1;
        int r = turn->currRow + stepR;
        int c = turn->currCol + stepC;

        while (r != turn->nextRow && c != turn->nextCol)
        {
            if (board[r][c] != ' ')
            {
                printf("Err, bishop blocked\n");
                return 1;
            }
            r += stepR;
            c += stepC;
        }
        return 0;
    }

    case 'Q':
    case 'q':
    {
        if (abs(rowD) == abs(colD))
        {
            int stepR = (rowD > 0) ? 1 : -1;
            int stepC = (colD > 0) ? 1 : -1;

            int r = turn->currRow + stepR;
            int c = turn->currCol + stepC;

            while (r != turn->nextRow && c != turn->nextCol)
            {
                if (board[r][c] != ' ')
                {
                    printf("Err, queen blocked\n");
                    return 1;
                }
                r += stepR;
                c += stepC;
            }
            return 0;
        }

        if (rowD == 0 || colD == 0)
        {
            if (rowD == 0)
            {
                int step = (colD > 0) ? 1 : -1;
                for (int c = turn->currCol + step;
                     c != turn->nextCol;
                     c += step)
                {
                    if (board[turn->currRow][c] != ' ')
                    {
                        printf("Err, queen blocked\n");
                        return 1;
                    }
                }
            }
            else
            {
                int step = (rowD > 0) ? 1 : -1;
                for (int r = turn->currRow + step;
                     r != turn->nextRow;
                     r += step)
                {
                    if (board[r][turn->currCol] != ' ')
                    {
                        printf("Err, queen blocked\n");
                        return 1;
                    }
                }
            }
            return 0;
        }
        printf("Err, queen move mismatch\n");
        return 1;
    }

    case 'K':
    case 'k':
    {
        if (abs(rowD) <= 1 && abs(colD) <= 1)
            return 0;
        printf("Err, king move mismatch\n");
        return 1;
    }
    }

    return 1;
}

void move(char board[8][8], __TURN *turn)
{
    board[turn->nextRow][turn->nextCol] =
        board[turn->currRow][turn->currCol];

    board[turn->currRow][turn->currCol] = ' ';

    turn->whosTurn = turn->whosTurn == 0 ? 1 : 0;
}



int abs(int n){
    n = n<0 ? n*-1:n;
    return n;
}

