BOOKS i'm reading |
/* * Module ID: chess.cpp * Titre : Definitions de classes pour le jeu d'echec * * Auteur : Olivier Langlois <olivier@olivierlanglois.net> * Date : 4 Fevrier 1998 * */ /* * Fichiers d'entetes */ #include "chess.h" /* * Definitions */ /* * Declarations de fonctions locales */ /* * Definition des fonctions */ ostream &operator <<( ostream &os, ChessBoard &s ) { s.print(os); return os; } ostream &operator <<( ostream &os, ChessCase &s ) { s.print(os); return os; } GamePiece::~GamePiece() {} void GamePiece::visitCase( ChessCase &Case ) { // Elimine la reference sur cette piece de la derniere case visitee. if( position ) position->setPiece(NULL); position = &Case; Case.setPiece(this); } void GamePiece::visitCol( Column &col ) { for( int i = 0; i < NUMROW; i++ ) col[i].accept(*this); } void GamePiece::visitBoard( ChessBoard &board ) { for( int i = 0; i < NUMCOL; i++ ) board[i].accept(*this); } void Knight::init(void) { GamePiece::numMove = 8; GamePiece::dx = new int[8]; GamePiece::dy = new int[8]; GamePiece::dx[0] = 2; GamePiece::dy[0] = 1; GamePiece::dx[1] = 1; GamePiece::dy[1] = 2; GamePiece::dx[2] = -1; GamePiece::dy[2] = 2; GamePiece::dx[3] = -2; GamePiece::dy[3] = 1; GamePiece::dx[4] = -2; GamePiece::dy[4] = -1; GamePiece::dx[5] = -1; GamePiece::dy[5] = -2; GamePiece::dx[6] = 1; GamePiece::dy[6] = -2; GamePiece::dx[7] = 2; GamePiece::dy[7] = -1; } Knight::~Knight() { delete[] GamePiece::dx; delete[] GamePiece::dy; } Column::Column() { for( int i = 0; i < NUMROW; i++ ) { ChessCases.insert(new ChessCase); } } void Column::initCases( int x, ChessBoard *cb ) { for( int y = 0; y < NUMROW; y++ ) { dynamic_cast<ChessCase *>(ChessCases[y])->setPosition( x, y ); dynamic_cast<ChessCase *>(ChessCases[y])->setBoard( cb ); dynamic_cast<ChessCase *>(ChessCases[y])->init(); } } void Column::print( ostream &os, int y ) { // Si aucune case n'est specifie, elles sont tous imprimees if( y == -1 ) for( int i = 0; i < NUMROW; i++ ) os << *dynamic_cast<ChessCase *>(ChessCases[i]); // Sinon seulement la case specifiee est imprimee. else os << *dynamic_cast<ChessCase *>(ChessCases[y]); } ChessBoard::ChessBoard() { for( int i = 0; i < NUMCOL; i++ ) columns.insert( new Column ); /* initColumns(); */ } void ChessBoard::initColumns( void ) { for( int x = 0; x < NUMCOL; x++ ) dynamic_cast<Column *>(columns[x])->initCases( x, this ); } void ChessBoard::print( ostream &os ) { for( int y = 0; y < NUMROW; y++ ) { for( int x = 0; x < NUMCOL; x++ ) dynamic_cast<Column *>(columns[x])->print( os, y ); os << endl; } } |