public class ConnectN
extends java.lang.Object
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.
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.
|
java.lang.String |
title |
Public game title.
|
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.
|
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.
|
public static final int MIN_WIDTH
public static final int MAX_WIDTH
public static final int MIN_HEIGHT
public static final int MAX_HEIGHT
public static final int MIN_N
public java.lang.String title
public ConnectN(int setWidth, int setHeight, int setN)
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()
.
setWidth
- the width for the new ConnectN boardsetHeight
- the height for the new ConnectN boardsetN
- the N value for the new ConnectN boardpublic ConnectN()
public ConnectN(int setWidth, int setHeight)
See the notes for the width, height, N ConnectN constructor.
setWidth
- the width for the new ConnectN boardsetHeight
- the height for the new ConnectN boardpublic ConnectN(ConnectN otherBoard)
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.
otherBoard
- the ConnectN board to copy width, height, and N frompublic static int getTotalGames()
public int getWidth()
public boolean setWidth(int setWidth)
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.
setWidth
- the new width to setpublic int getHeight()
public boolean setHeight(int setHeight)
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.
setHeight
- the new height to setpublic int getN()
public boolean setN(int newN)
Note that:
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.
newN
- the new Npublic int getID()
public boolean setBoardAt(Player player, int setX, int setY)
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:
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.
player
- the player attempting the movesetX
- the X coordinate that they are trying to place a tile atsetY
- the Y coordinate that they are trying to place a tile atpublic boolean setBoardAt(Player player, int setX)
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.
player
- the player attempting the movesetX
- the X coordinate for the stack that they are trying to drop a tile inpublic Player getBoardAt(int getX, int getY)
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.
getX
- the X coordinate to get the player atgetY
- the Y coordinate to get the player atpublic Player[][] getBoard()
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.
public Player getWinner()
public static ConnectN create(int width, int height, int n)
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.
width
- the width of the new ConnectN instance to createheight
- the height of the new ConnectN instance to createn
- the n value of the new ConnectN instance to createpublic static ConnectN[] createMany(int number, int width, int height, int n)
Like create()
, createMany should return null if the parameters
are invalid. Otherwise, it should return an array of newly-created ConnectN instances.
number
- the number of new ConnectN instances to createwidth
- the width of the new ConnectN instance to createheight
- the height of the new ConnectN instance to createn
- the n value of the new ConnectN instance to createpublic static boolean compareBoards(ConnectN firstBoard, ConnectN secondBoard)
This method should compare two ConnectN boards. Two boards are equal if:
Note that this is different from the {# link equals(Object)
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.
firstBoard
- the first ConnectN board to comparesecondBoard
- the second ConnectN board to comparepublic static boolean compareBoards(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.
boards
- the array of ConnectN boards to comparepublic final int hashCode()
This method should only use the id field of the instance. Note that Eclipse can auto-generate
this and equals
.
hashCode
in class java.lang.Object
Object.hashCode()
public boolean equals(java.lang.Object obj)
This method should only use the id field of the instance. Note that Eclipse can auto-generate
this and hashCode()
.
equals
in class java.lang.Object
Object.equals(java.lang.Object)