Connect4

Namespace

Connect4

Description:
  • Connect4.js is a module to model and play "Connect Four" and related games. https://en.wikipedia.org/wiki/Connect_Four

Source:
Version:
  • 2021/22
Author:
  • A. Freddie Page

Members

(static) token_strings :Array.<string>

Description:
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}

Description:
  • Create a new empty board. Optionally with a specified width and height, otherwise returns a standard 7 wide, 6 high board.

Source:
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>}

Description:
  • Returns an array of which column numbers are free to place a token in.

Source:
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}

Description:
  • Returns if a game has ended, either because a player has won or the board is full.

Source:
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}

Description:
  • 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.

Source:
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}

Description:
  • Returns which player is the next to make a ply for a board.

Source:
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}

Description:
  • 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.

Source:
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>}

Description:
  • Returns the size of a board as an array of [width, height].

Source:
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}

Description:
  • Returns a string representation of a board. I.e. for printing to the console rather than serialisation.

Source:
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}

Description:
  • Returns a Connect4.to_string like function, mapping tokens to provided string representations.

Source:
Parameters:
Name Type Description
token_strings Array.<string>

Strings to represent tokens as. Examples are given in Connect4.token_strings

Returns:

The string representation.

Type
function

(static) winning_slots(board) → {Array.<Array.<number>>}

Description:
  • 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.

Source:
Parameters:
Name Type Description
board Connect4.board

The board to analyse.

Returns:

An array of coordinates.

Type
Array.<Array.<number>>

Type Definitions

Board

Description:
  • 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)

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:

Token

Description:
  • A token is a coloured disk that players place in the grid.

Source:

A token is a coloured disk that players place in the grid.

Type:
  • 1 | 2

Token_or_empty

Description:
  • Either a token or an empty position.

Source:

Either a token or an empty position.

Type: