Skip to content

Packet Drift — A 2-player inertia-based game built for the DAA (23CSE211) course, designed to explore multiple algorithmic strategies.

Notifications You must be signed in to change notification settings

Nithin0306/packet-drift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Packet Drift - DAA Project

Design & Analysis of Algorithms (DAA) Project
A 2-player turn-based inertial sliding game on a 10×8 grid, where the CPU opponent uses an enhanced Greedy algorithm with Divide & Conquer and Dynamic Programming optimizations.


🎯 Project Objective

Demonstrate practical application of core DAA concepts in game AI design:

  • Greedy algorithm as the foundation
  • Divide & Conquer for efficient distance/clustering
  • Dynamic Programming (memoization) for reuse
  • Graph modeling with adjacency list
  • Sorting as preprocessing

Core Algorithms & Features Implemented

1. Greedy Algorithm (Core CPU Decision Engine)

File: src/cpu/GreedyStrategy.java

  • Evaluates all 8 directions every CPU turn
  • Simulates full inertial slide for each direction
  • Selects move with highest heuristic score:
    score = (data_collected × 100) - cluster_distance - (death_penalty if virus)
    
  • Time Complexity: O(1) per direction evaluation (8 × constant slide length)
  • Space Complexity: O(1)

Key Insight: Unlike traditional greedy (pick nearest), this uses cluster distance to prefer positions near remaining data groups.

2. Divide & Conquer – Cluster Distance

File: src/cpu/DCClusterDistance.java

  • Computes average distance to K=3 closest remaining data points
  • Uses Merge Sort (by x-coordinate) for preprocessing
  • Steps:
    1. Divide data points by x-coordinate (merge sort)
    2. Compute Euclidean distances to all remaining data
    3. Sort distances array to find 3 closest
    4. Return average distance

Time Complexity: O(N log N) where N = remaining data points
Space Complexity: O(N)

// Simplified merge sort for D&C demonstration
private static void mergeSort(Point[] points, int left, int right) {
    if (left < right) {
        int mid = left + (right - left) / 2;
        mergeSort(points, left, mid);      // Divide
        mergeSort(points, mid + 1, right); // Divide
        merge(points, left, mid, right);   // Conquer
    }
}

3. Minimal Dynamic Programming (Memoization)

File: src/cpu/DPMemoCache.java

  • Caches cluster distance results within one CPU turn
  • Cache key = (end_position + sorted hash of remaining data)
  • Reuses expensive D&C computation if duplicate end states occur
  • Scope: Per-turn only (fresh cache each CPU turn)

Time Complexity: O(1) amortized lookup
Space Complexity: O(8) max cache entries per turn

Example: If directions NE and E both end at position (5,3) with same remaining data, cluster distance computed only once.

4. Graph Representation

File: src/graph/Graph.java, src/graph/GraphNode.java

  • Board modeled as Graph with Adjacency List
  • Each node uses EnumMap<Direction, GraphNode> for neighbors
  • Why Adjacency List?
    • Sparse graph (max 8 neighbors per node)
    • O(1) directional lookup
    • O(N) space where N = grid size (10×8 = 80 nodes)
public class GraphNode {
    private EnumMap<Direction, GraphNode> neighbors;
    // O(1) neighbor access by direction
    public GraphNode getNeighbor(Direction dir) {
        return neighbors.get(dir);
    }
}

5. Sorting Algorithms Used

Custom Merge Sort (D&C demonstration):

  • Manual implementation in DCClusterDistance.java
  • Sorts data points by x-coordinate
  • Time Complexity: O(N log N)
  • Space Complexity: O(N)

Java Timsort (optimal for small arrays):

  • Used for sorting distances array
  • Hybrid merge sort + insertion sort
  • Time Complexity: O(N log N)

Algorithm Complexity Summary

Component Time Complexity (per CPU turn) Space Complexity Paradigm Used
Greedy Core O(1) O(1) Greedy
D&C Cluster Distance O(N log N) O(N) Divide & Conquer
Sorting (Merge Sort) O(N log N) O(N) Divide & Conquer
Minimal DP Memoization O(1) amortized O(8) Dynamic Programming
Overall per CPU Turn O(N log N) O(N) Hybrid

Note: N = number of remaining data points (typically 5-30) → practically constant time for small boards.


Key Project Features (Algorithmic Focus)

  1. Inertial Sliding Physics - Graph traversal in 8 directions until obstacle
  2. Cluster-aware Heuristic - CPU prefers positions near groups of data (not just nearest)
  3. Intra-turn Memoization - Avoids redundant D&C calculations within same turn
  4. Adjacency List Graph - Efficient O(1) neighbor lookup
  5. Grid-based Board Generation - Even distribution across 4 quadrants
  6. BFS Path Validation - Ensures playable board with reachable hub

Project Structure (DAA-Relevant Files)

src/
├── Game.java                       # Main game loop & turn management
├── board/
│   ├── Board.java                  # Grid initialization
│   ├── BoardRandomizer.java        # Grid-partitioned distribution
│   └── TileType.java               # Tile enumeration
├── graph/
│   ├── Graph.java                  # Adjacency list graph
│   └── GraphNode.java              # Node with EnumMap neighbors
├── movement/
│   └── Direction.java              # 8-direction enumeration
├── player/
│   └── Player.java                 # Player interface
├── cpu/
│   ├── CPUPlayer.java              # CPU player controller
│   ├── GreedyStrategy.java         # ⭐ Greedy algorithm implementation
│   ├── DCClusterDistance.java      # ⭐ D&C merge sort + clustering
│   └── DPMemoCache.java            # ⭐ DP memoization cache
└── ui/                             # (UI files - not algorithmic focus)

Core Algorithm Files (⭐ marked):

  • GreedyStrategy.java - Greedy decision making
  • DCClusterDistance.java - Divide & Conquer sorting + distance calculation
  • DPMemoCache.java - Dynamic Programming memoization

Game Rules (Brief)

  • Grid: 10 columns × 8 rows = 80 nodes
  • Movement: WASD (cardinal) + QEZC (diagonal)
  • Sliding: Packet slides until hitting hub, firewall, boundary, or virus
  • Scoring: 1 data packet = 1 byte
  • Win Condition: Highest score after all data collected OR 50 hops
  • Turns: Human and CPU alternate

How to Run

Prerequisites

  • Java JDK 8+
  • Windows/Linux/Mac terminal

Compilation & Execution

  1. Navigate to project
cd packet-drift
  1. Clean old class files (optional)
clean.bat          # Windows
# ./clean.sh       # Linux/Mac
  1. Compile
javac -d . src\*.java src\board\*.java src\graph\*.java src\movement\*.java src\player\*.java src\cpu\*.java src\ui\*.java
  1. Run
java src.Game

Testing Documentation

General Test Cases

File: TEST_CASES.md

  • 75+ test cases across 10 categories
  • Movement physics, scoring, turn management
  • Edge cases and error handling

CPU AI Algorithm Test Cases

File: cpu_ai_test_cases.md

  • 12 specialized algorithm validation tests
  • Cluster advantage verification
  • Memoization efficiency tests
  • D&C complexity validation
  • Edge cases (0-2 data points remaining)

Example Test Scenario:

Grid with clustered data (D1,D2,D3) vs isolated data (D4):
- Old greedy: picks D4 (nearest)
- New cluster-aware: picks D1 → positions near D2,D3 cluster
- Expected: New AI wins by 15-20% higher final score

Algorithm Comparison: Old vs New

Aspect Old Greedy (Simple) New Greedy (Enhanced) Improvement
Decision Metric Distance to nearest data Cluster distance (avg of 3) Strategic positioning
Time Complexity O(N) O(N log N) Slightly slower
Win Rate Baseline +15-25% Significant
Preprocessing None Merge sort + memoization More intelligent
Paradigms Used Greedy only Greedy + D&C + DP Multi-paradigm

Design Patterns & Techniques

Data Structures

  • Graph: Adjacency list (EnumMap)
  • Cache: HashMap for memoization
  • Arrays: For merge sort implementation

Algorithmic Techniques

  1. Greedy: Local optimal choice (best immediate score)
  2. D&C: Merge sort for sorting, recursive distance calculation
  3. DP: Memoization to avoid redundant calculations
  4. Graph Traversal: Neighbor exploration for sliding physics

Why This Combination?

  • Greedy alone is too myopic (ignores future positioning)
  • D&C provides efficient clustering analysis
  • DP eliminates redundant work within turns
  • Result: Smart AI that balances immediate gain with strategic positioning

Performance Metrics

Metric Value
Board size 10×8 = 80 nodes
Typical data points 10-15
CPU evaluation time < 5ms per turn
Average game length 20-35 turns
Memoization hit rate 15-25%
D&C overhead ~2ms (N log N)

DAA Concepts Demonstrated

Greedy Algorithm - CPU move selection
Divide & Conquer - Merge sort for clustering
Dynamic Programming - Memoization cache
Graph Theory - Adjacency list representation
Sorting Algorithms - Manual merge sort + Timsort
Complexity Analysis - Time/space tradeoffs
Algorithm Optimization - Hybrid approach for better performance


Conclusion

This project demonstrates that combining multiple algorithmic paradigms yields better results than using a single approach. The enhanced greedy AI outperforms simple greedy by 15-25% win rate while maintaining O(N log N) complexity, which is acceptable for small game boards.

Key Takeaways:

  1. Greedy alone is fast but shortsighted
  2. D&C preprocessing enables smarter decisions
  3. DP memoization reduces redundant work
  4. Proper data structures (adjacency list) matter for performance
  5. Algorithm complexity matters less than algorithmic intelligence for small inputs

Course: Design & Analysis of Algorithms
Language: Java 8+
License: MIT (Educational Use)


Repository: https://github.com/Nithin0306/packet-drift

About

Packet Drift — A 2-player inertia-based game built for the DAA (23CSE211) course, designed to explore multiple algorithmic strategies.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5