Skip to main content
1 of 9
calccrypto
  • 171
  • 1
  • 4

C 634 necessary chars

#define F for(; i < 4; i++)
#define B break;

// knight
N[8][2] = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {-2, -1}, {-2, -1}, {1, -2}, {2, -1}},
// left, right, up, down
S[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}},
// diagonal
D[4][2] = {{-1, 1}, {-1, -1}, {1, 1}, {1, -1}};

// check within bounds
int C(P, c, r){
    if ((0 <= c) && (c < 8) && (0 < r) && (r < 9)){
        printf("%c%c%d ", P, c + 'a', r);
        return 1;
    }
    return 0;
}

// recursive move
M(int P, int x, int y, int m[2]){
    int c = m[0] + x, r = m[1] + y;
    if (C(P, c, r))
        M(P, c, r, m);
}

main(int argc, char * argv[]){
    char *p = argv[1];
    int x = p[1] - 97, y = p[2] - 48, i = 0;
    switch (*p){
        case 75: // King
            F C(75, S[i][1] + x, S[i][0] + y), C(75, D[i][1] + x, D[i][0] + y);
            B
        case 81: // Queen
            F M(81, x, y, D[i]), M(81, x, y, S[i]);
            B
        case 78: // Knight
            for(; i < 8; i++) C(78, N[i][1] + x, N[i][0] + y);
            B
        case 66: // Bishop
            F M(66, x, y, D[i]);
            B
        case 82: // Rook
            F M(82, x, y, S[i]);
            B
    }
}

Any suggestions on how to improve this? This is my first time submitting an answer.

calccrypto
  • 171
  • 1
  • 4