Grid - UQcsse3200/2024-studio-2 GitHub Wiki

Overview

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.

Class: Grid

Package

package com.csse3200.game.components.minigame;

Description

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).

Fields

  • width

    • Type: int
    • Description: width of the grid, representing the number of cells along the x-axis.
  • height

    • Type: int
    • Description: height of the grid, representing the number of cells along the y-axis.
  • cells

    • Type: GridCell[][]
    • Description: A two-dimensional array representing the individual cells that make up the grid.

Constructor

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).

Methods

private void initialiseGrid()

  • Description: Initialises the grid by creating a GridCell at each position.

public GridCell getCell(int x, int y)

  • 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.

public boolean isOccupied(int x, int y)

  • 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.

public void setOccupied(int x, int y, boolean occupied)

  • 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.

public int getWidth() and public int getHeight()

  • Description: Returns the width and height of the grid.
  • Returns: The width and height of the grid.