-
Notifications
You must be signed in to change notification settings - Fork 1
Grid
rp3002 edited this page Aug 26, 2024
·
1 revision
The Grid
class in the Snake mini-game represents the game board where the snake moves and the apples spawn. The grid is composed of individual cells, each of which can either be occupied or unoccupied. This class provides the functionality to manage and interact with the grid, such as checking occupancy and setting cell states.
package com.csse3200.game.components.minigame;
The Grid
class is responsible for creating and managing a two-dimensional grid. It tracks the occupancy status of each cell, ensuring that the game's logic (snake movement and apple spawning).
-
width
-
Type:
int
- Description: width of the grid, representing the number of cells along the x-axis.
-
Type:
-
height
-
Type:
int
- Description: height of the grid, representing the number of cells along the y-axis.
-
Type:
-
cells
-
Type:
GridCell[][]
- Description: A two-dimensional array representing the individual cells that make up the grid.
-
Type:
public Grid(int width, int height)
-
Description: Creates a new
Grid
with the specified dimensions. Each cell in the grid is initialised as unoccupied. -
Parameters:
-
width
- The width of the grid (number of cells in the x-direction). -
height
- The height of the grid (number of cells in the y-direction).
-
- Description: Initialises the grid by creating a GridCell at each position.
- Description: Gets the GridCell at the specified coordinates.
-
Parameters:
-
x
- x-coordinate of the cell. -
y
- y-coordinate of the cell.
-
- Returns: The GridCell at the specified position, or null if the coordinates are out of bounds.
- Description: Checks if the cell at the specified coordinates is occupied.
-
Parameters:
-
x
- x-coordinate of the cell. -
y
- y-coordinate of the cell.
-
-
Returns:
true
if the cell is marked as occupied,false
otherwise.
- Description: Sets the occupancy status of the cell at the specified coordinates.
-
Parameters:
-
x
- x-coordinate of the cell. -
y
- y-coordinate of the cell. -
occupied
-true
if the cell should be marked as occupied,false
otherwise.
-
- Description: Returns the width and height of the grid.
- Returns: The width and height of the grid.