Computer Programming Contest Preparation

ToolBox - Source for: 101/10196/judged.c



/home/toolbox/public_html/solutions/101/10196/judged.c
    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 
    4 /*
    5  *  Author: Isaac Traxler
    6  *    Date: 2006 09 18
    7  * Purpose: practice
    8  * Problem: 10196
    9  */
   10 
   11 
   12 #define TRUE (1 == 1)
   13 #define FALSE (1 != 1)
   14 #define BOARD_SIZE 8
   15 #define DOT '.'
   16 #define POSSIBLE_KNIGHT_LOCATIONS 8
   17 #define POSSIBLE_BISHOP_DIRECTIONS 4
   18 #define POSSIBLE_ROOK_DIRECTIONS 4
   19 #define DEBUG if (FALSE)
   20 #define DEBUGknight if (FALSE)
   21 #define DEBUGbishop if (FALSE)
   22 #define DEBUGrook if (FALSE)
   23 
   24 char board[BOARD_SIZE][BOARD_SIZE];
   25 char rook[2] = { 'R', 'r' };
   26 char bishop[2] = { 'B', 'b' };
   27 char knight[2] = { 'N', 'n' };
   28 char queen[2] = { 'Q', 'q' };
   29 char pawn[2] = { 'P', 'p' };
   30 char king[2] = { 'k', 'K'};
   31 char answer[3][80] = {"black king is in check.", "white king is in check.", "no king is in check."};
   32 
   33 int kx;
   34 int ky;
   35 int gameNumber=0;
   36 
   37 int checkSpace(int x, int y, char piece)
   38 {
   39     /* FUNCTION checkSpace */
   40     return (piece == board[x][y]);
   41 } /* FUNCTION checkSpace */
   42 
   43 int findPiece(char piece)
   44 {
   45     /* FUNCTION findPiece */
   46     int notFound = TRUE;
   47     int i;
   48     int j;
   49 
   50     for (i=0; (notFound) && (i < 8); i++)
   51         {
   52             /* for i */
   53             for (j=0; (notFound) && (j < 8); j++)
   54                 {
   55                     /* for j */
   56                     notFound = (piece != board[i][j]);
   57                 } /* for j */
   58         } /* for i */
   59     kx = i-1;
   60     ky = j-1;
   61     return (! notFound);
   62 } /* FUNCTION findPiece */
   63 
   64 void dump(void)
   65 {
   66     /* FUNCTION dump */
   67     int i;
   68     int j;
   69 
   70     for (i=0; i < BOARD_SIZE; i++)
   71         {
   72             /* for i */
   73             for (j=0; j < BOARD_SIZE; j++)
   74                 {
   75                     /* for j */
   76                     printf("%c",board[i][j]);
   77                 } /* for j */
   78             printf("\n");
   79         } /* for i */
   80 } /* FUNCTION dump */
   81 
   82 
   83 int getInput()
   84 {
   85     /* FUNCTION getInput */
   86     int dataReadFlag;
   87     int x;
   88 
   89     gameNumber++;
   90     for (x=0; x< BOARD_SIZE; x++)
   91         {
   92             /* for x */
   93             scanf("%c%c%c%c%c%c%c%c\n", &board[x][0], &board[x][1], &board[x][2], &board[x][3], &board[x][4], &board[x][5], &board[x][6], &board[x][7]);
   94         } /* for x */
   95 
   96     dataReadFlag = findPiece('k');
   97 
   98     return (dataReadFlag);
   99 } /* FUNCTION getInput */
  100 
  101 int inBounds(int x)
  102 {
  103     /* FUNCTION inBounds */
  104     return ((0 <= x) && (BOARD_SIZE > x));
  105 } /* FUNCTION inBounds */
  106 
  107 char firstNonDot(int x, int y, int dx, int dy)
  108 {
  109     /* FUNCTION firstNonDot */
  110     int i;
  111     int j;
  112     char retval;
  113 
  114     i = x + dx;
  115     j = y + dy;
  116     while ((inBounds(i)) && (inBounds(j)) && (DOT == board[i][j]))
  117         {
  118             /* while */
  119             DEBUG printf("piece found:: (%d,%d)<%c>\n", i, j, board[i][j]);
  120             i = i + dx;
  121             j = j + dy;
  122         } /* while */
  123     if ((inBounds(i)) && (inBounds(j)))
  124         {
  125             /* piece found */
  126             retval = board[i][j];
  127             DEBUG printf("piece found: [%d+%d,%d+%d] (%d,%d) = <%c|%c>\n", x, dx, y, dy, i, j, retval, board[i][j]);
  128         } /* piece found */
  129     else
  130         retval = DOT;
  131 
  132     return retval;
  133 } /* FUNCTION firstNonDot */
  134 
  135 int checkPawn(int cx, int cy, int k)
  136 {
  137     /* FUNCTION checkPawn */
  138     int x;
  139     int y;
  140     int retval;
  141     int DX[2] = { 1, -1};
  142 
  143     x = cx + DX[k];
  144     if (inBounds(x))
  145         {
  146             /* y is valid */
  147             y = cy - 1;
  148             retval = ((inBounds(y)) && (pawn[k] == board[x][y]));
  149             if (! retval)
  150                 {
  151                     /* try other side */
  152                     y = cy + 1;
  153                     retval = ((inBounds(y)) && (pawn[k] == board[x][y]));
  154                 } /* try other side */
  155         } /* y is valid */
  156     else
  157         retval = FALSE;
  158     DEBUG printf(" pawn found: %d\n", retval);
  159 
  160     return retval;
  161 } /* FUNCTION checkPawn */
  162 
  163 int checkKnight(int cx, int cy, int k)
  164 {
  165     /* FUNCTION checkKnight */
  166     int x;
  167     int y;
  168     int i = 0;
  169     int knightNotFound = TRUE;
  170     /* Numbered as follows */
  171     /*  N N      7 0  */
  172     /* N   N    6   1 */
  173     /*   K            */
  174     /* N   N    5   2 */
  175     /*  N N      4 3  */
  176 
  177     int DX[POSSIBLE_KNIGHT_LOCATIONS] = { -2, -1,  1,  2,   2,  1, -1, -2 };
  178     int DY[POSSIBLE_KNIGHT_LOCATIONS] = {  1,  2,  2,  1,  -1, -2, -2, -1 };
  179 
  180     while ((knightNotFound) && (i < POSSIBLE_KNIGHT_LOCATIONS))
  181         {
  182             /* while */
  183             x = cx + DX[i];
  184             y = cy + DY[i];
  185             if ((inBounds(x)) && (inBounds(y)))
  186                 {
  187                     /* check locaiton */
  188                     DEBUGknight printf(" [knight] k-%d king-%c knight-%c i-%d cx-%d cy-%d board[%d][%d]=%d\n", k, king[k], knight[k], i,cx, cy, x, y, board[x][y]);
  189                     knightNotFound = (knight[k] != board[x][y]);
  190                 } /* check locaiton */
  191             i++;
  192         } /* while */
  193 
  194     DEBUG printf(" knight found: %d\n", !knightNotFound);
  195     return (! knightNotFound);
  196 } /* FUNCTION checkKnight */
  197 
  198 int checkBishop(int cx, int cy, int k)
  199 {
  200     /* FUNCTION checkBishop */
  201     int i = 0;
  202     char dotChar;
  203     int bishopNotFound = TRUE;
  204 
  205     /* bishop directions */
  206     /* 3   0 */
  207     /*   k   */
  208     /* 2   1 */
  209     int DX[POSSIBLE_BISHOP_DIRECTIONS] = { -1,  1,  1, -1 };
  210     int DY[POSSIBLE_BISHOP_DIRECTIONS] = {  1,  1, -1, -1 };
  211 
  212     while ((bishopNotFound) && (i < POSSIBLE_BISHOP_DIRECTIONS))
  213         {
  214             /* while */
  215             dotChar = firstNonDot(cx, cy, DX[i], DY[i]);
  216             DEBUGbishop printf(" [bishop] k-%d king-%c bishop-%c dotchar-%c i-%d cx-%d cy-%d\n", k, king[k], bishop[k], dotChar,i,cx, cy);
  217             bishopNotFound = ((bishop[k] != dotChar) && (queen[k] != dotChar));
  218             i++;
  219         } /* while */
  220 
  221     DEBUG printf(" bishop found: %d\n", !bishopNotFound );
  222     return (! bishopNotFound);
  223 } /* FUNCTION checkBishop */
  224 
  225 int checkRook(int cx, int cy, int k)
  226 {
  227     /* FUNCTION checkRook */
  228     int i = 0;
  229     char dotChar;
  230     int rookNotFound = TRUE;
  231 
  232     /* rook directions */
  233     /*   0   */
  234     /* 3 k 1 */
  235     /*   2   */
  236     int DX[POSSIBLE_BISHOP_DIRECTIONS] = { -1,  0,  1,  0 };
  237     int DY[POSSIBLE_BISHOP_DIRECTIONS] = {  0,  1,  0, -1 };
  238 
  239 
  240     while ((rookNotFound) && (i < POSSIBLE_ROOK_DIRECTIONS))
  241         {
  242             /* while */
  243             dotChar = firstNonDot(cx, cy, DX[i], DY[i]);
  244             DEBUGrook printf(" [rook] k-%d king-%c rook-%c dotchar-%c i-%d cx-%d cy-%d\n", k, king[k], rook[k], dotChar,i,cx, cy);
  245             rookNotFound = ((rook[k] != dotChar) && (queen[k] != dotChar));
  246             i++;
  247         } /* while */
  248 
  249     DEBUG printf(" rook found: %d\n", !rookNotFound);
  250     return (! rookNotFound);
  251 } /* FUNCTION checkRook */
  252 
  253 void process()
  254 {
  255     /* FUNCTION process */
  256     int i;
  257     int msg = -1;
  258 
  259     for (i=0; (0 > msg) && (i<2); i++)
  260         {
  261             /* for i -- each color */
  262             findPiece(king[i]);
  263             if (checkPawn(kx,ky, i))
  264                 msg = i;
  265             else
  266                 {
  267                     /* else */
  268                     if (checkKnight(kx,ky, i))
  269                         msg = i;
  270                     else
  271                         {
  272                             /* else */
  273                             if (checkBishop(kx,ky, i))
  274                                 msg = i;
  275                             else
  276                                 {
  277                                     /* else */
  278                                     if (checkRook(kx,ky, i))
  279                                         msg = i;
  280                                 } /* else */
  281                         } /* else */
  282                 } /* else */
  283         } /* for i -- each color */
  284     if (0 > msg)
  285         msg=2;
  286     printf("Game #%d: %s\n", gameNumber, answer[msg]);
  287 } /* FUNCTION process */
  288 
  289 int main ()
  290 {
  291     /* main */
  292     int moreToDo;
  293 
  294     moreToDo = getInput();
  295     DEBUG dump();
  296     while (moreToDo)
  297         {
  298             /* while */
  299             process();
  300             moreToDo = getInput();
  301             DEBUG dump();
  302         } /* while */
  303 
  304     return EXIT_SUCCESS;
  305 } /* main */
  306 
  307