Skip to content

ManojTestsigma/2048

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

8 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

2048 React Game

A modern, modular implementation of the popular 2048 puzzle game built with React and functional programming principles.

2048 Game React

๐ŸŽฎ Live Demo

Play the game here ๐ŸŽฎ

๐Ÿ“‹ Table of Contents

โœจ Features

  • Modular Architecture: Clean separation of concerns with reusable components
  • Functional Programming: Pure functions and immutable data structures
  • Configurable Board Size: Play on 3x3, 4x4, 5x5, or 6x6 grids
  • Responsive Design: Works on desktop and mobile devices
  • Keyboard Controls: Arrow keys or WASD for movement
  • Score Tracking: Persistent best score storage
  • Smooth Animations: CSS transitions for tile movements
  • Game States: Win detection, game over handling, and restart functionality
  • Accessibility: Focus indicators and keyboard navigation
  • Size Selection: Dropdown menu for easy board size selection
  • Attribution: Clean footer with developer contact information

๐Ÿš€ Installation

Prerequisites

  • Node.js (version 14 or higher)
  • npm or yarn package manager

Setup

  1. Clone the repository

    git clone https://github.com/yourusername/2048-react-game.git
    cd 2048-react-game
  2. Install dependencies

    npm install
    # or
    yarn install
  3. Start the development server

    npm start
    # or
    yarn start
  4. Open your browser Navigate to http://localhost:3000 to play the game.

๐ŸŽฏ Running the Game

Development Mode

npm start

Runs the app in development mode with hot reloading.

Production Build

npm run build

Creates an optimized production build in the build folder.

Testing

npm test

Launches the test runner in interactive watch mode.

๐ŸŽฎ Gameplay Instructions

Objective

Combine numbered tiles to reach the 2048 tile!

How to Play

  1. Movement: Use arrow keys or WASD to slide tiles

    • โ†‘/W: Move up
    • โ†“/S: Move down
    • โ†/A: Move left
    • โ†’/D: Move right
  2. Merging: Tiles with the same number merge when they collide

    • 2 + 2 = 4
    • 4 + 4 = 8
    • 8 + 8 = 16
    • And so on...
  3. Scoring: Each merge adds points to your score

    • Merging two 4s gives you 8 points
    • Merging two 8s gives you 16 points
  4. New Tiles: After each move, a new tile (2 or 4) appears randomly

  5. Game Over: The game ends when:

    • You reach 2048 (You Win!)
    • No more moves are possible (Game Over)

Controls

  • New Game: Start a fresh game
  • Size Dropdown: Select board size (3x3 to 6x6)
  • Keyboard: Arrow keys or WASD for movement

๐Ÿ—๏ธ Technical Implementation

Architecture Overview

The game follows a modular, component-based architecture with clear separation of concerns:

src/
โ”œโ”€โ”€ components/          # Reusable UI components
โ”œโ”€โ”€ hooks/              # Custom React hooks
โ”œโ”€โ”€ styles/             # Styled-components
โ”œโ”€โ”€ utils/              # Pure game logic functions
โ””โ”€โ”€ App.js              # Main application component

Key Design Patterns

  1. Functional Programming

    • Pure functions for game logic
    • Immutable data structures
    • No side effects in core logic
  2. Custom Hooks

    • useGame: Manages game state and logic
    • useKeyboard: Handles keyboard input
  3. Component Composition

    • Small, focused components
    • Props-based communication
    • Reusable UI elements

Data Flow

User Input โ†’ useKeyboard โ†’ useGame โ†’ Game Logic โ†’ State Update โ†’ UI Re-render

๐Ÿ“ Project Structure

2048-react-game/
โ”œโ”€โ”€ public/
โ”‚   โ””โ”€โ”€ index.html              # HTML template
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ components/             # React components
โ”‚   โ”‚   โ”œโ”€โ”€ Controls.js         # Game controls (New Game, Size)
โ”‚   โ”‚   โ”œโ”€โ”€ GameBoard.js        # Main game grid
โ”‚   โ”‚   โ”œโ”€โ”€ GameOverModal.js    # Win/Lose modal
โ”‚   โ”‚   โ”œโ”€โ”€ Instructions.js     # Game instructions
โ”‚   โ”‚   โ”œโ”€โ”€ Score.js            # Score display
โ”‚   โ”‚   โ””โ”€โ”€ Tile.js             # Individual tile component
โ”‚   โ”œโ”€โ”€ hooks/                  # Custom React hooks
โ”‚   โ”‚   โ”œโ”€โ”€ useGame.js          # Game state management
โ”‚   โ”‚   โ””โ”€โ”€ useKeyboard.js      # Keyboard event handling
โ”‚   โ”œโ”€โ”€ styles/                 # Styled-components
โ”‚   โ”‚   โ””โ”€โ”€ GlobalStyles.js     # All styled components
โ”‚   โ”œโ”€โ”€ utils/                  # Pure functions
โ”‚   โ”‚   โ””โ”€โ”€ gameLogic.js        # Core game logic
โ”‚   โ”œโ”€โ”€ App.js                  # Main application
โ”‚   โ””โ”€โ”€ index.js                # React entry point
โ”œโ”€โ”€ package.json                # Dependencies and scripts
โ””โ”€โ”€ README.md                   # This file

๐Ÿ”ง Functional Programming Principles

Pure Functions

All game logic functions are pure - they don't modify external state and always return the same output for the same input:

// Example: Pure function for sliding a row
export const slideRowLeft = (row) => {
  // No side effects, returns new data
  return { row: newRow, score: gainedScore };
};

Immutability

Game state is never mutated directly. Instead, new state objects are created:

// Example: Creating new board state
const newBoard = board.map(row => [...row]);
newBoard[row][col] = newValue;

Function Composition

Complex operations are built by composing simpler functions:

// Example: Slide up = transpose + slide left + transpose
export const slideUp = (board) => {
  const transposedBoard = transpose(board);
  const { board: slidBoard } = slideLeft(transposedBoard);
  return transpose(slidBoard);
};

๐ŸŽจ Customization

Changing Board Sizes

Add new sizes to the availableSizes array in the Controls component:

const availableSizes = [3, 4, 5, 6, 7, 8]; // Add 7x7 and 8x8

Modifying Tile Colors

Update the styled-components in GlobalStyles.js:

${props => props.value === 2048 && `
  background: #your-color;
  color: #your-text-color;
`}

Adding New Features

The modular architecture makes it easy to add new features:

  1. New Components: Add to src/components/
  2. New Hooks: Add to src/hooks/
  3. New Logic: Add pure functions to src/utils/

๐Ÿงช Testing

The project is set up with Jest and React Testing Library. Add tests to verify:

  • Game logic functions
  • Component rendering
  • User interactions
  • Keyboard controls

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Commit changes: git commit -am 'Add feature'
  4. Push to branch: git push origin feature-name
  5. Submit a pull request

๐Ÿ™ Acknowledgments

  • Original 2048 game by Gabriele Cirulli
  • React team for the amazing framework
  • Styled-components for CSS-in-JS solution

Happy Gaming! ๐ŸŽฎ

This is a dummy line for testing purposes.

About

This is the implementation of game 2048 with react.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •