seperated .c and .h file for game logic
TODO: implement ncurses things
This commit is contained in:
parent
129932eecf
commit
447fe75713
@ -27,7 +27,7 @@ STATIC_LIBS = $(shell find ./ -name "*.a")
|
|||||||
CC := gcc
|
CC := gcc
|
||||||
INCLUDE_LIST := $(foreach m,$(SUBMODULES),$(m)/$(INCLUDE_DIR)) $(INCLUDE_DIR) $(TESTS_INCL_DIR)
|
INCLUDE_LIST := $(foreach m,$(SUBMODULES),$(m)/$(INCLUDE_DIR)) $(INCLUDE_DIR) $(TESTS_INCL_DIR)
|
||||||
INCLUDES := $(addprefix -I,$(INCLUDE_LIST))
|
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
|
DFLAGS := -D_DEBUG
|
||||||
|
|
||||||
# Main Executable Filename
|
# Main Executable Filename
|
||||||
|
|||||||
@ -12,131 +12,25 @@
|
|||||||
#define TOTAL_SQUARES ROWS*COLS
|
#define TOTAL_SQUARES ROWS*COLS
|
||||||
#define DOF 2
|
#define DOF 2
|
||||||
|
|
||||||
|
|
||||||
typedef enum{OPEN, X, O} squarestate;
|
typedef enum{OPEN, X, O} squarestate;
|
||||||
enum turn {X_TURN, O_TURN};
|
enum turn {X_TURN, O_TURN};
|
||||||
|
|
||||||
typedef struct position_t{
|
typedef struct position_t{
|
||||||
uint8_t row;
|
uint8_t row;
|
||||||
uint8_t col;
|
uint8_t col;
|
||||||
}position_t;
|
}position_t;
|
||||||
|
|
||||||
|
enum turn currturn;
|
||||||
// point tracker. make any of these = N and win
|
// point tracker. make any of these = N and win
|
||||||
int8_t pts[ROWS+COLS+DIAGS];
|
int8_t pts[ROWS+COLS+DIAGS];
|
||||||
int round_counter;
|
int round_counter;
|
||||||
|
|
||||||
position_t cpos;
|
position_t cpos;
|
||||||
squarestate boardstate[ROWS * COLS];
|
squarestate boardstate[ROWS * COLS];
|
||||||
enum turn currturn;
|
void print_boardstate();
|
||||||
|
void init_gamestate();
|
||||||
void print_boardstate(){
|
int is_game_over();
|
||||||
for( int y = 0; y < ROWS; y++ ){
|
void add_point();
|
||||||
for( int x = 0; x < COLS; x++ ){
|
void update_boardstate();
|
||||||
printf("|%c|",
|
void update_gamestate(char input);
|
||||||
(boardstate[COLS*y + x] == X) ? 'X' :
|
void main_loop();
|
||||||
(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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
112
c_projects/tiktaktoe/src/gamelogic.c
Normal file
112
c_projects/tiktaktoe/src/gamelogic.c
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
#include "gamelogic.h"
|
||||||
|
|
||||||
|
|
||||||
|
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