Post

Created by @adamvaughn
 at November 6th 2023, 2:14:50 am.

Post 4: Exploring GridWorld Classes and Methods

In this post, we will explore the various classes and methods provided by GridWorld, such as Actor, Grid, and Location. These classes and methods serve as the building blocks for creating custom simulations and solving programming challenges in the GridWorld environment.

Actor class:

The Actor class in GridWorld represents objects that can be placed on the grid. An Actor can be anything from a person to a vehicle or even an animal. It is the parent class for a variety of specialized actor classes in GridWorld.

To create an Actor object, we use the following syntax:

Actor myActor = new Actor();

Grid class:

The Grid class in GridWorld represents the two-dimensional grid on which the actors are placed. It provides methods to manipulate and access different locations on the grid.

To create a Grid object, we use the following syntax:

Grid<Actor> myGrid = new BoundedGrid<>(rowCount, colCount);

Here, rowCount and colCount represent the number of rows and columns in the grid, respectively.

Location class:

The Location class in GridWorld represents a specific position on the grid. It is used to specify the location of an actor or to access actors at specific locations.

To create a Location object, we use the following syntax:

Location myLocation = new Location(row, col);

Here, row and col represent the row and column indices of the location, respectively.

Methods:

GridWorld provides various methods that can be used to interact with Actor, Grid, and Location objects. Here are some commonly used methods:

  1. getOccupiedLocations() - Returns a list of all locations on the grid that are occupied by actors.

  2. getEmptyLocations() - Returns a list of all locations on the grid that are currently empty.

  3. get() - Returns the actor at the specified location.

  4. put() - Places the given actor at the specified location.

  5. isValid() - Returns true if the given location is valid (i.e., within the bounds of the grid), otherwise returns false.

Example:

Let's look at an example that demonstrates the usage of GridWorld classes and methods:

Grid<Actor> grid = new BoundedGrid<>(5, 5);
Actor actor = new Actor();
Location location = new Location(2, 3);
grid.put(location, actor);

ArrayList<Location> occupiedLocations = grid.getOccupiedLocations();
System.out.println("Occupied locations: " + occupiedLocations);

Location newLocation = new Location(1, 2);
Actor movedActor = grid.get(location);
grid.remove(location);
grid.put(newLocation, movedActor);

Actor actorAtNewLocation = grid.get(newLocation);
System.out.println("Actor at new location: " + actorAtNewLocation);

In this example, we create a 5x5 grid, place an actor at location (2, 3), and then retrieve and print the occupied locations. We then move the actor to a new location (1, 2) and retrieve the actor at the new location.

This is just a basic example, but it demonstrates how GridWorld classes and methods can be used to manipulate actors and locations on the grid.

GridWorld's classes and methods provide a powerful framework for practicing and applying object-oriented programming concepts. By understanding and utilizing these classes and methods effectively, students can solve complex programming challenges and simulate real-world scenarios.