Cat-Simulation is a Java-based simulation program where a cat navigates a 40x40 grid world filled with obstacles, seas, and food. The program visualizes the cat's movement and tracks how much food it consumes over a specified number of steps. The world is represented as a matrix loaded from a text file, and the simulation employs the StdDraw
library for graphical representation.
-
World Representation:
- A 40x40 grid loaded from a
world.txt
file. - Cells represent different terrains:
- Empty spaces (0): Traversable by the cat.
- Walls (1): Impassable terrain.
- Sea (2): Impassable terrain.
- Food (3): Consumable by the cat.
- A 40x40 grid loaded from a
-
Cat Simulation:
- A cat starts at a predefined position and moves randomly.
- Consumes food if encountered and clears the cell.
- Avoids walls and seas during movement.
-
Graphical Visualization:
- Grid and terrains are visually represented using colors:
- Walls: Light gray.
- Sea: Light blue.
- Food: Magenta.
- Cat's movement is animated, leaving a trail behind.
- Grid and terrains are visually represented using colors:
-
Customizable Steps:
- The number of simulation steps can be adjusted to observe the cat's behavior over time.
-
Output:
- Saves a snapshot of the simulation as an image (
output_figure.png
).
- Saves a snapshot of the simulation as an image (
- Represents the cat's position, color, and food consumption count.
- Key methods:
draw(String direction)
: Moves and redraws the cat based on the specified direction.getX()
,getY()
: Get the cat's current coordinates.setFoodCount(int foodCount)
: Updates the food consumption count.
- Handles:
- Loading the world matrix from
world.txt
. - Visualizing the world using
StdDraw
. - Managing the cat's movement and interaction with the world.
- Loading the world matrix from
- The
world.txt
file represents the 40x40 grid as a matrix with values separated by semicolons (;
). - Example:
0;0;0;1;1;2;3;0;0;0;... 0;1;0;1;2;3;0;0;0;1;... ...
- The cat moves randomly in one of four directions:
- Up, Down, Left, or Right.
- Movement conditions:
- The cat avoids walls and seas.
- The cat eats food when encountered, increments its food counter, and clears the cell.
- The grid and terrains are drawn using
StdDraw
:- Each cell is represented as a square.
- The cat is drawn as a circle in orange.
- Movement is animated with color updates for the trail.
- Java Development Kit (JDK): Version 8 or higher.
- StdDraw Library: Ensure the
StdDraw
graphics library is included in the project. You can download it from Princeton StdDraw Library.
-
Clone the Repository:
git clone https://github.com/elifpulukcu/Cat-Simulation.git
-
Navigate to the Project Directory:
cd Cat-Simulation
-
Compile the Java Files:
javac *.java
-
Run the Application:
java Main
-
Output:
- A graphical simulation window will appear showing the cat's movement in the grid world.
- The program generates an output image file:
output_figure.png
.
-
Number of Steps:
- Change the
stepCount
variable in theMain
class to adjust the simulation duration. - Example:
int stepCount = 10000; // Simulates 10,000 steps
- Change the
-
World File:
- Modify the
world.txt
file to customize the grid's layout and initial content. - Ensure the file adheres to the 40x40 matrix format with values separated by semicolons.
- Modify the
-
Cat's Initial Position:
- Modify the cat's starting coordinates in the
Main
class:Cat cat = new Cat(30, 20, StdDraw.ORANGE); // Change (30, 20) to desired coordinates
- Modify the cat's starting coordinates in the
- The simulation is restricted to a 40x40 grid.
- Movement decisions are entirely random and do not consider optimization or pathfinding.
- The cat cannot revisit cells where food has already been consumed.
This project is licensed under the MIT License. See the LICENSE
file for details.
Developed by Elif Pulukçu.