#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; } }