Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* chess board
- Copyright (C) 2019 anon.
- This program is free software: you can redistribute it an d/or modify
- it under the terms of the GNU General Public License as p ublished by
- the Free Software Foundation, either version 3 of the Lic ense, or
- (at your option) any later version.
- This program is distributed in the hope that it will be u seful,
- but WITHOUT ANY WARRANTY; without even the implied warran ty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <https://www.gnu.or g/licenses/>. */
- #include <stdio.h>
- #include <stdlib.h>
- #include <ncurses.h>
- #include <signal.h>
- #include <time.h>
- #include <string.h>
- #define ON 1
- #define OFF 0
- static const char DEFAULT_BOARD[] = "rnbqkbnr"\
- "pppppppp"\
- " "\
- " "\
- " "\
- " "\
- "PPPPPPPP"\
- "RNBQKBNR\x00";
- #define PAWN_WHITE 'P'
- #define PAWN_BLACK 'p'
- #define ROOK_WHITE 'R'
- #define ROOK_BLACK 'r'
- #define BISHOP_WHITE 'B'
- #define BISHOP_BLACK 'b'
- #define KNIGHT_WHITE 'N'
- #define KNIGHT_BLACK 'n'
- #define QUEEN_WHITE 'Q'
- #define QUEEN_BLACK 'q'
- #define KING_WHITE 'K'
- #define KING_BLACK 'k'
- #define BOARD_COLS 8
- #define BOARD_ROWS 8
- #define DEFAULT_REDRAW 1000
- struct chess_board
- {
- char *squares;
- };
- static struct timespec start_clock, end_clock;
- static struct chess_board board;
- static int redraw_time = DEFAULT_REDRAW; // 1000ms
- void cleanup()
- {
- if(board.squares != NULL) {
- free(board.squares);
- }
- endwin(); // Close ncurses
- }
- void on_signal(const int signal)
- {
- exit(EXIT_SUCCESS);
- }
- void reset()
- {
- strncpy(board.squares, (const char*)&DEFAULT_BOARD, B OARD_COLS*BOARD_ROWS);
- }
- void init()
- {
- /* curses config */
- initscr();
- keypad(stdscr, TRUE);
- nodelay(stdscr, TRUE);
- curs_set(OFF);
- noecho();
- if(has_colors()) {
- start_color();
- init_pair(1, COLOR_BLACK, COLOR_WHITE);
- init_pair(2, COLOR_WHITE, COLOR_BLACK);
- }
- /* zero out variables */
- memset(&start_clock, 0, sizeof(struct timespec));
- memset(&board, 0, sizeof(struct chess_board));
- board.squares = malloc(BOARD_COLS*BOARD_ROWS*sizeof(c har));
- reset();
- }
- void handle_input(char c)
- {
- switch (c) {
- case ERR:
- return;
- case 'q':
- exit(EXIT_SUCCESS);
- }
- }
- void draw_board()
- {
- int i, j, y, x;
- uint8_t p;
- /* Draw the pieces themselves */
- for(i=0; i<BOARD_COLS*BOARD_ROWS; i++) {
- x = i%BOARD_COLS;
- y = i/BOARD_COLS;
- p = ((i+y)%2)+1;
- attron(COLOR_PAIR(p));
- for (j=0; j<3; j++) {
- move(y*3+j, x*5);
- if (j==1) {
- addch(' ');
- addch(' ');
- addch(board.squares[i]);
- addch(' ');
- addch(' ');
- } else {
- addch(' ');
- addch(' ');
- addch(' ');
- addch(' ');
- addch(' ');
- }
- }
- attroff(COLOR_PAIR(p));
- }
- }
- void main_loop()
- {
- int input = ERR;
- long long dt_ms = 0; // Extra long ;)
- do {
- clock_gettime(CLOCK_MONOTONIC, &end_clock);
- dt_ms = ((end_clock.tv_sec-start_clock.tv_sec )*1000 +
- (end_clock.tv_nsec-start_cloc k.tv_nsec)/1000000);
- // time since last redraw > redraw_time
- if(dt_ms > redraw_time) {
- start_clock = end_clock;
- draw_board();
- refresh();
- }
- handle_input(input);
- input=getch();
- } while (input != 'q');
- }
- int main(int argc, char** argv)
- {
- /* Ensure program is ready to close first */
- atexit(cleanup);
- signal(SIGINT, on_signal);
- signal(SIGTERM, on_signal);
- /* Then initialize */
- init();
- /* Then run in loop */
- main_loop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement