summaryrefslogtreecommitdiff
path: root/lib/input.c
blob: ea5f3686d8e4259422dd84c96b5c77fe8ad437a5 (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
#include <stdio.h>
#include <string.h>

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

#define LENGTH 80

Move input_move() {
    Coord orig, dest;
    char line[LENGTH], orig_line[3], dest_line[3];
    int done = 0;

    printf("Please, enter your movement: ");
    while (!done) {
        fgets(line, LENGTH, stdin);

        if (strnlen(line, LENGTH) == 6 && line[2] == '-' && line[5] == '\n') {
            orig_line[0] = line[0];
            orig_line[1] = line[1];
            orig_line[2] = 0;

            dest_line[0] = line[3];
            dest_line[1] = line[4];
            dest_line[2] = 0;

            printf("Coordinates %s-%s\n", orig_line, dest_line);

            if (!coord_is_valid(orig_line) || !coord_is_valid(dest_line))
                printf("Invalid coordinate, try again: ");
            else {
                orig = coord_init(orig_line);
                dest = coord_init(dest_line);
                done = 1;
            }
        }
        else
            printf("Invalid movement. Write something like \"e2-e4\": ");
    }
        
    return move_init(orig, dest);
}
nihil fit ex nihilo