move.cc
Go to the documentation of this file.
00001 #include "osl/piece.h"
00002 #include "osl/move.h"
00003 #include "move-phash.c"
00004 #include <boost/static_assert.hpp>
00005 #include <iostream>
00006 
00007 namespace osl
00008 {
00009   BOOST_STATIC_ASSERT(sizeof(Move) == 4);
00010 } //namespace osl
00011 
00012 bool osl::Move::isValid() const
00013 {
00014   if (! isNormal())
00015     return false;
00016   const Square from = this->from();
00017   if (! from.isValid())
00018     return false;
00019   const Square to = this->to();
00020   if (! to.isOnBoard())
00021     return false;
00022   return osl::isValid(ptype())
00023     && osl::isValid(capturePtype())
00024     && capturePtype()!=KING
00025     && osl::isValid(player());
00026 }
00027 
00028 const osl::Move osl::Move::rotate180() const
00029 {
00030   if (isPass())
00031     return Move::PASS(alt(player()));
00032   if (! isNormal())
00033     return *this;
00034   return Move(from().rotate180Safe(), to().rotate180(), ptype(),
00035               capturePtype(), isPromotion(), alt(player()));
00036 }
00037 
00038 std::ostream& osl::operator<<(std::ostream& os,const Move move)
00039 {
00040   if (move == Move::DeclareWin())
00041     return os << "MOVE_DECLARE_WIN";
00042   if (move.isInvalid())
00043     return os << "MOVE_INVALID";
00044   if (move.isPass())
00045     return os << "MOVE_PASS";
00046   const Player turn = move.player();
00047   if (move.isValid())
00048   {
00049     if (move.from().isPieceStand()) 
00050     {
00051       os << "Drop(" << turn << "," << move.ptype() << "," << move.to() << ")";
00052     }
00053     else
00054     {
00055       const Ptype capture_ptype=move.capturePtype();
00056       os << "Move(" << turn << "," << move.ptype() << "," 
00057          << move.from() << "->" << move.to() ;
00058       if (move.promoteMask())
00059         os << ",promote";
00060       if (capture_ptype != PTYPE_EMPTY)
00061         os << ",capture=" << capture_ptype;
00062       os << ")";
00063     }
00064   }
00065   else
00066   {
00067     os << "InvalidMove " << move.from() << " " << move.to() 
00068        << " " << move.ptypeO() << " " << move.oldPtypeO()
00069        << " " << move.promoteMask()
00070        << " " << move.capturePtype() << "\n";
00071   }
00072   return os;
00073 }
00074 
00075 unsigned int osl::Move::hash() const
00076 {
00077   assert(capturePtype() == PTYPE_EMPTY);
00078   return move_phash(intValue());
00079 }
00080 
00081 // ;;; Local Variables:
00082 // ;;; mode:c++
00083 // ;;; c-basic-offset:2
00084 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines