Still missing the win condition check.
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
+#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "io.h"
#include "array.h"
-#include "utils.h"
int prompt_value(const char* msg, int* result) {
puts(msg);
*old_size = new_size;
}
+void handle_prompt_error(int errno) {
+ if (errno != 0) {
+ printf("\nMerci de saisir un nombre entier, exiting\n");
+ /* it's somewhat violent but better than looping forever */
+ exit(EXIT_FAILURE);
+ }
+}
+
void display_array(int array[], unsigned size) {
if (array != NULL) {
printf("\n--array begin--\n");
int prompt_value(const char* msg, int* result);
int* prompt_array(int array[], unsigned* size);
+void handle_prompt_error(int errno);
void display_choice_menu();
v1 = v2;
v2 = tmp;
}
-
-void handle_prompt_error(int errno) {
- if (errno != 0) {
- printf("\nMerci de saisir un nombre entier, exiting\n");
- /* it's somewhat violent but better than looping forever */
- exit(EXIT_FAILURE);
- }
-}
void swap_int(int* v1, int* v2);
void swap_ptr(void* v1, void* v2);
-void handle_prompt_error(int errno);
-
#endif /* UTILS_H */
return new_coordinates;
}
-unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array) {
+/* the function do a lot of sanity checks before adding new board elements,
+ * hence the loop. moving the checks in the main loop is also possible */
+unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array, unsigned round) {
/* valid coordinates are in the [1-3] range */
if (new_coordinates.y < 1 || new_coordinates.y > 3 || new_coordinates.x < 1 || new_coordinates.x > 3) {
return 3; /* error value for invalid coordinates */
+ } else if (round == MAX_COORDINATES + 1) {
+ return 1; /* error value for full array */
}
+
for (unsigned i = 0; i < MAX_COORDINATES; i++) {
/* check if already entered */
if (new_coordinates.y == (coordinates_array + i)->y && new_coordinates.x == (coordinates_array + i)->x) {
return 0; /* error value when everything if fine */
}
}
- return 1; /* error value for full array */
+ return 4; /* error value for unknown error case - should never happen - */
+}
+
+bool chk_win_conditions(coordinates_t* coordinates_array) {
+ for (unsigned i = 0; i < MAX_COORDINATES; i++) {
+
+ }
+ return false;
}
#ifndef COORDINATES_H
#define COORDINATES_H
+#include <stdbool.h>
+
/* we only have nine elements in the grid */
#define MAX_COORDINATES 9
void init_coordinates(coordinates_t* coordinates_array);
coordinates_t set_coordinates(int y, int x, unsigned type);
-unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array);
+unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array, unsigned round);
+bool chk_win_conditions(coordinates_t* coordinates_array);
#endif /* COORDINATES_H */
int main() {
int row, col, errno = 0, round = 0, player = 0, key_pressed;
- char* top_msg = "";
- char* back_msg = "";
+ const int str_max_length = 255;
+ char* top_msg = malloc(str_max_length * sizeof(char));
+ char* back_msg = malloc(str_max_length * sizeof(char));
initscr();
getmaxyx(stdscr, row, col);
print_board(base_y, base_x);
do {
- if (round % 2 == 0 || round == 0) {
- player = 0;
- top_msg = "Joueur 1 joue";
- } else {
+ if (errno == 0) round++;
+
+ if (round % 2 == 0) {
player = 1;
top_msg = "Joueur 2 joue";
+ } else {
+ player = 0;
+ top_msg = "Joueur 1 joue";
}
mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg);
- mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg);
print_coordinates(coordinates_array, base_y, base_x);
+ /* getch() is blocking */
key_pressed = getch();
switch (key_pressed) {
case 'a':
new_coordinates = set_coordinates(3, 3, player);
break;
default:
- continue;
+ /* set invalid coordinates */
+ new_coordinates = set_coordinates(0, 0, player);
break;
}
- errno = add_coordinates(new_coordinates, coordinates_array);
+ errno = add_coordinates(new_coordinates, coordinates_array, round);
if (errno == 2) {
back_msg = "Choisir une case vide";
- continue;
} else if (errno == 3) {
back_msg = "Coordonnees invalides";
- continue;
+ } else if (errno == 1) {
+ back_msg = "Tableau rempli sans gagnant: egalite";
+ } else if (errno == 4) {
+ back_msg = "Erreur inconnue";
} else if (errno == 0) {
+ /* FIXME: properly zero the string */
back_msg = "";
}
- round++;
+ mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg);
+
+ refresh();
+
+ } while (errno != 1);
- } while (errno == 0 || errno == 2);
+ if (!top_msg)
+ free(top_msg);
+ if (!back_msg)
+ free(back_msg);
endwin();