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

---
title: Moving Around a Grid and Respecting Boundaries | daily.dev
description: A beginner-friendly tutorial walks through moving a character around a 2D grid in JavaScript while enforcing boundary limits. It introduces the propose,...
canonical: https://daily.dev/posts/moving-around-a-grid-and-respecting-boundaries-mqsynai0v
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Moving Around a Grid and Respecting Boundaries | daily.dev
og:description: A beginner-friendly tutorial walks through moving a character around a 2D grid in JavaScript while enforcing boundary limits. It introduces the propose,...
og:url: https://daily.dev/posts/moving-around-a-grid-and-respecting-boundaries-mqsynai0v
og:image: https://api.daily.dev/og/posts/mqSynAI0V.png
og:image:alt: Moving Around a Grid and Respecting Boundaries
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 Respecting Boundaries

**[Kirupa](https://daily.dev/sources/kirupa)** · 12 min read · 12 upvotes · 3 comments

## Summary

A beginner-friendly tutorial walks through moving a character around a 2D grid in JavaScript while enforcing boundary limits. It introduces the propose, validate, commit pattern: translating a direction into an x/y offset, calculating a candidate position, checking it against grid bounds with an inBounds function, and only updating the actual position if the candidate is valid. Full HTML and JavaScript code is provided, including keyboard, button, and touch/swipe input handling, building a working interactive demo of an alien character named Zorb confined to a 10x10 grid.

## 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.htm>

## Questions this post answers

### How do I stop a character from moving off the edge of a 2D grid in JavaScript?

Check whether the candidate x and y coordinates fall within valid ranges before updating the actual position. A helper like inBounds(x, y) returns true only when x is between 0 and cols-1 and y is between 0 and rows-1; if the check fails, the position update is skipped and the character stays where it was.

_Explore more grid movement and boundary-check patterns for game logic on daily.dev._

### What is the propose validate commit pattern for handling movement in a grid-based game?

It is a three-stage approach for safely updating position: first calculate a candidate x and y from the requested direction offset (propose), then check whether that candidate lies inside the grid bounds (validate), and only update the real position if the check passes (commit). This keeps invalid coordinates from ever becoming part of the actual game state.

_daily.dev surfaces practical patterns like this for developers building grid-based logic._

## Community discussion

Top comments from developers on daily.dev.

**@agustinbarrientos** · 1 upvotes

> Very clear tutorial, thanks. The propose, validate, commit example is easy to follow.

## Similar posts on daily.dev

- [Moving Around a Grid and Avoiding Obstacles](https://daily.dev/posts/moving-around-a-grid-and-avoiding-obstacles-hwmnsrr9m) · Kirupa · 9 upvotes · 0 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
- [Animating CSS Grid Layouts with CSS Anchor Positioning](https://daily.dev/posts/animating-css-grid-layouts-with-css-anchor-positioning-1ufjfyoc1) · Bram.us · 15 upvotes · 2 comments
- [Building a Scroll-Reactive 3D Gallery with Three.js, Velocity, and Mood-Based Backgrounds](https://daily.dev/posts/building-a-scroll-reactive-3d-gallery-with-three-js-velocity-and-mood-based-backgrounds-fpqk8vowy) · Codrops · 1 upvotes · 2 comments
- [Game Dev Without An Engine: The 2025/2026 Renaissance](https://daily.dev/posts/game-dev-without-an-engine-the-2025-2026-renaissance-pvy79vrpp) · SitePoint · 0 upvotes · 0 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/moving-around-a-grid-and-respecting-boundaries-mqsynai0v)

```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 Respecting Boundaries","url":"https://daily.dev/posts/moving-around-a-grid-and-respecting-boundaries-mqsynai0v","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/moving-around-a-grid-and-respecting-boundaries-mqsynai0v"},"datePublished":"2026-08-26T05:18:24.027Z","dateModified":"2026-09-14T09:14:09.452Z","description":"A beginner-friendly tutorial walks through moving a character around a 2D grid in JavaScript while enforcing boundary limits. It introduces the propose,...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/dcf6410751da3bbb42ae2fd57aab5860?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/dcf6410751da3bbb42ae2fd57aab5860?_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":3,"discussionUrl":"https://daily.dev/posts/moving-around-a-grid-and-respecting-boundaries-mqsynai0v","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":12},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":3}],"keywords":"webdev,javascript","timeRequired":"PT12M"}
{"@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 Respecting Boundaries"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/moving-around-a-grid-and-respecting-boundaries-mqsynai0v","comment":[{"@type":"Comment","text":"Very clear tutorial, thanks. The propose, validate, commit example is easy to follow.","datePublished":"2026-08-27T13:40:11.301Z","url":"https://daily.dev/posts/mqSynAI0V#c-d9c52jJov","author":{"@type":"Person","name":"Agustin Barrientos","url":"https://daily.dev/agustinbarrientos","image":"https://media.daily.dev/image/upload/s--5ayxQnqn--/f_auto/v1788281802/avatars/avatar_wQYYVe5Tbj0NJ7C7qPoa8?_a=BAMAMicg0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/moving-around-a-grid-and-respecting-boundaries-mqsynai0v#faq","mainEntity":[{"@type":"Question","name":"How do I stop a character from moving off the edge of a 2D grid in JavaScript?","acceptedAnswer":{"@type":"Answer","text":"Check whether the candidate x and y coordinates fall within valid ranges before updating the actual position. A helper like inBounds(x, y) returns true only when x is between 0 and cols-1 and y is between 0 and rows-1; if the check fails, the position update is skipped and the character stays where it was. Explore more grid movement and boundary-check patterns for game logic on daily.dev."}},{"@type":"Question","name":"What is the propose validate commit pattern for handling movement in a grid-based game?","acceptedAnswer":{"@type":"Answer","text":"It is a three-stage approach for safely updating position: first calculate a candidate x and y from the requested direction offset (propose), then check whether that candidate lies inside the grid bounds (validate), and only update the real position if the check passes (commit). This keeps invalid coordinates from ever becoming part of the actual game state. daily.dev surfaces practical patterns like this for developers building grid-based logic."}}]}
```

