diff --git a/c_projects/tiktaktoe/Makefile b/c_projects/tiktaktoe/Makefile index 678e82b..7918112 100644 --- a/c_projects/tiktaktoe/Makefile +++ b/c_projects/tiktaktoe/Makefile @@ -27,7 +27,7 @@ STATIC_LIBS = $(shell find ./ -name "*.a") CC := gcc INCLUDE_LIST := $(foreach m,$(SUBMODULES),$(m)/$(INCLUDE_DIR)) $(INCLUDE_DIR) $(TESTS_INCL_DIR) INCLUDES := $(addprefix -I,$(INCLUDE_LIST)) -CFLAGS := -std=c99 -ggdb -Wall -Wextra -MMD -MP -lncurses -fsanitize=address +CFLAGS := -std=c99 -ggdb -Wall -Wextra -MMD -MP -lcurses -lncurses -fsanitize=address DFLAGS := -D_DEBUG # Main Executable Filename diff --git a/c_projects/tiktaktoe/include/gamelogic.h b/c_projects/tiktaktoe/include/gamelogic.h new file mode 100644 index 0000000..634210a --- /dev/null +++ b/c_projects/tiktaktoe/include/gamelogic.h @@ -0,0 +1,105 @@ +#ifndef GAMELOGIC_H +#define GAMELOGIC_H + +#include +#include +#include + +#define ROWS 3 +#define COLS 3 +#define TOTAL_SQUARES ROWS*COLS +#define DOF 2 + + +typedef enum{OPEN, X, O} squarestate; +enum turn {X_TURN, O_TURN}; + +typedef struct position_t{ + uint8_t row; + uint8_t col; +}position_t; + +position_t cpos; +squarestate boardstate[ROWS * COLS]; +enum turn currturn; + +void print_boardstate(){ + for( int y = 0; y < ROWS; y++ ){ + for( int x = 0; x < COLS; x++ ){ + printf("|%d|", boardstate[COLS*y + x]); + } + printf("\n"); + } +} + +void init_gamestate(){ + // init cursor pos to 0,0 + cpos = (position_t){0,0}; + currturn = X_TURN; + + for( int i = 0; i < TOTAL_SQUARES; i++ ){ + boardstate[i] = OPEN; + } +} + +void check_game_over(){ + //TODO: no idea +} + +void update_boardstate(){ + int idx = COLS * cpos.row + cpos.col; + printf("%d\n", idx); + if(boardstate[idx] == OPEN){ + if(currturn == X_TURN){ + boardstate[idx] = X; + } + else if(currturn == O_TURN){ + boardstate[idx] = O; + } + } + print_boardstate(); + check_game_over(); +} + +void update_gamestate(char input){ + + switch(input){ + case('w'): + if(cpos.row > 0){ + cpos.row--; + } + break; + case('s'): + if(cpos.row < (ROWS - 1)){ + cpos.row++; + } + break; + case('a'): + if(cpos.col > 0){ + cpos.col--; + } + break; + case('d'): + if(cpos.col < (COLS - 1)){ + cpos.col++; + } + break; + case('e'): + update_boardstate(); + currturn = !currturn; + break; + } + + printf("cpos.row = %d, cpos.col = %d,", cpos.row, cpos.col); + printf(" Turn: %d\n", currturn); +} + +void main_loop(){ + int input = 0; + while(1){ + while((input = getchar()) == '\n'); + update_gamestate(input); + } +} + +#endif diff --git a/c_projects/tiktaktoe/include/tiktaktoe.h b/c_projects/tiktaktoe/include/tiktaktoe.h index 4bd394d..ea6bcdf 100644 --- a/c_projects/tiktaktoe/include/tiktaktoe.h +++ b/c_projects/tiktaktoe/include/tiktaktoe.h @@ -1,29 +1,4 @@ #ifndef TIKTAKTOE_H #define TIKTAKTOE_H -#include -#include - -#define GRIDSIZE 3 -#define HEADERSIZE 5 -#define FOOTERSIZE 5 - -extern const char game_end; - -typedef struct w_data_t{ - WINDOW *w_ptr; - int height; - int width; -}w_data_t; - - -// gamelogic -int start_tiktaktoe(void); -void get_input(w_data_t *board); - -// render -void init_windows(w_data_t *board, w_data_t *header, w_data_t *footer); -void create_grid(w_data_t *currwin); -void draw_board(w_data_t *board, w_data_t *header, w_data_t *footer); - #endif diff --git a/c_projects/tiktaktoe/obj/main.d b/c_projects/tiktaktoe/obj/main.d deleted file mode 100644 index 0a34ac8..0000000 --- a/c_projects/tiktaktoe/obj/main.d +++ /dev/null @@ -1,2 +0,0 @@ -obj/main.o: src/main.c include/tiktaktoe.h -include/tiktaktoe.h: diff --git a/c_projects/tiktaktoe/obj/main.o b/c_projects/tiktaktoe/obj/main.o deleted file mode 100644 index 6f31d6b..0000000 Binary files a/c_projects/tiktaktoe/obj/main.o and /dev/null differ diff --git a/c_projects/tiktaktoe/obj/tiktaktoe.d b/c_projects/tiktaktoe/obj/tiktaktoe.d deleted file mode 100644 index cb06b98..0000000 --- a/c_projects/tiktaktoe/obj/tiktaktoe.d +++ /dev/null @@ -1,2 +0,0 @@ -obj/tiktaktoe.o: src/tiktaktoe.c include/tiktaktoe.h -include/tiktaktoe.h: diff --git a/c_projects/tiktaktoe/obj/tiktaktoe.o b/c_projects/tiktaktoe/obj/tiktaktoe.o deleted file mode 100644 index 2e7df79..0000000 Binary files a/c_projects/tiktaktoe/obj/tiktaktoe.o and /dev/null differ diff --git a/c_projects/tiktaktoe/src/main.c b/c_projects/tiktaktoe/src/main.c index 9ec66c6..4436b68 100644 --- a/c_projects/tiktaktoe/src/main.c +++ b/c_projects/tiktaktoe/src/main.c @@ -1,6 +1,8 @@ +#include "gamelogic.h" #include "tiktaktoe.h" int main(){ - start_tiktaktoe(); + init_gamestate(); + main_loop(); return 0; } diff --git a/c_projects/tiktaktoe/src/tiktaktoe.c b/c_projects/tiktaktoe/src/tiktaktoe.c index a91d138..a94ea1b 100644 --- a/c_projects/tiktaktoe/src/tiktaktoe.c +++ b/c_projects/tiktaktoe/src/tiktaktoe.c @@ -1,79 +1 @@ #include "tiktaktoe.h" - -const char game_end = 0; - -int start_tiktaktoe(void){ - w_data_t header; - w_data_t board; - w_data_t footer; - initscr(); - - init_windows(&board, &header, &footer); - - while (!game_end){ - get_input(&board); - draw_board(&board, &header, &footer); - } - - wgetch(board.w_ptr); - endwin(); - return 0; -} - -void get_input(w_data_t *w){ - char s = wgetch(w->w_ptr); - if (s == 'w'){ - mvwprintw(w->w_ptr, w->height / 2, w->width / 2, "x"); - } -} - -void init_windows(w_data_t *board, w_data_t *header, w_data_t *footer){ - const int board_size = LINES - HEADERSIZE - FOOTERSIZE; - - header->w_ptr = newwin(HEADERSIZE,0,0,0); - board->w_ptr = newwin(board_size,0,HEADERSIZE,0); - footer->w_ptr = newwin(FOOTERSIZE,0,HEADERSIZE + board_size,0); - - getmaxyx(header->w_ptr, header->height, header->width); - getmaxyx(board->w_ptr, board->height, board->width); - getmaxyx(footer->w_ptr, footer->height, footer->width); - - box(header->w_ptr, 0, 0); - box(footer->w_ptr, 0, 0); - box(board->w_ptr, 0, 0); - - create_grid(board); - - wrefresh(header->w_ptr); - wrefresh(board->w_ptr); - wrefresh(footer->w_ptr); -} - -void create_grid(w_data_t *currwin){ - int buffer = 10; - int hline_pos = 0; - int vline_pos = 0; - - for (int i = 0; i < GRIDSIZE - 1; i++){ - hline_pos += currwin->height/GRIDSIZE; - mvwhline(currwin->w_ptr, hline_pos, buffer,0,currwin->width - 2 * buffer); - } - for (int i = 0; i < GRIDSIZE - 1; i++){ - vline_pos += currwin->width/GRIDSIZE; - mvwvline(currwin->w_ptr, buffer / 2,vline_pos,0,currwin->height - buffer); - } - -} - -void draw_board(w_data_t *board, w_data_t *header, w_data_t *footer){ - create_grid(board); - - getmaxyx(header->w_ptr, header->height, header->width); - getmaxyx(board->w_ptr, board->height, board->width); - getmaxyx(footer->w_ptr, footer->height, footer->width); - - wrefresh(header->w_ptr); - wrefresh(board->w_ptr); - wrefresh(footer->w_ptr); - -} diff --git a/c_projects/tiktaktoe/tiktaktoe b/c_projects/tiktaktoe/tiktaktoe deleted file mode 100755 index b06a1b8..0000000 Binary files a/c_projects/tiktaktoe/tiktaktoe and /dev/null differ