summaryrefslogtreecommitdiff
path: root/qml/javascript
diff options
context:
space:
mode:
Diffstat (limited to 'qml/javascript')
-rw-r--r--qml/javascript/goban_util.js28
-rw-r--r--qml/javascript/navigator.js134
2 files changed, 148 insertions, 14 deletions
diff --git a/qml/javascript/goban_util.js b/qml/javascript/goban_util.js
index eec6392..333959e 100644
--- a/qml/javascript/goban_util.js
+++ b/qml/javascript/goban_util.js
@@ -64,7 +64,7 @@ function getChainToRemove(index, grid, filter) {
* filter wich keep only free places.
*/
function freePlaces(x) {
- return grid.getElementAtIndex(x).getType() === "";
+ return grid.itemAt(x).getType() === "";
}
var piece = index;
@@ -72,8 +72,8 @@ function getChainToRemove(index, grid, filter) {
/* if the case has already been marked, do not check it again.
*/
- if (!grid.getElementAtIndex(piece).mark) {
- grid.getElementAtIndex(piece).mark = true;
+ if (!grid.itemAt(piece).mark) {
+ grid.itemAt(piece).mark = true;
piecesToRemove.push(piece);
var neighbors = getNeighbors(piece, grid.columns, grid.rows);
@@ -128,7 +128,7 @@ function undo(grid, step) {
* filter wich keep only free places.
*/
function freePlaces(x) {
- return grid.getElementAtIndex(x).getType() === "";
+ return grid.itemAt(x).getType() === "";
}
var piece = index;
@@ -137,7 +137,7 @@ function undo(grid, step) {
while (piece !== undefined) {
- var point = grid.getElementAtIndex(piece);
+ var point = grid.itemAt(piece);
if (point.mark || !point.getType === "") {
piece = space.pop();
@@ -167,7 +167,7 @@ function undo(grid, step) {
fillWith(point, !step.player);
});
}
- grid.getElementAtIndex(step.added).remove(false);
+ grid.itemAt(step.added).remove(false);
}
clearMarks(grid);
@@ -182,7 +182,7 @@ function undo(grid, step) {
* grid(object): the grid where to put the stone:
* - grid.rows: number of rows in the grid
* - grid.columns: number of columes in the grid
- * - grid.getElementAtIndex(index) should return the stone a the given index
+ * - grid.itemAt(index) should return the stone a the given index
* currentPlayer(bool): player color
* animation(bool): should we add animation on the goban
* allowSuicide(bool): if suicide an autorized action
@@ -191,7 +191,7 @@ function undo(grid, step) {
*/
function addPiece(index, grid, currentPlayer, animation, allowSuicide, allowOveride) {
- var point = grid.getElementAtIndex(index);
+ var point = grid.itemAt(index);
var elementType = point.getType();
if (!allowOveride && elementType !== "") {
@@ -205,15 +205,15 @@ function addPiece(index, grid, currentPlayer, animation, allowSuicide, allowOver
step.player = currentPlayer;
function isPlayer(x) {
- return grid.getElementAtIndex(x).getType() === (currentPlayer ? "white" : "black");
+ return grid.itemAt(x).getType() === (currentPlayer ? "white" : "black");
}
function isOponnent(x) {
- return grid.getElementAtIndex(x).getType() === (currentPlayer ? "black" : "white");
+ return grid.itemAt(x).getType() === (currentPlayer ? "black" : "white");
}
function freeOrChain(x) {
- var pointType = grid.getElementAtIndex(x).getType();
+ var pointType = grid.itemAt(x).getType();
return pointType === "" || pointType === (currentPlayer ? "white" : "black");
}
@@ -241,7 +241,7 @@ function addPiece(index, grid, currentPlayer, animation, allowSuicide, allowOver
step.removed.push(neighbor);
somethingToRemove = true;
piecesToRemove.forEach(function(x) {
- grid.getElementAtIndex(x).remove(animation);
+ grid.itemAt(x).remove(animation);
});
}
});
@@ -257,7 +257,7 @@ function addPiece(index, grid, currentPlayer, animation, allowSuicide, allowOver
step.suicide = true;
suicides.forEach(function(x) {
- grid.getElementAtIndex(x).remove(animation);
+ grid.itemAt(x).remove(animation);
});
} else {
point.remove(false);
@@ -289,6 +289,6 @@ function addPiece(index, grid, currentPlayer, animation, allowSuicide, allowOver
*/
function clearMarks(grid) {
for (var i = 0; i < grid.columns * grid.rows; i++) {
- grid.getElementAtIndex(i).mark = false;
+ grid.itemAt(i).mark = false;
}
}
diff --git a/qml/javascript/navigator.js b/qml/javascript/navigator.js
new file mode 100644
index 0000000..984b149
--- /dev/null
+++ b/qml/javascript/navigator.js
@@ -0,0 +1,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);
+
+}