seperated .c and .h file for game logic
TODO: implement ncurses things
This commit is contained in:
parent
129932eecf
commit
47a30e426a
@ -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 -lcurses -lncurses -fsanitize=address
|
||||
CFLAGS := -std=c99 -ggdb -Wall -Wextra -MMD -MP -lncurses -fsanitize=address
|
||||
DFLAGS := -D_DEBUG
|
||||
|
||||
# Main Executable Filename
|
||||
|
||||
@ -12,131 +12,19 @@
|
||||
#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;
|
||||
|
||||
// point tracker. make any of these = N and win
|
||||
int8_t pts[ROWS+COLS+DIAGS];
|
||||
int round_counter;
|
||||
|
||||
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("|%c|",
|
||||
(boardstate[COLS*y + x] == X) ? 'X' :
|
||||
(boardstate[COLS*y + x] == O) ? 'O' : '_');
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void init_gamestate(){
|
||||
// init cursor pos to 0,0
|
||||
cpos = (position_t){0,0};
|
||||
currturn = X_TURN;
|
||||
round_counter = 0;
|
||||
|
||||
for( int i = 0; i < TOTAL_SQUARES; i++ ){
|
||||
boardstate[i] = OPEN;
|
||||
}
|
||||
}
|
||||
|
||||
int is_game_over(){
|
||||
for (int i = 0; i < ROWS + COLS + DIAGS; i++){
|
||||
if ( pts[i] == N ){
|
||||
printf("X won\n");
|
||||
return 1;
|
||||
}else if ( pts[i] == -N ){
|
||||
printf("O won\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (round_counter == TOTAL_SQUARES){
|
||||
printf("Stalemate\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void add_point(){
|
||||
int val = (currturn == X_TURN) ? 1 : -1;
|
||||
pts[cpos.row] += val;
|
||||
pts[ROWS + cpos.col] += val;
|
||||
if ( cpos.row == cpos.col){
|
||||
pts[ROWS + COLS + 0] += val;
|
||||
}
|
||||
if( (cpos.row + cpos.col) == (N - 1)){
|
||||
pts[ROWS + COLS + 1] += val;
|
||||
}
|
||||
}
|
||||
|
||||
void update_boardstate(){
|
||||
int idx = COLS * cpos.row + cpos.col;
|
||||
if(boardstate[idx] == OPEN){
|
||||
if(currturn == X_TURN){
|
||||
boardstate[idx] = X;
|
||||
}
|
||||
else if(currturn == O_TURN){
|
||||
boardstate[idx] = O;
|
||||
}
|
||||
add_point();
|
||||
round_counter++;
|
||||
currturn = !currturn;
|
||||
}
|
||||
print_boardstate();
|
||||
}
|
||||
|
||||
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();
|
||||
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);
|
||||
if (is_game_over()) break;
|
||||
}
|
||||
}
|
||||
void print_boardstate();
|
||||
void init_gamestate();
|
||||
int is_game_over();
|
||||
void add_point();
|
||||
void update_boardstate();
|
||||
void update_gamestate(char input);
|
||||
void main_loop();
|
||||
|
||||
#endif
|
||||
|
||||
120
c_projects/tiktaktoe/src/gamelogic.c
Normal file
120
c_projects/tiktaktoe/src/gamelogic.c
Normal file
@ -0,0 +1,120 @@
|
||||
#include "gamelogic.h"
|
||||
|
||||
|
||||
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];
|
||||
|
||||
|
||||
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' : '_');
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void init_gamestate(){
|
||||
// init cursor pos to 0,0
|
||||
cpos = (position_t){0,0};
|
||||
currturn = X_TURN;
|
||||
round_counter = 0;
|
||||
|
||||
for( int i = 0; i < TOTAL_SQUARES; i++ ){
|
||||
boardstate[i] = OPEN;
|
||||
}
|
||||
}
|
||||
|
||||
int is_game_over(){
|
||||
for (int i = 0; i < ROWS + COLS + DIAGS; i++){
|
||||
if ( pts[i] == N ){
|
||||
printf("X won\n");
|
||||
return 1;
|
||||
}else if ( pts[i] == -N ){
|
||||
printf("O won\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (round_counter == TOTAL_SQUARES){
|
||||
printf("Stalemate\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void add_point(){
|
||||
int val = (currturn == X_TURN) ? 1 : -1;
|
||||
pts[cpos.row] += val;
|
||||
pts[ROWS + cpos.col] += val;
|
||||
if ( cpos.row == cpos.col){
|
||||
pts[ROWS + COLS + 0] += val;
|
||||
}
|
||||
if( (cpos.row + cpos.col) == (N - 1)){
|
||||
pts[ROWS + COLS + 1] += val;
|
||||
}
|
||||
}
|
||||
|
||||
void update_boardstate(){
|
||||
int idx = COLS * cpos.row + cpos.col;
|
||||
if(boardstate[idx] == OPEN){
|
||||
if(currturn == X_TURN){
|
||||
boardstate[idx] = X;
|
||||
}
|
||||
else if(currturn == O_TURN){
|
||||
boardstate[idx] = O;
|
||||
}
|
||||
add_point();
|
||||
round_counter++;
|
||||
currturn = !currturn;
|
||||
}
|
||||
print_boardstate();
|
||||
}
|
||||
|
||||
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();
|
||||
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);
|
||||
if (is_game_over()) break;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user