File size: 2,925 Bytes
ae4627d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var fileName = 'words.txt';
var syllables;
var syllablesArray;
var words;
var wordsArray;
var usedWords = [];
var countUsedWords = [];
var countOfWords;
var markUsed = [];
var indexOfWord;
var countLevels = 3;

function randomNumber(min, max) {
  'use strict';
  return Math.floor(Math.random() * (max - min + 1) + min);
}

function shuffle(array) {
  'use strict';
  var counter = array.length, temp, index;

  while (counter > 0) {
    index = Math.floor(Math.random() * counter);
    counter -= 1;
    temp = array[counter];
    array[counter] = array[index];
    array[index] = temp;
  }

  return array;
}

function resetRound() {
  'use strict';
  shuffle(syllables);
  $('#syllablesHolder').empty();
  for (var i = 0; i < syllables.length; i += 1) {
    $('<span />',
      {text: syllables[i], class: 'draggable droppable'})
      .appendTo('#syllablesHolder');
  }
  $('.draggable').draggable({revert: true, revertDuration: 0});

  $('.droppable').droppable({
    hoverClass: 'drop-hover',
    drop: function (event, ui) {
      var oldSyllable = $(this).html();
      $(this).html(ui.draggable.html());
      ui.draggable.html(oldSyllable);
    }
  });
}

function startNewRound() {
  'use strict';
  var level = Dyslexio.getLevel('syllables');
  indexOfWord = getWordIndex(level);  
  words = wordsArray[indexOfWord];
  syllables = syllablesArray[indexOfWord];
  resetRound();
}

function getWordIndex(level) {
  'use strict';
  if (countUsedWords[level] === countOfWords) {
    countUsedWords[level] = 0;
    markUsed[level] += 1;
  }
  var index;
  while (true) {
    index = randomNumber(level  * countOfWords, (level + 1) * countOfWords - 1);
    if (usedWords[index] !== markUsed[level]) {
      usedWords[index] = markUsed[level];
      countUsedWords[level] += 1;
      break;
    }
  }
  return index;
}

function init() {
  'use strict';
  var jsonData = JSON.parse($('#helpDiv').text());
  wordsArray = jsonData.words;
  countOfWords = jsonData.countOfWordsForEachLevel;
  syllablesArray = jsonData.syllables;
  var length = countOfWords * countLevels;
  while (length) {
    usedWords.push(0);
    length -= 1;
    if (length < countLevels) {
      countUsedWords.push(0);
      markUsed.push(1);
    }
  }
  startNewRound();
}

function checkSolution() {
  'use strict';
  var spelledWord = $('#syllablesHolder').text();
  var correct = $.inArray(spelledWord, words) > -1;
  if (correct) {
    alert('Поздравления! Решихте правилно задачата');
    Dyslexio.correctSolution('syllables');
    startNewRound();
  } else {
    alert('Имате грешки в решението. Опитайте отново');
    Dyslexio.incorrectSolution('syllables');
  }
}

function initGame() {
  'use strict';
  $('#checkButton').click(checkSolution);
  $('#resetButton').click(resetRound);
  $('#newRoundButton').click(startNewRound);
  $('#helpDiv').load(fileName, init);
}