0

I am making a game, in which a user types or to be precise clicks on a 4x4 board to create a meaningful word. The issue is my random distribution of letters on the board leads to prevailing of consonants, which makes it hard to find words. Like on the picture (only 3 vowels). So my question, is there any formula or algorithm to distribute consonants and vowels nearly equally maybe 10 (consonants) to 6 (vowels)? Or can you suggest a solution.

enter image description here

The way I display letters randomly is the following:

    const letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t',
 'u','v','w','x','y','z'];

    var searched_word = '';
    var tr = '';
    var wordsBank = [];
    var wordsCount = 0;

    var boxes = document.querySelectorAll('.square');
    //console.log(boxes);

    boxes.forEach(function(box) {
      box.addEventListener('click', function(e) {
        e.preventDefault();
        searched_word += this.innerHTML;
        document.querySelector('.input').value = searched_word;
      });

      console.log(searched_word);
      return searched_word;
    });

    function randomLetters(boxes) {
      for (var i = 0; i < boxes.length; i++) {
        let box = boxes[i];
        let randomLetterNumber = Math.floor(Math.random() * letters.length);
        box.innerHTML = letters[randomLetterNumber].toLocaleUpperCase();
        //console.log(box);
        //console.log(randomLetterNumber);
      }
    }
NZMAI
  • 536
  • 5
  • 27
  • 1
    what about defining two arrays one for vowels and one for consonants, take adequate count of letters from each set, put these chosen chars to one array and then distribute them randomly over the matrix? – Coreggon Jul 06 '18 at 11:33
  • how many vowels do you have in total? – Nina Scholz Jul 06 '18 at 11:35
  • Every time I have different number of letters, sometimes 3, 4. – NZMAI Jul 06 '18 at 11:38
  • 1
    Why not try to roughly match standard letter frequencies in whatever the target language is, rather than simply focusing on a vowel-consonant ratio? – John Coleman Jul 06 '18 at 11:42
  • Hi John, is there a formula to do that? – NZMAI Jul 06 '18 at 11:44
  • No formula, but you can get standard frequencies from e.g Wikipedia and it is then easy enough to write code to make weighted-choices from an array. – John Coleman Jul 06 '18 at 11:57

1 Answers1

3

make two arrays, one for consonants and one for vowels

now get 10 randoms from consonants and 6 from vowels

voila :-)

example for shuffling

function shuffle(a) {
  for (let i = a.length - 1; i > 0; i--) {
     const j = Math.floor(Math.random() * (i + 1));
     [a[i], a[j]] = [a[j], a[i]];
  }
  return a;
}
let selectedChars = selectedVowels.concat(selectedConsonants);
let shuffledChars = shuffle(selectedChars);

source for shuffle function: How can I shuffle an array?

Kapsonfire
  • 1,013
  • 5
  • 17
  • Hi Kapsonfire, thank you for suggestion, the way I distribute letters on the board is that I loop through each tile and assign a letter randomly from first letter to the last. And I do not want to allocate specific letters for specific tiles. Anyway maybe I will think about implementation of your method. – NZMAI Jul 06 '18 at 12:08
  • you dont need two.... let selectedChars = selectedVowels.concat(selectedConsonants); after that shuffle the array see also: https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array – Kapsonfire Jul 06 '18 at 12:09
  • Thanks Kapsonfire! I will check it out! – NZMAI Jul 06 '18 at 13:22