TP 13 exo2: More routine for
[TD_C.git] / TP_13 / exo2 / lib / display.c
CommitLineData
7515f897
JB
1/*
2 * =====================================================================================
3 *
4 * Filename: display.c
5 *
6 * Description: Routines to handle the display
7 *
8 * Version: 1.0
9 * Created: 15/03/2017 20:06:11
10 * Revision: none
11 * Compiler: gcc
12 *
13 * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org
14 * Organization: Piment Noir
15 *
16 * =====================================================================================
17 */
18
19#include <ncurses.h>
20
5df3071e
JB
21#include "display.h"
22
7515f897
JB
23/* in all print routine, y and x are the coordinates of the first character of the shape
24 * which can be a space ' ' */
25
26void print_board(int y, int x) {
27 mvprintw(y, x, " | |");
28 mvprintw(y+1, x, " | |");
29 mvprintw(y+2, x, "----+----+----");
30 mvprintw(y+3, x, " | |");
31 mvprintw(y+4, x, " | |");
32 mvprintw(y+5, x, "----+----+----");
33 mvprintw(y+6, x, " | |");
34 mvprintw(y+7, x, " | |");
35}
36
37/* there's only nine valid (y, x) 2-uplets for this two shapes
38 * that are : - base_y, base_x +1
39 * - base_y, base_x + 6
40 * - base_y, base_x + 11
41 * - base_y + 3, base_x + 1
42 * - base_y + 6, base_x + 1
43 * - base_y + 3, base_x + 6
44 * - base_y + 3, base_x + 11
45 * - base_y + 6, base_x + 6
46 * - base_y + 6, base_x + 11
5df3071e
JB
47 * The added (y, x) couple values can be {0, 3, 6}x{1, 6, 11}
48 */
7515f897
JB
49
50void print_x(int y, int x) {
51 mvprintw(y, x, "\\/");
52 mvprintw(y+1, x,"/\\");
53}
54
55void print_o(int y, int x) {
56 mvprintw(y, x, "/\\");
57 mvprintw(y+1, x, "\\/");
58}
5df3071e
JB
59
60/* y: 1 -> +0 x: 1 -> +1
61 * 2 -> +3 2 -> +6
62 * 3 -> +6 3 -> +11 */
63static int remap_y(int y) {
64 if (y == 1) {
65 return 0;
66 } else if (y == 2) {
67 return 3;
68 } else {
69 return 6;
70 }
71}
72
73static int remap_x(int x) {
74 if (x == 1) {
75 return 1;
76 } else if (x == 2) {
77 return 6;
78 } else {
79 return 11;
80 }
81}
82
83void print_coordinates(coordinates_t coordinates_array[], int base_y, int base_x) {
84 unsigned i = 0;
85 while ((coordinates_array + i)->y != 0 && (coordinates_array + i)->x != 0) {
86 if ((coordinates_array + i)->type == 0) {
87 print_o(base_y + remap_y((coordinates_array + i)->y), base_x + remap_x((coordinates_array + i)->x));
88 } else {
89 print_x(base_y + remap_y((coordinates_array + i)->y), base_x + remap_x((coordinates_array + i)->x));
90 }
91 i++;
92 }
93}