File indexing completed on 2024-04-28 15:07:42
0001 /* GCompris - algorithm.js 0002 * 0003 * SPDX-FileCopyrightText: 2014 Bharath M S " <brat.197@gmail.com> 0004 * 0005 * Authors: 0006 * Christof Petig and Ingo Konrad (GTK+ version) 0007 * "Bharath M S" <brat.197@gmail.com> (Qt Quick port) 0008 * 0009 * SPDX-License-Identifier: GPL-3.0-or-later 0010 */ 0011 .pragma library 0012 .import QtQuick 2.12 as Quick 0013 .import "qrc:/gcompris/src/core/core.js" as Core 0014 0015 /* 0016 0017 functions :- 0018 0019 setUp() - the function is called to set the basic game layout each time (questionTray, answerTray) 0020 getImages() - returns a random array of length 8 that is based on the chosen sample 0021 setQuestion() - the function is called to set questionTray 0022 setAnswer() - the function is called to set answerTray 0023 clickHandler() - called to handle click event 0024 0025 variables :- 0026 0027 sample - this array has an array of arrays for each level 0028 choiceCount (int) - initialised with the value 5 and the game is won when choiceCount is 8 0029 0030 Example 1: 0031 sample: [0,1,0,1,0,1,0,1] 0032 getImages() sample output - names of images with indexes [6,7,6,7,6,7,6,7] 0033 0034 Example 2: 0035 sample: [0,1,2,0,1,2,0,1] 0036 getImages() sample output - names of images with indexes [3,5,7,3,5,7,3,5] 0037 0038 Example 3: 0039 sample: [0,1,2,3,3,2,1,0] 0040 getImages() sample output - names of images with indexes [1,3,2,5,5,2,3,1] 0041 0042 */ 0043 0044 var matchesVisible = 4 0045 var max = 8 0046 var index 0047 var answerIndex 0048 var items 0049 var number 0050 var images = ["apple", 0051 'banana', 0052 'cherries', 0053 'lemon', 0054 'orange', 0055 'pear', 0056 'pineapple', 0057 'plum'] 0058 var url = "qrc:/gcompris/src/activities/algorithm/resource/" 0059 0060 // Add more cases to sample and differentiate arrange according to level of difficulty. 0061 // Develop an algo to choose them accordingly for each level. 0062 var sample = [[[0,1,0,1,0,1,0,1],[0,1,1,0,0,1,1,0],[1,1,0,0,0,0,1,1],[1,0,0,1,0,1,1,0]],//level1 0063 [[0,1,2,0,1,2,0,1],[0,1,2,3,0,1,2,3],[0,1,2,3,3,2,1,0],[0,1,2,1,0,1,2,0]],//2 0064 [[0,1,2,3,1,0,0,1],[0,1,2,3,0,1,0,1],[0,1,2,3,1,2,1,2],[0,1,2,3,2,3,2,3]],//3 0065 [[0,1,2,3,0,1,2,0],[0,1,2,3,1,2,3,1],[0,1,2,3,2,1,3,1],[0,1,2,3,3,1,2,1]],//4 0066 [[0,1,2,3,1,2,3,0],[0,1,2,3,2,3,0,1],[0,1,2,3,3,0,1,2],[0,1,2,3,3,0,1,2]],//5 0067 [[0,1,2,3,3,1,2,0],[0,1,2,3,0,2,1,3],[0,1,2,3,2,3,1,0],[0,1,2,3,2,1,3,0]],//6 0068 [[0,1,2,3,3,0,1,1],[0,1,2,3,2,2,3,2],[0,1,2,3,1,1,0,3],[0,1,2,3,1,2,3,2]]]//7 0069 var numberOfLevel = sample.length 0070 0071 function start(items_) { 0072 items = items_ 0073 items.currentLevel = Core.getInitialLevel(numberOfLevel); 0074 initLevel() 0075 } 0076 0077 function stop() { 0078 } 0079 0080 function initLevel() { 0081 setUp() 0082 } 0083 0084 function setUp() { 0085 number = Math.floor(Math.random() * 10000) % sample[items.currentLevel].length 0086 0087 index = getImages(number, items.currentLevel) 0088 setQuestion(index) 0089 0090 answerIndex = getImages(number, items.currentLevel) 0091 setAnswer(answerIndex) 0092 0093 choiceCount = matchesVisible 0094 items.blockClicks = false 0095 } 0096 0097 // Returns a set of images that is used to set either the Sample algorithm or the Answer tray. 0098 function getImages(number, level) { 0099 var substitution = Core.shuffle(images) 0100 // Create results table based on sample and substitution 0101 var results = [] 0102 for(var i=0; i<sample[level][number].length; i++) { 0103 results.push(substitution[sample[level][number][i]]) 0104 } 0105 return results 0106 } 0107 0108 // The source of questionTray is changed to reflect the chosen 0109 // set of random indices 0110 function setQuestion(indices){ 0111 items.question.model = indices 0112 } 0113 0114 // The first `matchesVisible` images of answerTray are set and the `matchesVisible+1`th is a question mark. 0115 function setAnswer(indices){ 0116 0117 var tempIndex = [] 0118 for(var i=0; i < matchesVisible; i++) { 0119 tempIndex.push(indices[i]) 0120 } 0121 tempIndex.push('question_mark') 0122 items.answer.model = tempIndex 0123 } 0124 0125 // Game is won when choiceCount == 8. 0126 var choiceCount = matchesVisible 0127 0128 function clickHandler(id){ 0129 var tempIndex = [] 0130 0131 // Correct answer 0132 if(id === answerIndex[choiceCount]) { 0133 tempIndex = items.answer.model 0134 choiceCount++; 0135 0136 if(choiceCount < max) { 0137 tempIndex.push('question_mark') 0138 } 0139 0140 tempIndex[choiceCount - 1] = answerIndex[choiceCount - 1] 0141 items.answer.model = tempIndex 0142 0143 if(choiceCount == max) { 0144 items.blockClicks = true 0145 items.currentSubLevel++ 0146 items.score.playWinAnimation(); 0147 items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/completetask.wav"); 0148 } else { 0149 items.audioEffects.play('qrc:/gcompris/src/core/resource/sounds/bleep.wav') 0150 } 0151 return 1 0152 } else { // Wrong answer, try again 0153 items.audioEffects.play('qrc:/gcompris/src/core/resource/sounds/brick.wav') 0154 } 0155 } 0156 0157 function nextLevel() { 0158 items.score.stopWinAnimation(); 0159 items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel); 0160 items.currentSubLevel = 0; 0161 initLevel(); 0162 } 0163 0164 function nextSubLevel() { 0165 // Increment level after 3 successful games. 0166 if (items.currentSubLevel === items.nbSubLevel) { 0167 items.bonus.good("tux"); 0168 } else { 0169 setUp(); 0170 } 0171 } 0172 0173 function previousLevel() { 0174 items.score.stopWinAnimation(); 0175 items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel); 0176 items.currentSubLevel = 0; 0177 initLevel(); 0178 }