Home
Fractals
Tutorials
Books
Archive
My blog
My LinkedIn Profile

BOOKS i'm reading

Napoleon Hill Keys to Success: The 17 Principles of Personal Achievement, Napoleon Hill, ISBN: 978-0452272811
The 4-Hour Workweek: Escape 9-5, Live Anywhere, and Join the New Rich (Expanded and Updated), Timothy Ferriss, ISBN: 978-0307465351
The Fountainhead, Ayn Rand, ISBN: 0452273331

/*
 * Module ID: lab2ches.cpp
 * Titre    : Module Secondaire du Lab 2 du cours ELE-440
 *
 * Auteur   : Olivier Langlois <olivier@olivierlanglois.net>
 * Date     : 4 Fevrier 1998
 *
 */

#ifdef __BORLANDC__
// Indique au compilateur que les instances de template utilisees dans ce
// module seront definies dans un autre module.
#pragma option -Jgx
#endif

#include "collect/pq.cpp"
#include "lab2ches.h"

#ifdef __GNUG__
Lab2ChessCase *Lab2ChessBoard::sentinel = NULL;
#endif

Lab2ChessCase::Lab2ChessCase( int oc, unsigned s )
: originCounter(oc), seq(s), possibleNextMoves(NULL)
{}

Lab2ChessCase::Lab2ChessCase( const Lab2ChessCase &h )
{
  possibleNextMoves = NULL;
  operator=(h);
}

void Lab2ChessCase::init( void )
{
  ChessCase::init();
  PQ<Lab2ChessCase> *tmp = new PQ<Lab2ChessCase>(9);
#ifdef __GNUG__
  tmp->setSentinel(Lab2ChessBoard::sentinel);
#endif
  possibleNextMoves      = dynamic_cast<oliCollection *>(tmp);
}

void Lab2ChessCase::operator=(const Lab2ChessCase &h)
{
  originCounter = h.originCounter;
  seq           = h.seq;
}

int Lab2ChessCase::operator==(const Lab2ChessCase &h) const
{
  return (originCounter == h.originCounter);
}

int Lab2ChessCase::operator!=(const Lab2ChessCase &h) const
{
  return (originCounter != h.originCounter);
}

int Lab2ChessCase::operator <(const Lab2ChessCase &h) const
{
  // Note : Plus originCounter est petit, plus la priorite est grande.
  return (originCounter > h.originCounter);
}

int Lab2ChessCase::operator >(const Lab2ChessCase &h) const
{
  // Note : Plus originCounter est petit, plus la priorite est grande.
  return (originCounter < h.originCounter);
}

int Lab2ChessCase::operator<=(const Lab2ChessCase &h) const
{
  return ( operator<(h) || operator==(h) );
}

int Lab2ChessCase::operator>=(const Lab2ChessCase &h) const
{
  return ( operator>(h) || operator==(h) );
}

Lab2Column::Lab2Column() : Column(0)
{
  for( int i = 0; i < NUMROW; i++ )
     ChessCases.insert(new Lab2ChessCase);
}

Lab2ChessBoard::Lab2ChessBoard() : ChessBoard(0)
{
#ifndef __GNUG__
  PQ<Lab2ChessCase>::setSentinel(new Lab2ChessCase(-1));
#endif
  for( int i = 0; i < NUMCOL; i++ )
    columns.insert(new Lab2Column[NUMCOL]);
  /* initColumns(); */
}

Lab2ChessBoard::~Lab2ChessBoard()
{
#ifdef __BORLANDC__
  delete PQ<Lab2ChessCase>::getSentinel();
#endif
}

/******************************************************************************
 *
 * Nom       : addCase
 *
 ****************************************************************************/
void Lab2ChessCase::addCase( int x, int y )
{
  Lab2ChessCase &refCase = dynamic_cast<Lab2ChessCase &>((*Board)[x][y]);
  if( !refCase.seq )

     /*
      * Il est important d'espacer les 2 > dans l'expression
      * dynamic_cast< PQ<Lab2ChessCase> * >(possibleNextMoves)
      * car autrement il est possible que l'analyseur lexique du compilateur
      * se confonde avec l'operateur >>
      */
     dynamic_cast< PQ<Lab2ChessCase> * >(possibleNextMoves)->insert( &refCase );
}

/******************************************************************************
 *
 * Nom       : visitBoard
 *
 ****************************************************************************/
void Lab2Knight::visitBoard( ChessBoard &board )
{
  int u, v;

  for( int x = 0; x < NUMCOL; x++ )
     for( int y = 0; y < NUMROW; y++ )
        for( int nm = 0; nm < GamePiece::numMove; nm++ )
        {
          u = x + GamePiece::dx[nm];
          v = y + GamePiece::dy[nm];

          if( (0 <= u) && (u <= NUMCOL-1) && (0 <= v) && (v <= NUMROW-1) )
             ((Lab2ChessCase &)board[u][v]).originCounter++;
        }
}

/******************************************************************************
 *
 * Nom       : visitCase
 *
 ****************************************************************************/
void Lab2Knight::visitCase( ChessCase &c )
{
  int x, y, new_x, new_y, i;

  Knight::visitCase(c);

  Lab2ChessCase &lab2c = dynamic_cast<Lab2ChessCase &>(c);

  lab2c.possibleNextMoves->clear();
  lab2c.getPosition( x, y );

  for( i = 0; i < GamePiece::numMove; i++ )
  {
     new_x = x + GamePiece::dx[i];
     new_y = y + GamePiece::dy[i];

     if( (0 <= new_x) && (new_x <= NUMCOL-1) &&
          (0 <= new_y) && (new_y <= NUMROW-1) )
      lab2c.addCase( new_x, new_y );
  }
}

/******************************************************************************
 *
 * Nom       : getNext
 *
 * Utilite   : Retourne la prochaine case a essayer.
 *             (Sert aussi a decoupler PQ du module lab2)
 *
 * Reference : lab2ches.h
 *
 ****************************************************************************/
Lab2ChessCase *getNext( Lab2ChessCase *ptrCase )
{
  return dynamic_cast< PQ<Lab2ChessCase> * >(ptrCase->possibleNextMoves)->removeFirst();
}

#ifdef __GNUG__
template class PQ<Lab2ChessCase>;
#endif

Home :: Fractals :: Tutorials :: Books :: Archive :: My blog :: My LinkedIn Profile :: Contact