fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. enum piece_type
  6. {
  7. PAWN,KNIGHT,BISHOP,ROOK,QUEEN,KING
  8. };
  9.  
  10. enum piece_type board[8][8];
  11.  
  12. //Each side's scores, initialized to their scores at the start of a
  13. //standard chess match.
  14. int score_white = 39;
  15. int score_black = 39;
  16.  
  17. //Maximum input buffer size. This should be plenty.
  18. const int BUFSIZE = 128;
  19.  
  20. int get_file_num(char file)
  21. {
  22. return file - (int)'a';
  23. }
  24.  
  25. //Just a simple way to get the rank number.
  26. int get_rank_num(char rank)
  27. {
  28. return rank - (int)'1';
  29. }
  30.  
  31. //Get the piece type, based on the character. Uses the gcc range
  32. //extension to simplify the pawn case.
  33. enum piece_type get_piece_type(char pt)
  34. {
  35. switch(pt)
  36. {
  37. case 'a' ... 'h':
  38. return PAWN;
  39. case 'K':
  40. return KING;
  41. case 'N':
  42. return KNIGHT;
  43. case 'Q':
  44. return QUEEN;
  45. case 'R':
  46. return ROOK;
  47. case 'B':
  48. return BISHOP;
  49. default:
  50. return -1;
  51. }
  52. }
  53.  
  54. //Get the piece's score.
  55. int get_piece_score(enum piece_type type)
  56. {
  57. switch(type)
  58. {
  59. case PAWN:
  60. return 1;
  61. case KNIGHT:
  62. case BISHOP:
  63. return 3;
  64. case ROOK:
  65. return 5;
  66. case QUEEN:
  67. return 9;
  68. default:
  69. return -1;
  70. }
  71. }
  72.  
  73. //Reads a side's move.
  74. void read_side(char *buf, int *opp_score, int base_rank)
  75. {
  76. int cur = 0;
  77. enum piece_type piece;
  78. if(strncmp("O-O", buf, 3) == 0) {
  79. board[5][base_rank] = ROOK;
  80. board[6][base_rank] = KING;
  81. return;
  82. }
  83. else if(strncmp("O-O-O", buf, 5) == 0) {
  84. board[3][base_rank] = ROOK;
  85. board[2][base_rank] = KING;
  86. return;
  87. }
  88.  
  89. //First character is always the piece (In a non-castling situation)
  90. piece = get_piece_type(buf[cur]);
  91.  
  92.  
  93. if(piece != PAWN || buf[cur + 1] == 'x')
  94. cur++;
  95.  
  96. //If the current symbol is only to resolve ambiguity (Nbd2 or Nbxd2), skip it
  97. if(isalpha(buf[cur + 1]) && buf[cur] != 'x')
  98. cur++;
  99.  
  100. if(buf[cur] == 'x') { //Capture occurrs
  101. cur++;
  102. enum piece_type cap_piece =
  103. board[get_file_num(buf[cur])][get_rank_num(buf[cur + 1])];
  104. *opp_score -= get_piece_score(cap_piece);
  105. }
  106. //Update board.
  107. board[get_file_num(buf[cur])][get_rank_num(buf[cur + 1])] = piece;
  108. }
  109.  
  110. void read_line(char *buf)
  111. {
  112. char *black_move = strchr(buf, ' ');
  113. //Read white, skip the whitespace, then read black
  114. read_side(buf, &score_black, 0);
  115. read_side(black_move + 1, &score_white, 7);
  116. }
  117.  
  118.  
  119. int main(void)
  120. {
  121. char input[BUFSIZE];
  122. do {
  123. fgets(input, BUFSIZE, stdin);
  124. read_line(input);
  125. } while(!feof(stdin));
  126.  
  127. printf("%d-%d\n", score_white, score_black);
  128. return 0;
  129. }
  130.  
Success #stdin #stdout 0s 2252KB
stdin
c5 exc2
stdout
7 captures 0 at c2
38-39