diff --git a/main.c b/main.c index 9c3e10981ec45baeb100dbd489387b373660c22e..ae79ccc08d937e50875d935d51d077c3aa8601ee 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,7 @@ int main() { puissance game; game_init(&game, RAND_AI, DEFAULT_ROW, DEFAULT_COL); + print_game(game); game_destroy(&game); diff --git a/puissance.c b/puissance.c index f926d3fad94ef34a226f5210574d18d72dc06035..fd0eb3ed835c996ee6e78ac53c77c163ed005d4f 100644 --- a/puissance.c +++ b/puissance.c @@ -5,20 +5,109 @@ #include "puissance.h" #include <stdio.h> +#include <stdlib.h> +#include <string.h> void game_init(puissance *p, GameMode mode, int row, int col) { p->mode = mode; p->current_player = PLAYER_ONE; p->row = row; p->col = col; + // Allocate memory for the data p->data = malloc(col * sizeof(int*)); for (int i = 0; i < col; i++) { p->data[i] = malloc(row * sizeof(int*)); } + // Set a default value for the data + for (int r = 0; r < row; r++) { + for (int c = 0; c < col; c++) { + p->data[r][c] = -1; + } + } +} + +void print_top(char *display, int col) { + // Print top + strcat(display, "┌"); + for (int i = 1; i <= col * 2 - 1; i++) { + if (i % 2 == 1) { + strcat(display, "─"); + } else { + strcat(display, "┬"); + } + } + strcat(display, "┐\n"); +} + +void print_bot(char *display, int col) { + // Print bot + strcat(display, "└"); + for (int i = 1; i <= col * 2 - 1; i++) { + if (i % 2 == 1) { + strcat(display, "─"); + } else { + strcat(display, "┴"); + } + } + strcat(display, "┘\n"); + + // Print col numbers + for (int i = 1; i <= col; i++) { + char i_has_string[256]; + sprintf(i_has_string, "%d", i); + strcat(display, " "); + strcat(display, i_has_string); + } +} + +void print_row_separator(char *display, int col) { + strcat(display, "├"); + for (int i = 1; i <= col * 2 - 1; i++) { + if (i % 2 == 1) { + strcat(display, "─"); + } else { + strcat(display, "┼"); + } + } + strcat(display, "┤\n"); } void print_game(puissance p) { + char display[1024]; + + // Print the top of the game + print_top(display, p.col); + + // Print the rest of the game (except bottom) + for (int row_index = 0; row_index < p.row; row_index++) { + for (int col_index = 0; col_index < p.col; col_index++) { + // Print each row + strcat(display, "│"); + if (p.data[row_index][col_index] == PLAYER_ONE) { + strcat(display, PLAYER_ONE_STRING); + } + else if (p.data[row_index][col_index] == PLAYER_TWO) { + strcat(display, PLAYER_TWO_STRING); + } + else { + strcat(display, " "); + } + + // Close the row before going to the next line + if (col_index == p.col - 1) { + strcat(display, "│\n"); + } + } + // Print the row separator (except when we are at the bottom) + if (row_index != p.row - 1) { + print_row_separator(display, p.col); + } + } + + // Print the bottom then display the game + print_bot(display, p.col); + printf("%s\n", display); } void verify_game(puissance p) { @@ -44,6 +133,9 @@ bool smart_play(puissance *p) { } void game_destroy(puissance *p) { + for (int i = 0; i < p->col; i++) { + free(p->data[i]); + } free(p->data); p = NULL; } diff --git a/puissance.h b/puissance.h index fdfec3c8fc70e9bccd94dd47debdbb808742a246..af9ea04b50cdc18036c18afc50d212511023084a 100644 --- a/puissance.h +++ b/puissance.h @@ -13,6 +13,8 @@ #define COL_MIN 4 #define DEFAULT_ROW 6 #define DEFAULT_COL 7 +#define PLAYER_ONE_STRING "O" +#define PLAYER_TWO_STRING "X" typedef enum { RAND_AI, @@ -48,4 +50,6 @@ bool random_play(puissance *p); bool smart_play(puissance *p); void game_destroy(puissance *p); +// TODO: add get top of col + #endif \ No newline at end of file