Skip to content
Snippets Groups Projects
Commit 228c5bfe authored by dario.genga's avatar dario.genga
Browse files

Add print of the game

parent 78dfd10b
Branches
No related tags found
No related merge requests found
......@@ -11,6 +11,7 @@ int main() {
puissance game;
game_init(&game, RAND_AI, DEFAULT_ROW, DEFAULT_COL);
print_game(game);
game_destroy(&game);
......
......@@ -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;
}
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment