diff --git a/puissance4.c b/puissance4.c index fbc810b4984fe9ec5235f6ad31fcc4cb03ef3581..0d88ab7409af14389705c3ec844f108a90cc74df 100644 --- a/puissance4.c +++ b/puissance4.c @@ -1,8 +1,25 @@ #include <stdio.h> #include <stdlib.h> +#include <stdbool.h> -void init_board(int rows, int cols, char board[rows][cols]); -void print_board(int rows, int cols, char board[rows][cols]); +void init_board(int rows, int cols, int board[rows][cols]); +void print_board(int rows, int cols, int board[rows][cols]); +void add_token(int col, int player, int rows, int cols, int board[rows][cols]); +bool is_player_winning(int col, int row, int rows, int cols, int board[rows][cols]); +void flush_input(); + + +enum STATE { + EMPTY = 0, + PLAYER1 = 1, + PLAYER2 = -1 +}; +char transform_state_to_char(enum STATE state); + +// enum PLAYER { +// PLAYER1, +// PLAYER2 +// }; int main(int argc, char const* argv[]) { @@ -14,24 +31,117 @@ int main(int argc, char const* argv[]) { int rows = 4; int cols = 5; - char board[rows][cols]; + int board[rows][cols]; init_board(rows, cols, board); print_board(rows, cols, board); + int col, player; + while (true) { + // flush_input(); + printf("\nColumn number? (starts at 1): "); + scanf("%d", &col); + if (col < 1 || col > cols) { + continue; + } + player++; + add_token(col, player % 2, rows, cols, board); + print_board(rows, cols, board); + // if (is_player_winning(rows, cols, board)) { + // printf("Player %d win", player % 2); + // break; + // } + } return EXIT_SUCCESS; } -void init_board(int rows, int cols, char board[rows][cols]) { +void add_token(int col, int player, int rows, int cols, int board[rows][cols]) { + // for (int row = rows; row >= 0; row--) { + // if (board[row][col - 1] == EMPTY) { + // board[row][col - 1] = player == 0 ? PLAYER1 : PLAYER2; + // break; + // } + // } + for (int i = 0; i < rows; i++) { + if (board[i][col - 1] == EMPTY) { + board[i][col - 1] = player == 0 ? PLAYER1 : PLAYER2; + // We should add verify here + if (is_player_winning(col - 1, i, rows, cols, board)) { + + printf("adasdasdada"); + } + + break; + } + } + +} + + +void init_board(int rows, int cols, int board[rows][cols]) { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + board[i][j] = EMPTY; + } + } +} +void flush_input() { + int c; + while ((c = getchar()) != '\n' && c != EOF); +} + +bool are_on_same_diagonal(int coordinate_one_x, int coordinate_one_y, int coordinate_two_x, int coordinate_two_y) { + // Retourne la valeur absolue, c'est à dire jamais en négatif + return abs(coordinate_one_x - coordinate_two_x) == abs(coordinate_one_y - coordinate_two_y); +} + +bool is_player_winning(int col, int row, int rows, int cols, int board[rows][cols]) { + int counter = 0; + for (int i = 0; i < rows; i++) { + counter += board[i][col]; + if (abs(counter) == 4) { + return true; + } + } + + counter = 0; + for (int i = 0; i < cols; i++) { + counter += board[row][i]; + if (abs(counter) == 4) { + return true; + } + } + + counter = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { - board[i][j] = ' '; + if (are_on_same_diagonal(row, col, i, j)) { + counter += board[i][j]; + } + if (abs(counter) == 4) { + return true; + } } } + return false; } -void print_board(int rows, int cols, char board[rows][cols]) { +char transform_state_to_char(enum STATE state) { + switch (state) { + case PLAYER1: + return 'X'; + break; + case PLAYER2: + return 'O'; + break; + default: + return ' '; + break; + } +} + +void print_board(int rows, int cols, int board[rows][cols]) { // Top border of the board printf("┌"); for (int col = 0; col < cols; col++) { @@ -48,7 +158,7 @@ void print_board(int rows, int cols, char board[rows][cols]) { printf("│"); // Print data and vertical separators for (int col = 0; col < cols; col++) { - printf("%c", board[row][col]); + printf("%c", transform_state_to_char(board[row][col])); if (col < cols - 1) { printf("│"); } @@ -78,4 +188,8 @@ void print_board(int rows, int cols, char board[rows][cols]) { } } printf("┘\n"); + for (int i = 1; i < cols + 1; i++) { + printf(" %d", i); + } + printf("\n"); }