diff options
author | Sébastien Dailly <sebastien@chimrod.com> | 2014-08-31 19:52:37 +0200 |
---|---|---|
committer | Sébastien Dailly <sebastien@chimrod.com> | 2014-08-31 19:52:37 +0200 |
commit | fcca4f01fec5c97a0e2a6ca6cd9088a87aa76816 (patch) | |
tree | a9e5ec1e732ac9becad328935fb4224e72addf9a /qml/pages/Goban.qml | |
parent | d48ac3970ef04a404c42f30c72cbdc327178f0dc (diff) |
Added history management
Diffstat (limited to 'qml/pages/Goban.qml')
-rw-r--r-- | qml/pages/Goban.qml | 31 |
1 files changed, 27 insertions, 4 deletions
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; } } |