JSFiddle - React, Tailwind, and code Playground

by Ben Gillbanks

JavaScript

function board(cols, rows) { //instantiator object for making gameboards
    this.cols = cols;
    this.rows = rows;
    var activeWordList = []; //keeps array of words actually placed in board
    var acrossCount = 0;
    var downCount = 0;

    var grid = new Array(cols); //create 2 dimensional array for letter grid
    for (var i = 0; i < rows; i++) {
        grid[i] = new Array(rows);
    }

    for (var x = 0; x < cols; x++) {
        for (var y = 0; y < rows; y++) {
            grid[x][y] = {};
            grid[x][y].targetChar = EMPTYCHAR; //target character, hidden
            grid[x][y].indexDisplay = ''; //used to display index number of word start
            grid[x][y].value = '-'; //actual current letter shown on board
        }
    }

    function suggestCoords(word) { //search for potential cross placement locations
        var c = '';
        coordCount = [];
        coordCount = 0;
        for (i = 0; i < word.length; i++) { //cycle through each character of the word
            for (x = 0; x < GRID_HEIGHT; x++) {
                for (y = 0; y < GRID_WIDTH; y++) {
                    c = word[i];
                    if (grid[x][y].targetChar == c) { //check for letter match in cell
                        if (x - i + 1> 0 && x - i + word.length-1 < GRID_HEIGHT) { //would fit vertically?
                            coordList[coordCount] = {};
                            coordList[coordCount].x = x - i;
                            coordList[coordCount].y = y;
                            coordList[coordCount].score = 0;
                            coordList[coordCount].vertical = true;
                            coordCount++;
                        }

                        if (y - i + 1 > 0 && y - i + word.length-1 < GRID_WIDTH) { //would fit horizontally?
                            coordList[coordCount] = {};
                            coordList[coordCount].x = x;
                            coordList[coordCount].y = y - i;
                  ...