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-2014  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.  * move.hpp
  21.  *
  22.  *  Created on: Feb 25, 2012
  23.  *      Author: petero
  24.  */
  25.  
  26. #ifndef MOVE_HPP_
  27. #define MOVE_HPP_
  28.  
  29. #include <iosfwd>
  30. #include "util/util.hpp"
  31.  
  32. /** Represents a chess move. */
  33. class Move {
  34. public:
  35.     /** Create empty move object. */
  36.     Move();
  37.  
  38.     /** Create a move object. */
  39.     Move(int from, int to, int promoteTo, int score = 0);
  40.  
  41.     /** Copy constructor. */
  42.     Move(const Move& m);
  43.  
  44.     /** Set move properties. */
  45.     void setMove(int from, int to, int promoteTo, int score);
  46.  
  47.     void setScore(int score);
  48.  
  49.     /** Get 16 bit compressed representation of move, not including score. */
  50.     U16 getCompressedMove() const;
  51.  
  52.     /** Set move from 16 bit compressed representation. Score not changed. */
  53.     void setFromCompressed(U16 move);
  54.  
  55.     int from() const;
  56.     int to() const;
  57.     int promoteTo() const;
  58.     int score() const;
  59.  
  60.     bool isEmpty() const;
  61.  
  62.     /** Note that score is not included in the comparison. */
  63.     bool equals(const Move& other) const;
  64.  
  65.     bool operator==(const Move& other) const;
  66.  
  67.     int hashCode() const;
  68.  
  69.     /** Not declared "nothrow". Avoids nullptr check in generated assembly code when using placement new. */
  70.     void* operator new (std::size_t size, void* ptr);
  71.  
  72.     /** For debugging. */
  73.     std::ostream& operator<<(std::ostream& os);
  74.  
  75. private:
  76.     /** From square, 0-63. */
  77.     int from_;
  78.  
  79.     /** To square, 0-63. */
  80.     int to_;
  81.  
  82.     /** Promotion piece. */
  83.     int promoteTo_;
  84.  
  85.     /** Score. */
  86.     int score_;
  87. };
  88.  
  89. inline
  90. Move::Move()
  91.     : from_(0), to_(0), promoteTo_(0), score_(0) {
  92. }
  93.  
  94. inline
  95. Move::Move(int from, int to, int promoteTo, int score) {
  96.     from_ = from;
  97.     to_ = to;
  98.     promoteTo_ = promoteTo;
  99.     score_ = score;
  100. }
  101.  
  102. inline
  103. Move::Move(const Move& m) {
  104.     from_ = m.from_;
  105.     to_ = m.to_;
  106.     promoteTo_ = m.promoteTo_;
  107.     score_ = m.score_;
  108. }
  109.  
  110. inline void
  111. Move::setMove(int from, int to, int promoteTo, int score)
  112. {
  113.     from_ = from;
  114.     to_ = to;
  115.     promoteTo_ = promoteTo;
  116.     score_ = score;
  117. }
  118.  
  119. inline void
  120. Move::setScore(int score) {
  121.     score_ = score;
  122. }
  123.  
  124. inline U16
  125. Move::getCompressedMove() const {
  126.     return (U16)(from() + (to() << 6) + (promoteTo() << 12));
  127. }
  128.  
  129. inline void
  130. Move::setFromCompressed(U16 move) {
  131.     setMove(move & 63, (move >> 6) & 63, (move >> 12) & 15, score());
  132. }
  133.  
  134. inline int
  135. Move::from() const {
  136.     return from_;
  137. }
  138.  
  139. inline int
  140. Move::to() const {
  141.     return to_;
  142. }
  143.  
  144. inline int
  145. Move::promoteTo() const {
  146.     return promoteTo_;
  147. }
  148.  
  149. inline int
  150. Move::score() const {
  151.     return score_;
  152. }
  153.  
  154. inline bool
  155. Move::isEmpty() const {
  156.     return (from_ == 0) && (to_ == 0);
  157. }
  158.  
  159. inline bool
  160. Move::equals(const Move& other) const {
  161.     if (from_ != other.from_)
  162.         return false;
  163.     if (to_ != other.to_)
  164.         return false;
  165.     if (promoteTo_ != other.promoteTo_)
  166.         return false;
  167.     return true;
  168. }
  169.  
  170. inline bool
  171. Move::operator==(const Move& other) const {
  172.     return (*this).equals(other);
  173. }
  174.  
  175. inline int
  176. Move::hashCode() const {
  177.     return (from_ * 64 + to_) * 16 + promoteTo_;
  178. }
  179.  
  180. inline void*
  181. Move::operator new (std::size_t size, void* ptr) {
  182.     return ptr;
  183. }
  184.  
  185. #endif /* MOVE_HPP_ */
  186.