- Source:
- Version:
- 2021/22
Connect4.js is a module to model and play "Connect Four" and related games. https://en.wikipedia.org/wiki/Connect_Four
Members
(static) token_strings :Array.<string>
- Source:
Properties:
Name | Type | Description |
---|---|---|
default |
Array.<string> | ["0", "1", "2"] Displays tokens by their index. |
disks |
Array.<string> | ["⚫", "🔴", "🟡"] Displays tokens as coloured disks. |
zombies |
Array.<string> | ["🟫", "🚧", "🧟"] Displays tokens as zombies and barricades. |
A set of template token strings for Connect4.to_string_with_tokens
.
Type:
- Array.<string>
Methods
(static) empty_board(widthopt, heightopt) → {Connect4.Board}
- Source:
Create a new empty board. Optionally with a specified width and height, otherwise returns a standard 7 wide, 6 high board.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
width |
number |
<optional> |
7
|
The width of the new board. |
height |
number |
<optional> |
6
|
The height of the new board. |
Returns:
An empty board for starting a game.
- Type
- Connect4.Board
(static) free_columns(board) → {Array.<number>}
- Source:
Returns an array of which column numbers are free to place a token in.
Parameters:
Name | Type | Description |
---|---|---|
board |
Connect4.Board | The board to check for free columns. |
Returns:
An array of column indices of free columns.
- Type
- Array.<number>
(static) is_ended(board) → {boolean}
- Source:
Returns if a game has ended, either because a player has won or the board is full.
Parameters:
Name | Type | Description |
---|---|---|
board |
Connect4.Board | The board to test. |
Returns:
Whether the game has ended.
- Type
- boolean
(static) is_winning_for_player(player, board) → {boolean}
- Source:
Returns if the board is in a winning state for any player. A board is won for a player if that player has four tokens in a row, either horizontally, vertically, or diagonally, at any position on the board.
Parameters:
Name | Type | Description |
---|---|---|
player |
1 | 2 | Which player to check has a win. |
board |
Connect4.Board | The board to check. |
Returns:
Returns if the board is in a winning state for the specified player.
- Type
- boolean
(static) player_to_ply(board) → {1|2}
- Source:
Returns which player is the next to make a ply for a board.
Parameters:
Name | Type | Description |
---|---|---|
board |
Connect4.Board | The board to check. |
Returns:
The player next to play.
- Type
- 1 | 2
(static) ply(token, column_index, board) → {Connect4.Board|undefined}
- Source:
A ply is one turn taken by one of the players. Return a new board after a player places a token in a specified column.
Parameters:
Name | Type | Description |
---|---|---|
token |
Connect4.Token | The token to be added to the board. |
column_index |
number | The column the player adds the token to |
board |
Connect4.Board | The board state that the ply is made on. |
Returns:
If the ply was legal,
return the new board, otherwise return undefined
.
- Type
- Connect4.Board | undefined
(static) size(board) → {Array.<number>}
- Source:
Returns the size of a board as an array of [width, height].
Parameters:
Name | Type | Description |
---|---|---|
board |
Connect4.Board | The board to check the size of. |
Returns:
The width and height of the board, [width, height].
- Type
- Array.<number>
(static) to_string(board) → {string}
- Source:
Returns a string representation of a board. I.e. for printing to the console rather than serialisation.
Parameters:
Name | Type | Description |
---|---|---|
board |
Connect4.Board | The board to represent. |
Returns:
The string representation.
- Type
- string
(static) to_string_with_tokens(token_strings) → {function}
- Source:
Returns a Connect4.to_string
like function,
mapping tokens to provided string representations.
Parameters:
Name | Type | Description |
---|---|---|
token_strings |
Array.<string> | Strings to represent tokens as. Examples are given in
|
Returns:
The string representation.
- Type
- function
(static) winning_slots(board) → {Array.<Array.<number>>}
- Source:
For a board that is won, returns the coordinates (col, row) of slots contributing to the win. Will return more than four coordinates if there is a win along multiple axes, or a longer streak than four in a row. Returns the empty array if the board is not won.
Parameters:
Name | Type | Description |
---|---|---|
board |
Connect4.board | The board to analyse. |
Returns:
An array of coordinates.
- Type
- Array.<Array.<number>>
Type Definitions
Board
- Source:
A Board is an rectangular grid that tokens can be placed into one at a time. Tokens fill up empty positions from the bottom of a column upwards. It is implemented as an array of columns (rather than rows) of tokens (or empty positions)
Type:
- Array.<Array.<Connect4.Token_or_empty>>
Token
- Source:
A token is a coloured disk that players place in the grid.
Type:
- 1 | 2
Token_or_empty
- Source:
Either a token or an empty position.
Type:
- Connect4.Token | 0