使用jquery的Boggle类型单词检查器

时间:2019-05-28 15:41:41

标签: javascript jquery recursion

我正在尝试使用jQuery创建Boggle类型单词检查器的变体。不同之处在于,一个字母可以多次使用,并且面板始终为两行。

我一直在使用一分为二的字符串作为“木板”,并使用递归函数搜索给定的单词。我看过类似的示例,但似乎无法正确编写该函数。

例如,如果板子字符串为OMRAWNLET。董事会看起来像:

O M R A W  
N L E T    

如果要搜索的单词是true,它将返回watermelon。谁能指导我或编写功能?谢谢。

2 个答案:

答案 0 :(得分:1)

我将在板上使用两个二维数组,并在单词上使用字母数组。 这是一个快速的原型:

var isWordOnTheBoard = function(board, word) {
    var passedTest = true;
    var previousLetterCoordinates;
    
    //written as a loop; rewrite to use recursion
    for (let letter of word) {
        // console.log('current letter is ' + letter);
        var letterCoordinates = getCoordinatesOnTheBoard(board, letter);
        if(!letterCoordinates){
            passedTest = false;
            console.log('letter ' + letter + ' is not on the board');
            break;
        }
        if(previousLetterCoordinates && !areNeighbors(previousLetterCoordinates, letterCoordinates)){
            passedTest = false;
            console.log('letter ' + letter + ' is not a neighbor to the previous letter');
            break;
        }
        previousLetterCoordinates = letterCoordinates;
    }
    return passedTest;
};

//assumes letters on the board are unique with no repeated letters
var getCoordinatesOnTheBoard = function(board, letter) {
    //undefined means letter is not on the board
    var coordinates;
    
    var firstRowIndex = board[0].indexOf(letter);
    if(firstRowIndex >= 0){
        coordinates = {
            row: 0,
            column: firstRowIndex
        };
    }
    
    var secondRowIndex = board[1].indexOf(letter);
    if(secondRowIndex >= 0){
        coordinates = {
            row: 1,
            column: secondRowIndex
        };
    }
    
    
    return coordinates;
}
//neighbors of a:
// | NW | N | NE |
// | W  | a | E  |
// | SW | S | SE |
// where NW is North West, N is North, NE is North East and so on

var areNeighbors = function(a, b) {
    var nw_of_a = {
        row: a.row - 1,
        column: a.column - 1
    }
    if (JSON.stringify(b) === JSON.stringify(nw_of_a)){return true}
    var n_of_a = {
        row: a.row - 1,
        column: a.column
    }
    if (JSON.stringify(b) === JSON.stringify(n_of_a)){return true}
    
    var ne_of_a = {
        row: a.row - 1,
        column: a.column + 1
    }
    if (JSON.stringify(b) === JSON.stringify(ne_of_a)){return true}
    
    var e_of_a = {
        row: a.row,
        column: a.column + 1
    }
    if (JSON.stringify(b) === JSON.stringify(e_of_a)){return true}
    
    var se_of_a = {
        row: a.row + 1,
        column: a.column + 1
    }
    if (JSON.stringify(b) === JSON.stringify(se_of_a)){return true}
    
    var s_of_a = {
        row: a.row + 1,
        column: a.column
    }
    if (JSON.stringify(b) === JSON.stringify(s_of_a)){return true}
    
    var sw_of_a = {
        row: a.row + 1,
        column: a.column - 1
    }
    if (JSON.stringify(b) === JSON.stringify(sw_of_a)){return true}
    
    var w_of_a = {
        row: a.row,
        column: a.column - 1
    }
    if (JSON.stringify(b) === JSON.stringify(w_of_a)){return true}
    
    return false;
}

var omraw_nlet = [
    ['O','M','R','A','W'],
    ['N','L','E','T']
];
var watermelon = ['W', 'A', 'T', 'E', 'R', 'M', 'E', 'L', 'O', 'N'];
var lemon = ['L', 'E', 'M', 'O', 'N'];

console.log(isWordOnTheBoard(omraw_nlet, watermelon)); //true
console.log(isWordOnTheBoard(omraw_nlet, ['W', 'A', 'R'])); //true
console.log(isWordOnTheBoard(omraw_nlet, lemon)); //true
console.log(isWordOnTheBoard(omraw_nlet, lemon.concat(['S']))); //false
console.log(isWordOnTheBoard(omraw_nlet, ['T', 'E', 'N'])); //false

答案 1 :(得分:0)

如果您要检查的是根据黑板上的字母输入的单词是否有效,则可以很简单地做到这一点。只需将字母作为数组传递并输入为字符串即可。从那里开始,只需通过输入逐个字母地搜索字母,看看它是否在字母数组中。

function validate_input(letters, input){
input = input.toUpperCase();

  var errors = input.split("").filter(function(letter){
    return (letters.indexOf(letter) == -1);
  });

 return (errors == "");
}

var letters = ["O","M","R","A","W","N","L","E","T"];

console.log(validate_input(letters, "lemfon"));
console.log(validate_input(letters, "watermelon"));