Class ConnectN


  • public class ConnectN
    extends java.lang.Object
    A class that implements a Connect4-like game.

    ConnectN is a tile-based game played on a grid. Like Connect4, players try to get a run of tiles of a given length (N). However, unlike Connect4 ConnectN does not check diagonal runs, although you can add this feature if you like.

    This ConnectN class is also not responsible for tracking players turns. That could be done by a separate class, allowing for game variations in which players can sometimes take more than one turn in a row. Other variations allow more than two players. In any case, the ConnectN class only monitors game play to determine when the game has ended and returns the winning player.

    See Also:
    MP3 Documentation
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_HEIGHT
      Maximum board height is 16.
      static int MAX_WIDTH
      Maximum board width is 16.
      static int MIN_HEIGHT
      Minimum board height is 6.
      static int MIN_N
      Minimum board N value is 4.
      static int MIN_WIDTH
      Minimum board width is 6.
    • Constructor Summary

      Constructors 
      Constructor Description
      ConnectN()
      Create a new ConnectN board with uninitialized width, height, and N value.
      ConnectN(int setWidth, int setHeight)
      Create a new ConnectN board with given width and height and uninitialized N value.
      ConnectN(int setWidth, int setHeight, int setN)
      Creates a new ConnectN board with a given width, height, and N value.
      ConnectN(ConnectN otherBoard)
      Create a new ConnectN board with dimensions and N value copied from another board.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean compareBoards(ConnectN... boards)
      Compare any number of ConnectN boards.
      static boolean compareBoards(ConnectN firstBoard, ConnectN secondBoard)
      Compare two ConnectN boards.
      static ConnectN create(int width, int height, int n)
      Class method to create a new ConnectN board.
      static ConnectN[] createMany(int number, int width, int height, int n)
      Creates multiple new ConnectN instances.
      boolean equals(java.lang.Object obj)
      Define equality for the ConnectN class.
      Player[][] getBoard()
      Return a copy of the board.
      Player getBoardAt(int getX, int getY)
      Get the player at a specific board position.
      int getHeight()
      Get the current board height.
      int getID()
      Get the current board's id.
      int getN()
      Get the current board N value.
      static int getTotalGames()
      Return the total number of games that have been created.
      int getWidth()
      Get the current board width.
      Player getWinner()
      Return the winner of the game, or null if the game has not ended.
      int hashCode()
      Define the hash code for the ConnectN class.
      boolean setBoardAt(Player player, int setX)
      Drop a tile in a particular column.
      boolean setBoardAt(Player player, int setX, int setY)
      Set the board at a specific position.
      boolean setHeight(int setHeight)
      Attempt to set the board height.
      boolean setN(int newN)
      Attempt to set the current board N value.
      boolean setWidth(int setWidth)
      Attempt to set the board width.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ConnectN

        public ConnectN(int setWidth,
                        int setHeight,
                        int setN)
        Creates a new ConnectN board with a given width, height, and N value.

        Note that this method should not reject invalid values. Constructors must return a new object, or throw an exception, so there is no way for you to reject bad parameters yet. Instead, attempts to set the width, height, or N value to invalid value should lead to them being uninitialized.

        For example, new ConnectN(1, 10, 4) should return a ConnectN game with width=0, height=10, and N=0, since 1 is an invalid width (too small) and N cannot be set until the width is defined.

        Each call to any ConnectN constructor should also track the total number of games that have been created, which can be retrieved by getTotalGames(). Each board should also receive a unique ID equal to the number of games minus 1. So the first board gets 0, etc. This should be returned by getID().

        Parameters:
        setWidth - the width for the new ConnectN board
        setHeight - the height for the new ConnectN board
        setN - the N value for the new ConnectN board
      • ConnectN

        public ConnectN()
        Create a new ConnectN board with uninitialized width, height, and N value.
      • ConnectN

        public ConnectN(int setWidth,
                        int setHeight)
        Create a new ConnectN board with given width and height and uninitialized N value.

        See the notes for the width, height, N ConnectN constructor.

        Parameters:
        setWidth - the width for the new ConnectN board
        setHeight - the height for the new ConnectN board
      • ConnectN

        public ConnectN(ConnectN otherBoard)
        Create a new ConnectN board with dimensions and N value copied from another board.

        This is a so-called copy constructor. It takes another object of the same type (in this case, a ConnectN board) and initializes a new object using values from the existing object. In this case, you should use the width, height, and N value from the passed board. You should not copy any other features of the other board: it's title, it's board state, etc. You should also not copy the board ID. This creates a copy, not the same board.

        Parameters:
        otherBoard - the ConnectN board to copy width, height, and N from
    • Method Detail

      • getTotalGames

        public static int getTotalGames()
        Return the total number of games that have been created.
        Returns:
        the total number of games that have been created.
      • getWidth

        public int getWidth()
        Get the current board width.
        Returns:
        the current board width
      • setWidth

        public boolean setWidth(int setWidth)
        Attempt to set the board width.

        Fails if the width is invalid, or if the game has already started. If the new width would cause the current N value to become invalid, setWidth should reset the current N value to zero.

        Parameters:
        setWidth - the new width to set
        Returns:
        true if the width was set successfully, false on error
      • getHeight

        public int getHeight()
        Get the current board height.
        Returns:
        the current board height
      • setHeight

        public boolean setHeight(int setHeight)
        Attempt to set the board height.

        Fails if the height is invalid, or if the game has already started. If the new height would cause the current N value to become invalid, setHeight should reset the current N value to zero.

        Parameters:
        setHeight - the new height to set
        Returns:
        true if the height was set successfully, false on error
      • getN

        public int getN()
        Get the current board N value.
        Returns:
        the current board N value
      • setN

        public boolean setN(int newN)
        Attempt to set the current board N value.

        Note that:

        • N cannot be set after the game has started
        • N cannot be set before the width or the height
        • N cannot be less than 4
        • N can be at most 1 less than the maximum of the width and height

        Games with large N values relative to their dimension are fairly uninteresting, since it is trivial for a player to block their opponent and cause a draw. But you should only enforce the constraint that the N value is at most 1 less than the maximum of the width and height. So on a 6x10 board, the minimum N value is 4 and the maximum is 9. On a 10x8 board, the minimum is 4 and the maximum is 9.

        Setting N should never affect the width or the height.

        Parameters:
        newN - the new N
        Returns:
        true, if successful
      • getID

        public int getID()
        Get the current board's id.
        Returns:
        the current board's id
      • setBoardAt

        public boolean setBoardAt(Player player,
                                  int setX,
                                  int setY)
        Set the board at a specific position.

        Allows a player to attempt to place a tile at a specific location on the board. If the move is successful, the board should track that this player has played at this location so that it can determine a winner and prevent future invalid moves.

        A move should fail and return false if:

        • any board parameters remain uninitialized, including width, height, and N
        • the player is invalid
        • the position is invalid for this board
        • the game has already ended

        This function also needs to enforce the rules of ConnectN. A tile cannot be played at a particular location if there are empty squares below it. Put another way, a tile can only be placed on top of a stack of existing tiles. If the requested location is invalid, you should return false and no tile should be added to the board.

        If a given play results in the game ending, future plays should fail and getWinner() should return the player that won.

        Note that the first successful call to setBoardAt represents the start of game.

        Parameters:
        player - the player attempting the move
        setX - the X coordinate that they are trying to place a tile at
        setY - the Y coordinate that they are trying to place a tile at
        Returns:
        true if the move succeeds, false on error
      • setBoardAt

        public boolean setBoardAt(Player player,
                                  int setX)
        Drop a tile in a particular column.

        This method is similar to setBoardAt(player, x, y), but it does not specify a Y coordinate. Instead, it allows a player to try to drop a tile on the stack with the given X value.

        This method should fail for the same reasons as setBoardAt(player, x, y). It should also fail if this stack is full and cannot accept any additional tiles. It should also ensure that getWinner() returns the correct value after every play.

        Note that the first successful call to setBoardAt represents the start of game.

        Parameters:
        player - the player attempting the move
        setX - the X coordinate for the stack that they are trying to drop a tile in
        Returns:
        true if the move succeeds, false on error
      • getBoardAt

        public Player getBoardAt(int getX,
                                 int getY)
        Get the player at a specific board position.

        Should return null if the board position is invalid, if the game has not started, or if nobody has played yet at that position. Otherwise returns the player whose tile is at that position.

        Parameters:
        getX - the X coordinate to get the player at
        getY - the Y coordinate to get the player at
        Returns:
        the player whose tile is at that position, or null or error or if nobody has played at that position
      • getBoard

        public Player[][] getBoard()
        Return a copy of the board.

        Once the width and height are set, this function should not return null. Until then, it should return null.

        Note that this function should not expose the private board instance variable. Any changes to the board or players returned should not affect the state maintained by the class.

        Returns:
        a copy of the current board
      • getWinner

        public Player getWinner()
        Return the winner of the game, or null if the game has not ended.
        Returns:
        the winner of the game, or null if the game has not ended
      • create

        public static ConnectN create(int width,
                                      int height,
                                      int n)
        Class method to create a new ConnectN board.

        Unlike the class constructor, static methods can return null on failure. Sometimes these methods are referred to as static factory methods.

        This method should return null if its arguments are invalid. Otherwise, it should return a new ConnectN instance.

        Parameters:
        width - the width of the new ConnectN instance to create
        height - the height of the new ConnectN instance to create
        n - the n value of the new ConnectN instance to create
        Returns:
        the new ConnectN instance, or null if the parameters are invalid
      • createMany

        public static ConnectN[] createMany(int number,
                                            int width,
                                            int height,
                                            int n)
        Creates multiple new ConnectN instances.

        Like create(), createMany should return null if the parameters are invalid. Otherwise, it should return an array of newly-created ConnectN instances.

        Parameters:
        number - the number of new ConnectN instances to create
        width - the width of the new ConnectN instance to create
        height - the height of the new ConnectN instance to create
        n - the n value of the new ConnectN instance to create
        Returns:
        an array of new ConnectN instances, or null if the parameters are invalid or if asked to create zero boards
      • compareBoards

        public static boolean compareBoards(ConnectN firstBoard,
                                            ConnectN secondBoard)
        Compare two ConnectN boards.

        This method should compare two ConnectN boards. Two boards are equal if:

        • they have the same dimensions
        • they have the same n value
        • they have tiles by the same players in the same position

        Note that this is different from the equals method for the ConnectN class, which should test for the same ID. You can also use the Player.equals method to compare two players.

        Parameters:
        firstBoard - the first ConnectN board to compare
        secondBoard - the second ConnectN board to compare
        Returns:
        true if the boards are the same, false otherwise
      • compareBoards

        public static boolean compareBoards(ConnectN... boards)
        Compare any number of ConnectN boards.

        This methods takes a variadic number of arguments. It should return true if all the boards are the same. See the notes on compareBoards(first, second) for a definition of board equality.

        Parameters:
        boards - the array of ConnectN boards to compare
        Returns:
        true if all passed boards are the same, false otherwise
      • hashCode

        public final int hashCode()
        Define the hash code for the ConnectN class.

        This method should only use the id field of the instance. Note that IntelliJ can auto-generate this and equals.

        Overrides:
        hashCode in class java.lang.Object
        See Also:
        Object.hashCode()
      • equals

        public boolean equals(java.lang.Object obj)
        Define equality for the ConnectN class.

        This method should only use the id field of the instance. Note that IntelliJ can auto-generate this and hashCode().

        Overrides:
        equals in class java.lang.Object
        See Also:
        Object.equals(java.lang.Object)