summaryrefslogtreecommitdiff
path: root/lib/input.c
blob: 8b696e17f5674d08f06e3f83efbb331398b9db25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <string.h>

#include "coordinate.h"
#include "input.h"
#include "move.h"

#define LENGTH 80

static Coord _input_coord() {
    char line[LENGTH];
    int done = 0;   

    while (!done) {
        fgets(line, LENGTH, stdin);

        if (strnlen(line, LENGTH) == 3 && line[2] == '\n')
            line[2] = 0;

        if (!coord_is_valid(line))
            printf("Invalid coordinate. Write something like \"e2\": ");
        else
            done = 1;
    }
        
    return coord_init(line);
}

static Coord _input_orig_coord() {
    printf("Orig coordinate: ");

    return _input_coord();
}

static Coord _input_dest_coord() {
    printf("Dest coordinate: ");

    return _input_coord();
}

Move input_move() {
    Coord orig = _input_orig_coord();
    Coord dest = _input_dest_coord();

    return move_init(orig, dest);
}
nihil fit ex nihilo