From f07882d76d1221c1a60e016429212abb07fd9db1 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Tue, 17 Feb 2015 21:49:00 +0100 Subject: Level navigation tree Load Level collection --- qml/pages/Board.qml | 64 +++++++++++++++++------ qml/pages/Goban.qml | 114 +++++++++++++++++++++++++---------------- qml/pages/collections_list.qml | 64 +++++++++++++++++++++++ 3 files changed, 183 insertions(+), 59 deletions(-) create mode 100644 qml/pages/collections_list.qml (limited to 'qml/pages') diff --git a/qml/pages/Board.qml b/qml/pages/Board.qml index 16c4e6b..885fb90 100644 --- a/qml/pages/Board.qml +++ b/qml/pages/Board.qml @@ -2,18 +2,17 @@ import QtQuick 2.0 import Sailfish.Silica 1.0 -import io.thp.pyotherside 1.2 - -Page { - - width: Screen.width; height: Screen.height; +import io.thp.pyotherside 1.3 +Item { anchors.fill: parent + id : board; + Column { - id : column + id : column; anchors.fill: parent; spacing: 25 @@ -46,6 +45,10 @@ Page { id:goban width: parent.width; height: column.height - (row.height + view.height); + onCompletedLevel: { + overlay.text = status ? "X" : "✓"; + overlay.color = status ? "red" : "green" ; + } } SlideshowView { @@ -53,25 +56,43 @@ Page { width: parent.width height: 200 itemWidth: width / 2 - onCurrentIndexChanged: {py.call('board.getGame', [view.currentIndex], goban.setGoban)} + onCurrentIndexChanged: { + py.call('board.getGame', [view.currentIndex], goban.setGoban) + } model: 1 delegate: Text { - horizontalAlignment: Text.AlignHCenter - verticalAlignment: Text.AlignVCenter + horizontalAlignment: Text.AlignHCenter; + verticalAlignment: Text.AlignVCenter; - color: Theme.primaryColor - font.family: Theme.fontFamily - font.pixelSize: Theme.fontSizeMedium + color: Theme.primaryColor; + font.family: Theme.fontFamily; + font.pixelSize: Theme.fontSizeMedium; - width: view.itemWidth - height: view.height + width: view.itemWidth; + height: view.height; text: "Problem " + (index + 1); } } } + Text { + id: overlay + opacity: goban.completed ? 1 : 0 + anchors { + centerIn:parent + } + font.family: Theme.fontFamily; + font.pixelSize: goban.height; + + Behavior on opacity { NumberAnimation { duration: 500 } } + } + + function loadBoard(path) { + py.loadBoard(path); + } + Python { id:py Component.onCompleted: { @@ -82,7 +103,7 @@ Page { importModule('board', function() { console.log('module loaded'); console.log('Python version: ' + pythonVersion()); - }) + }); setHandler('log', function (content) { console.log(content); @@ -90,12 +111,23 @@ Page { call('board.setPath', [pythonpath]); call('board.loadBoard', ["easy.sgf"], function (result) { - console.log(result + " problems found in the file") + console.log(result + " problems found in the file"); view.model = result call('board.getGame', [0], goban.setGoban); }); + } + function loadBoard(path) { + call('board.loadBoard', [path], function (result) { + console.log(result + " problems found in the file"); + view.model = result + call('board.getGame', [0], goban.setGoban); + }); } } + function showHint() { + goban.showHint(); + } + } diff --git a/qml/pages/Goban.qml b/qml/pages/Goban.qml index 66002f7..11e6328 100644 --- a/qml/pages/Goban.qml +++ b/qml/pages/Goban.qml @@ -32,6 +32,11 @@ Item { property bool freePlay: false; + /* + * flag to tell if player is on a wrong branch. + */ + property bool isWrong: false; + /** * The game tree. */ @@ -47,13 +52,16 @@ Item { */ property variant history; + signal completedLevel(bool status); + signal startup(); + /* - * Start the game. - * - * Initialize the board with the stones, and set player color. + * Start the game. Initialize the board with the stones, and set player color. + * Function called at first launch, or whene resetting the game. */ function start() { + startup(); completed = false; for (var i = 0; i < goban.rows * goban.columns; i++) { @@ -86,6 +94,7 @@ Item { goban.itemAt(pos).put(false, false); }); } + isWrong = false; } function setGoban(ret) { @@ -109,7 +118,6 @@ Item { } initialPlayer = (ret.current_player === 'W'); - console.log(ret.current_player); /* * Put the initials stones @@ -118,6 +126,16 @@ Item { start(); } + function showHint() { + + if (path === undefined) { + console.log("no hint to show"); + return; + } + var action = TreeNavigator.getNextMove(path, tree, currentPlayer); + clickHandler(action); + } + /* * Undo the last move. */ @@ -126,6 +144,7 @@ Item { return; } + completed = false; var currentHistory = history; var actions = currentHistory.pop(); @@ -135,9 +154,13 @@ Item { Actions.undo(goban, x); currentPlayer = x.player; path = x.path; + if (x.wrong !== undefined) { + isWrong = false; + } }); history = currentHistory; + } /** @@ -145,10 +168,6 @@ Item { */ function clickHandler(index) { - if (completed) { - return; - } - if ( (!limitLeft && Actions.isFirstCol(index, goban.columns)) || (!limitRight && Actions.isLastCol(index, goban.columns)) || (!limitTop && Actions.isFirstRow(index, goban.columns)) @@ -158,56 +177,65 @@ Item { } var step = Actions.addPiece(index, goban, currentPlayer, true, false, false); + if (step === undefined) { + /* Movement not allowed. */ + return; + } - if (step !== undefined) { - + if (completed) { + completed = false; + } - /* - * Update the path. - */ - var currentPosition = path[path.length - 1]; - step.path = path; - var action = TreeNavigator.checkAction(path, tree, currentPlayer, index); - path = action; + /* + * Update the path. + */ + step.path = path; + /* + * Update the history with the last move. + */ + var currentHistory = history; + var actions = [step]; - /* - * Update the history with the last move. - */ - var currentHistory = history; - var actions = [step]; + var followLevel = TreeNavigator.checkAction(path, tree, currentPlayer, index, function (newPath, properties) { + if (properties.wrong !== undefined) { + isWrong = true; + step.wrong = true; + } - if (action === undefined) { - /* - * Switch to variation mode - */ + if (TreeNavigator.getCurrentAction(newPath, tree) === undefined) { + completed = true; + completedLevel(isWrong); } else { - if (TreeNavigator.getCurrentAction(action, tree) === undefined) { - console.log("Level completed!"); - completed = true; - return; - } else { + /* Play the openent move. */ + path = newPath; + TreeNavigator.playComputer(newPath, tree, currentPlayer, function(x, newPath) { + var oponentAction = Actions.addPiece(x, goban, !currentPlayer, true, false, false) + oponentAction.path = path; + path = newPath; + actions.push(oponentAction); + }); + if (TreeNavigator.getCurrentAction(path, tree) === undefined) { /* - * Play the openent move. + * Level has been completed by the computer move. */ - - TreeNavigator.play(action, tree, currentPlayer, function(x, newPath) { - console.log(x); - var oponentAction = Actions.addPiece(x, goban, !currentPlayer, true, false, false) - oponentAction.path = path; - path = newPath; - actions.push(oponentAction); - }); - + completed = true; + currentPlayer = !currentPlayer; + completedLevel(isWrong); } } - currentHistory.push(actions); - history = currentHistory; + }); + if (!followLevel || completed) { + path = undefined; + currentPlayer = !currentPlayer; } + currentHistory.push(actions); + history = currentHistory; + } /** diff --git a/qml/pages/collections_list.qml b/qml/pages/collections_list.qml new file mode 100644 index 0000000..6701869 --- /dev/null +++ b/qml/pages/collections_list.qml @@ -0,0 +1,64 @@ +import QtQuick 2.0 +import Sailfish.Silica 1.0 + +import io.thp.pyotherside 1.3 + +Page { + width: Screen.width; height: Screen.height; + + signal openCollection(string path); + + Column { + Text { + id: txt; + text: qsTr("Select the collection to open"); + + } + + SilicaListView { + + width: parent.width; height: Screen.height; + + model: ListModel {id: levelsList} + + delegate: ListItem { + Label { + text: name + } + onClicked: { + openCollection(path); + pageStack.pop() + } + + } + } + + } + + + Python { + id:py + Component.onCompleted: { + var pythonpath = Qt.resolvedUrl('../python').substr('file://'.length); + addImportPath(pythonpath); + console.log(pythonpath); + + importModule('configuration', function() { + console.log('module loaded'); + console.log('Python version: ' + pythonVersion()); + }); + + setHandler('log', function (content) { + console.log(content); + }); + + call('configuration.get_levels', [pythonpath, StandardPaths.documents], function(result) { + result.forEach(function(elem) { + console.log(elem); + levelsList.append(elem); + }) + }); + } + } + +} -- cgit v1.2.3