Tips for solving the corner issue in a game of minesweeper

Currently, I am working on the reveal function using recursion in my coding project. However, I have encountered an issue where the cycle is unable to find any cell when it reaches the corner, resulting in an error. I have come across the '?.' operator, which returns a value or undefined even if there is no actual value present. I'm struggling to understand how I can incorporate this operator into my cycle.

export function reveal(boardWithMines: CellEnum[][], boardWithOutMines: CellEnum[][], x: number, y: number) {  
    if (boardWithOutMines[x][y] === CellEnum.Hidden || boardWithMines[x][y] === CellEnum.Cero) {
        boardWithOutMines[x][y] = boardWithMines[x][y];
        for (let xOffset = -1; xOffset <= 1; xOffset++) {
            for (let yOffset = -1; yOffset <= 1; yOffset++) {
                reveal(boardWithMines, boardWithOutMines, x + xOffset, y + yOffset);
            }
        }
    }
}

I am encountering this specific error message in the console window

Answer №1

Make sure to validate the boundaries in the loops:

function revealHiddenCells(boardWithMines: CellEnum[][], boardWithOutMines: CellEnum[][], x: number, y: number) {  
    if (boardWithOutMines[x][y] === CellEnum.Hidden || boardWithMines[x][y] === CellEnum.Cero) {
        boardWithOutMines[x][y] = boardWithMines[x][y];
        for (let i = Math.max(0, x-1), iMax = Math.min(boardWithMines.length, x+2); i < iMax; ++i) {
            for (let j = Math.max(0, y-1), jMax = Math.min(boardWithMines[i].length, y+2); j < jMax; ++j) {
                revealHiddenCells(boardWithMines, boardWithOutMines, i, j);
            }
        }
    }
}

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

Node.js issue: Unexpected syntax error "import" discovered during execution on node version 6.10.2

Currently, my node version is 6.10.2 and I am attempting to execute the following code snippet: import * as events from "events" class MyClass extends events.EventEmitter { constructor(x, y){ this.x = x; this.y = y; } compute ...

Add a preventDefault event listener to a submit button that triggers a specific function

$(function() { $('#login').submit(function(e){ preventSubmission(); e.preventDefault(); }); }); function preventSubmission() { $('#btnLogin').attr('disabled','disabled'); $("#btnLogi ...

What is causing my app.css to be reluctant in displaying the outcome?

Having an HTML page called chat.blade.php and a CSS file named app.css, I am facing an issue where the CSS changes are not reflecting on the page after running the PHP dev command. It might be possible that I have incorrectly written the path in the HTML f ...

Unable to prevent key blocking with event listener

<body id="body" runat="server" onkeydown="return showKeyCode(event)"> Whenever a key is pressed, IE8 (or in compatibility mode) triggers an exception pointing to an issue in line x, which is related to the body tag. How can I resolve this? The JavaS ...

Utilizing Angular's capabilities to smoothly transfer objects between controllers

Currently, I am in the midst of developing an AngularJS application integrated with a C# Web API. I have two controllers: A and B. In controller A, there is a list of objects. When I click "Add" (in between two list items), I am redirected to controller ...

What is the best way to test the stopPropagation functionality of a click event using React Testing Library?

In my current project, I have a custom Icon component that has an onClick() prop. This prop is triggered when the icon is clicked, and it also includes a function called event.stopPropagation() which stops the event from bubbling up the DOM tree. I need t ...

Vue continues to execute the timeout method even after it has been successfully cleared

In an attempt to postpone an API call for fetching search results, I have implemented the use of setTimeout and clearTimeout methods in my Vue application. A watcher has been set up on a search variable so that whenever it changes, the corresponding code ...

Collaborate on sharing CSS and TypeScript code between multiple projects to

I am looking for a solution to efficiently share CSS and TS code across multiple Angular projects. Simply copy-pasting the code is not an ideal option. Is there a better way to achieve this? ...

Tips for adding Firebase 3 typings to a TypeScript 2 project installation

Running npm install @types/firebase --save-dev will actually install type definition files for version 2.4.30, not version 3. I suspect that the type definition files for version 3 may not be available through npm at this time. Can anyone confirm? It&apo ...

Offering various language options on a website determined by the URL

I've been contemplating how to add multi-language support to my personal website, which I developed using ExpressJS and NodeJS with EJS as the template engine. Currently, the website is only available in English, but I want to add a German version as ...

VSCode API alerts user when a rejected promise is left unhandled for more than a second

After working diligently on developing my first vscode extension, I encountered a roadblock when the debugger halted the execution of my extension. As a newcomer to JavaScript, I suspect that I may be overlooking something related to "thenables" that is c ...

Exiting the browser in the Intel XDK to switch back to the application

Currently working on a small app using Intel XDK. I have successfully implemented code to open an external browser window with the function below. However, I am now facing difficulties in trying to figure out how to close this browser window and return b ...

What causes jquery height and javascript height to both be returned as 0?

I'm facing an issue with a visible div on my screen - despite being visible, its height always returns as 0. I've attempted various jQuery and JavaScript methods to retrieve the height, but it consistently shows as 0. Here's the structure of ...

Adjust the autofocus to activate once the select option has been chosen

Is there a way to automatically move the cursor after selecting an option from a form select? <select name="id" class="form-control"> <option>1</option> <option>2</option> <option>3</option&g ...

Browser crashes due to JavaScript for loop

I am facing an issue where a particular loop is crashing the browser when executed. This loop is part of a set of three functions designed to factorize quadratic equations. After conducting tests, I am confident that the problem lies within the loop itse ...

The impact of redefining TypeScript constructor parameter properties when inheriting classes

Exploring the inner workings of TypeScript from a more theoretical perspective. Referencing this particular discussion and drawing from personal experiences, it appears that there are two distinct methods for handling constructor parameter properties when ...

Creating a module within a component in angular - step by step guide

I am interested in dynamically creating a component inside another component. This will allow me to pass my dynamic HTML template directly to the decorator like this: //code /** * @param template is the HTML template * @param container is @ViewChild(& ...

Save the retrieved data in a variable and pass the entire JSON object as a prop to a React component

Having some trouble with my code. I need the app to fetch a JSON from an API and pass it as a prop so that each component file can use it and display the element on the screen. The issue is, I can't seem to figure out how to store the fetched informa ...

The Concept of Inheritance in Ember.js Models and Utilizing Templates

I'm facing a challenge displaying two different models on the index template in my Ember application. There are two models, Cats and Dogs, that I want to showcase under the index route. It seems like a simple task, but I am struggling to implement it ...

Angular keeps throwing an error saying "Provider not found" when trying to inject a factory

Issue: Encountering an Unknown Provider Error in Angular App for: UnitProvider <- Unit Error Details: Error: [$injector:unpr] Unknown provider: UnitProvider <- Unit Codepen Link: View LIVE CODE Example I recently came across a fascinating vide ...