Package edu.ntnu.idi.idatt.view.util
Class ViewUtils
java.lang.Object
edu.ntnu.idi.idatt.view.util.ViewUtils
ViewUtils.
A utility class providing static helper methods for various view-related operations. This includes coordinate transformations between board-specific coordinate systems and screen coordinates, as well as other common calculations useful for rendering game boards and their elements.
This class is not meant to be instantiated; all its methods are static.
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
calculateTileId
(int row, int col, int columns) Calculates the tile ID for a given row and column on a board with a "snake" numbering pattern.static double[]
ladderBoardToScreenCoordinates
(int[] coordinates, LadderGameBoard board, double boardWidth, double boardHeight) Converts ladder board coordinates (row, column) to screen coordinates (x, y).static double[]
ludoBoardToScreenCoordinates
(int[] coordinates, LudoGameBoard board, double boardWidth, double boardHeight) Converts Ludo board coordinates (row, column) to screen coordinates (x, y).static int
randomPortalDestination
(int portalId, int tiles, List<Integer> occupiedTiles) Generates a random destination tile ID for a portal (e.g., a snake or ladder).
-
Method Details
-
ladderBoardToScreenCoordinates
public static double[] ladderBoardToScreenCoordinates(int[] coordinates, LadderGameBoard board, double boardWidth, double boardHeight) Converts ladder board coordinates (row, column) to screen coordinates (x, y). The ladder board typically uses a coordinate system with the origin (0,0) at the bottom-left, while the screen's origin (0,0) is at the top-left. This method performs the necessary transformation.- Parameters:
coordinates
- An integer array[row, col]
representing the coordinates in the ladder board's system.board
- TheLadderGameBoard
instance, used to get total rows and columns.boardWidth
- The visual width of the board as displayed on the screen.boardHeight
- The visual height of the board as displayed on the screen.- Returns:
- A double array
[x, y]
representing the corresponding coordinates in the screen's system.
-
ludoBoardToScreenCoordinates
public static double[] ludoBoardToScreenCoordinates(int[] coordinates, LudoGameBoard board, double boardWidth, double boardHeight) Converts Ludo board coordinates (row, column) to screen coordinates (x, y). For Ludo, both the board coordinate system and the screen coordinate system typically have their origin (0,0) at the top-left, so this is a direct scaling.- Parameters:
coordinates
- An integer array[row, col]
representing the coordinates in the Ludo board's system.board
- TheLudoGameBoard
instance, used to get the board size.boardWidth
- The visual width of the board as displayed on the screen.boardHeight
- The visual height of the board as displayed on the screen.- Returns:
- A double array
[x, y]
representing the corresponding coordinates in the screen's system.
-
calculateTileId
public static int calculateTileId(int row, int col, int columns) Calculates the tile ID for a given row and column on a board with a "snake" numbering pattern. The pattern starts from tile 1 at the bottom-left (row 0, col 0), proceeds right on even rows (0, 2, ...), and left on odd rows (1, 3, ...).- Parameters:
row
- The row number (0-indexed from the bottom).col
- The column number (0-indexed from the left).columns
- The total number of columns in the grid.- Returns:
- The calculated tile ID (1-indexed).
-
randomPortalDestination
Generates a random destination tile ID for a portal (e.g., a snake or ladder). The destination will be different from the portal's own ID, not already occupied by another portal, and not the very last tile of the game.- Parameters:
portalId
- The ID of the tile where the portal starts.tiles
- The total number of tiles on the board.occupiedTiles
- A list of tile IDs that are already occupied by other portals (either as starts or ends).- Returns:
- A randomly selected, valid tile ID for the portal's destination.
-