Lab 9
[valid 2025-2026]
Concurrency
Consider a valid maze designed in the previous lab.
Inside the maze, at a random location, there is a bunny 🐰 trying to find the exit.
A number of bad robots 🤖 🤖 🤖 must catch the bunny by exploring the maze using various strategies.
The robots are initially placed at distinct random positions.
Two robots cannot occupy the same cell at the same time.
The robots can access a shared memory where they can store information about the maze and the bunny
(if a robot is very close to the bunny, it might see it and share this information).
The game ends when the bunny finds the exit or a robot catches it.
The main specifications of the application are:
Compulsory (1p)
- Create an object-oriented model of the problem.
-
The bunny and the robots must act concurrently, moving randomly around the maze while trying to accomplish their goals.
Simulate the exploration using one thread for each of them.
- Implement a text-based mode to display the state of the game.
- Use thread synchronization when moving to a cell or sharing information.
Homework (2p)
- Design an algorithm so that each robot tries to explore the map in a systematic fashion.
- Determine if the game is finished and stop the threads.
- Implement commands to control the speed of the robots or the bunny (slow down, speed up, stop, resume) either all of them or only a specific one.
The commands must be entered using the keyboard.
- Implement a daemon manager thread that runs concurrently with the other threads.
This thread will periodically display the game state and running time in the console, and will stop the game if it exceeds a specified time limit.
Advanced (2p+)
- Implement the shared memory so that the robots can work collaboratively; for example, a robot might try not to explore a cell visited by another robot, if possible.
- Implement the robots' proximity sensors so that they can "sense" the bunny if they are close to it and inform the others, via the shared memory.
Once a robot is informed of the bunny's location, it must go to that location using the shortest path in the maze.
- Create a graphical user interface for the game.
- What if there are more bunnies? 🐰 🐰 🐰
References
Objectives
- Understand the basic principles of concurrent programming.
- Create and run threads using the Thread class and the Runnable interface.
- Understand resource contention and thread interference.
- Create synchronized methods or statements.
- Implement the wait-notify mechanism.
- Understand the thread pool and fork/join concepts.