From f29f867a18ee82d6c79900420b6a6d5f66a75f18 Mon Sep 17 00:00:00 2001 From: sumuel Date: Wed, 15 Jul 2026 06:46:24 +0100 Subject: init --- Makefile | 22 ++++++++++++++++++++++ board.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ board.h | 9 +++++++++ input.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ input.h | 18 ++++++++++++++++++ main.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 198 insertions(+) create mode 100644 Makefile create mode 100644 board.c create mode 100644 board.h create mode 100644 input.c create mode 100644 input.h create mode 100644 main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..87b33c8 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +PREFIX = /usr/local + +CFLAGS = -std=c99 -pedantic -Wall -Wno-deprecated-declarations -Os +NCURSES = -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 -lncurses -ltinfo + +CC = gcc + +SRC = $(shell find . -name "*.c") +TARGET = chess + +all: + ${CC} ${SRC} -o ${TARGET} ${CFLAGS} ${NCURSES} + +install: all + ${CC} ${NCURSES} ${CFLAGS} ${SRC} -o ${TARGET} + cp -f stats ${DESTDIR}${PREFIX}/bin + chmod 755 ${DESTDIR}${PREFIX}/bin/${TARGET} + +uninstall: + rm -f ${DESTDIR}${PREFIX}/bin/${TARGET} + + diff --git a/board.c b/board.c new file mode 100644 index 0000000..a4faad3 --- /dev/null +++ b/board.c @@ -0,0 +1,49 @@ +#include +#include "board.h" +#include "input.h" +int cursorPosX = 4; +int cursorPosY = 3; + +/* Create the board as a 2d array */ +char board[8][8] = { + {'r','n','b','q','k','b','n','r'}, + {'p','p','p','p','p','p','p','p'}, + {' ',' ',' ',' ',' ',' ',' ',' '}, + {' ',' ',' ',' ',' ',' ',' ',' '}, + {' ',' ',' ',' ',' ',' ',' ',' '}, + {' ',' ',' ',' ',' ',' ',' ',' '}, + {'P','P','P','P','P','P','P','P'}, + {'R','N','B','Q','K','B','N','R'}, +}; + +void PrintBoard(char board[8][8]){ + for (int i=0; i<8;i++){ + for (int j=0; j<8;j++){ + + if ( i == cursorPosX && j == cursorPosY ) + { + attron(COLOR_PAIR(3)); + printw(" %c ", board[i][j]); + attroff(COLOR_PAIR(3)); + } + else if ((i + j) & 1) /* Make all the even positions white */ + { + attron(COLOR_PAIR(1)); + printw(" %c ", board[i][j]); + attroff(COLOR_PAIR(1)); + } + else + { + attron(COLOR_PAIR(2)); + printw(" %c ", board[i][j]); + attroff(COLOR_PAIR(2)); + } + + } + attron(A_BOLD | COLOR_PAIR(4)); + if ( i == 1) printw(" BLACK "); + if ( i == 6) printw(" WHITE "); + attroff(A_BOLD | COLOR_PAIR(4)); + printw("\n"); + } +} diff --git a/board.h b/board.h new file mode 100644 index 0000000..74529db --- /dev/null +++ b/board.h @@ -0,0 +1,9 @@ +#ifndef BOARD_H_ /* Include guard */ +#define BOARD_H_ + +extern char board[8][8]; + +void PrintBoard(char board[8][8]); /* declare the function */ + +#endif // BOARD_H_ + diff --git a/input.c b/input.c new file mode 100644 index 0000000..d7c8ffb --- /dev/null +++ b/input.c @@ -0,0 +1,54 @@ +#include +#include +#include "input.h" +#include "board.h" +char hand = ' '; +int turnPart = 1; +int turns = 0; + +int InputBoard(char board[8][8], int keypress) { + + /* Handle key input */ + switch (keypress) { + case 'q': + endwin(); + exit(0); + break; + case KEY_UP: + if (cursorPosX > 0) cursorPosX--; + break; + case KEY_DOWN: + if (cursorPosX < 7) cursorPosX++; + break; + case KEY_LEFT: + if (cursorPosY > 0 ) cursorPosY--; + break; + case KEY_RIGHT: + if (cursorPosY < 7) cursorPosY++; + break; + case '\n': /* This is the ENTER key. KEY_ENTER does not work */ + if ( hand == ' ' && board[cursorPosX][cursorPosY] != ' '){ + hand = board[cursorPosX][cursorPosY]; + board[cursorPosX][cursorPosY] = ' '; + turnPart++; + } + + else if ( hand != ' ' && board[cursorPosX][cursorPosY] != ' ') { + board[cursorPosX][cursorPosY] = hand; + hand = ' '; + turnPart++; + } + else if ( hand != ' ' && board[cursorPosX][cursorPosY] == ' ') { + board[cursorPosX][cursorPosY] = hand; + hand = ' '; + turnPart++; + } + if (turnPart & 1) turns++; + + + break; + } + + + return 0; +} diff --git a/input.h b/input.h new file mode 100644 index 0000000..d5f026f --- /dev/null +++ b/input.h @@ -0,0 +1,18 @@ +#ifndef INPUT_H_ /* Include guard */ +#define INPUT_H_ + +/* declare cursor position */ +extern int cursorPosX; +extern int cursorPosY; + +/* Error stuff */ + +extern int turns; + +/* The piece to be moved goes here */ +extern char hand; + +int InputBoard(char board[8][8], int keypress); /* declare the function */ + +#endif // INPUT_H_ + diff --git a/main.c b/main.c new file mode 100644 index 0000000..18327a3 --- /dev/null +++ b/main.c @@ -0,0 +1,46 @@ +#include +#include +#include "board.h" +#include "input.h" + +int main() { + int ch; + + initscr(); /* Start curses mode */ + if(has_colors() == FALSE) + { endwin(); + printf("Your terminal does not support colour\n"); + exit(1); + } + start_color(); /* Start color */ + noecho(); + curs_set(0); + keypad(stdscr, TRUE); + /* End of ncurses init nonsense */ + + /* Don't use black it will mess with the background colour */ + init_color(COLOR_RED, 556, 388, 176); + init_color(COLOR_WHITE, 820, 840, 880); + init_pair(1, COLOR_RED, COLOR_WHITE); + init_pair(2, COLOR_WHITE, COLOR_RED); + init_pair(3, COLOR_YELLOW, COLOR_MAGENTA); + init_pair(4, COLOR_CYAN, COLOR_BLACK); + + while (turns < 1000){ + refresh(); /* Print it on to the real screen */ + char *player; + player = (turns & 1) ? "BLACK" : "WHITE"; + + printw("Here is the board:\n"); + PrintBoard(board); /* Print out the chess board */ + printw("Turn No. %d (%s)", turns, player); + + ch = getch(); /* Wait for user input */ + InputBoard(board, ch); + clear(); + + } + + endwin(); /* End curses mode */ + return 0; +} -- cgit v1.2.3