Board module

class Board.Board(matrix=None, winner=None)

Bases: object

This class represents the actual connect4 board the game is played on.

Parameters
  • matrix (ndarray, optional) – ndarray of the current board representation, defaults to None

  • winner (int, optional) – player who won the game, defaults to None

Attributes
  • ROW_COUNT (int)

    number of rows

  • COL_COUNT (int)

    number of columns

  • winner (int)

    player who won the game, defaults to None

duplicate()

A function to create a copy of the Board object itself.

Returns

Duplicate board instance

Return type

Board

get_valid_positions()

A function to return a list of valid columns positions on board.

Returns

list of valid columns on the board

Return type

list of int values

isValidMove(col)

A function to return the next valid row of a given column.

Parameters

col (int) – column position of position on board

Returns

row containing valid move for col, None if no valid move exists

Return type

int or None

makeMove(col, playerValue)

A function to make a move on the board in the given column for the given player value.

Parameters
  • col (int) – column position of position on board

  • playerValue (int: 1 or 2) – value of player, used for coloring pieces

Raises

ValueError: if playerValue is invalid

Returns

Duplicate board with move made, None if no move can be made

Return type

Board or None

neighbors(point, position)

Return array of points located next to (a.k.a. ‘neighboring’) point.

Parameters
  • point ((int,int) tuple) – valid (row,col) position on board

  • position (int) – value specifiying direction of neighbor

Raises

ValueError: if point does not exist on Board ValueError: if position is invalid

Returns

List of valid points neighboring point, None if neighbor does not exist

Return type

list or None

score_board(playerValue)

A function to score the board for a given player.

Parameters

playerValue (int: 1 or 2) – value of player, used for coloring pieces

Returns

score of board for given playerValue

Return type

int

score_neighbors(neighbors, playerValue)

A function to score a list of 4 neighboring points.

Parameters
  • neighbors (list) – 4 points in a row

  • playerValue (int: 1 or 2) – value of player, used for coloring pieces

Returns

score for the given points

Return type

int

winstate(point)

A function to check if the move made resulted in a winning state.

Parameters

point ((int,int) tuple) – valid (row,col) position on board

Returns

final point (row,col) of winning streak, None if no win state is achieved

Return type

(int,int) tuple or None