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