--- /dev/null
+Tic-tac-toe
+-----------
+
+Touches correspondantes au case de la grille:
+
+a|z|e
+-+-+-
+q|s|d
+-+-+-
+w|x|c
--- /dev/null
+/*
+ * =====================================================================================
+ *
+ * Filename: coordinates.c
+ *
+ * Description: Data definition and functions to manipulate elements in the grid
+ *
+ * Version: 1.0
+ * Created: 16/03/2017 19:05:02
+ * Revision: none
+ * Compiler: gcc
+ *
+ * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org
+ * Organization: Piment Noir
+ *
+ * =====================================================================================
+ */
+
+#include "coordinates.h"
+
+void init_coordinates(coordinates_t* coordinates_array) {
+ for (unsigned i = 0; i < MAX_COORDINATES; i++) {
+ coordinates_array[i] = set_coordinates(0, 0, 0);
+ }
+}
+
+coordinates_t set_coordinates(int y, int x, unsigned type) {
+ coordinates_t new_coordinates;
+
+ new_coordinates.y = y;
+ new_coordinates.x = x;
+ new_coordinates.type = type;
+ return new_coordinates;
+}
+
+unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array) {
+ /* 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 */
+ }
+ 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 2; /* error value for duplicates */
+ } else if ((coordinates_array + i)->y == 0 && (coordinates_array + i)->x == 0) {
+ coordinates_array[i] = new_coordinates;
+ return 0; /* error value when everything if fine */
+ }
+ }
+ return 1; /* error value for full array */
+}
--- /dev/null
+/*
+ * =====================================================================================
+ *
+ * Filename: coordinates.h
+ *
+ * Description: Header for data definition and functions to manipulate elements in the grid
+ *
+ * Version: 1.0
+ * Created: 16/03/2017 19:06:16
+ * Revision: none
+ * Compiler: gcc
+ *
+ * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org
+ * Organization: Piment Noir
+ *
+ * =====================================================================================
+ */
+
+#ifndef COORDINATES_H
+#define COORDINATES_H
+
+/* we only have nine elements in the grid */
+#define MAX_COORDINATES 9
+
+typedef struct coordinates_s {
+ int y;
+ int x;
+ unsigned type; /* 0 = O, 1 = X */
+} coordinates_t;
+
+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);
+
+#endif /* COORDINATES_H */
#include <ncurses.h>
+#include "display.h"
+
/* in all print routine, y and x are the coordinates of the first character of the shape
* which can be a space ' ' */
* - base_y + 3, base_x + 11
* - base_y + 6, base_x + 6
* - base_y + 6, base_x + 11
- * The added y value can be {0, 3, 6}
- * The added x value can be {1, 6, 11} */
+ * The added (y, x) couple values can be {0, 3, 6}x{1, 6, 11}
+ */
void print_x(int y, int x) {
mvprintw(y, x, "\\/");
mvprintw(y, x, "/\\");
mvprintw(y+1, x, "\\/");
}
+
+/* y: 1 -> +0 x: 1 -> +1
+ * 2 -> +3 2 -> +6
+ * 3 -> +6 3 -> +11 */
+static int remap_y(int y) {
+ if (y == 1) {
+ return 0;
+ } else if (y == 2) {
+ return 3;
+ } else {
+ return 6;
+ }
+}
+
+static int remap_x(int x) {
+ if (x == 1) {
+ return 1;
+ } else if (x == 2) {
+ return 6;
+ } else {
+ return 11;
+ }
+}
+
+void print_coordinates(coordinates_t coordinates_array[], int base_y, int base_x) {
+ unsigned i = 0;
+ while ((coordinates_array + i)->y != 0 && (coordinates_array + i)->x != 0) {
+ if ((coordinates_array + i)->type == 0) {
+ print_o(base_y + remap_y((coordinates_array + i)->y), base_x + remap_x((coordinates_array + i)->x));
+ } else {
+ print_x(base_y + remap_y((coordinates_array + i)->y), base_x + remap_x((coordinates_array + i)->x));
+ }
+ i++;
+ }
+}
#ifndef DISPLAY_H
#define DISPLAY_H
+#include "coordinates.h"
+
void print_board(int y, int x);
void print_x(int y, int x);
void print_o(int y, int x);
+void print_coordinates(coordinates_t coordinates_array[], int base_y, int base_x);
#endif /* DISPLAY_H */
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include <ncurses.h>
#include "display.h"
+#include "coordinates.h"
int main() {
int row, col;
+ char* top_msg = "";
+ char* back_msg = "";
initscr();
- getmaxyx(stdscr,row,col);
+ getmaxyx(stdscr, row, col);
noecho();
curs_set(0);
+ /* array of the active coordinates in the entered order */
+ coordinates_t coordinates_array[MAX_COORDINATES];
+ init_coordinates(coordinates_array);
+
/* center base coordinates for the board */
- const int base_y = row/2 - 4;
- const int base_x = col/2 - 7;
+ int base_y = row/2 - 4;
+ int base_x = col/2 - 7;
+ if (!top_msg) mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg);
print_board(base_y, base_x);
+ if (!back_msg) mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg);
+
+ int errno = add_coordinates(set_coordinates(1, 3, 0), coordinates_array);
+ errno = add_coordinates(set_coordinates(1, 3, 0), coordinates_array);
+ errno = add_coordinates(set_coordinates(2, 3, 1), coordinates_array);
+ errno = add_coordinates(set_coordinates(1, 1, 0), coordinates_array);
- print_x(base_y, base_x + 1);
- print_o(base_y, base_x + 6);
- print_o(base_y, base_x + 11);
- print_o(base_y + 3, base_x + 1);
- print_o(base_y + 6, base_x + 1);
- print_o(base_y + 3, base_x + 6);
- print_x(base_y + 3, base_x + 11);
- print_x(base_y + 6, base_x + 6);
- print_x(base_y + 6, base_x + 11);
+ print_coordinates(coordinates_array, base_y, base_x);
refresh();