Redid everything
This commit is contained in:
parent
d80bdc6f51
commit
680ad15b25
@ -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
|
||||
|
||||
105
c_projects/tiktaktoe/include/gamelogic.h
Normal file
105
c_projects/tiktaktoe/include/gamelogic.h
Normal file
@ -0,0 +1,105 @@
|
||||
#ifndef GAMELOGIC_H
|
||||
#define GAMELOGIC_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
#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
|
||||
@ -1,29 +1,4 @@
|
||||
#ifndef TIKTAKTOE_H
|
||||
#define TIKTAKTOE_H
|
||||
|
||||
#include <ncurses.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
|
||||
@ -1,2 +0,0 @@
|
||||
obj/main.o: src/main.c include/tiktaktoe.h
|
||||
include/tiktaktoe.h:
|
||||
Binary file not shown.
@ -1,2 +0,0 @@
|
||||
obj/tiktaktoe.o: src/tiktaktoe.c include/tiktaktoe.h
|
||||
include/tiktaktoe.h:
|
||||
Binary file not shown.
@ -1,6 +1,8 @@
|
||||
#include "gamelogic.h"
|
||||
#include "tiktaktoe.h"
|
||||
|
||||
int main(){
|
||||
start_tiktaktoe();
|
||||
init_gamestate();
|
||||
main_loop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
Loading…
Reference in New Issue
Block a user