#include "head.h"


int main()
{
    char _board[8][8];
    { //Set Def desk
        char _cleanDesk[] = {"rnbqkbnrpppppppp"};
        for(int i = 0;i<8;i++)
            for(int j = 0;j<8;j++)
                _board[i][j] = ' ';
        for (int i = 0; i < 2;i++)
            for(int j = 0; j <8;j++)
                _board[i][j] = _cleanDesk[i*8+j];
        for (int i = 6; i < 8;i++)
            for(int j = 0; j < 8;j++)
                _board[i][j] = i==6? (_cleanDesk[8+j])-32 : (_cleanDesk[j])-32;
        print_board(*_board);
    }

    char charsInInput=1;
    char _input[CHARS_IN_INPUT_MAX];
    __TURN __turn; 
    __turn.whosTurn = 0; // 0 - white 1 - black
    while(charsInInput>0){
    if (input_(_input,&charsInInput)) return 0;
        unsigned char ptr;
        {//Turn info
        ptr = (*_input>='A'&&*_input<='Z')?3:2; // Qe2-e4
        __turn.currCol = _input[ptr-2]-'a';
        __turn.currRow = 8-(_input[ptr-1]-'0');
        __turn.nextCol = _input[ptr+1]-'a';
        __turn.nextRow = 8-(_input[ptr+2]-'0');
        }

        {//Rules
            char currFigure = _board[__turn.currRow][__turn.currCol];
            { // 'Q'e2-e5 Missmatch
            if(ptr==3)
                if(*_input!=currFigure&&*_input!=currFigure-32){
                    printf("Err, figure missmatch");
                    return 1;
                }  
            }

            if(match_turn(currFigure,__turn.whosTurn))return 1;
            
            { // TAKE MISMATCH
                if
                ((_board[__turn.nextRow][__turn.nextCol]!=' ' && _input[ptr]!='x' 
                                                )||(
                  _board[__turn.nextRow][__turn.nextCol]==' ' && _input[ptr]=='x'))
                {
                printf("Err, take missmatch");
                return 1;
                }
            }
        
            if(match_move(_board,&__turn,currFigure))return 1;
        }
        
        move(_board,&__turn);
        print_board(*_board);
        if(_input[charsInInput-1]=='#')break;
    }
    

    return 0;
}
