Added stalemate logic
This commit is contained in:
parent
680ad15b25
commit
129932eecf
@ -5,8 +5,10 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
|
||||||
#define ROWS 3
|
#define N 3
|
||||||
#define COLS 3
|
#define ROWS N
|
||||||
|
#define COLS N
|
||||||
|
#define DIAGS 2
|
||||||
#define TOTAL_SQUARES ROWS*COLS
|
#define TOTAL_SQUARES ROWS*COLS
|
||||||
#define DOF 2
|
#define DOF 2
|
||||||
|
|
||||||
@ -19,6 +21,10 @@ typedef struct position_t{
|
|||||||
uint8_t col;
|
uint8_t col;
|
||||||
}position_t;
|
}position_t;
|
||||||
|
|
||||||
|
// point tracker. make any of these = N and win
|
||||||
|
int8_t pts[ROWS+COLS+DIAGS];
|
||||||
|
int round_counter;
|
||||||
|
|
||||||
position_t cpos;
|
position_t cpos;
|
||||||
squarestate boardstate[ROWS * COLS];
|
squarestate boardstate[ROWS * COLS];
|
||||||
enum turn currturn;
|
enum turn currturn;
|
||||||
@ -26,7 +32,9 @@ enum turn currturn;
|
|||||||
void print_boardstate(){
|
void print_boardstate(){
|
||||||
for( int y = 0; y < ROWS; y++ ){
|
for( int y = 0; y < ROWS; y++ ){
|
||||||
for( int x = 0; x < COLS; x++ ){
|
for( int x = 0; x < COLS; x++ ){
|
||||||
printf("|%d|", boardstate[COLS*y + x]);
|
printf("|%c|",
|
||||||
|
(boardstate[COLS*y + x] == X) ? 'X' :
|
||||||
|
(boardstate[COLS*y + x] == O) ? 'O' : '_');
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
@ -36,19 +44,46 @@ void init_gamestate(){
|
|||||||
// init cursor pos to 0,0
|
// init cursor pos to 0,0
|
||||||
cpos = (position_t){0,0};
|
cpos = (position_t){0,0};
|
||||||
currturn = X_TURN;
|
currturn = X_TURN;
|
||||||
|
round_counter = 0;
|
||||||
|
|
||||||
for( int i = 0; i < TOTAL_SQUARES; i++ ){
|
for( int i = 0; i < TOTAL_SQUARES; i++ ){
|
||||||
boardstate[i] = OPEN;
|
boardstate[i] = OPEN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void check_game_over(){
|
int is_game_over(){
|
||||||
//TODO: no idea
|
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(){
|
void update_boardstate(){
|
||||||
int idx = COLS * cpos.row + cpos.col;
|
int idx = COLS * cpos.row + cpos.col;
|
||||||
printf("%d\n", idx);
|
|
||||||
if(boardstate[idx] == OPEN){
|
if(boardstate[idx] == OPEN){
|
||||||
if(currturn == X_TURN){
|
if(currturn == X_TURN){
|
||||||
boardstate[idx] = X;
|
boardstate[idx] = X;
|
||||||
@ -56,9 +91,11 @@ void update_boardstate(){
|
|||||||
else if(currturn == O_TURN){
|
else if(currturn == O_TURN){
|
||||||
boardstate[idx] = O;
|
boardstate[idx] = O;
|
||||||
}
|
}
|
||||||
|
add_point();
|
||||||
|
round_counter++;
|
||||||
|
currturn = !currturn;
|
||||||
}
|
}
|
||||||
print_boardstate();
|
print_boardstate();
|
||||||
check_game_over();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_gamestate(char input){
|
void update_gamestate(char input){
|
||||||
@ -86,7 +123,6 @@ void update_gamestate(char input){
|
|||||||
break;
|
break;
|
||||||
case('e'):
|
case('e'):
|
||||||
update_boardstate();
|
update_boardstate();
|
||||||
currturn = !currturn;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,6 +135,7 @@ void main_loop(){
|
|||||||
while(1){
|
while(1){
|
||||||
while((input = getchar()) == '\n');
|
while((input = getchar()) == '\n');
|
||||||
update_gamestate(input);
|
update_gamestate(input);
|
||||||
|
if (is_game_over()) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user