<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/moving-around-a-grid-and-avoiding-obstacles-hwmnsrr9m" -->

---
title: Moving Around a Grid and Avoiding Obstacles | daily.dev
description: A step-by-step tutorial extends a prior JavaScript grid-movement exercise by adding obstacle collision detection. It covers representing rock obstacles as...
canonical: https://daily.dev/posts/moving-around-a-grid-and-avoiding-obstacles-hwmnsrr9m
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Moving Around a Grid and Avoiding Obstacles | daily.dev
og:description: A step-by-step tutorial extends a prior JavaScript grid-movement exercise by adding obstacle collision detection. It covers representing rock obstacles as...
og:url: https://daily.dev/posts/moving-around-a-grid-and-avoiding-obstacles-hwmnsrr9m
og:image: https://api.daily.dev/og/posts/HWmnsrr9m.png
og:image:alt: Moving Around a Grid and Avoiding Obstacles
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Moving Around a Grid and Avoiding Obstacles

**[Kirupa](https://daily.dev/sources/kirupa)** · 13 min read · 9 upvotes · 0 comments

## Summary

A step-by-step tutorial extends a prior JavaScript grid-movement exercise by adding obstacle collision detection. It covers representing rock obstacles as coordinate strings stored in a Set, writing an isObstacle helper for O(1) membership checks, rendering obstacles visually and in ARIA labels, and adding a second validation check inside the existing tryMove function so a candidate move is rejected if it lands on a rock, in addition to the existing boundary check. The piece stresses that this is collision detection and response, not pathfinding — the code only blocks invalid moves rather than routing around obstacles.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.kirupa.com/data_structures_algorithms/moving_around_a_grid_with_obstacles.htm>

## Questions this post answers

### How do I check if a grid cell is blocked by an obstacle in JavaScript?

Store each obstacle's coordinate as a string like "5,4" in a Set, then check membership with obstacles.has(`${x},${y}`). Wrapping this in a helper function such as isObstacle(x, y) keeps the rest of the movement code working with plain x and y numbers instead of strings. A Set is used because it provides fast yes-or-no membership checks and automatically prevents duplicate obstacle coordinates.

_daily.dev surfaces practical patterns like Set-based lookups for developers building grid or game logic._

### How do you combine boundary checking and obstacle checking in a single move validation function?

Run two sequential checks inside the move function: first call inBounds(nextX, nextY) to reject moves outside the grid, then call isObstacle(nextX, nextY) to reject moves onto an occupied cell. Only after both checks pass do the coordinates actually update. This keeps the propose, validate, and commit pattern intact while adding a second validator.

_developers refining movement or physics logic can find similar validation patterns on daily.dev._

## Similar posts on daily.dev

- [Moving Around a Grid and Respecting Boundaries](https://daily.dev/posts/moving-around-a-grid-and-respecting-boundaries-mqsynai0v) · Kirupa · 12 upvotes · 3 comments
- [Auto-navigating our Grid using Breadth-First Search \(BFS\)](https://daily.dev/posts/auto-navigating-our-grid-using-breadth-first-search-bfs--py2fzjsmb) · Kirupa · 6 upvotes · 1 comments
- [Let’s Get Puzzled\! – Frontend Masters Blog](https://daily.dev/posts/let-s-get-puzzled-frontend-masters-blog-ux3ydulfv) · Frontend Masters · 2 upvotes · 0 comments
- [Auto-navigating our Grid using Dijkstra's Algorithm](https://daily.dev/posts/auto-navigating-our-grid-using-dijkstra-s-algorithm-damnfl5mm) · Kirupa · 1 upvotes · 0 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript)

[View this post on daily.dev](https://daily.dev/posts/moving-around-a-grid-and-avoiding-obstacles-hwmnsrr9m)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Moving Around a Grid and Avoiding Obstacles","url":"https://daily.dev/posts/moving-around-a-grid-and-avoiding-obstacles-hwmnsrr9m","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/moving-around-a-grid-and-avoiding-obstacles-hwmnsrr9m"},"datePublished":"2026-09-16T17:21:37.457Z","dateModified":"2026-09-16T17:47:46.872Z","description":"A step-by-step tutorial extends a prior JavaScript grid-movement exercise by adding obstacle collision detection. It covers representing rock obstacles as...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2563d6b17ff5300235e19b07f96b17d3?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2563d6b17ff5300235e19b07f96b17d3?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Kirupa","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Kirupa","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ff2fdb4927d34b81bd07cd18d6049a98","url":"https://daily.dev/sources/kirupa"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/moving-around-a-grid-and-avoiding-obstacles-hwmnsrr9m","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":9},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript","timeRequired":"PT13M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kirupa","item":"https://daily.dev/sources/kirupa"},{"@type":"ListItem","position":3,"name":"Moving Around a Grid and Avoiding Obstacles"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/moving-around-a-grid-and-avoiding-obstacles-hwmnsrr9m#faq","mainEntity":[{"@type":"Question","name":"How do I check if a grid cell is blocked by an obstacle in JavaScript?","acceptedAnswer":{"@type":"Answer","text":"Store each obstacle's coordinate as a string like \"5,4\" in a Set, then check membership with obstacles.has(`${x},${y}`). Wrapping this in a helper function such as isObstacle(x, y) keeps the rest of the movement code working with plain x and y numbers instead of strings. A Set is used because it provides fast yes-or-no membership checks and automatically prevents duplicate obstacle coordinates. daily.dev surfaces practical patterns like Set-based lookups for developers building grid or game logic."}},{"@type":"Question","name":"How do you combine boundary checking and obstacle checking in a single move validation function?","acceptedAnswer":{"@type":"Answer","text":"Run two sequential checks inside the move function: first call inBounds(nextX, nextY) to reject moves outside the grid, then call isObstacle(nextX, nextY) to reject moves onto an occupied cell. Only after both checks pass do the coordinates actually update. This keeps the propose, validate, and commit pattern intact while adding a second validator. developers refining movement or physics logic can find similar validation patterns on daily.dev."}}]}
```

