Knight-Path 1.0.D018
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes

position Class Reference

#include <position.h>

List of all members.

Public Types

typedef std::vector< positionlist_t

Public Member Functions

 ~position ()
 The destructor.
 position ()
 The default constructor.
 position (char rank, char file)
 The default constructor.
 position (const position &rhs)
positionoperator= (const position &rhs)
list_t knight_moves (void) const
void write (std::ostream &os) const
bool operator== (const position &rhs) const
bool operator!= (const position &rhs) const
void plot (char board[8][8], char c) const
int distance (const position &elsewhere) const
int get_x (void) const
int get_y (void) const

Static Public Member Functions

static position read (std::istream &is)

Private Member Functions

 position (int x, int y)
bool valid (void) const
char get_rank (void) const
char get_file (void) const

Private Attributes

unsigned p

Detailed Description

The position class is used to represent a position on a chess board. The board is assumed to be a standard 8x8 board.

Input and output using algebraic notation is available. See http://en.wikipedia.org/wiki/Algebraic_notation_%28chess%29 for more information.

Rows (called "ranks") are numbered 1..8, when viewed from white's side of the board, with 1 closest (at the bottom) and 8 farthest (at the top).

Columns (called "files") are numbered 'a' to 'h', when viewed from white's side of the board with 'a' on the left and 'h' on the right.

Definition at line 40 of file position.h.


Member Typedef Documentation

typedef std::vector<position> position::list_t

Definition at line 43 of file position.h.


Constructor & Destructor Documentation

position::~position ( )

The destructor.

It is not virtual, thou shalt not derive from this class.

Definition at line 25 of file position.cc.

position::position ( )

The default constructor.

The position is undefined, and will result in a run-time assert failure if used without being initialised.

Definition at line 30 of file position.cc.

position::position ( char  rank,
char  file 
)

The default constructor.

The position is undefined, and will result in a run-time assert failure if used without being initialised.

Parameters:
rankThe Y coordinate, as a char between '1' and '8'.
fileThe X coordinate, as a char between 'a' and 'h' (or, to be generous, from 'A' to 'H').

Definition at line 45 of file position.cc.

position::position ( const position rhs)

The copy constructor.

Parameters:
rhsThe right hand side of the initialization.

Definition at line 67 of file position.cc.

position::position ( int  x,
int  y 
) [private]

A constructor. This is not part of the public interface.

Parameters:
xThe X coordinate of the position (0..7)
yThe Y coordinate of the position (0..7)
Note:
The arguments are the reverse order to the char,char constructor.

Definition at line 56 of file position.cc.


Member Function Documentation

int position::distance ( const position elsewhere) const

The distance method may be used to calculate the taxi-cab distance (see http://en.wikipedia.org/wiki/Taxicab_geometry) between this point and the given point.

Parameters:
elsewhereThe other position of interest.

Definition at line 247 of file position.cc.

char position::get_file ( void  ) const [private]

The get_file method is used to obtain the column number. Each column (called a "file" in chess jargon) are numbered 'a' to 'h', when viewed from white's side of the board with 'a' on the left and 'h' on the right.

Returns:
the file, as a character from 'a' to 'h'

Definition at line 108 of file position.cc.

char position::get_rank ( void  ) const [private]

The get_rank method is used to obtain the row number. Each row (called a "rank" in chess jargon) are numbered 1..8, when viewed from white's side of the board, with 1 closest (at the bottom) and 8 farthest (at the top).

Returns:
the rank, as a character from '1' to '8'

Definition at line 91 of file position.cc.

int position::get_x ( void  ) const

The get_x method is used to obtain the X coordinate of the position.

Returns:
the X coordinate of the position (0..7)

Definition at line 116 of file position.cc.

int position::get_y ( void  ) const

The get_y method is used to obtain the Y coordinate of the position.

Returns:
the Y coordinate of the position (0..7)

Definition at line 99 of file position.cc.

position::list_t position::knight_moves ( void  ) const

The knight_moves method is used to generate a list of legal knight moves from this position. It is assumed there are no other pieces on the board.

Returns:
A list of positions. There could be as many as 8, or a few as 2.

Definition at line 200 of file position.cc.

bool position::operator!= ( const position rhs) const

The inequality operator is used to test two position for inequality.

Parameters:
rhsThe right hand side of the comparison.
Returns:
true of the positions are different, false if they are the same.

Definition at line 190 of file position.cc.

position & position::operator= ( const position rhs)

The assignment operator.

Parameters:
rhsThe right hand side of the assignment.

Definition at line 74 of file position.cc.

bool position::operator== ( const position rhs) const

The equality operator is used to test two position for equality.

Parameters:
rhsThe right hand side of the comparison.
Returns:
true of the positions are the same, false if they are different.

Definition at line 180 of file position.cc.

void position::plot ( char  board[8][8],
char  c 
) const

The plot method is used to draw the given character into the given board at this position.

Parameters:
boardThe board to be drawn on. The first domension is y|rank, the second dimension is x|file.
cThe character to be drawn on the board.

Definition at line 239 of file position.cc.

position position::read ( std::istream &  is) [static]

The read class method is used to read a position, in text form, from an input stream. Both upper and lower case are accepted for the file/column portion.

Parameters:
isThe text input stream to be read.
Returns:
A valid position value
Note:
This class method does not return if there is an error (not present, malformed, eof-of-file, etc) instead a fatal error message will be printed, and the program will exit indicating failure.

Definition at line 133 of file position.cc.

bool position::valid ( void  ) const [private]

The valid method is used to ensure that the position is valid before any processing takes place.

Definition at line 83 of file position.cc.

void position::write ( std::ostream &  os) const

The write method is used to write a text representation of a chess board position.

Parameters:
osThe output stream to write the position onto.

Definition at line 171 of file position.cc.


Member Data Documentation

unsigned position::p [private]

The p instance variable is used to remember the rank and file of the position, in a single variable.

Note:
The internal representation is undefined, and subject to change without notice.

Definition at line 194 of file position.h.


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