summaryrefslogtreecommitdiff
path: root/qml/javascript/navigator.js
blob: 984b149d885aa1c8432a22bfa1c39d311b96a54e (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
.pragma library

function getCurrentAction(path, tree) {

    var way = tree;
    var ended = false;

    /*
     * Get the action pointed by the path.
     */
    path.forEach( function(element, index, array) {
        if (!ended && element < way.length) {
            way = way[element];
        } else {
            ended = true;
        }
    });

    if ( !ended ) {
        return way;
    } else {
        return undefined;
    }

}

function checkAction(path, tree, player, playedPosition) {

    var way = getCurrentAction(path, tree);
    var pathIndex;

    if (Array.isArray(way)) {
        /*
         * We have choice between different possibilities.
         * We check each of them to get the player action.
         */
        if (way.some( function(element, index, array) {

            /*
             * Increment the path to the next position, and check the expected
             * result.
             */
            path.push(index);
            var next = getCurrentAction(path, tree)[0];

            console.log( next );

            var expectedIndex;
            if (player) {
                expectedIndex = next.W[0];
                console.log("W", next.W);
            } else {
                expectedIndex = next.B[0];
                console.log("B", next.B);
            }

            if (playedPosition === expectedIndex) {
                path.push(0);
                return true;
            }

            /*
             * The position was not the expected one. Restore the path to the
             * original one.
             */
            path.pop();
            return false;

        })) {
            /*
             * We got the rigth action. Now, get the next position in the path.
             */
            console.log("Good !!");
            pathIndex = path.length - 1;
            path[pathIndex] = path[pathIndex] + 1;
            return path;
        } else {
            /*
             * The played position does not match the recorded game.
             */
            return undefined;
        }

    } else {

        /*
         * We only have one possibility, return it.
         */
        console.log("Single result", way);

        var move;
        if (player) {
            move = way.W[0];
        } else {
            move = way.B[0];
        }

        if (move === playedPosition) {
            console.log("Good !!", path);
            pathIndex = path.length - 1;
            path[pathIndex] = path[pathIndex] + 1;
            return path;
        } else {
            return undefined;
        }
    }

}

function play(path, tree, player, addPiece) {

    var way = getCurrentAction(path, tree);
    var pathIndex;

    if (Array.isArray(way)) {

    } else {

        var move;
        if (!player) {
            move = way.W[0];
        } else {
            move = way.B[0];
        }
        pathIndex = path.length - 1;
        path[pathIndex] = path[pathIndex] + 1;
        addPiece(move, path);
    }
}

function undo(path) {
    var way = getCurrentAction(path, tree);

}