Advertisement
Guest User

Chess Bort

a guest
May 15th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.74 KB | None | 0 0
  1. /*  chess board
  2.     Copyright (C) 2019  anon.
  3.  
  4.     This program is free software: you can redistribute it an                                                                                          d/or modify
  5.     it under the terms of the GNU General Public License as p                                                                                          ublished by
  6.     the Free Software Foundation, either version 3 of the Lic                                                                                          ense, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be u                                                                                          seful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warran                                                                                          ty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                                                                                           the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public                                                                                           License
  15.     along with this program.  If not, see <https://www.gnu.or                                                                                          g/licenses/>. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <ncurses.h>
  20. #include <signal.h>
  21. #include <time.h>
  22. #include <string.h>
  23.  
  24. #define ON 1
  25. #define OFF 0
  26.  
  27. static const char DEFAULT_BOARD[] = "rnbqkbnr"\
  28.         "pppppppp"\
  29.         "        "\
  30.         "        "\
  31.         "        "\
  32.         "        "\
  33.         "PPPPPPPP"\
  34.         "RNBQKBNR\x00";
  35.  
  36. #define PAWN_WHITE 'P'
  37. #define PAWN_BLACK 'p'
  38. #define ROOK_WHITE 'R'
  39. #define ROOK_BLACK 'r'
  40. #define BISHOP_WHITE 'B'
  41. #define BISHOP_BLACK 'b'
  42. #define KNIGHT_WHITE 'N'
  43. #define KNIGHT_BLACK 'n'
  44. #define QUEEN_WHITE 'Q'
  45. #define QUEEN_BLACK 'q'
  46. #define KING_WHITE 'K'
  47. #define KING_BLACK 'k'
  48.  
  49. #define BOARD_COLS 8
  50. #define BOARD_ROWS 8
  51.  
  52. #define DEFAULT_REDRAW 1000
  53.  
  54. struct chess_board
  55. {
  56.         char *squares;
  57. };
  58.  
  59. static struct timespec start_clock, end_clock;
  60. static struct chess_board board;
  61. static int redraw_time = DEFAULT_REDRAW; // 1000ms
  62.  
  63. void cleanup()
  64. {
  65.         if(board.squares != NULL) {
  66.                 free(board.squares);
  67.         }
  68.         endwin(); // Close ncurses
  69. }
  70.  
  71. void on_signal(const int signal)
  72. {
  73.         exit(EXIT_SUCCESS);
  74. }
  75.  
  76. void reset()
  77. {
  78.         strncpy(board.squares, (const char*)&DEFAULT_BOARD, B                                                                                          OARD_COLS*BOARD_ROWS);
  79. }
  80.  
  81. void init()
  82. {
  83.         /* curses config */
  84.  
  85.         initscr();
  86.         keypad(stdscr, TRUE);
  87.         nodelay(stdscr, TRUE);
  88.         curs_set(OFF);
  89.         noecho();
  90.         if(has_colors()) {
  91.                 start_color();
  92.                 init_pair(1, COLOR_BLACK, COLOR_WHITE);
  93.                 init_pair(2, COLOR_WHITE, COLOR_BLACK);
  94.         }
  95.         /* zero out variables */
  96.         memset(&start_clock, 0, sizeof(struct timespec));
  97.         memset(&board, 0, sizeof(struct chess_board));
  98.         board.squares = malloc(BOARD_COLS*BOARD_ROWS*sizeof(c                                                                                          har));
  99.         reset();
  100. }
  101.  
  102. void handle_input(char c)
  103. {
  104.         switch (c) {
  105.                 case ERR:
  106.                         return;
  107.                 case 'q':
  108.                         exit(EXIT_SUCCESS);
  109.         }
  110. }
  111.  
  112. void draw_board()
  113. {
  114.         int i, j, y, x;
  115.         uint8_t p;
  116.         /* Draw the pieces themselves */
  117.         for(i=0; i<BOARD_COLS*BOARD_ROWS; i++) {
  118.                 x = i%BOARD_COLS;
  119.                 y = i/BOARD_COLS;
  120.                 p = ((i+y)%2)+1;
  121.                 attron(COLOR_PAIR(p));
  122.                 for (j=0; j<3; j++) {
  123.                         move(y*3+j, x*5);
  124.                         if (j==1) {
  125.                                 addch(' ');
  126.                                 addch(' ');
  127.                                 addch(board.squares[i]);
  128.                                 addch(' ');
  129.                                 addch(' ');
  130.                         } else {
  131.                                 addch(' ');
  132.                                 addch(' ');
  133.                                 addch(' ');
  134.                                 addch(' ');
  135.                                 addch(' ');
  136.                         }
  137.                 }
  138.                 attroff(COLOR_PAIR(p));
  139.         }
  140. }
  141.  
  142. void main_loop()
  143. {
  144.         int input = ERR;
  145.         long long dt_ms = 0; // Extra long ;)
  146.         do {
  147.                 clock_gettime(CLOCK_MONOTONIC, &end_clock);
  148.                 dt_ms = ((end_clock.tv_sec-start_clock.tv_sec                                                                                          )*1000 +
  149.                                 (end_clock.tv_nsec-start_cloc                                                                                          k.tv_nsec)/1000000);
  150.                 // time since last redraw > redraw_time
  151.                 if(dt_ms > redraw_time) {
  152.                         start_clock = end_clock;
  153.                         draw_board();
  154.                         refresh();
  155.                 }
  156.                 handle_input(input);
  157.                 input=getch();
  158.         } while (input != 'q');
  159. }
  160.  
  161. int main(int argc, char** argv)
  162. {
  163.         /* Ensure program is ready to close first */
  164.         atexit(cleanup);
  165.         signal(SIGINT, on_signal);
  166.         signal(SIGTERM, on_signal);
  167.         /* Then initialize */
  168.         init();
  169.         /* Then run in loop */
  170.         main_loop();
  171.  
  172.         return EXIT_SUCCESS;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement