home *** CD-ROM | disk | FTP | other *** search
- // Copyright 1993 by Jon Dart. All Rights Reserved.
-
- #include "movearr.h"
-
- const Initial_Move_Array_Size = 100; // initial array size
- // (expandable if necessary)
-
- Move_Record::Move_Record(const Board &board, const ExtendedMove &move)
- : my_move(move),my_hashcode(board.HashCode())
- {
- my_caststat[White] = board.CastleStatus(White);
- my_caststat[Black] = board.CastleStatus(Black);
- }
-
- Move_Record::Move_Record()
- : my_move(ExtendedMove()),my_hashcode(0)
- {
- my_caststat[White] = Board::CanCastleEitherSide;
- my_caststat[Black] = Board::CanCastleEitherSide;
- }
-
- int Move_Record::operator == ( const Move_Record &l ) const
- {
- return my_hashcode == l.my_hashcode &&
- my_caststat[White] == l.my_caststat[White] &&
- my_caststat[Black] == l.my_caststat[Black];
- }
-
- int Move_Record::operator != ( const Move_Record &l ) const
- {
- return my_hashcode != l.my_hashcode ||
- my_caststat[White] != l.my_caststat[White] ||
- my_caststat[Black] != l.my_caststat[Black];
- }
-
- Move_Array::Move_Array() : Array<Move_Record>(Initial_Move_Array_Size,False)
- {
- for (int i=0; i<Rep_Table_Size;i++)
- rep_table[i] = 0;
- }
-
- Move_Array::~Move_Array()
- {
- }
-
- void Move_Array::add_move( const Board &board, const ExtendedMove &emove )
- {
- Move_Record entry( board, emove );
- *this += entry;
- rep_table[board.HashCode() % Rep_Table_Size]++;
- }
-
- void Move_Array::remove_move()
- {
- if (num_moves() == 0)
- return;
- Move_Record &entry = (*this)[num_moves()-1];
- rep_table[entry.hashcode() % Rep_Table_Size]--;
- resize(size()-1);
- }
-
- const ExtendedMove &Move_Array::move( const unsigned n )
- {
- return (*this)[n].move();
- }
-
- int Move_Array::rep_count( const Board &board) const
- {
- // we do a quick check first to see if any entry has the same
- // lower 7 bits in the hash code.
- if (num_moves() == 0 ||
- rep_table[board.HashCode() % Rep_Table_Size] == 0)
- return 0;
- int n = num_moves()-1;
- int count = 0;
- Move_Record e(board,ExtendedMove());
- for (int i = n ; i >= 0; --i )
- {
- Move_Record &cur_entry = (*this)[i];
- if (cur_entry == e)
- count++;
- const ExtendedMove &cur_move = cur_entry.move();
- if (!cur_move.Capture().IsEmpty() ||
- cur_move.PieceMoved().Type() == Piece::Pawn ||
- cur_move.Special() != ExtendedMove::Normal)
- // irExtended move
- break;
- }
- return count;
- }
-
- void Move_Array::clear()
- {
- resize(0);
- }
-
- unsigned Move_Array::num_moves(const ColorType side)
- {
- if (side == White)
- return num_moves() ? num_moves()/2 + 1 : 0;
- else
- return num_moves() ? (num_moves()-1)/2 : 0;
- }
-
-