[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [gcompris/gsoc_pulkit_digital_electricity] src/activities/digital_electricity: digital_electricity: 
From:       Rudra Nil Basu <null () kde ! org>
Date:       2017-08-22 19:07:32
Message-ID: E1dkEWS-0002dp-2C () code ! kde ! org
[Download RAW message or body]

Git commit a1f1ed34e1047b5cbc6ad50dc7c80af0a6571db5 by Rudra Nil Basu.
Committed on 22/08/2017 at 19:06.
Pushed by rudranilbasu into branch 'gsoc_pulkit_digital_electricity'.

digital_electricity: Result checking: check result in dataset

Previously, we were checking the result for each level in the js, via
brute force checking of the level numbers to decide the answer of an
operation:

  if (currentLevel == ...) {
        ...
  } else if (currentLevel == ...) {
        ...
  }
  ...

This is avoided by keeping a result() function in the dataset itself,
and the operationResult is calculated via:

  operationResult = levelProperties[currentLevel - 1].result(A, B)

Signed-off-by: Rudra Nil Basu <rudra.nil.basu.1996@gmail.com>

M  +30   -0    src/activities/digital_electricity/Dataset.qml
M  +3    -20   src/activities/digital_electricity/digital_electricity.js

https://commits.kde.org/gcompris/a1f1ed34e1047b5cbc6ad50dc7c80af0a6571db5

diff --git a/src/activities/digital_electricity/Dataset.qml \
b/src/activities/digital_electricity/Dataset.qml index 12e0da34..108998c9 100644
--- a/src/activities/digital_electricity/Dataset.qml
+++ b/src/activities/digital_electricity/Dataset.qml
@@ -232,6 +232,9 @@ QtObject {
             playAreaComponentPositionX: [0.2, 0.2, 0.2, 0.8],
             playAreaComponentPositionY: [0.2, 0.5, 0.8, 0.5],
             type: [problemType.equation3Variables],
+            result: function (A, B, C) {
+                return A | (B & C)
+            },
             introMessage: [
                 qsTr("Light the bulb using the components provided such that the \
bulb will glow under the following two circumstances:"),  qsTr("1. The first switch \
is turned ON, or"), @@ -262,6 +265,9 @@ QtObject {
             playAreaComponentPositionX: [0.2, 0.2, 0.2, 0.8],
             playAreaComponentPositionY: [0.2, 0.4, 0.6, 0.4],
             type: [problemType.equation3Variables],
+            result: function (A, B, C) {
+                return A ^ B ^ C
+            },
             introMessage: [
                 qsTr("Light the bulb using the three switches such that the bulb \
glows when odd number of the switches are turned on")  ]
@@ -303,6 +309,9 @@ QtObject {
             playAreaComponentPositionX: [0.0, 0.2, 0.2, 0.8],
             playAreaComponentPositionY: [0.5, 0.4, 0.6, 0.5],
             type: [problemType.equation2Variables],
+            result: function (A, B) {
+                return A & B
+            },
             introMessage: [
                 qsTr("Create a circuit using the components provided such that the \
bulb glows only when both of the switches are turned on.")  ]
@@ -316,6 +325,9 @@ QtObject {
             playAreaComponentPositionX: [0.0, 0.2, 0.2, 0.8],
             playAreaComponentPositionY: [0.5, 0.4, 0.6, 0.5],
             type: [problemType.equation2Variables],
+            result: function (A, B) {
+                return A | B
+            },
             introMessage: [
                 qsTr("Create a circuit using the components provided such that the \
bulb glows when either of the switches are turned on.")  ]
@@ -329,6 +341,9 @@ QtObject {
             playAreaComponentPositionX: [0.0, 0.2, 0.2, 0.8],
             playAreaComponentPositionY: [0.5, 0.4, 0.6, 0.5],
             type: [problemType.equation2Variables],
+            result: function (A, B) {
+                return !(A | B)
+            },
             introMessage: [
                 qsTr("Create a circuit using the components provided such that the \
bulb glows only when both of the switches are turned off.")  ]
@@ -355,6 +370,9 @@ QtObject {
             playAreaComponentPositionX: [0.0, 0.2, 0.2, 0.8],
             playAreaComponentPositionY: [0.5, 0.4, 0.6, 0.5],
             type: [problemType.equation2Variables],
+            result: function (A, B) {
+                return A & B
+            },
             introMessage: [
                 qsTr("Create a circuit using the components provided such that the \
bulb glows only when both of the switches are turned on.")  ]
@@ -368,6 +386,9 @@ QtObject {
             playAreaComponentPositionX: [0.0, 0.2, 0.2, 0.8],
             playAreaComponentPositionY: [0.5, 0.4, 0.6, 0.5],
             type: [problemType.equation2Variables],
+            result: function (A, B) {
+                return A | B
+            },
             introMessage: [
                 qsTr("Create a circuit using the components provided such that the \
bulb glows when either of the switches are turned on.")  ]
@@ -381,6 +402,9 @@ QtObject {
             playAreaComponentPositionX: [0.0, 0.2, 0.2, 0.8],
             playAreaComponentPositionY: [0.5, 0.4, 0.6, 0.5],
             type: [problemType.equation2Variables],
+            result: function (A, B) {
+                return !(A & B)
+            },
             introMessage: [
                 qsTr("Create a circuit using the components provided such that the \
bulb will glow when at least one of the switches are turned off.")  ]
@@ -394,6 +418,9 @@ QtObject {
             playAreaComponentPositionX: [0.0, 0.0, 0.4, 0.9],
             playAreaComponentPositionY: [0.2, 0.4, 0.5, 0.5],
             type: [problemType.equation2Variables],
+            result: function (A, B) {
+                return A <= B
+            },
             introMessage: [
                 qsTr("A comparator takes two numbers (A and B) as input and produces \
3 values as output. First value is 1 if A < B, second value is 1 for A = B and third \
value is 1 for A > B."),  qsTr("Create a circuit using the components provided such \
that the bulb will glow when the value of the current flowing through the first \
switch is less than or equal to that of the second switch.") @@ -439,6 +466,9 @@ \
QtObject {  playAreaComponentPositionX: [0.0, 0.0, 0.1, 0.1, 0.8],
             playAreaComponentPositionY: [0.0, 0.8, 0.3, 0.6, 0.4],
             type: [problemType.equation2Variables],
+            result: function (A, B) {
+                return A ^ B
+            },
             introMessage: [
                 qsTr("Light the bulb using both the switches such that the bulb will \
glow only when either the first switch is on and the second switch is off or the \
first switch is off and the second switch is on.")  ]
diff --git a/src/activities/digital_electricity/digital_electricity.js \
b/src/activities/digital_electricity/digital_electricity.js index d480c774..f3c46140 \
                100644
--- a/src/activities/digital_electricity/digital_electricity.js
+++ b/src/activities/digital_electricity/digital_electricity.js
@@ -185,6 +185,7 @@ function checkAnswer() {
 
     processingAnswer = true
     var problemType = items.tutorialDataset.tutorialLevels[currentLevel - 1].type
+    var levelProperties = items.tutorialDataset.tutorialLevels[currentLevel - 1]
 
     if (problemType == items.tutorialDataset.problemType.lightTheBulb) {
         if (determiningComponents[0].inputTerminals.itemAt(0).value == 1) {
@@ -232,20 +233,7 @@ function checkAnswer() {
                 updateComponent(switch1.index)
                 updateComponent(switch2.index)
 
-                var operationResult
-                if (currentLevel == 22) {
-                    operationResult = A ^ B
-                } else if (currentLevel == 12 || currentLevel == 16) {
-                    operationResult = A & B
-                } else if (currentLevel == 13 || currentLevel == 17) {
-                    operationResult = A | B
-                } else if (currentLevel == 14) {
-                    operationResult = !(A | B)
-                } else if (currentLevel == 18) {
-                    operationResult = !(A & B)
-                } else if (currentLevel == 19) {
-                    operationResult = (A <= B)
-                }
+                var operationResult = levelProperties.result(A, B)
 
                 if (operationResult != digitalLight.inputTerminals.itemAt(0).value) \
{  switch1.imgSrc = switch1InitialState
@@ -281,12 +269,7 @@ function checkAnswer() {
                     updateComponent(switch2.index)
                     updateComponent(switch3.index)
 
-                    var operationResult
-                    if (currentLevel == 7) {
-                        operationResult = A | (B & C)
-                    } else if (currentLevel == 9) {
-                        operationResult = A ^ (B ^ C)
-                    }
+                    var operationResult = levelProperties.result(A, B, C)
 
                     if (operationResult != \
digitalLight.inputTerminals.itemAt(0).value) {  switch1.imgSrc = switch1InitialState


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic