Chess Game  1.1.0
A full chess game for Unity
cgBoard Class Reference

The chess board, here moves are generated, checked for legality, performed and reverted. More...

Public Types

enum  OUTCOME { Undecided = 0, Draw = 1, BlackWin = 2, WhiteWin = 3 }
 

Public Member Functions

 cgBoard (List< sbyte > positions=null, byte boardWidth=8, byte boardHeight=8, Dictionary< string, cgMoveSet > allHypotheticalMoves=null)
 Create a new instance of the board. More...
 
void findAllMoves ()
 
void move (cgSimpleMove move)
 Perform the provided move, capturing if necessary. More...
 
bool longCastlingRights (bool white)
 Does the provided color still have its long castling rights? More...
 
bool shortCastlingRights (bool white)
 Does the provided color still have its short castling rights? More...
 
void revert ()
 Revert the last performed move. More...
 
cgBoard revertToStart ()
 Revert the board all the way to the start. More...
 
int Evaluate ()
 Evaluate the current board, adding together material values of both sides, positional values, castling values etc. More...
 
bool verifyLegality (cgSimpleMove testMove)
 Final verification, checks if the kings is attempting to perform a capture that leads to the king being taken next, which is the only illegal move findLegalMoves does not check(because it would be very intensive for the AI to search for this kind of move and also slightly redundant, as such a move leads immediately to loss of king which is the most highly valued piece on the board and thusly would be weighed as a terrible move). More...
 
List< cgSimpleMovefindLegalMoves (bool asWhite)
 Returns all legal moves for the provided color for the current board. Note: it may return a move in which the king captures an enemy piece to which the enemy can then next capture the king, use findStrictLegalMoves to avoid - however for the engine the computation to verify such a move are too costly. And since such a move immediatly leads to king capture, the AI will once it check the next moves find it to be a very very bad move. More...
 
List< cgSimpleMovefindStrictLegalMoves (bool asWhite)
 Returns all legal moves for the provided color for the current board. More...
 
string moveToString (cgSimpleMove move)
 Converts a move to a human readable string. Useful for debugging purposes. More...
 
void LoadGame (cgNotation notation)
 Generates a board matching the provided notation. More...
 
byte IndexFromCellName (string p)
 Translates a coordinate string(e3, d1 etc.) to an index(0-64). More...
 
bool isChecked (bool asWhite)
 Is the color checked? More...
 
string boardToNeatString ()
 Creates a neat human readable string of the current board. Useful for debugging purposes. More...
 
string boardToString ()
 Returns the board as not so neat string, useful for generating a unique string of current board state. More...
 
cgBoard duplicate ()
 Create a new board that is a duplication of this instance. More...
 
void setDuplicateValues (byte enpassantSquare, byte enPassantCapture, bool whiteToMove, byte whiteARook, byte whiteHRook, byte blackARook, byte blackHRook, byte whiteKing, byte blackKing, byte[] _blackRooksStart, byte[] _whiteRooksStart, byte _blackKingStart, byte _whiteKingStart, byte[] _whiteLeftCastleCrossSquares, byte[] _whiteRightCastleCrossSquares, byte[] _blackLeftCastleCrossSquares, byte[] _blackRightCastleCrossSquares, byte _whiteKingLeftCastleTo, byte _whiteKingRightCastleTo, byte _blackKingLeftCastleTo, byte _blackKingRightCastleTo, byte _whiteRookLeftCastleTo, byte _whiteRookRightCastleTo, byte _blackRookLeftCastleTo, byte _blackRookRightCastleTo)
 Setting all private vars for a new duplicate instance, to match its target. More...
 

Public Attributes

List< sbyte > squares = new List<sbyte>(64)
 The 0-64 index presentation of the board. This is where the magic happends. Below is a detailed list of what values can be expected, and what each index position corresponds to on the board. More...
 
List< cgSimpleMovemoves = new List<cgSimpleMove>()
 A list of all unreverted moves in chronological order since the start of the game. More...
 
int moveCount = 0
 Total number of moves that have been performed(regardless of reverting). More...
 
int revertCount = 0
 Total number of reverts that have been performed. More...
 
bool whiteHasCastled = false
 Has white castled? used to add value on board avaluation. More...
 
bool blackHasCastled = false
 Has black castled? used to subtract value on board avaluation. More...
 
byte illegalCellIndex = 65
 the index at which squares are out of bounds, in other words the index that exceeds the board. More...
 
byte boardWidth = 8
 Width of the board. More...
 
byte boardHeight = 8
 Height of board More...
 
string [] SquareNames
 Cell names according to index position. More...
 

Static Public Attributes

static List< sbyte > defaultStartPosition
 The default starting position. More...
 

Properties

bool whiteTurnToMove [get]
 Is it whites turn to move? More...
 

Detailed Description

The chess board, here moves are generated, checked for legality, performed and reverted.

Constructor & Destructor Documentation

◆ cgBoard()

cgBoard.cgBoard ( List< sbyte >  positions = null,
byte  boardWidth = 8,
byte  boardHeight = 8,
Dictionary< string, cgMoveSet allHypotheticalMoves = null 
)

Create a new instance of the board.

Parameters
positions

Member Function Documentation

◆ boardToNeatString()

string cgBoard.boardToNeatString ( )

Creates a neat human readable string of the current board. Useful for debugging purposes.

Returns
A neat human readable string.

◆ boardToString()

string cgBoard.boardToString ( )

Returns the board as not so neat string, useful for generating a unique string of current board state.

Returns
A not so neat string with all information about current board.

◆ duplicate()

cgBoard cgBoard.duplicate ( )

Create a new board that is a duplication of this instance.

Returns
A duplicate of this instance.

◆ Evaluate()

int cgBoard.Evaluate ( )

Evaluate the current board, adding together material values of both sides, positional values, castling values etc.

◆ findLegalMoves()

List<cgSimpleMove> cgBoard.findLegalMoves ( bool  asWhite)

Returns all legal moves for the provided color for the current board. Note: it may return a move in which the king captures an enemy piece to which the enemy can then next capture the king, use findStrictLegalMoves to avoid - however for the engine the computation to verify such a move are too costly. And since such a move immediatly leads to king capture, the AI will once it check the next moves find it to be a very very bad move.

Parameters
asWhiteMove as white?
Returns
All legal moves for provided color

◆ findStrictLegalMoves()

List<cgSimpleMove> cgBoard.findStrictLegalMoves ( bool  asWhite)

Returns all legal moves for the provided color for the current board.

Parameters
asWhiteMove as white?
Returns
All legal moves for provided color

◆ IndexFromCellName()

byte cgBoard.IndexFromCellName ( string  p)

Translates a coordinate string(e3, d1 etc.) to an index(0-64).

Parameters
p
Returns

◆ isChecked()

bool cgBoard.isChecked ( bool  asWhite)

Is the color checked?

Parameters
asWhiteShould we check if white is checked(true) or if black is checked(false)
Returns
Is the color checked?

◆ LoadGame()

void cgBoard.LoadGame ( cgNotation  notation)

Generates a board matching the provided notation.

Parameters
notationThe notation of the game to be recreated

◆ longCastlingRights()

bool cgBoard.longCastlingRights ( bool  white)

Does the provided color still have its long castling rights?

Parameters
whiteThe color whoms castling right should be checked,false=black
Returns
Does still have its long castling rights?

◆ move()

void cgBoard.move ( cgSimpleMove  move)

Perform the provided move, capturing if necessary.

Parameters
moveThe move to perform.

◆ moveToString()

string cgBoard.moveToString ( cgSimpleMove  move)

Converts a move to a human readable string. Useful for debugging purposes.

Parameters
move
Returns
A readable string for output logging.

◆ revert()

void cgBoard.revert ( )

Revert the last performed move.

◆ revertToStart()

cgBoard cgBoard.revertToStart ( )

Revert the board all the way to the start.

Returns
This board, for method chaining.

◆ setDuplicateValues()

void cgBoard.setDuplicateValues ( byte  enpassantSquare,
byte  enPassantCapture,
bool  whiteToMove,
byte  whiteARook,
byte  whiteHRook,
byte  blackARook,
byte  blackHRook,
byte  whiteKing,
byte  blackKing,
byte []  _blackRooksStart,
byte []  _whiteRooksStart,
byte  _blackKingStart,
byte  _whiteKingStart,
byte []  _whiteLeftCastleCrossSquares,
byte []  _whiteRightCastleCrossSquares,
byte []  _blackLeftCastleCrossSquares,
byte []  _blackRightCastleCrossSquares,
byte  _whiteKingLeftCastleTo,
byte  _whiteKingRightCastleTo,
byte  _blackKingLeftCastleTo,
byte  _blackKingRightCastleTo,
byte  _whiteRookLeftCastleTo,
byte  _whiteRookRightCastleTo,
byte  _blackRookLeftCastleTo,
byte  _blackRookRightCastleTo 
)

Setting all private vars for a new duplicate instance, to match its target.

◆ shortCastlingRights()

bool cgBoard.shortCastlingRights ( bool  white)

Does the provided color still have its short castling rights?

Parameters
whiteThe color whoms castling right should be checked,false=black
Returns
Does still have its short castling rights?

◆ verifyLegality()

bool cgBoard.verifyLegality ( cgSimpleMove  testMove)

Final verification, checks if the kings is attempting to perform a capture that leads to the king being taken next, which is the only illegal move findLegalMoves does not check(because it would be very intensive for the AI to search for this kind of move and also slightly redundant, as such a move leads immediately to loss of king which is the most highly valued piece on the board and thusly would be weighed as a terrible move).

Parameters
testMoveThe move to be verified
Returns
Is this an illegal king capture?

Member Data Documentation

◆ blackHasCastled

bool cgBoard.blackHasCastled = false

Has black castled? used to subtract value on board avaluation.

◆ boardHeight

byte cgBoard.boardHeight = 8

Height of board

◆ boardWidth

byte cgBoard.boardWidth = 8

Width of the board.

◆ defaultStartPosition

List<sbyte> cgBoard.defaultStartPosition
static
Initial value:
= new List<sbyte> {
-2, -3, -4, -5, -6, -4, -3, -2,
-1, -1, -1, -1, -1, -1, -1, -1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
2, 3, 4, 5, 6, 4, 3, 2
}

The default starting position.

◆ illegalCellIndex

byte cgBoard.illegalCellIndex = 65

the index at which squares are out of bounds, in other words the index that exceeds the board.

◆ moveCount

int cgBoard.moveCount = 0

Total number of moves that have been performed(regardless of reverting).

◆ moves

List<cgSimpleMove> cgBoard.moves = new List<cgSimpleMove>()

A list of all unreverted moves in chronological order since the start of the game.

0 = unoccupied square

1 = white pawn 2 = white rook 3 = white knight 4 = white bishop 5 = white queen 6 = white king

-1 = black pawn -2 = black rook -3 = black knight -4 = black bishop -5 = black queen -6 = black king

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

◆ revertCount

int cgBoard.revertCount = 0

Total number of reverts that have been performed.

◆ SquareNames

string [] cgBoard.SquareNames
Initial value:
= {
"a8","b8","c8","d8","e8","f8","g8","h8",
"a7","b7","c7","d7","e7","f7","g7","h7",
"a6","b6","c6","d6","e6","f6","g6","h6",
"a5","b5","c5","d5","e5","f5","g5","h5",
"a4","b4","c4","d4","e4","f4","g4","h4",
"a3","b3","c3","d3","e3","f3","g3","h3",
"a2","b2","c2","d2","e2","f2","g2","h2",
"a1","b1","c1","d1","e1","f1","g1","h1",
"Out ouf bounds"}

Cell names according to index position.

◆ squares

List<sbyte> cgBoard.squares = new List<sbyte>(64)

The 0-64 index presentation of the board. This is where the magic happends. Below is a detailed list of what values can be expected, and what each index position corresponds to on the board.

◆ whiteHasCastled

bool cgBoard.whiteHasCastled = false

Has white castled? used to add value on board avaluation.

Property Documentation

◆ whiteTurnToMove

bool cgBoard.whiteTurnToMove
get

Is it whites turn to move?


The documentation for this class was generated from the following file: