Subversion Repositories Games.Chess Giants

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.     Texel - A UCI chess engine.
  3.     Copyright (C) 2012-2013  Peter Ă–sterlund, peterosterlund2@gmail.com
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. /*
  20.  * piece.hpp
  21.  *
  22.  *  Created on: Feb 25, 2012
  23.  *      Author: petero
  24.  */
  25.  
  26. #ifndef PIECE_HPP_
  27. #define PIECE_HPP_
  28.  
  29. /**
  30.  * Constants for different piece types.
  31.  */
  32. class Piece {
  33. public:
  34.     enum Type {
  35.       EMPTY = 0,
  36.       WKING = 1,
  37.       WQUEEN = 2,
  38.       WROOK = 3,
  39.       WBISHOP = 4,
  40.       WKNIGHT = 5,
  41.       WPAWN = 6,
  42.  
  43.       BKING = 7,
  44.       BQUEEN = 8,
  45.       BROOK = 9,
  46.       BBISHOP = 10,
  47.       BKNIGHT = 11,
  48.       BPAWN = 12,
  49.  
  50.       nPieceTypes = 13
  51.     };
  52.  
  53.     /**
  54.      * Return true if p is a white piece, false otherwise.
  55.      * Note that if p is EMPTY, an unspecified value is returned.
  56.      */
  57.     static bool isWhite(int pType);
  58.  
  59.     static int makeWhite(int pType);
  60.  
  61.     static int makeBlack(int pType);
  62. };
  63.  
  64. template <bool wtm> struct ColorTraits {
  65. };
  66.  
  67. template<> struct ColorTraits<true> {
  68.     static const Piece::Type KING   = Piece::WKING;
  69.     static const Piece::Type QUEEN  = Piece::WQUEEN;
  70.     static const Piece::Type ROOK   = Piece::WROOK;
  71.     static const Piece::Type BISHOP = Piece::WBISHOP;
  72.     static const Piece::Type KNIGHT = Piece::WKNIGHT;
  73.     static const Piece::Type PAWN   = Piece::WPAWN;
  74. };
  75.  
  76. template<> struct ColorTraits<false> {
  77.     static const Piece::Type KING   = Piece::BKING;
  78.     static const Piece::Type QUEEN  = Piece::BQUEEN;
  79.     static const Piece::Type ROOK   = Piece::BROOK;
  80.     static const Piece::Type BISHOP = Piece::BBISHOP;
  81.     static const Piece::Type KNIGHT = Piece::BKNIGHT;
  82.     static const Piece::Type PAWN   = Piece::BPAWN;
  83. };
  84.  
  85. inline bool
  86. Piece::isWhite(int pType) {
  87.     return pType < BKING;
  88. }
  89.  
  90. inline int
  91. Piece::makeWhite(int pType) {
  92.     return pType < BKING ? pType : pType - (BKING - WKING);
  93. }
  94.  
  95. inline int
  96. Piece::makeBlack(int pType) {
  97.     return ((pType > EMPTY) && (pType < BKING)) ? pType + (BKING - WKING) : pType;
  98. }
  99.  
  100. #endif /* PIECE_HPP_ */
  101.