File indexing completed on 2024-05-12 03:42:58
0001 /* GCompris - tens_complement_find.js 0002 * 0003 * SPDX-FileCopyrightText: 2022 Samarth Raj <mailforsamarth@gmail.com> 0004 * SPDX-FileCopyrightText: 2022 Timothée Giet <animtim@gmail.com> 0005 * SPDX-License-Identifier: GPL-3.0-or-later 0006 * 0007 */ 0008 .pragma library 0009 .import "../../core/core.js" as Core 0010 0011 var numberOfLevel; 0012 var numberOfSubLevel; 0013 var items; 0014 0015 function start(items_) { 0016 items = items_; 0017 numberOfLevel = items.levels.length; 0018 items.currentLevel = Core.getInitialLevel(numberOfLevel); 0019 items.score.currentSubLevel = 0; 0020 initLevel(); 0021 } 0022 0023 function stop() { 0024 } 0025 0026 function initLevel() { 0027 items.okButton.visible = false; 0028 items.cardListModel.clear(); 0029 items.holderListModel.clear(); 0030 items.selectedIndex = -1; 0031 0032 var currentDataset = items.levels[items.currentLevel]; 0033 var equations = []; 0034 var cards = []; 0035 if(currentDataset.randomValues) { 0036 numberOfSubLevel = currentDataset.numberOfSublevels; 0037 var cardToDisplayIndex = 0; 0038 for(var equationIndex = 0 ; equationIndex < currentDataset.numberOfEquations ; ++ equationIndex) { 0039 // First fill the left hand containers with all the numbers 0040 var minValue = currentDataset.minimumFirstValue; 0041 var maxValue = currentDataset.maximumFirstValue; 0042 var leftHandSide; 0043 var numberAlreadyExists = true; 0044 var tryCount = 10; // Avoid too many attemps. There should not be too much duplicates. 0045 while(numberAlreadyExists && tryCount > 0) { 0046 leftHandSide = Math.floor(Math.random() * (maxValue - minValue + 1)) + minValue; 0047 numberAlreadyExists = false; 0048 // We check if the tens complement of the number is already used. 0049 // This way, for levels where all numbers have to be found, we are sure to not have duplicates 0050 for(var i = 0 ; i < cards.length ; ++ i) { 0051 var counterpart = (10 - leftHandSide).toString(); 0052 if(counterpart == cards[i].value) { 0053 numberAlreadyExists = true; 0054 } 0055 } 0056 tryCount --; 0057 } 0058 0059 var rightHandSide = 10 - leftHandSide; 0060 var card = { 0061 "value": rightHandSide.toString(), 0062 "visibility": true, 0063 "isSignSymbol": false, 0064 "clickable": true 0065 } 0066 cards.push(card); 0067 0068 if(currentDataset.findBothNumbers) { 0069 var card = { 0070 "value": leftHandSide.toString(), 0071 "visibility": true, 0072 "isSignSymbol": false, 0073 "clickable": true 0074 } 0075 cards.push(card); 0076 } 0077 0078 var toShuffleQuestionValue; 0079 if(!currentDataset.findBothNumbers) { 0080 toShuffleQuestionValue = [leftHandSide.toString(), "?"]; 0081 } 0082 else { 0083 toShuffleQuestionValue = ["?", "?"]; 0084 } 0085 if(currentDataset.randomQuestionPosition) { 0086 Core.shuffle(toShuffleQuestionValue); 0087 } 0088 equations.push(createEquation(toShuffleQuestionValue)); 0089 } 0090 // Append more random cards if needed between 1 and 9 0091 for(var i = cards.length ; i < currentDataset.numberOfNumbersInLeftContainer ; ++ i) { 0092 var randomNumber = Math.floor(Math.random() * 9) + 1; 0093 var card = { 0094 "value": randomNumber.toString(), 0095 "visibility": true, 0096 "isSignSymbol": false, 0097 "clickable": true 0098 } 0099 cards.push(card); 0100 } 0101 } 0102 else { 0103 var sublevel = currentDataset.values[currentSubLevel]; 0104 numberOfSubLevel = currentDataset.values.length; 0105 0106 var cardsToDisplay = sublevel.numberValue.length; 0107 for(var cardToDisplayIndex = 0 ; cardToDisplayIndex < cardsToDisplay ; cardToDisplayIndex++) { 0108 var card = { 0109 "value": sublevel.numberValue[cardToDisplayIndex].toString(), 0110 "visibility": true, 0111 "isSignSymbol": false, 0112 "clickable": true 0113 } 0114 cards.push(card); 0115 } 0116 0117 var equationsToDisplay = sublevel.questionValue; 0118 for(var equationIndex = 0 ; equationIndex < equationsToDisplay.length ; equationIndex++) { 0119 var toShuffleQuestionValue = [equationsToDisplay[equationIndex].toString(), "?"]; 0120 if(currentDataset.randomQuestionPosition) { 0121 Core.shuffle(toShuffleQuestionValue); 0122 } 0123 equations.push(createEquation(toShuffleQuestionValue)); 0124 } 0125 } 0126 Core.shuffle(cards); 0127 for(var i = 0 ; i < cards.length ; ++ i) { 0128 cards[i].index = i; 0129 items.cardListModel.append(cards[i]); 0130 } 0131 Core.shuffle(equations); 0132 for(var i = 0 ; i < equations.length ; ++ i) { 0133 equations[i].rowIndex = i; 0134 items.holderListModel.append(equations[i]); 0135 } 0136 items.score.numberOfSubLevels = numberOfSubLevel; 0137 items.buttonsBlocked = false; 0138 } 0139 0140 function createEquation(values) { 0141 return { 0142 "addition": [ 0143 { 0144 "value": values[0], 0145 "visibility": true, 0146 "clickable": values[0] === "?", 0147 "isSignSymbol": false 0148 }, 0149 { 0150 "value": "+", 0151 "visibility": true, 0152 "clickable": false, 0153 "isSignSymbol": true 0154 }, 0155 { 0156 "value": values[1], 0157 "visibility": true, 0158 "clickable": values[1] === "?", 0159 "isSignSymbol": false 0160 }, 0161 { 0162 "value": "=", 0163 "visibility": true, 0164 "clickable": false, 0165 "isSignSymbol": true 0166 }, 0167 { 0168 "value": "10", 0169 "visibility": true, 0170 "clickable": false, 0171 "isSignSymbol": false 0172 } 0173 ], 0174 "firstCardClickable": values[0] == "?" ? true : false, 0175 "secondCardClickable": values[1] == "?" ? true : false, 0176 "isCorrect": false, 0177 "tickVisibility": false 0178 } 0179 } 0180 0181 function nextLevel() { 0182 items.score.stopWinAnimation(); 0183 items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel); 0184 items.score.currentSubLevel = 0; 0185 initLevel(); 0186 } 0187 0188 function nextSubLevel() { 0189 if(items.score.currentSubLevel >= numberOfSubLevel) { 0190 items.bonus.good("flower"); 0191 } else { 0192 initLevel(); 0193 } 0194 } 0195 0196 function previousLevel() { 0197 items.score.stopWinAnimation(); 0198 items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel); 0199 items.score.currentSubLevel = 0; 0200 initLevel(); 0201 } 0202 0203 function reappearNumberCard(value) { 0204 var cardsToDisplay = items.cardListModel.count; 0205 for(var i = 0 ; i < cardsToDisplay ; i++) { 0206 if(value == items.cardListModel.get(i).value && !items.cardListModel.get(i).visibility) { 0207 items.cardListModel.setProperty(i, "visibility", true); 0208 break; 0209 } 0210 } 0211 } 0212 0213 function getSelectedValue() { 0214 var selectedValue = items.selectedIndex != -1 ? items.cardListModel.get(items.selectedIndex).value.toString() : "?"; 0215 return selectedValue; 0216 } 0217 0218 function getEnteredCard() { 0219 if(items.selectedIndex == -1) { 0220 return "?"; 0221 } 0222 items.cardListModel.setProperty(items.selectedIndex, "visibility", false); 0223 var tempSelected = items.selectedIndex; 0224 items.selectedIndex = -1; 0225 return items.cardListModel.get(tempSelected).value.toString(); 0226 } 0227 0228 function updateVisibility(rowIndex) { 0229 items.holderListModel.get(rowIndex).tickVisibility = false; 0230 if(items.selectedIndex != -1) { 0231 // Unselect it 0232 items.cardListModel.setProperty(selected, "selected", false); 0233 items.cardListModel.setProperty(selected, "visibility", false); 0234 items.selectedIndex = -1; 0235 } 0236 items.okButton.visible = showOkButton(); 0237 } 0238 0239 function showOkButton() { 0240 var checkQuestionMark = true; 0241 for(var i = 0; i < items.holderListModel.count; i++) { 0242 var equation = items.holderListModel.get(i).addition; 0243 for(var j = 0; j < equation.count; j++) { 0244 var answer = equation.get(j); 0245 if(answer.value == "?") { 0246 checkQuestionMark = false; 0247 break; 0248 } 0249 } 0250 } 0251 items.okButton.visible = checkQuestionMark; 0252 } 0253 0254 function checkAnswer() { 0255 var isAllCorrect = true; 0256 for(var i = 0 ; i < items.holderListModel.count ; i ++) { 0257 var equation = items.holderListModel.get(i); 0258 var solution = equation.addition 0259 var isGood = parseInt(solution.get(0).value) + parseInt(solution.get(2).value) == 10 ? true : false; 0260 equation.isCorrect = isGood; 0261 equation.tickVisibility = true; 0262 isAllCorrect = isGood & isAllCorrect; 0263 } 0264 if(isAllCorrect) { 0265 items.buttonsBlocked = true; 0266 items.score.currentSubLevel++ 0267 items.score.playWinAnimation() 0268 items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/completetask.wav") 0269 } 0270 else { 0271 items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/crash.wav") 0272 } 0273 }