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

Do various changes to validate the tests

- Improvement of the diagonal check.
- Changed the text displayed to be the same than those in the tests.
- Fixed an unknown char that was displayed at the second game display.
parent f7b71388
Branches
No related tags found
No related merge requests found
......@@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "puissance.h"
void play_against_random_ai(puissance game) {
......@@ -13,7 +12,7 @@ void play_against_random_ai(puissance game) {
while(game.state == ONGOING) {
if (game.current_player == PLAYER_ONE) {
printf("Waiting for player one (" PLAYER_ONE_STRING ")...\n");
printf("\nColumn number? (starts at 1):");
do {
scanf("%d", &selected_col_index);
selected_col_index -= 1;
......@@ -29,14 +28,8 @@ void play_against_human(puissance game) {
int selected_col_index = -1;
while(game.state == ONGOING) {
if (game.current_player == PLAYER_ONE) {
printf("Waiting for player one (" PLAYER_ONE_STRING ")...\n");
} else {
printf("Waiting for player two (" PLAYER_TWO_STRING ")...\n");
}
do {
printf("\nColumn number? (starts at 1):");
scanf("%d", &selected_col_index);
selected_col_index -= 1;
} while (manual_play(&game, selected_col_index) == false);
......@@ -45,7 +38,7 @@ void play_against_human(puissance game) {
}
int main(int argc, char *argv[]) {
srand(time(NULL));
srand(0);
puissance game;
if (argc != 4) {
......@@ -58,6 +51,7 @@ int main(int argc, char *argv[]) {
int nb_cols = atoi(argv[3]);
game_init(&game, mode, nb_rows, nb_cols);
printf("Board size is %dx%d (rows x col)", nb_rows, nb_cols);
print_game(game);
switch (mode) {
......
......@@ -59,7 +59,6 @@ void print_bot(char *display, int col) {
strcat(display, " ");
strcat(display, i_has_string);
}
strcat(display, "\n");
}
void print_row_separator(char *display, int col) {
......@@ -76,6 +75,8 @@ void print_row_separator(char *display, int col) {
void print_game(puissance p) {
char display[1024];
// Ensure that the display string is empty before doing anything
display[0] = '\0';
// Print the top of the game
print_top(display, p.col);
......@@ -114,7 +115,7 @@ void print_game(puissance p) {
display[0] = '\0';
if (p.state != ONGOING) {
display_game_result(p.state);
display_game_result(p);
}
}
......@@ -194,6 +195,20 @@ bool diagonal_parse(puissance *p, int x_r, int y_c, int starting_row, int starti
bool four_aligned = true;
int last_played_value = p->data[starting_row][starting_col];
// Search the end position of the diagonal
do {
int x = -x_r + starting_row;
int y = -y_c + starting_col;
if (x > 0 && x < p->row && y > 0 && y < p->col && p->data[x][y] == last_played_value) {
starting_row = x;
starting_col = y;
} else {
break;
}
} while(true);
// Verify if we have the same value aligned enough times
for (int i = 1; i <= NB_VERIFICATION_FOR_WIN; i++) {
int r = x_r * i + starting_row;
int c = y_c * i + starting_col;
......@@ -216,7 +231,6 @@ GameResult diagonal_game_check(puissance *p, int last_col_index_played) {
return p->state;
}
bool four_aligned = false;
// Get the row index
int last_row_index_played = get_available_row_index(p, last_col_index_played) + 1;
......@@ -240,6 +254,23 @@ GameResult diagonal_game_check(puissance *p, int last_col_index_played) {
return ONGOING;
}
GameResult verify_space_remaining(puissance *p) {
if (p->state != ONGOING) {
return p->state;
}
bool space_available = false;
for (int i = 0; i < p->col; i++) {
if (p->data[0][i] == EMPTY_CELL_VALUE) {
space_available = true;
break;
}
}
if (space_available == false) {
p->state = DRAW;
}
return p->state;
}
GameResult verify_game(puissance *p, int last_col_index_played) {
// Vertical check
vertical_game_check(p, last_col_index_played);
......@@ -247,22 +278,28 @@ GameResult verify_game(puissance *p, int last_col_index_played) {
horizontal_game_check(p, last_col_index_played);
// Diagonal check
diagonal_game_check(p, last_col_index_played);
// Verify remaining space
verify_space_remaining(p);
return p->state;
}
GameResult display_game_result(GameResult gr) {
if (gr == PLAYER_ONE_WIN) {
printf("The player one (" PLAYER_ONE_STRING ") win !\n");
GameResult display_game_result(puissance p) {
if (p.state == PLAYER_ONE_WIN) {
printf("\nPlayer one won!\n");
}
if (gr == PLAYER_TWO_WIN) {
printf("The player two (" PLAYER_TWO_STRING ") win !\n");
if (p.state == PLAYER_TWO_WIN) {
if (p.mode == TWO_PLAYERS) {
printf("\nPlayer two won!\n");
} else {
printf("\nComputer won!\n");
}
}
if (gr == DRAW) {
printf("It's a draw !\n");
if (p.state == DRAW) {
printf("\nIt is a draw.\n");
}
return gr;
return p.state;
}
int get_available_row_index(puissance *p, int selected_col_index) {
......
......@@ -49,7 +49,7 @@ typedef struct _puissance {
void game_init(puissance *p, GameMode mode, int row, int col);
void print_game(puissance p);
GameResult verify_game(puissance *p, int last_col_index_played);
GameResult display_game_result(GameResult gr);
GameResult display_game_result(puissance p);
int get_available_row_index(puissance *p, int selected_col_index);
bool manual_play(puissance *p, int selected_col_index);
bool random_play(puissance *p);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment