数组数组中字母分配的改进 [英] Improvements in letter allocation in array of arrays

查看:44
本文介绍了数组数组中字母分配的改进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了以下代码来在数组数组中分配单词。就像文字游戏一样。

I made the following code to allocate words in a array of arrays. Like a word game.

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

const directions = [
  'horizontal',
  'vertical'
];

function chooseRandomPlace() {
  return [Math.floor(Math.random() * 10),
          Math.floor(Math.random() * 10)];
}

let i = 0;
function horizontal(word) {
  i++;
  console.log(i);
  let wordLength = word.length;
  let [x, y] = chooseRandomPlace();
  if (9 - y < wordLength) return horizontal(word);

  for (let i = 0; i < wordLength; i++) {
    if (typeof board[x][y + i] !== 'string') {
      return horizontal(word)
    }
  }

  for (let i = 0; i < wordLength; i++) {
    board[x][y + i] = {value: word[i]};
  }
}

function vertical(word) {
  i++;
  console.log(i);
  let wordLength = word.length;
  let [x, y] = chooseRandomPlace();
  if (9 - x < wordLength) return vertical(word);


  for (let i = 0; i < wordLength; i++) {
    if (typeof board[x + i][y] !== 'string') {
      return vertical(word);
    }
  }

  for (let i = 0; i < wordLength; i++) {
    board[x + i][y] = {value: word[i]};
  }
}

function alocate(word) {
  let direction = directions[Math.floor(Math.random() * directions.length)];
  if (direction === 'horizontal') {
    horizontal(word)
  } else {
    vertical(word);
  }
  console.log(JSON.stringify(board));
}

const words = ['SOMETHIN', 'SOMETHIN', 'SOMETHIN', 'SOMETHIN', 'SOMETHIN'];
for (let word of words) {
  let location = alocate(word);
}

并且正在运作,但它可以导致最大调用堆栈大小超出错误错误,具体取决于单词数(最多10个)和每个单词的长度。是否有任何类型的改进可以避免这种情况...如果没有,是否有某种数学算法来设置具有最大长度的单词的安全限制?

And is working, but it can cause a Maximum call stack size exceeded error error depending on the number of words (max of 10) and the length of each word. Is there any type of improvement to do to avoid this... and if not, is there some sort of mathematic algorithm to set a secure limit of words with max length?

谢谢。

推荐答案

将表示单词的字符串转换为数组。创建两个变量,反映数组数组中数组的起始索引和数组数组中的索引,当前索引的字符串应设置为重新形成单词。

Convert string representing word to an array. Create two variables reflecting starting index of array within array of arrays and index within array of arrays which current index of string should be set at to re-form the word.

发布将垂直索引增加到,它对应于数组数组中的相邻数组。将每个相邻数组设置为index,其中,对应于当前数组的索引,为string的索引值。
要水平设置字母,请从变量中增加

Post increment vertical index to, which corresponds to adjacent array within array of arrays. Set each adjacent array at index where, which corresponds to the index of current array, to value of index of string. To set letter horizontally, post increment from variable.

您可以创建一个接受可选对象,其中值可以在反映水平和对角线坐标的板阵列的索引处设置,以生成在板上具有水平,垂直和对角线的字的填字游戏板。

You can create a function which accepts an optional object where values can be set at indexes of board array reflecting horizontal and diagonal coordinates to generate a crossword board having words in horizontal, vertical and diagonal lines on the board.

在stacksnippets的填字游戏板上至少有九个单词, .color 的元素的 .textContent 点击字母是单词的一部分。

There are at least nine words at crossword board at stacksnippets, where .color of .textContent of element is changed at click where the letter is part of a word.

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

const div = document.querySelector("div");

const settings = {list:void 0, diagonal:false, horizontal:false, from:0, to:0};

const setWord = (coords) => {

  div.innerHTML = "";
  
  let {list, from, to, diagonal, horizontal} = Object.assign({}, settings, coords);
  
  if (!list || list.length && list.length === 0) 
    throw new Error("list is not defined");
  
  for (let letter of list) {
    let [x, y] = [horizontal ? to : to++
                 , diagonal || horizontal ? from++ : from];
    board[x][y] = `<span onclick="this.className='letter'">${letter}</span>`;
  }

  for (let arr of board) {
    div.innerHTML += `${arr.map(letter =>
      /^</.test(letter) ? letter : `<span>${letter}</span>`).join(" ")}<br>`;
  }

}

setWord({list:"su", diagonal:false, from:4, to:2});
setWord({list:"ha", diagonal:false, from:2, to:0});
setWord({list:"lemur", diagonal:true, from:2, to:2});
setWord({list:"f", diagonal:false, from:2, to:3});
setWord({list:"d", diagonal:false, from:5, to:3});
setWord({list:"a", diagonal:false, from:6, to:3});
setWord({list:"l", diagonal:false, from:7, to:3});
setWord({list:"l", diagonal:false, from:7, to:3});
setWord({list:"p", diagonal:false, from:5, to:6});
setWord({list:"pa", horizontal:true, from:0, to:2});
setWord({list:"m", horizontal:true, from:3, to:2});
setWord({list:"clone", diagonal:false, from:7, to:2});

.letter {
  color: green;
}

div {
  background: #000;
  width: 180px;
  margin: 4px;
  padding: 4px;
  border: 4px solid goldenrod;
}

div span {
  color: red;
  white-space: pre;
  position: relative;
  display: inline-block;
  width: 14px;
}

<div></div>

这篇关于数组数组中字母分配的改进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆