From fcca4f01fec5c97a0e2a6ca6cd9088a87aa76816 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sun, 31 Aug 2014 19:52:37 +0200 Subject: Added history management --- qml/pages/Goban.qml | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'qml/pages/Goban.qml') diff --git a/qml/pages/Goban.qml b/qml/pages/Goban.qml index 420d22d..5c86700 100644 --- a/qml/pages/Goban.qml +++ b/qml/pages/Goban.qml @@ -27,6 +27,8 @@ Item { property variant tree; + property variant history; + /* * Start the game. * @@ -48,13 +50,13 @@ Item { i++; } - initial = tree[i] + initial = tree[i]; + history = []; var aw = initial.AW; if (aw !== undefined) { aw.forEach(function (pos) { goban.getItemAt(pos[0], pos[1]).put(currentPlayer, false); -// Actions.addPiece(pos[0] + (pos[1] * goban.columns), goban, currentPlayer, false, true, true); }); } @@ -62,7 +64,6 @@ Item { if (ab !== undefined) { ab.forEach(function (pos) { goban.getItemAt(pos[0], pos[1]).put(!currentPlayer, false); -// Actions.addPiece(pos[0] + (pos[1] * goban.columns), goban, !currentPlayer, false, true, true); }); } } @@ -94,6 +95,22 @@ Item { start(); } + /* + * Undo the last move. + */ + function undo() { + if (history.length === 0) { + return; + } + + var currentHistory = history; + var step = currentHistory.pop(); + history = currentHistory; + + Actions.undo(goban, step); + currentPlayer = step.player; + } + /** * Handle a click on the goban. */ @@ -107,8 +124,14 @@ Item { return; } - if (Actions.addPiece(index, goban, currentPlayer, true, false, false)) { + var step = Actions.addPiece(index, goban, currentPlayer, true, false, false); + + if (step !== undefined) { currentPlayer = !currentPlayer; + + var currentHistory = history; + currentHistory.push(step); + history = currentHistory; } } -- cgit v1.2.3