Developing a Boggle game: mapping coordinates to alphabet characters

Imagine a grid with 16 letters arranged in a 4x4 board. Each letter is uniquely positioned, where [2,1] points to the letter in the third row and second column.

const coordPairs = [
  [ [2, 1], [4, 1] ]
  [ [4, 0], [4, 1], [4, 2], [4, 3] ]
  [ [0, 0], [0, 1], [0, 2] ],
  [ [1, 0], [3, 0], [4, 0] ]
]

I am currently pondering on how to associate each pair like [2,1] with a specific letter on the game board represented by an array of 16 strings (letters).

The ultimate goal is for a function to generate words based on the coordinates you input and the arrangement of letters on the game board.

If you would like to view the code with detailed comments, visit this JSFiddle link: https://jsfiddle.net/8euxzgy2/4/

Answer №1

Suppose we are working with a zero indexed square matrix. To access the elements, you can calculate the position using the formula rows * coord[0] + coord[1]:

let str = "abcdefghijklonop"
let rows = 4
const coordPairs = [[0, 0], [2, 1], [3, 1] ];
  
/*  a b c d
*   e f g h
*   i j k l
*   m n o p  */ 

letters = coordPairs.map(coord => str[coord[0]* rows + coord[1]])
console.log(letters)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to determine the number of characters in an emoji?

When a new user signs up on my page, I need to validate the name. One important check is ensuring that the character limit does not exceed 100. However, validating names becomes tricky when dealing with emojis like ...

Popup modal is obstructed by carousel indicator

I'm having trouble with the following issue: The 3 dots represent my carousel indicator. My problem is that the carousel indicator is blocking my pop-up modal image. I've tried adjusting the z-index of the modal without success. Here is the code ...

Protractor: What is the best way to retrieve the baseUrl from the command line input?

How can I retrieve the baseUrl that was passed through the command line in Protractor? For example: protractor conf.js --baseUrl=http://myawesomesite.com I attempted to use browser.baseUrl to access the baseUrl set in my conf.js file, but it does not see ...

Comparing and merging values using PHP array functions

Thank you for taking the time! After going through numerous 'Compare and Merge' discussions, I have come to a point where I need assistance with a very specific scenario. $input = array( [ 2616 ] => array( [ 9878767654 ] => array( ...

Update the outer function variable using the inner function variable in JavaScript

I've been diving into JavaScript for a new project and I could really use some guidance. My current hurdle involves updating the outer variables xPosition and yPosition with the new positions generated by the getMousePosition() function within the po ...

Do I need to include a callback in my AWS Lambda handler function?

What is the function of the callback in the lambda handler? It appears to be utilizing the sns variable and I am looking to make some modifications to the variables. exports.handler = function(event, context, callback) { console.log("AWS lambda and ...

How to create filter components that can be chained together in React?

What is the most efficient way to create reusable, chainable filter components using React? Consider having an array of input data: [ {name: 'Generic t-shirt', size: 'xxl', available: 35}, {name: 'Generic t-shirt', size: &apo ...

Recursively arrange nested arrays by establishing parent-child relationships before sorting

My database structure is organized as follows: ID name sort parent 1 item1 1 0 2 subitem1 2 1 3 subsubitem1 1 2 4 subitem2 1 1 I have converted the database into an array array (size=4) 0 =&g ...

What is the best way to set an array as the value for an HTML form input?

I am attempting to set the value of an array to an input element in HTML. When I run the following code in my browser console, it works: let between0400am0800am = [1669352400000, 1669438800000]; document.getElementById("time400am800amEveryDay"). ...

How to Generate a JSON Cookie Array?

Currently, I am in the process of leveraging jquery's json to form a cookie array. The script I have managed to put together thus far is functional except for the part pertaining to the array. Would someone be able to guide me on how to construct an a ...

Validate user credentials with React Router Dom

In the process of developing a React application, I encountered an issue with the Header component. This specific component includes a Menu button that should toggle the sidebar and trigger a logout function, but only when the user is logged in. My approa ...

The slide experiences overflow as it continuously slides up and down

Hey everyone, I've been working on creating a slider effect when a button is clicked. Details about a specific part appear in a designated div upon button click. However, I've encountered a bug where, if I click quickly on different slides, the c ...

Occasionally, Knockout associates an element with [object HTMLDivElement] through binding

My UI application is utilizing requireJs and knockout js. In the main html file (index.html), there is a div element as follows: <html style="height:100%" lang="en"> <head> <title>Main</title> <script data-main="my ...

Using file types in Vue 3: a beginner's guide

In order to use file-type to determine not only the extension but also ensure the headers are correct I would need to use one of the methods listed on their GitHub page. In version 19.0.0 it says fileFromFileType doesn't have such an export and in 16. ...

What is the best way to dynamically incorporate Before After CSS code in Vue?

I am facing a unique challenge in my current project. I need to dynamically apply colors from data inside a v-for loop, specifically for the :after CSS pseudo-element. While normal CSS properties are easily applicable, I am struggling with applying styles ...

Retrieving data from a related table using a getter function in Sequelize

After two decades of C++, I've finally ventured into the realm of node.js web development. However, I find myself in need of some guidance on a particular issue. My current project involves creating a server-side application that handles data related ...

When developing a multi-page application using React, encountering the error "TypeError: Cannot read property 'pathname' of undefined" may indicate an issue

I am facing an issue while trying to develop a multi-page application using react-router. As I am new to React, I find it difficult to explain the problem clearly. My goal is to incorporate a login page into the application. Suggestions and alternative app ...

The Ladda spin animation continues spinning for an additional second after the stop() function is called

I employ the ladda spinner in this manner: var l = Ladda.create(document.getElementById('ladda-test')); l.start(); l.stop(); console.log('ladda is stoped'); The issue I am facing is that following the execution of l.stop(), the animat ...

Having trouble extracting the date modified from a JSON file

I am able to retrieve section name, URL, web title, and headline from parsing JSON data with this code snippet. However, I seem to be encountering an issue where I cannot extract the last modified date. Here is the JSON structure: { "response":{ ...

Troubleshooting a problem with parsing a PHP array encoded in JSON format into a JavaScript object using JSON.parse() in

Currently, I am developing a web app for my workplace that involves downloading 40,000 rows of data from an SQL table at once. After fetching the data, it is organized into nested PHP arrays and then encoded as JSON to be captured by a JavaScript variable. ...