code_snippets/c_projects/tiktaktoe/include/gamelogic.h
2026-01-02 23:16:27 -08:00

106 lines
1.6 KiB
C

#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