File indexing completed on 2024-05-05 15:52:59
0001 /* GCompris - binary_bulb.js 0002 * 0003 * SPDX-FileCopyrightText: 2018 Rajat Asthana <rajatasthana4@gmail.com> 0004 * 0005 * Authors: 0006 * "RAJAT ASTHANA" <rajatasthana4@gmail.com> 0007 * 0008 * SPDX-License-Identifier: GPL-3.0-or-later 0009 */ 0010 .pragma library 0011 .import QtQuick 2.12 as Quick 0012 .import "qrc:/gcompris/src/core/core.js" as Core 0013 0014 var numberOfLevel 0015 var items 0016 var dataset 0017 var url = "qrc:/gcompris/src/activities/binary_bulb/resource/" 0018 var tutorialInstructions = [ 0019 { 0020 "instruction": qsTr("This activity teaches how to convert decimal numbers to binary numbers."), 0021 "instructionQml" : "qrc:/gcompris/src/activities/binary_bulb/resource/tutorial1.qml" 0022 }, 0023 { 0024 "instruction": qsTr("Computers use transistors to count and transistors have only two states, 0 and 1. Mathematically, these states are represented by 0 and 1, which makes up the binary system of numeration."), 0025 "instructionQml" : "qrc:/gcompris/src/activities/binary_bulb/resource/tutorial2.qml" 0026 }, 0027 { 0028 "instruction": qsTr("In the activity 0 and 1 are simulated by bulbs, switched on or off."), 0029 "instructionQml": "qrc:/gcompris/src/activities/binary_bulb/resource/tutorial3.qml" 0030 }, 0031 { 0032 "instruction": qsTr("Binary system uses these numbers very efficiently, allowing to count from 0 to 255 with 8 bits only."), 0033 "instructionQml": "qrc:/gcompris/src/activities/binary_bulb/resource/tutorial4.qml" 0034 }, 0035 { 0036 "instruction": qsTr("Each bit adds a progressive value, corresponding to the powers of 2, ascending from right to left: bit 1 → 2⁰=1 , bit 2 → 2¹=2 , bit 3 → 2²=4 , bit 4 → 2³=8 , bit 5 → 2⁴=16 , bit 6 → 2⁵=32 , bit 7 → 2⁶=64 , bit 8 → 2⁷=128."), 0037 "instructionQml": "qrc:/gcompris/src/activities/binary_bulb/resource/tutorial5.qml" 0038 }, 0039 { 0040 "instruction": qsTr("To convert a decimal 5 to a binary value, 1 and 4 are added."), 0041 "instructionQml": "qrc:/gcompris/src/activities/binary_bulb/resource/tutorial6.qml" 0042 }, 0043 { 0044 "instruction": qsTr("Their corresponding bits are set to 1, the others are set to 0. Decimal 5 is equal to binary 101."), 0045 "instructionQml": "qrc:/gcompris/src/activities/binary_bulb/resource/tutorial7.qml" 0046 }, 0047 { 0048 "instruction": qsTr("This image will help you to compute bits' value."), 0049 "instructionQml": "qrc:/gcompris/src/activities/binary_bulb/resource/tutorial5.qml" 0050 } 0051 ] 0052 var levelDataset 0053 0054 function start(items_, dataset_) { 0055 items = items_ 0056 dataset = dataset_.get() 0057 numberOfLevel = dataset.length 0058 items.currentLevel = Core.getInitialLevel(numberOfLevel) 0059 } 0060 0061 function stop() { 0062 } 0063 0064 function resetBulbs() { 0065 for(var i = 0; i < items.numberOfBulbs; i++) { 0066 items.bulbs.itemAt(i).state = "off" 0067 } 0068 } 0069 0070 function initializeValues() { 0071 items.currentSelectedBulb = -1 0072 items.numberSoFar = 0 0073 items.numberToConvert = levelDataset[items.score.currentSubLevel] 0074 } 0075 0076 function equalityCheck() { 0077 items.buttonsBlocked = true 0078 if(items.numberSoFar == items.numberToConvert) { 0079 items.score.currentSubLevel++ 0080 items.score.playWinAnimation() 0081 items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/completetask.wav") 0082 0083 } else { 0084 items.errorRectangle.startAnimation() 0085 items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/crash.wav") 0086 } 0087 } 0088 0089 function nextSubLevel() { 0090 if (items.score.currentSubLevel >= items.score.numberOfSubLevels) { 0091 items.bonus.good("lion") 0092 } else { 0093 resetBulbs() 0094 initializeValues() 0095 items.buttonsBlocked = false 0096 } 0097 } 0098 0099 function changeState(index) { 0100 var currentBulb = items.bulbs.itemAt(index) 0101 if(currentBulb.state == "off") { 0102 currentBulb.state = "on" 0103 items.numberSoFar += currentBulb.value 0104 } 0105 else { 0106 currentBulb.state = "off" 0107 items.numberSoFar -= currentBulb.value 0108 } 0109 } 0110 0111 function initLevel() { 0112 items.errorRectangle.resetState() 0113 items.score.numberOfSubLevels = dataset[items.currentLevel].numbersToBeConverted.length 0114 items.score.currentSubLevel = 0 0115 items.numberOfBulbs = dataset[items.currentLevel].bulbCount 0116 levelDataset = Core.shuffle(dataset[items.currentLevel].numbersToBeConverted) 0117 initializeValues() 0118 resetBulbs() 0119 items.buttonsBlocked = false 0120 } 0121 0122 function nextLevel() { 0123 items.score.stopWinAnimation() 0124 items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel); 0125 initLevel(); 0126 } 0127 0128 function previousLevel() { 0129 items.score.stopWinAnimation() 0130 items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel); 0131 initLevel(); 0132 }