Board no is overwritten every frame

This commit is contained in:
xavi 2026-01-21 21:18:51 -08:00
parent 47a30e426a
commit fcea0483b4
9 changed files with 135 additions and 60 deletions

3
c_projects/tiktaktoe/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
./obj/*
tags
tiktaktoe

View File

@ -3,7 +3,6 @@
#include <stdio.h>
#include <stdint.h>
#include <ncurses.h>
#define N 3
#define ROWS N
@ -12,13 +11,17 @@
#define TOTAL_SQUARES ROWS*COLS
#define DOF 2
typedef enum{OPEN, X, O} squarestate;
typedef enum{OPEN_SPACE, X_SPACE, O_SPACE} squarestate;
enum turn {X_TURN, O_TURN};
typedef struct position_t{
uint8_t row;
uint8_t col;
}position_t;
extern squarestate boardstate[ROWS * COLS];
void print_boardstate();
void init_gamestate();
int is_game_over();

View File

@ -1,4 +0,0 @@
#ifndef TIKTAKTOE_H
#define TIKTAKTOE_H
#endif

View File

@ -0,0 +1,12 @@
#ifndef UI_H
#define UI_H
#include <ncurses.h>
#include "gamelogic.h"
void init();
void render();
void main_loop();
#endif

View File

@ -5,16 +5,18 @@ static enum turn currturn;
// point tracker. make any of these = N and win
static int8_t pts[ROWS+COLS+DIAGS];
static int round_counter;
static position_t cpos;
static squarestate boardstate[ROWS * COLS];
// Globals
position_t cpos;
squarestate boardstate[ROWS * COLS];
void print_boardstate(){
for( int y = 0; y < ROWS; y++ ){
for( int x = 0; x < COLS; x++ ){
printf("|%c|",
(boardstate[COLS*y + x] == X) ? 'X' :
(boardstate[COLS*y + x] == O) ? 'O' : '_');
(boardstate[COLS*y + x] == X_SPACE) ? 'X' :
(boardstate[COLS*y + x] == O_SPACE) ? 'O' : '_');
}
printf("\n");
}
@ -27,7 +29,7 @@ void init_gamestate(){
round_counter = 0;
for( int i = 0; i < TOTAL_SQUARES; i++ ){
boardstate[i] = OPEN;
boardstate[i] = OPEN_SPACE;
}
}
@ -64,18 +66,18 @@ void add_point(){
void update_boardstate(){
int idx = COLS * cpos.row + cpos.col;
if(boardstate[idx] == OPEN){
if(boardstate[idx] == OPEN_SPACE){
if(currturn == X_TURN){
boardstate[idx] = X;
boardstate[idx] = X_SPACE;
}
else if(currturn == O_TURN){
boardstate[idx] = O;
boardstate[idx] = O_SPACE;
}
add_point();
round_counter++;
currturn = !currturn;
}
print_boardstate();
//print_boardstate();
}
void update_gamestate(char input){
@ -106,15 +108,9 @@ void update_gamestate(char input){
break;
}
printf("cpos.row = %d, cpos.col = %d,", cpos.row, cpos.col);
printf(" Turn: %d\n", currturn);
//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);
if (is_game_over()) break;
}
}
//void main_loop(){
//}

View File

@ -1,8 +1,7 @@
#include "gamelogic.h"
#include "tiktaktoe.h"
#include "ui.h"
int main(){
init_gamestate();
init();
main_loop();
return 0;
endwin();
}

View File

@ -1 +0,0 @@
#include "tiktaktoe.h"

View File

@ -0,0 +1,35 @@
#include "ui.h"
void init(){
initscr();
raw();
noecho();
init_gamestate();
}
void render(){
move(0,0);
for( int y = 0; y < ROWS; y++ ){
for( int x = 0; x < COLS; x++ ){
printw("|%c|",
(boardstate[COLS*y + x] == X_SPACE) ? 'X' :
(boardstate[COLS*y + x] == O_SPACE) ? 'O' : '_');
}
printw("\n");
}
refresh();
}
void main_loop(){
while(!is_game_over()){
int input = 0;
input = getch();
update_gamestate(input);
render();
}
getch();
endwin();
return;
}

View File

@ -104,23 +104,29 @@ $(TESTS_DIR) Makefile /^$(SRC_DIR) $(OBJ_DIR) $(TESTS_DIR) $(INCLUDE_DIR) $(SUBM
% Makefile /^$(TESTS:$(TESTS_DIR)\/%.c=%): %: $(OBJ_DIR)\/%.o $(OBJ) check_submods$/;" t
CC Makefile /^CC := gcc$/;" m
CFLAGS Makefile /^CFLAGS := -std=c99 -ggdb -Wall -Wextra -MMD -MP -lncurses -fsanitize=address$/;" m
COLS include/gamelogic.h /^#define COLS /;" d
DEPENDENCIES Makefile /^DEPENDENCIES := $(OBJ:%.o=%.d)$/;" m
DFLAGS Makefile /^DFLAGS := -D_DEBUG$/;" m
DIAGS include/gamelogic.h /^#define DIAGS /;" d
DOF include/gamelogic.h /^#define DOF /;" d
END tests/include/test.h /^typedef enum {START, END} header_opt;$/;" e enum:__anon431ebd300103
FAIL tests/include/test.h /^typedef enum {PASS, FAIL}rc_opt;$/;" e enum:__anon431ebd300203
FOOTERSIZE include/tiktaktoe.h /^#define FOOTERSIZE /;" d
GRIDSIZE include/tiktaktoe.h /^#define GRIDSIZE /;" d
HEADERSIZE include/tiktaktoe.h /^#define HEADERSIZE /;" d
GAMELOGIC_H include/gamelogic.h /^#define GAMELOGIC_H$/;" d
INCLUDES Makefile /^INCLUDES := $(addprefix -I,$(INCLUDE_LIST))$/;" m
INCLUDE_DIR Makefile /^INCLUDE_DIR := include$/;" m
INCLUDE_LIST Makefile /^INCLUDE_LIST := $(foreach m,$(SUBMODULES),$(m)\/$(INCLUDE_DIR)) $(INCLUDE_DIR) $(TESTS_INCL_DIR)$/;" m
MAIN Makefile /^MAIN := main.c$/;" m
Makefile Makefile 1;" F epoch:1767240744
Makefile Makefile 1;" F epoch:1767585271
N include/gamelogic.h /^#define N /;" d
OBJ Makefile /^OBJ := $(SRC:$(SRC_DIR)\/%.c=$(OBJ_DIR)\/%.o)$/;" m
OBJ_DIR Makefile /^OBJ_DIR := obj$/;" m
OPEN_SPACE include/gamelogic.h /^typedef enum{OPEN_SPACE, X_SPACE, O_SPACE} squarestate;$/;" e enum:__anon9083c9160103
O_SPACE include/gamelogic.h /^typedef enum{OPEN_SPACE, X_SPACE, O_SPACE} squarestate;$/;" e enum:__anon9083c9160103
O_TURN include/gamelogic.h /^enum turn {X_TURN, O_TURN};$/;" e enum:turn
PASS tests/include/test.h /^typedef enum {PASS, FAIL}rc_opt;$/;" e enum:__anon431ebd300203
PROJECT_NAME Makefile /^PROJECT_NAME := tiktaktoe$/;" m
PROJECT_OBJ Makefile /^PROJECT_OBJ := $(OBJ_DIR)\/$(PROJECT_NAME).o$/;" m
ROWS include/gamelogic.h /^#define ROWS /;" d
SRC Makefile /^SRC := $(filter-out $(SRC_DIR)\/$(MAIN), $(wildcard $(SRC_DIR)\/*.c))$/;" m
SRC_DIR Makefile /^SRC_DIR := src$/;" m
START tests/include/test.h /^typedef enum {START, END} header_opt;$/;" e enum:__anon431ebd300103
@ -136,68 +142,94 @@ TEST tests/test.c /^TEST(success){$/;" f
TESTS Makefile /^TESTS := $(wildcard $(TESTS_DIR)\/*.c)$/;" m
TESTS_DIR Makefile /^TESTS_DIR := tests$/;" m
TESTS_INCL_DIR Makefile /^TESTS_INCL_DIR := $(TESTS_DIR)\/include$/;" m
TIKTAKTOE_H include/tiktaktoe.h /^#define TIKTAKTOE_H$/;" d
TOTAL_SQUARES include/gamelogic.h /^#define TOTAL_SQUARES /;" d
UI_H include/ui.h /^#define UI_H$/;" d
X_GREEN tests/include/test.h /^#define X_GREEN /;" d
X_RED tests/include/test.h /^#define X_RED /;" d
X_RST tests/include/test.h /^#define X_RST /;" d
X_SPACE include/gamelogic.h /^typedef enum{OPEN_SPACE, X_SPACE, O_SPACE} squarestate;$/;" e enum:__anon9083c9160103
X_TURN include/gamelogic.h /^enum turn {X_TURN, O_TURN};$/;" e enum:turn
X_YELLOW tests/include/test.h /^#define X_YELLOW /;" d
__anon431ebd300103 tests/include/test.h /^typedef enum {START, END} header_opt;$/;" g
__anon431ebd300203 tests/include/test.h /^typedef enum {PASS, FAIL}rc_opt;$/;" g
__anon9083c9160103 include/gamelogic.h /^typedef enum{OPEN_SPACE, X_SPACE, O_SPACE} squarestate;$/;" g
add_point include/gamelogic.h /^void add_point();$/;" p typeref:typename:void
add_point src/gamelogic.c /^void add_point(){$/;" f typeref:typename:void
all Makefile /^all: $(PROJECT_NAME)$/;" t
bmaxx include/tiktaktoe.h /^ int bmaxx;$/;" m struct:w_data_t typeref:typename:int
bmaxx src/tiktaktoe.c /^ int bmaxx = 0;$/;" l function:draw_board typeref:typename:int file:
bmaxy include/tiktaktoe.h /^ int bmaxy;$/;" m struct:w_data_t typeref:typename:int
bmaxy src/tiktaktoe.c /^ int bmaxy = 0;$/;" l function:draw_board typeref:typename:int file:
board src/tiktaktoe.c /^ w_data_t* board = NULL;$/;" l function:start_tiktaktoe typeref:typename:w_data_t * file:
board_size src/tiktaktoe.c /^ const int board_size = LINES - HEADERSIZE - FOOTERSIZE;$/;" l function:init_windows typeref:typename:const int file:
buffer src/tiktaktoe.c /^ int buffer = 10;$/;" l function:draw_board typeref:typename:int file:
boardstate src/gamelogic.c /^squarestate boardstate[ROWS * COLS];$/;" v typeref:typename:squarestate[]
check_submods Makefile /^check_submods: $(SUBMODULE_SLIB)$/;" t
clean Makefile /^clean:$/;" t
col include/gamelogic.h /^ uint8_t col;$/;" m struct:position_t typeref:typename:uint8_t
cpid tests/include/test.h /^ pid_t cpid = fork();$/;" l function:run_child typeref:typename:pid_t file:
cpos src/gamelogic.c /^position_t cpos;$/;" v typeref:typename:position_t
currturn src/gamelogic.c /^static enum turn currturn;$/;" v typeref:enum:turn file:
dirs Makefile /^dirs: $(SRC_DIR) $(OBJ_DIR) $(TESTS_DIR) $(INCLUDE_DIR) $(SUBMODULES_DIR) $(SRC_DIR)\/$(MAIN)$/;" t
draw_board include/tiktaktoe.h /^void draw_board(w_data_t *board, w_data_t *header, w_data_t *footer);$/;" p typeref:typename:void
draw_board src/tiktaktoe.c /^void draw_board(w_data_t *board, w_data_t *header, w_data_t *footer){$/;" f typeref:typename:void
errors tests/include/test.h /^ int errors = 0;$/;" l function:run_tests typeref:typename:int file:
footer src/tiktaktoe.c /^ w_data_t* footer = NULL;$/;" l function:start_tiktaktoe typeref:typename:w_data_t * file:
gamelogic.c src/gamelogic.c 1;" F epoch:1769056543
gamelogic.d obj/gamelogic.d 1;" F epoch:1769056575
gamelogic.h include/gamelogic.h 1;" F epoch:1769056484
head tests/include/test.h /^static testnode* head = NULL;$/;" v typeref:typename:testnode *
header src/tiktaktoe.c /^ w_data_t* header = NULL;$/;" l function:start_tiktaktoe typeref:typename:w_data_t * file:
header_opt tests/include/test.h /^typedef enum {START, END} header_opt;$/;" t typeref:enum:__anon431ebd300103
help Makefile /^help:$/;" t
hline_pos src/tiktaktoe.c /^ int hline_pos = 0;$/;" l function:draw_board typeref:typename:int file:
i src/gamelogic.c /^ for (int i = 0; i < ROWS + COLS + DIAGS; i++){$/;" l function:is_game_over typeref:typename:int file:
i src/gamelogic.c /^ for( int i = 0; i < TOTAL_SQUARES; i++ ){$/;" l function:init_gamestate typeref:typename:int file:
i tests/test.c /^ int i = 1;$/;" l function:TEST typeref:typename:int file:
init_windows src/tiktaktoe.c /^void init_windows(w_data_t *board, w_data_t *header, w_data_t *footer){$/;" f typeref:typename:void
idx src/gamelogic.c /^ int idx = COLS * cpos.row + cpos.col;$/;" l function:update_boardstate typeref:typename:int file:
init include/ui.h /^void init();$/;" p typeref:typename:void
init src/ui.c /^void init(){$/;" f typeref:typename:void
init_gamestate include/gamelogic.h /^void init_gamestate();$/;" p typeref:typename:void
init_gamestate src/gamelogic.c /^void init_gamestate(){$/;" f typeref:typename:void
input src/ui.c /^ int input = 0;$/;" l function:main_loop typeref:typename:int file:
is_game_over include/gamelogic.h /^int is_game_over();$/;" p typeref:typename:int
is_game_over src/gamelogic.c /^int is_game_over(){$/;" f typeref:typename:int
main src/main.c /^int main(){$/;" f typeref:typename:int
main tests/test.c /^int main(void){$/;" f typeref:typename:int
main.c src/main.c 1;" F epoch:1766803538
main.d obj/main.d 1;" F epoch:1766865434
main.c src/main.c 1;" F epoch:1767938963
main.d obj/main.d 1;" F epoch:1768023978
main_loop include/gamelogic.h /^void main_loop();$/;" p typeref:typename:void
main_loop include/ui.h /^void main_loop();$/;" p typeref:typename:void
main_loop src/ui.c /^void main_loop(){$/;" f typeref:typename:void
name tests/include/test.h /^ char* name;$/;" m struct:testnode typeref:typename:char *
next tests/include/test.h /^ struct testnode* next;$/;" m struct:testnode typeref:struct:testnode *
op tests/include/test.h /^ int (*op)(void);$/;" m struct:testnode typeref:typename:int (*)(void)
part src/tiktaktoe.c /^const char part = '-';$/;" v typeref:typename:const char
position_t include/gamelogic.h /^typedef struct position_t{$/;" s
position_t include/gamelogic.h /^}position_t;$/;" t typeref:struct:position_t
ppass_fail tests/include/test.h /^static void ppass_fail(rc_opt rc){$/;" f typeref:typename:void
presult tests/include/test.h /^static int presult(int status){$/;" f typeref:typename:int
print_boardstate include/gamelogic.h /^void print_boardstate();$/;" p typeref:typename:void
print_boardstate src/gamelogic.c /^void print_boardstate(){$/;" f typeref:typename:void
ptest_headers tests/include/test.h /^static void ptest_headers(header_opt option){$/;" f typeref:typename:void
ptr tests/include/test.h /^static testnode* ptr = NULL;$/;" v typeref:typename:testnode *
pts src/gamelogic.c /^static int8_t pts[ROWS+COLS+DIAGS];$/;" v typeref:typename:int8_t[] file:
rc tests/include/test.h /^ int rc = WEXITSTATUS(status);$/;" l function:presult typeref:typename:int file:
rc tests/include/test.h /^ int rc = ptr->op();$/;" l function:run_child typeref:typename:int file:
rc_opt tests/include/test.h /^typedef enum {PASS, FAIL}rc_opt;$/;" t typeref:enum:__anon431ebd300203
render include/ui.h /^void render();$/;" p typeref:typename:void
render src/ui.c /^void render(){$/;" f typeref:typename:void
round_counter src/gamelogic.c /^static int round_counter;$/;" v typeref:typename:int file:
row include/gamelogic.h /^ uint8_t row;$/;" m struct:position_t typeref:typename:uint8_t
run_child tests/include/test.h /^static int run_child(){$/;" f typeref:typename:int
run_tests tests/include/test.h /^static int run_tests(){$/;" f typeref:typename:int
start_tiktaktoe include/tiktaktoe.h /^int start_tiktaktoe(void);$/;" p typeref:typename:int
start_tiktaktoe src/tiktaktoe.c /^int start_tiktaktoe(void){$/;" f typeref:typename:int
squarestate include/gamelogic.h /^typedef enum{OPEN_SPACE, X_SPACE, O_SPACE} squarestate;$/;" t typeref:enum:__anon9083c9160103
status tests/include/test.h /^ int status;$/;" l function:run_child typeref:typename:int file:
tail tests/include/test.h /^static testnode* tail = NULL;$/;" v typeref:typename:testnode *
test.c tests/test.c 1;" F epoch:1766557221
test.h tests/include/test.h 1;" F epoch:1766556714
testnode tests/include/test.h /^typedef struct testnode{$/;" s
testnode tests/include/test.h /^}testnode;$/;" t typeref:struct:testnode
tiktaktoe.c src/tiktaktoe.c 1;" F epoch:1767390945
tiktaktoe.d obj/tiktaktoe.d 1;" F epoch:1767390946
tiktaktoe.h include/tiktaktoe.h 1;" F epoch:1767390908
total_tests tests/include/test.h /^ int total_tests = 0;$/;" l function:run_tests typeref:typename:int file:
vline_pos src/tiktaktoe.c /^ int vline_pos = 0;$/;" l function:draw_board typeref:typename:int file:
w_data_t include/tiktaktoe.h /^typedef struct w_data_t{$/;" s
w_data_t include/tiktaktoe.h /^}w_data_t;$/;" t typeref:struct:w_data_t
w_ptr include/tiktaktoe.h /^ WINDOW *w_ptr;$/;" m struct:w_data_t typeref:typename:WINDOW *
turn include/gamelogic.h /^enum turn {X_TURN, O_TURN};$/;" g
ui.c src/ui.c 1;" F epoch:1768023862
ui.d obj/ui.d 1;" F epoch:1769056486
ui.h include/ui.h 1;" F epoch:1767939704
update_boardstate include/gamelogic.h /^void update_boardstate();$/;" p typeref:typename:void
update_boardstate src/gamelogic.c /^void update_boardstate(){$/;" f typeref:typename:void
update_gamestate include/gamelogic.h /^void update_gamestate(char input);$/;" p typeref:typename:void
update_gamestate src/gamelogic.c /^void update_gamestate(char input){$/;" f typeref:typename:void
val src/gamelogic.c /^ int val = (currturn == X_TURN) ? 1 : -1;$/;" l function:add_point typeref:typename:int file:
x src/gamelogic.c /^ for( int x = 0; x < COLS; x++ ){$/;" l function:print_boardstate typeref:typename:int file:
x src/ui.c /^ for( int x = 0; x < COLS; x++ ){$/;" l function:render typeref:typename:int file:
x tests/test.c /^ int* x = NULL;$/;" l function:TEST typeref:typename:int * file:
y src/gamelogic.c /^ for( int y = 0; y < ROWS; y++ ){$/;" l function:print_boardstate typeref:typename:int file:
y src/ui.c /^ for( int y = 0; y < ROWS; y++ ){$/;" l function:render typeref:typename:int file:
y tests/test.c /^ int y = *x;$/;" l function:TEST typeref:typename:int file: