home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / ARASAN_S.ZIP / MATERIAL.CPP < prev    next >
C/C++ Source or Header  |  1993-11-17  |  1KB  |  71 lines

  1. // Copyright 1993 by Jon Dart.  All Rights Reserved.
  2.  
  3. #include "material.h"
  4.  
  5. // data layout in info field:
  6. //bits    
  7. //0-2    pawns
  8. //3-5    knights
  9. //6-8    bishops
  10. //9-11    rooks
  11. //12-14    queens
  12. //15    kings
  13.  
  14. static const int16 masks[] =
  15.  { 0, 1, 0x8, 0x40, 0x200, 0x1000, 0x8000, 0 };
  16.  
  17. static const int16 masks2[] =
  18.  { 0, 0x7, 0x38, 0x1c0, 0xe00, 0x7000, 0x8000, 0 };
  19.  
  20. void Material::add_piece(const Piece::PieceType p)
  21. {
  22.     info += masks[(int)p];
  23.     total += Piece::Value(p);
  24. }
  25.  
  26. void Material::remove_piece(const Piece::PieceType p)
  27. {
  28.     info -= masks[(int)p];
  29.     total -= Piece::Value(p);
  30. }
  31.  
  32. unsigned Material::count(const Piece::PieceType p) const
  33. {
  34.     int16 info2 = info & masks2[(int)p];
  35.     if (info2 == 0)
  36.        return 0;
  37.     switch (p)
  38.     {
  39.        case Piece::Pawn:
  40.           return info2;
  41.        case Piece::Knight:
  42.           return info2 >> 3;
  43.        case Piece::Bishop:
  44.           return info2 >> 6;
  45.        case Piece::Rook:
  46.           return info2 >> 9;
  47.        case Piece::Queen:
  48.           return info2 >> 12;
  49.        case Piece::King:
  50.           return 1;
  51.        default:
  52.          return 0;
  53.     }
  54. }
  55.  
  56. unsigned Material::piece_count() const
  57. {
  58.     unsigned info2 = info & 0x7007;
  59.     unsigned count = 0;
  60.     for (int i = 0; i < 4; i++)
  61.     {
  62.        info2 = info2 >> 3;
  63.        count += info2 & 0x7;
  64.     }
  65.     return count;
  66. }
  67.  
  68. void Material::clear()
  69. {
  70.     info = total = 0;
  71. }