This project is an AI agent designed to compete in the "Case Closed" game, a Tron/Snake-like game of spatial strategy on a wraparound grid. The goal is to survive longer than the opponent and box them in, maximizing our own agent's trail length.

How We Built It

Our agent, ManojSunita, is implemented as a Flask web server (agent.py) that communicates with the judge_engine.py via a REST API to receive the game state and submit a move.

1. The Core Strategy

The strategy is centered on a Greedy Search/Evaluation approach, implemented within the AdvancedAgent class. For every turn, the agent evaluates all legal (non-suicidal) moves based on a weighted scoring function.

The key evaluation functions include:

  • Reachable Space: Calculated using a Breadth-First Search (BFS) (_calculate_reachable_space) from the potential new head position. This is the most heavily weighted factor as it represents the fundamental goal: maximizing future movement.
  • Local Freedom: The number of immediate adjacent open cells. A low freedom score heavily penalizes the move, as it signals being boxed in.
  • Opponent Proximity & Trail Length: A dynamic factor that changes with the game phase and relative trail length. If our agent is longer, it seeks closer contact; if shorter, it avoids the opponent's head to prevent a head-on defeat.
  • Wall Count: Penalizes moving into areas surrounded by trails, as this rapidly reduces future reachable space.

2. Game Phasing and Boost Control

The agent dynamically adjusts its weights based on three Game Phases (_update_game_phase): EARLY (Turns < 150), MID (Turns < 350), and LATE (Turns >= 350).

  • EARLY: Prioritizes moving away from the center (_distance_to_center) and maximizing general territory to secure a good starting area.
  • LATE: The "aggressive" factor is amplified. If the agent is longer than the opponent, it uses boosts and tries to move closer to cut off the opponent's path (_blocks_opponent_path).

The Boost Mechanic (_should_use_boost) is governed by heuristics:

  • Panic Boost: If reachable space is critically low (<20), use a boost to escape.
  • Late-Game Aggression: Use a boost in the late game if we have a significant trail length advantage and are close to the opponent.
  • Resource Management: Prioritize using the boost when it provides maximum strategic value, and generally avoid using the last boost too early (before Turn 400).

3. Torus Logic

All spatial checks, like Manhattan distance (_manhattan_distance), safely handle the board's torus (wraparound) topology, ensuring the agent does not miscalculate distances or collision points near the board edges.


Challenges We Faced

  1. Torus Collision Complexity: Implementing robust collision detection and pathfinding on a torus proved tricky. Normal distance and pathfinding algorithms break down on a wraparound grid. We had to implement custom logic in _get_next_position and _manhattan_distance to account for the shortest path wrapping around the board.

  2. State Synchronization: The agent receives the game state via a REST API. We initially struggled with race conditions and inconsistent state. We solved this by using a Python threading.Lock (game_lock) around the global game state (GLOBAL_GAME) to ensure all reads and writes were atomic, preventing the move calculation from using a partially updated board.

  3. Heuristic Tuning: The scoring function required extensive manual tuning and testing. Initially, certain weights (e.g., the wall count) were too high, causing the agent to be overly cautious and lose to simple "spiral" agents. We iterated by simulating matches against the sample_agent.py and adjusting the weights for my_territory, freedom, and opponent interaction until a balance was struck.


What We Learned

We gained significant experience in adversarial AI design and real-time system architecture:

  • Heuristic Search: We learned how to decompose a complex goal (winning a spatial game) into a manageable, weighted scoring function, understanding that a perfect search is often intractable, but a well-tuned heuristic is highly effective.
  • API-Driven Agent Design: The project reinforced best practices for building a responsive, stateful web service (Flask) that acts as an agent in a timed competition.
  • Spatial Pathfinding: We deepened our understanding of how graph search algorithms (BFS) and distance metrics must be adapted for non-Euclidean topologies like the torus.

Built With

Share this project:

Updates