Add rudimentary drop_tile and interactive mode

Adds basic drop_tile implementation. Though doesn't check for wins

Adds an interactive mode for testing drop_tile, which allows the player
to input which tile-space to drop on.
This commit is contained in:
Julia Lange 2024-07-14 03:51:47 -07:00
parent 5e86f3ccb9
commit 28a3188ea3
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
3 changed files with 39 additions and 7 deletions

View file

@ -29,15 +29,41 @@ out_tilemap:
} }
int drop_tile(struct Board *board, size_t drop_pos) { 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; return 0;
} }
void print_board(struct Board *board) { void print_board(struct Board *board) {
for (int j = 0; j < board->height; j++) { for (int j = 0; j < board->height; j++) {
printf("|");
for (int i = 0; i < board->width; i++) { 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("\033[0m|\n");
} }
printf("\n"); printf("\n");
} }
}

View file

@ -4,9 +4,9 @@
#include <stddef.h> #include <stddef.h>
enum Tile { enum Tile {
BLACK = -1,
EMPTY = 0, EMPTY = 0,
RED, RED = 1,
BLACK
}; };
struct Board { struct Board {
@ -17,7 +17,7 @@ struct Board {
enum Tile next_player; 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 // Returns a board struct with height and width based on parameters
struct Board *make_board(size_t height, size_t width); struct Board *make_board(size_t height, size_t width);

6
main.c
View file

@ -5,6 +5,12 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct Board *my_board = make_board(6, 7); struct Board *my_board = make_board(6, 7);
while (1) {
print_board(my_board); 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; return EXIT_SUCCESS;
} }