00001
00002
00003 #include "osl/pieceStand.h"
00004
00005 #include "osl/misc/mask.h"
00006 #include "osl/ptypeTable.h"
00007 #include "osl/state/simpleState.h"
00008 #include <boost/static_assert.hpp>
00009 #include <boost/foreach.hpp>
00010 #include <iostream>
00011
00012 namespace osl
00013 {
00014 BOOST_STATIC_ASSERT(sizeof(unsigned int)*8>=32);
00015
00016 const CArray<Ptype,7> PieceStand::order =
00017 {{
00018 ROOK, BISHOP, GOLD, SILVER, KNIGHT, LANCE, PAWN,
00019 }};
00020
00021 const CArray<unsigned char,PTYPE_MAX+1> PieceStand::shift =
00022 {{
00023 0,0,0,0,0,0,0,0,
00024 28, 24, 18, 14, 10, 6, 3, 0,
00025 }};
00026 const CArray<unsigned char,PTYPE_MAX+1> PieceStand::mask =
00027 {{
00028 0,0,0,0,0,0,0,0,
00029 (1<<2)-1, (1<<3)-1, (1<<5)-1, (1<<3)-1, (1<<3)-1, (1<<3)-1, (1<<2)-1, (1<<2)-1
00030 }};
00031
00032 const unsigned int PieceStand::carryMask;
00033 }
00034
00035 osl::PieceStand::
00036 PieceStand(Player pl, const SimpleState& state)
00037 : flags(0)
00038 {
00039 BOOST_FOREACH(Ptype ptype, PieceStand::order)
00040 add(ptype, state.countPiecesOnStand(pl, ptype));
00041 }
00042
00043 bool osl::PieceStand::canAdd(Ptype type) const
00044 {
00045 const int max
00046 = Ptype_Table.getIndexLimit(type) - Ptype_Table.getIndexMin(type);
00047 assert(max >= 0);
00048 return (static_cast<int>(get(type)) != max);
00049 }
00050
00051 void osl::PieceStand::tryAdd(Ptype type)
00052 {
00053 if (canAdd(type))
00054 add(type);
00055 }
00056
00057 bool osl::PieceStand::atMostOneKind() const
00058 {
00059 return misc::BitOp::countBit(getFlags()) <= 1;
00060 }
00061
00062 #ifndef MINIMAL
00063 bool osl::PieceStand::
00064 carryUnchangedAfterAdd(const PieceStand& original, const PieceStand& other) const
00065 {
00066 if (original.testCarries() == testCarries())
00067 return true;
00068 std::cerr << original << " + " << other << " = " << *this << "\n";
00069 return false;
00070 }
00071
00072 bool osl::PieceStand::
00073 carryUnchangedAfterSub(const PieceStand& original, const PieceStand& other) const
00074 {
00075 if (original.testCarries() == testCarries())
00076 return true;
00077 std::cerr << original << " - " << other << " = " << *this << "\n";
00078 return false;
00079 }
00080
00081 std::ostream& osl::operator<<(std::ostream& os, osl::PieceStand stand)
00082 {
00083 os << "(stand";
00084 BOOST_FOREACH(Ptype ptype, PieceStand::order)
00085 {
00086 os << ' ' << stand.get(ptype);
00087 }
00088 return os << ")";
00089 }
00090 #endif
00091
00092
00093
00094
00095