Post

Escape from woods C# exam

Monkey Escape Simulation

Overview

This project simulates monkeys escaping from a forest by jumping from tree to tree. The forest is randomly generated, and each monkey attempts to escape by finding the nearest tree until they reach the boundary. The simulation is designed to run asynchronously, leveraging multi-threading to execute the escape attempts. The project includes a visual output of the paths taken by the monkeys and stores the data in a database for analysis.

The source code can be found here

Features

  • Forest Generation: The forest is generated with random trees placed within specified boundaries. Each tree is uniquely positioned to avoid overlap.
  • Monkey Generation: Monkeys are randomly generated with unique names and colors. Each monkey is assigned a random starting tree within the forest.
  • Escape Simulation: Monkeys attempt to escape by jumping to the nearest unvisited tree. If a wall is closer than any tree, the monkey escapes the forest.
  • Asynchronous Execution: The escape simulation runs asynchronously with multiple threads to improve performance.
  • Logging: All actions and paths taken by the monkeys are logged into a logfile for future analysis.
  • Visual Output: The simulation includes a visual representation of the paths taken by the monkeys during their escape attempts.

How It Works

  1. Forest Setup: A forest map is generated with a random number of trees.
  2. Monkey Assignment: Monkeys are randomly placed in the forest, each starting from a different tree.
  3. Escape Process: Monkeys jump from tree to tree, avoiding previously visited trees and other monkeys until they escape or run out of trees to jump to.
  4. Logging and Analysis: Each step is logged to a logfile.

Tech Stack

  • C#/.NET: The core language and framework used to build the simulation.
  • Multi-threading: Utilized for parallel execution of the simulation.
  • Database: Stores the simulation data and logs for analysis.
  • Drawing: Visual output for the paths taken by the monkeys.

Running the Simulation

  1. Database Reset: Before starting, the database is reset to ensure a clean slate for the new simulation.
  2. Start Simulation: The main program initializes the forest and monkeys, then runs the escape simulation asynchronously across multiple threads.
  3. Save Results: After all monkeys have either escaped or been trapped, the results are saved to the database, and the visual output is generated.
This post is licensed under CC BY 4.0 by the author.