diff --git a/connect4.c b/connect4.c index 8e397cb..7fb4b98 100644 --- a/connect4.c +++ b/connect4.c @@ -29,15 +29,41 @@ out_tilemap: } int drop_tile(struct Board *board, size_t drop_pos) { + if (drop_pos >= board->width) + return -1; + + size_t tower_height = board->height-board->tile_heights[drop_pos]-1; + + if (tower_height < 0) + return -1; + + IDX(drop_pos, tower_height, board) = board->next_player; + + board->tile_heights[drop_pos]++; + board->next_player *= -1; + + // Check if player won in separate function + return 0; } void print_board(struct Board *board) { for (int j = 0; j < board->height; j++) { + printf("|"); for (int i = 0; i < board->width; i++) { - printf("%d", IDX(i, j, board)); + switch (IDX(i,j,board)) { + case BLACK: + printf("\033[90m"); + break; + case RED: + printf("\033[31m"); + break; + case EMPTY: + printf("\033[36m"); + } + printf("O"); } - printf("\n"); + printf("\033[0m|\n"); } - + printf("\n"); } diff --git a/connect4.h b/connect4.h index 0084e6d..a6cf828 100644 --- a/connect4.h +++ b/connect4.h @@ -4,9 +4,9 @@ #include enum Tile { + BLACK = -1, EMPTY = 0, - RED, - BLACK + RED = 1, }; struct Board { @@ -17,7 +17,7 @@ struct Board { enum Tile next_player; }; -#define IDX(i, j, board) (board->tilemap[j*(board->height) + i]) +#define IDX(i, j, board) (board->tilemap[j*(board->width) + i]) // Returns a board struct with height and width based on parameters struct Board *make_board(size_t height, size_t width); diff --git a/main.c b/main.c index eb5960a..4e6f967 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,12 @@ int main(int argc, char *argv[]) { struct Board *my_board = make_board(6, 7); - print_board(my_board); + while (1) { + print_board(my_board); + int drop_spot = 0; + printf("Where to drop? "); + scanf("%d",&drop_spot); + drop_tile(my_board, drop_spot-1); + } return EXIT_SUCCESS; }