Package cpsc2150.extendedConnectX.models

# Class GameBoardMem

java.lang.Object
cpsc2150.extendedConnectX.models.AbsGameBoard
cpsc2150.extendedConnectX.models.GameBoardMem

All Implemented Interfaces:
  `IGameBoard`

public class GameBoardMem extends AbsGameBoard implements IGameBoard
Class that represents an entire game board for the ExtendedConnectX game using a more memory-efficient map instead of an array.

This class includes methods to place tokens, check token locations, and determine whether or not the game has been won or is over.

Invariant:
  board.containsKey(' ') = false AND MIN_ROWS `<=` numRows `<` MAX_ROWS AND MIN_COLUMNS `<=` numColumns `<` MAX_COLUMNS MIN_NUM_TO_WIN `<=` numToWin `<=` MAX_NUM_TO_WIN

- ## Field Summary

  ### Fields inherited from interface cpsc2150.extendedConnectX.models.IGameBoard

  `MAX_COLUMNS, MAX_NUM_TO_WIN, MAX_ROWS, MIN_COLUMNS, MIN_NUM_TO_WIN, MIN_ROWS`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `GameBoardMem(int rows, int columns, int win)`
  Constructor for GameBoardMem that takes parameters for the number of rows, the number of columns, and the number needed to win, and initializes a HashMap for the game board.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int`
  `getNumColumns()`
  Returns the number of columns in the GameBoard.
  `int`
  `getNumRows()`
  Returns the number of rows in the GameBoard.
  `int`
  `getNumToWin()`
  Returns the number of tokens in a row needed to win the game.
  `boolean`
  `isPlayerAtPos(BoardPosition pos, char player)`
  This function returns true if the player is at pos; otherwise, it returns false.
  `void`
  `placeToken(char p, int c)`
  Function that places the character p in column c.
  `char`
  `whatsAtPos(BoardPosition pos)`
  Function that returns what is in the GameBoard at position pos.

  ### Methods inherited from class cpsc2150.extendedConnectX.models.AbsGameBoard

  `toString`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface cpsc2150.extendedConnectX.models.IGameBoard

  `checkDiagWin, checkForWin, checkHorizWin, checkIfFree, checkTie, checkVertWin`

- ## Constructor Details

  - ### GameBoardMem

    public GameBoardMem(int rows, int columns, int win)
    Constructor for GameBoardMem that takes parameters for the number of rows, the number of columns, and the number needed to win, and initializes a HashMap for the game board.
    Parameters:
      `rows` - Number of rows
      `columns` - Number of columns
      `win` - Number needed to win
    Pre:
      MIN_ROWS `<=` rows `<` MAX_ROWS AND MIN_COLUMNS `<=` columns `<` MAX_COLUMNS AND MIN_NUM_TO_WIN `<=` win `<=` MAX_NUM_TO_WIN AND win `<=` rows AND win `<=` columns
    Post:
      board = [ empty HashMap with key type Character and value type BoardPosition ] AND numRows = rows AND numColumns = columns AND numToWin = win

- ## Method Details

  - ### placeToken

    public void placeToken(char p, int c)
    Description copied from interface: `IGameBoard`
    Function that places the character p in column c. The token will be placed in the lowest available row in column c.
    Specified by:
      `placeToken` in interface `IGameBoard`
    Parameters:
      `p` - Character to be placed (representing player token)
      `c` - Column to place token in

  - ### whatsAtPos

    public char whatsAtPos(BoardPosition pos)
    Description copied from interface: `IGameBoard`
    Function that returns what is in the GameBoard at position pos. If no marker is there, it returns a blank space char.
    Specified by:
      `whatsAtPos` in interface `IGameBoard`
    Parameters:
      `pos` - BoardPosition pair of row and column
    Returns:
      Character representing player token or space

  - ### isPlayerAtPos

    public boolean isPlayerAtPos(BoardPosition pos, char player)
    This function returns true if the player is at pos; otherwise, it returns false.
    Specified by:
      `isPlayerAtPos` in interface `IGameBoard`
    Parameters:
      `pos` - BoardPosition pair of row and column
      `player` - Player to check with
    Returns:
      Whether a player's token is at a position
    Pre:
      (0 `>=` pos.row `<` numRows AND 0 `>=` pos.column `<` numColumns) AND player != ' '
    Post:
      isPlayerAtPos = true iff (board.containsKey(player) = true AND board.get(player).contains(pos) = true) AND board = #board isPlayerAtPos = false iff (board.containsKey(player) = false) AND board = #board isPlayerAtPos = false iff (board.containsKey(player) = true AND board.get(player).contains(pos) = false) AND board = #board

  - ### getNumRows

    public int getNumRows()
    Description copied from interface: `IGameBoard`
    Returns the number of rows in the GameBoard.
    Specified by:
      `getNumRows` in interface `IGameBoard`
    Returns:
      The number of rows

  - ### getNumColumns

    public int getNumColumns()
    Description copied from interface: `IGameBoard`
    Returns the number of columns in the GameBoard.
    Specified by:
      `getNumColumns` in interface `IGameBoard`
    Returns:
      The number of columns

  - ### getNumToWin

    public int getNumToWin()
    Description copied from interface: `IGameBoard`
    Returns the number of tokens in a row needed to win the game.
    Specified by:
      `getNumToWin` in interface `IGameBoard`
    Returns:
      The number of tokens